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 
45 
47  BOARD_ITEM( aParent, PCB_TARGET_T )
48 {
49  m_Shape = 0;
50  m_Size = Millimeter2iu( 5 ); // Gives a decent size
51  m_Width = Millimeter2iu( DEFAULT_COPPER_LINE_WIDTH );
52  m_Layer = Edge_Cuts; // a target is on all layers
53 }
54 
55 PCB_TARGET::PCB_TARGET( BOARD_ITEM* aParent, int aShape, PCB_LAYER_ID aLayer,
56  const wxPoint& aPos, int aSize, int aWidth ) :
57  BOARD_ITEM( aParent, PCB_TARGET_T )
58 {
59  m_Shape = aShape;
60  m_Layer = aLayer;
61  m_Pos = aPos;
62  m_Size = aSize;
63  m_Width = aWidth;
64 }
65 
66 
68 {
69 }
70 
71 
72 /* Print PCB_TARGET object: 2 segments + 1 circle
73  * The circle radius is half the radius of the target
74  * 2 lines have length the diameter of the target
75  */
76 void PCB_TARGET::Print( PCB_BASE_FRAME* aFrame, wxDC* DC, const wxPoint& offset )
77 {
78  int radius, ox, oy, width;
79  int dx1, dx2, dy1, dy2;
80 
81  ox = m_Pos.x + offset.x;
82  oy = m_Pos.y + offset.y;
83 
84  BOARD* brd = GetBoard( );
85 
86  if( brd->IsLayerVisible( m_Layer ) == false )
87  return;
88 
89  auto gcolor = aFrame->Settings().Colors().GetLayerColor( m_Layer );
90  auto displ_opts = aFrame->GetDisplayOptions();
91  bool filled = displ_opts.m_DisplayDrawItemsFill;
92  width = m_Width;
93 
94  radius = m_Size / 3;
95 
96  if( GetShape() ) // shape X
97  radius = m_Size / 2;
98 
99  if( filled )
100  GRCircle( nullptr, DC, ox, oy, radius, width, gcolor );
101  else
102  {
103  GRCircle( nullptr, DC, ox, oy, radius + (width / 2), gcolor );
104  GRCircle( nullptr, DC, ox, oy, radius - (width / 2), gcolor );
105  }
106 
107 
108  radius = m_Size / 2;
109  dx1 = radius;
110  dy1 = 0;
111  dx2 = 0;
112  dy2 = radius;
113 
114  if( GetShape() ) // shape X
115  {
116  dx1 = dy1 = radius;
117  dx2 = dx1;
118  dy2 = -dy1;
119  }
120 
121  if( filled )
122  {
123  GRLine( nullptr, DC, ox - dx1, oy - dy1, ox + dx1, oy + dy1, width, gcolor );
124  GRLine( nullptr, DC, ox - dx2, oy - dy2, ox + dx2, oy + dy2, width, gcolor );
125  }
126  else
127  {
128  GRCSegm( nullptr, DC, ox - dx1, oy - dy1, ox + dx1, oy + dy1, width, gcolor );
129  GRCSegm( nullptr, DC, ox - dx2, oy - dy2, ox + dx2, oy + dy2, width, gcolor );
130  }
131 }
132 
133 
134 bool PCB_TARGET::HitTest( const wxPoint& aPosition, int aAccuracy ) const
135 {
136  int dX = aPosition.x - m_Pos.x;
137  int dY = aPosition.y - m_Pos.y;
138  int radius = aAccuracy + ( m_Size / 2 );
139  return abs( dX ) <= radius && abs( dY ) <= radius;
140 }
141 
142 
143 bool PCB_TARGET::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
144 {
145  EDA_RECT arect = aRect;
146  arect.Inflate( aAccuracy );
147 
148  if( aContained )
149  return arect.Contains( GetBoundingBox() );
150  else
151  return GetBoundingBox().Intersects( arect );
152 }
153 
154 
155 void PCB_TARGET::Rotate(const wxPoint& aRotCentre, double aAngle)
156 {
157  RotatePoint( &m_Pos, aRotCentre, aAngle );
158 }
159 
160 
161 void PCB_TARGET::Flip(const wxPoint& aCentre, bool aFlipLeftRight )
162 {
163  if( aFlipLeftRight )
164  m_Pos.x = aCentre.x - ( m_Pos.x - aCentre.x );
165  else
166  m_Pos.y = aCentre.y - ( m_Pos.y - aCentre.y );
167 
168  SetLayer( FlipLayer( GetLayer() ) );
169 }
170 
171 
173 {
174  EDA_RECT bBox;
175  bBox.SetX( m_Pos.x - m_Size/2 );
176  bBox.SetY( m_Pos.y - m_Size/2 );
177  bBox.SetWidth( m_Size );
178  bBox.SetHeight( m_Size );
179 
180  return bBox;
181 }
182 
183 
185 {
186  // Targets are on *every* layer by definition
187  return wxString::Format( _( "Target size %s" ), MessageTextFromValue( aUnits, m_Size ) );
188 }
189 
190 
191 BITMAP_DEF PCB_TARGET::GetMenuImage() const
192 {
193  return add_pcb_target_xpm;
194 }
195 
196 
198 {
199  return new PCB_TARGET( *this );
200 }
201 
202 
204 {
205  assert( aImage->Type() == PCB_TARGET_T );
206 
207  std::swap( *((PCB_TARGET*) this), *((PCB_TARGET*) aImage) );
208 }
209 
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.
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
PCB_LAYER_ID FlipLayer(PCB_LAYER_ID aLayerId, int aCopperLayersCount)
Function FlippedLayerNumber.
Definition: lset.cpp:475
#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:206
const PCB_DISPLAY_OPTIONS & GetDisplayOptions() const
Function GetDisplayOptions returns the display options current in use Display options are relative to...
#define abs(a)
Definition: auxiliary.h:84
bool Contains(const wxPoint &aPoint) const
Function Contains.
This file contains miscellaneous commonly used macros and functions.
wxString MessageTextFromValue(EDA_UNITS_T aUnits, int aValue, bool aUseMils)
Definition: base_units.cpp:125
PCB_LAYER_ID m_Layer
COLOR4D GetLayerColor(LAYER_NUM aLayer) const
Function GetLayerColor.
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.
PCB_GENERAL_SETTINGS & Settings()
void Flip(const wxPoint &aCentre, bool aFlipLeftRight) override
Function Flip Flip this object, i.e.
wxString GetSelectMenuText(EDA_UNITS_T aUnits) const override
Function GetSelectMenuText returns the text to display to be used in the selection clarification cont...
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
#define _(s)
COLORS_DESIGN_SETTINGS & Colors()
void SetY(int val)
Definition: eda_rect.h:174
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:100
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:450
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
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:161
void GRCircle(EDA_RECT *ClipBox, wxDC *DC, int xc, int yc, int r, int width, COLOR4D Color)
Definition: gr_basic.cpp:600
Class 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.
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:163
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.
class PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
EDA_UNITS_T
Definition: common.h:133
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:210