KiCad PCB EDA Suite
class_pcb_target.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2012 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
5  * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
6  * Copyright (C) 2012 Wayne Stambaugh <stambaughw@verizon.net>
7  * Copyright (C) 1992-2019 KiCad Developers, see AUTHORS.txt for contributors.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
32 #include <fctsys.h>
33 #include <gr_basic.h>
34 #include <common.h>
35 #include <kicad_string.h>
36 #include <trigo.h>
37 #include <macros.h>
38 #include <richio.h>
39 #include <bitmaps.h>
40 #include <pcb_edit_frame.h>
41 #include <class_board.h>
42 #include <class_pcb_target.h>
43 #include <base_units.h>
44 #include <pgm_base.h>
47 
48 
50  BOARD_ITEM( aParent, PCB_TARGET_T )
51 {
52  m_Shape = 0;
53  m_Size = Millimeter2iu( 5 ); // Gives a decent size
54  m_Width = Millimeter2iu( DEFAULT_COPPER_LINE_WIDTH );
55  m_Layer = Edge_Cuts; // a target is on all layers
56 }
57 
58 PCB_TARGET::PCB_TARGET( BOARD_ITEM* aParent, int aShape, PCB_LAYER_ID aLayer,
59  const wxPoint& aPos, int aSize, int aWidth ) :
60  BOARD_ITEM( aParent, PCB_TARGET_T )
61 {
62  m_Shape = aShape;
63  m_Layer = aLayer;
64  m_Pos = aPos;
65  m_Size = aSize;
66  m_Width = aWidth;
67 }
68 
69 
71 {
72 }
73 
74 
75 /* Print PCB_TARGET object: 2 segments + 1 circle
76  * The circle radius is half the radius of the target
77  * 2 lines have length the diameter of the target
78  */
79 void PCB_TARGET::Print( PCB_BASE_FRAME* aFrame, wxDC* DC, const wxPoint& offset )
80 {
81  int radius, ox, oy, width;
82  int dx1, dx2, dy1, dy2;
83 
84  ox = m_Pos.x + offset.x;
85  oy = m_Pos.y + offset.y;
86 
87  BOARD* brd = GetBoard( );
88 
89  if( brd->IsLayerVisible( m_Layer ) == false )
90  return;
91 
92  COLOR4D gcolor = Pgm().GetSettingsManager().GetColorSettings()->GetColor( m_Layer );
93  auto displ_opts = aFrame->GetDisplayOptions();
94  bool filled = displ_opts.m_DisplayDrawItemsFill;
95  width = m_Width;
96 
97  radius = m_Size / 3;
98 
99  if( GetShape() ) // shape X
100  radius = m_Size / 2;
101 
102  if( filled )
103  GRCircle( nullptr, DC, ox, oy, radius, width, gcolor );
104  else
105  {
106  GRCircle( nullptr, DC, ox, oy, radius + (width / 2), gcolor );
107  GRCircle( nullptr, DC, ox, oy, radius - (width / 2), gcolor );
108  }
109 
110 
111  radius = m_Size / 2;
112  dx1 = radius;
113  dy1 = 0;
114  dx2 = 0;
115  dy2 = radius;
116 
117  if( GetShape() ) // shape X
118  {
119  dx1 = dy1 = radius;
120  dx2 = dx1;
121  dy2 = -dy1;
122  }
123 
124  if( filled )
125  {
126  GRLine( nullptr, DC, ox - dx1, oy - dy1, ox + dx1, oy + dy1, width, gcolor );
127  GRLine( nullptr, DC, ox - dx2, oy - dy2, ox + dx2, oy + dy2, width, gcolor );
128  }
129  else
130  {
131  GRCSegm( nullptr, DC, ox - dx1, oy - dy1, ox + dx1, oy + dy1, width, gcolor );
132  GRCSegm( nullptr, DC, ox - dx2, oy - dy2, ox + dx2, oy + dy2, width, gcolor );
133  }
134 }
135 
136 
137 bool PCB_TARGET::HitTest( const wxPoint& aPosition, int aAccuracy ) const
138 {
139  int dX = aPosition.x - m_Pos.x;
140  int dY = aPosition.y - m_Pos.y;
141  int radius = aAccuracy + ( m_Size / 2 );
142  return abs( dX ) <= radius && abs( dY ) <= radius;
143 }
144 
145 
146 bool PCB_TARGET::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
147 {
148  EDA_RECT arect = aRect;
149  arect.Inflate( aAccuracy );
150 
151  if( aContained )
152  return arect.Contains( GetBoundingBox() );
153  else
154  return GetBoundingBox().Intersects( arect );
155 }
156 
157 
158 void PCB_TARGET::Rotate(const wxPoint& aRotCentre, double aAngle)
159 {
160  RotatePoint( &m_Pos, aRotCentre, aAngle );
161 }
162 
163 
164 void PCB_TARGET::Flip(const wxPoint& aCentre, bool aFlipLeftRight )
165 {
166  if( aFlipLeftRight )
167  m_Pos.x = aCentre.x - ( m_Pos.x - aCentre.x );
168  else
169  m_Pos.y = aCentre.y - ( m_Pos.y - aCentre.y );
170 
171  SetLayer( FlipLayer( GetLayer() ) );
172 }
173 
174 
176 {
177  EDA_RECT bBox;
178  bBox.SetX( m_Pos.x - m_Size/2 );
179  bBox.SetY( m_Pos.y - m_Size/2 );
180  bBox.SetWidth( m_Size );
181  bBox.SetHeight( m_Size );
182 
183  return bBox;
184 }
185 
186 
187 wxString PCB_TARGET::GetSelectMenuText( EDA_UNITS aUnits ) const
188 {
189  // Targets are on *every* layer by definition
190  return wxString::Format( _( "Target size %s" ), MessageTextFromValue( aUnits, m_Size ) );
191 }
192 
193 
195 {
196  return add_pcb_target_xpm;
197 }
198 
199 
201 {
202  return new PCB_TARGET( *this );
203 }
204 
205 
207 {
208  assert( aImage->Type() == PCB_TARGET_T );
209 
210  std::swap( *((PCB_TARGET*) this), *((PCB_TARGET*) aImage) );
211 }
212 
EDA_UNITS
Definition: common.h:184
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:103
PNG memory record (file in memory).
Definition: bitmap_def.h:29
BITMAP_DEF GetMenuImage() const override
Function GetMenuImage returns a pointer to an image to be used in menus.
virtual void SetLayer(PCB_LAYER_ID aLayer)
Function SetLayer sets the layer this item is on.
Implementation of conversion functions that require both schematic and board internal units.
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
wxString GetSelectMenuText(EDA_UNITS aUnits) const override
Function GetSelectMenuText returns the text to display to be used in the selection clarification cont...
PCB_LAYER_ID FlipLayer(PCB_LAYER_ID aLayerId, int aCopperLayersCount)
Function FlippedLayerNumber.
Definition: lset.cpp:485
#define DEFAULT_COPPER_LINE_WIDTH
void GRCSegm(EDA_RECT *ClipBox, wxDC *DC, int x1, int y1, int x2, int y2, int width, int aPenSize, COLOR4D Color)
Definition: gr_basic.cpp:312
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
const PCB_DISPLAY_OPTIONS & GetDisplayOptions() const
Function GetDisplayOptions returns the display options current in use Display options are relative to...
bool Contains(const wxPoint &aPoint) const
Function Contains.
This file contains miscellaneous commonly used macros and functions.
PCB_LAYER_ID m_Layer
bool HitTest(const wxPoint &aPosition, int aAccuracy=0) const override
Function HitTest tests if aPosition is contained within or on the bounding box of an item.
PCB_LAYER_ID
A quick note on layer IDs:
void SetHeight(int val)
Definition: eda_rect.h:186
EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
virtual void SwapData(BOARD_ITEM *aImage) override
Swap data between aItem and aImage.
void Flip(const wxPoint &aCentre, bool aFlipLeftRight) override
Function Flip Flip this object, i.e.
virtual BOARD * GetBoard() const
Function GetBoard returns the BOARD in which this BOARD_ITEM resides, or NULL if none.
void SetX(int val)
Definition: eda_rect.h:168
void SetWidth(int val)
Definition: eda_rect.h:180
void GRLine(EDA_RECT *ClipBox, wxDC *DC, int x1, int y1, int x2, int y2, int width, COLOR4D Color, wxPenStyle aStyle)
Definition: gr_basic.cpp:230
const BITMAP_OPAQUE add_pcb_target_xpm[1]
void SetY(int val)
Definition: eda_rect.h:174
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:101
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aUseMils, EDA_DATA_TYPE aType)
Definition: base_units.cpp:127
void Rotate(const wxPoint &aRotCentre, double aAngle) override
Function Rotate Rotate this object.
bool IsLayerVisible(PCB_LAYER_ID aLayer) const
Function IsLayerVisible is a proxy function that calls the correspondent function in m_BoardSettings ...
Definition: class_board.h:431
see class PGM_BASE
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:163
#define _(s)
Definition: 3d_actions.cpp:33
void GRCircle(EDA_RECT *ClipBox, wxDC *DC, int xc, int yc, int r, int width, COLOR4D Color)
Definition: gr_basic.cpp:596
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
int GetShape() const
void Print(PCB_BASE_FRAME *aFrame, wxDC *DC, const wxPoint &offset=ZeroOffset) override
Function Print BOARD_ITEMs have their own color information.
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:166
The common library.
bool Intersects(const EDA_RECT &aRect) const
Function Intersects tests for a common area between rectangles.
PCB_TARGET class definition.
PCB_TARGET(BOARD_ITEM *aParent)
virtual PCB_LAYER_ID GetLayer() const
Function GetLayer returns the primary layer this item is on.
PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes.
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:212
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:40