KiCad PCB EDA Suite
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, jp.charras at wanadoo.fr
5  * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
6  * Copyright (C) 2012 Wayne Stambaugh <[email protected]>
7  * Copyright (C) 1992-2022 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 
27 #include <bitmaps.h>
28 #include <board.h>
29 #include <board_design_settings.h>
30 #include <pcb_target.h>
31 #include <base_units.h>
34 #include <trigo.h>
35 #include <i18n_utility.h>
36 #include <geometry/shape_circle.h>
37 #include <eda_draw_frame.h>
38 #include <pcb_shape.h>
39 
41  BOARD_ITEM( aParent, PCB_TARGET_T )
42 {
43  m_shape = 0;
44  m_size = Millimeter2iu( 5 ); // Gives a decent size
46  m_layer = Edge_Cuts; // a target is on all layers
47 }
48 
49 PCB_TARGET::PCB_TARGET( BOARD_ITEM* aParent, int aShape, PCB_LAYER_ID aLayer,
50  const wxPoint& aPos, int aSize, int aWidth ) :
51  BOARD_ITEM( aParent, PCB_TARGET_T )
52 {
53  m_shape = aShape;
54  m_layer = aLayer;
55  m_pos = aPos;
56  m_size = aSize;
57  m_lineWidth = aWidth;
58 }
59 
60 
62 {
63 }
64 
65 
66 bool PCB_TARGET::HitTest( const wxPoint& aPosition, int aAccuracy ) const
67 {
68  int dX = aPosition.x - m_pos.x;
69  int dY = aPosition.y - m_pos.y;
70  int radius = aAccuracy + ( m_size / 2 );
71  return abs( dX ) <= radius && abs( dY ) <= radius;
72 }
73 
74 
75 bool PCB_TARGET::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
76 {
77  EDA_RECT arect = aRect;
78  arect.Inflate( aAccuracy );
79 
80  if( aContained )
81  return arect.Contains( GetBoundingBox() );
82  else
83  return GetBoundingBox().Intersects( arect );
84 }
85 
86 
87 void PCB_TARGET::Rotate(const wxPoint& aRotCentre, double aAngle)
88 {
89  RotatePoint( &m_pos, aRotCentre, aAngle );
90 }
91 
92 
93 void PCB_TARGET::Flip(const wxPoint& aCentre, bool aFlipLeftRight )
94 {
95  if( aFlipLeftRight )
96  m_pos.x = aCentre.x - ( m_pos.x - aCentre.x );
97  else
98  m_pos.y = aCentre.y - ( m_pos.y - aCentre.y );
99 
100  SetLayer( FlipLayer( GetLayer(), GetBoard()->GetCopperLayerCount() ) );
101 }
102 
103 
105 {
106  EDA_RECT bBox;
107  bBox.SetX( m_pos.x - m_size / 2 );
108  bBox.SetY( m_pos.y - m_size / 2 );
109  bBox.SetWidth( m_size );
110  bBox.SetHeight( m_size );
111 
112  return bBox;
113 }
114 
115 
116 std::shared_ptr<SHAPE> PCB_TARGET::GetEffectiveShape( PCB_LAYER_ID aLayer, FLASHING aFlash ) const
117 {
118  return std::make_shared<SHAPE_CIRCLE>( m_pos, m_size / 2 );
119 }
120 
121 
122 wxString PCB_TARGET::GetSelectMenuText( EDA_UNITS aUnits ) const
123 {
124  // Targets are on *every* layer by definition
125  return _( "Target" );
126 }
127 
128 
130 {
132 }
133 
134 
136 {
137  return new PCB_TARGET( *this );
138 }
139 
140 
142 {
143  assert( aImage->Type() == PCB_TARGET_T );
144 
145  std::swap( *((PCB_TARGET*) this), *((PCB_TARGET*) aImage) );
146 }
147 
148 
149 void PCB_TARGET::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
150 {
151  EDA_UNITS units = aFrame->GetUserUnits();
152 
153  aList.emplace_back( _( "PCB Target" ), wxEmptyString );
154 
155  aList.emplace_back( _( "Layer" ), GetLayerName() );
156 
157  aList.emplace_back( _( "Size" ), MessageTextFromValue( units, GetSize() ) );
158  aList.emplace_back( _( "Width" ), MessageTextFromValue( units, GetWidth() ) );
159  aList.emplace_back( _( "Shape" ), GetShape() == 0 ? wxT( "+" ) : wxT( "X" ) );
160 }
161 
162 
164  PCB_LAYER_ID aLayer, int aClearanceValue,
165  int aError, ERROR_LOC aErrorLoc,
166  bool ignoreLineWidth ) const
167 {
168  int size = GetShape() ? GetSize() / 1.5 : GetSize() / 2.0;
169  int radius = GetShape() ? GetSize() / 2.0 : GetSize() / 3.0;
170 
171  PCB_SHAPE line1, line2;
172  PCB_SHAPE circle( nullptr, SHAPE_T::CIRCLE );
173  line1.SetStart( wxPoint( -size, 0.0 ) );
174  line1.SetEnd( wxPoint( size, 0.0 ) );
175  line2.SetStart( wxPoint( 0.0, -size ) );
176  line2.SetEnd( wxPoint( 0.0, size ) );
177  circle.SetEndX( radius );
178 
179  if( GetShape() ) // shape x
180  {
181  line1.Rotate( wxPoint(0,0), 450 );
182  line2.Rotate( wxPoint(0,0), 450 );
183  }
184 
185  for( PCB_SHAPE* item: { &line1, &line2, &circle } )
186  {
187  item->SetWidth( GetWidth() );
188  item->Move( GetPosition() );
189  item->TransformShapeWithClearanceToPolygon( aCornerBuffer, aLayer, aClearanceValue,
190  aError, aErrorLoc, ignoreLineWidth );
191  }
192 }
193 
194 
195 static struct PCB_TARGET_DESC
196 {
198  {
202  propMgr.AddProperty( new PROPERTY<PCB_TARGET, int>( _HKI( "Size" ),
204  propMgr.AddProperty( new PROPERTY<PCB_TARGET, int>( _HKI( "Width" ),
206 
207  auto shape = new PROPERTY<PCB_TARGET, int>( _HKI( "Shape" ),
209  // TODO change the integer to an enum?
210  //shape->SetValues( { { 0, _HKI( "Cross" ) }, { 1, ( "Plus" ) } } );
211  propMgr.AddProperty( shape );
212  }
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aAddUnitLabel, EDA_DATA_TYPE aType)
Convert a value to a string using double notation.
Definition: base_units.cpp:104
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:65
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
Definition: pcb_target.cpp:129
#define TYPE_HASH(x)
Definition: property.h:59
void SetEnd(const wxPoint &aEnd)
Definition: eda_shape.h:135
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition: board_item.h:164
Implementation of conversion functions that require both schematic and board internal units.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:49
void SetSize(int aSize)
Definition: pcb_target.h:61
wxString GetSelectMenuText(EDA_UNITS aUnits) const override
Return the text to display to be used in the selection clarification context menu when multiple items...
Definition: pcb_target.cpp:122
int GetSize() const
Definition: pcb_target.h:62
PCB_LAYER_ID FlipLayer(PCB_LAYER_ID aLayerId, int aCopperLayersCount)
Definition: lset.cpp:530
#define DEFAULT_COPPER_LINE_WIDTH
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:229
The base class for create windows for drawing purpose.
virtual void Rotate(const wxPoint &aRotCentre, double aAngle) override
Rotate this object.
Definition: pcb_shape.cpp:109
void GetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList) override
Populate aList of MSG_PANEL_ITEM objects with it's internal state for display purposes.
Definition: pcb_target.cpp:149
#define REGISTER_TYPE(x)
Definition: property_mgr.h:248
bool Contains(const wxPoint &aPoint) const
Definition: eda_rect.cpp:57
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
void SetStart(const wxPoint &aStart)
Definition: eda_shape.h:110
wxPoint GetPosition() const override
Definition: pcb_target.h:56
bool HitTest(const wxPoint &aPosition, int aAccuracy=0) const override
Test if aPosition is inside or on the boundary of this item.
Definition: pcb_target.cpp:66
int GetWidth() const
Definition: pcb_target.h:65
Display value expressed in distance units (mm/inch)
Definition: property.h:53
void SetHeight(int val)
Definition: eda_rect.h:185
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: pcb_target.cpp:135
virtual void SwapData(BOARD_ITEM *aImage) override
Swap data between aItem and aImage.
Definition: pcb_target.cpp:141
Represent a set of closed polygons.
void Flip(const wxPoint &aCentre, bool aFlipLeftRight) override
Flip this object, i.e.
Definition: pcb_target.cpp:93
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
#define _(s)
void SetShape(int aShape)
Definition: pcb_target.h:58
void SetX(int val)
Definition: eda_rect.h:167
void SetWidth(int val)
Definition: eda_rect.h:179
FLASHING
Enum used during connectivity building to ensure we do not query connectivity while building the data...
Definition: layer_ids.h:153
void SetY(int val)
Definition: eda_rect.h:173
static struct PCB_TARGET_DESC _PCB_TARGET_DESC
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:104
int m_lineWidth
Definition: pcb_target.h:124
Some functions to handle hotkeys in KiCad.
wxPoint m_pos
Definition: pcb_target.h:125
EDA_UNITS
Definition: eda_units.h:38
void SetEndX(int x)
Definition: eda_shape.h:147
void Rotate(const wxPoint &aRotCentre, double aAngle) override
Rotate this object.
Definition: pcb_target.cpp:87
std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer, FLASHING aFlash=FLASHING::DEFAULT) const override
Some pad shapes can be complex (rounded/chamfered rectangle), even without considering custom shapes.
Definition: pcb_target.cpp:116
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:32
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:65
void AddProperty(PROPERTY_BASE *aProperty)
Register a property.
Handle the component boundary box.
Definition: eda_rect.h:42
int GetShape() const
Definition: pcb_target.h:59
void TransformShapeWithClearanceToPolygon(SHAPE_POLY_SET &aCornerBuffer, PCB_LAYER_ID aLayer, int aClearanceValue, int aError, ERROR_LOC aErrorLoc, bool ignoreLineWidth=false) const override
Convert the shape to a closed polygon.
Definition: pcb_target.cpp:163
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:99
bool Intersects(const EDA_RECT &aRect) const
Test for a common area between rectangles.
Definition: eda_rect.cpp:150
void SetWidth(int aWidth)
Definition: pcb_target.h:64
PCB_LAYER_ID m_layer
Definition: board_item.h:313
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
Definition: board_item.cpp:36
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:62
PCB_TARGET(BOARD_ITEM *aParent)
Definition: pcb_target.cpp:40
wxString GetLayerName() const
Return the name of the PCB layer on which the item resides.
Definition: board_item.cpp:75
static constexpr int Millimeter2iu(double mm)
#define _HKI(x)
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:143
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
const EDA_RECT GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: pcb_target.cpp:104
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Inflate the rectangle horizontally by dx and vertically by dy.
Definition: eda_rect.cpp:364
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:112