KiCad PCB EDA Suite
pcbnew/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 <dick@softplc.com>
6  * Copyright (C) 2012 Wayne Stambaugh <stambaughw@verizon.net>
7  * Copyright (C) 1992-2020 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 <pcb_target.h>
30 #include <base_units.h>
33 #include <trigo.h>
34 #include <i18n_utility.h>
35 #include <geometry/shape_circle.h>
36 #include <eda_draw_frame.h>
37 
39  BOARD_ITEM( aParent, PCB_TARGET_T )
40 {
41  m_shape = 0;
42  m_size = Millimeter2iu( 5 ); // Gives a decent size
44  m_layer = Edge_Cuts; // a target is on all layers
45 }
46 
47 PCB_TARGET::PCB_TARGET( BOARD_ITEM* aParent, int aShape, PCB_LAYER_ID aLayer,
48  const wxPoint& aPos, int aSize, int aWidth ) :
49  BOARD_ITEM( aParent, PCB_TARGET_T )
50 {
51  m_shape = aShape;
52  m_layer = aLayer;
53  m_pos = aPos;
54  m_size = aSize;
55  m_lineWidth = aWidth;
56 }
57 
58 
60 {
61 }
62 
63 
64 bool PCB_TARGET::HitTest( const wxPoint& aPosition, int aAccuracy ) const
65 {
66  int dX = aPosition.x - m_pos.x;
67  int dY = aPosition.y - m_pos.y;
68  int radius = aAccuracy + ( m_size / 2 );
69  return abs( dX ) <= radius && abs( dY ) <= radius;
70 }
71 
72 
73 bool PCB_TARGET::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
74 {
75  EDA_RECT arect = aRect;
76  arect.Inflate( aAccuracy );
77 
78  if( aContained )
79  return arect.Contains( GetBoundingBox() );
80  else
81  return GetBoundingBox().Intersects( arect );
82 }
83 
84 
85 void PCB_TARGET::Rotate(const wxPoint& aRotCentre, double aAngle)
86 {
87  RotatePoint( &m_pos, aRotCentre, aAngle );
88 }
89 
90 
91 void PCB_TARGET::Flip(const wxPoint& aCentre, bool aFlipLeftRight )
92 {
93  if( aFlipLeftRight )
94  m_pos.x = aCentre.x - ( m_pos.x - aCentre.x );
95  else
96  m_pos.y = aCentre.y - ( m_pos.y - aCentre.y );
97 
98  SetLayer( FlipLayer( GetLayer(), GetBoard()->GetCopperLayerCount() ) );
99 }
100 
101 
103 {
104  EDA_RECT bBox;
105  bBox.SetX( m_pos.x - m_size / 2 );
106  bBox.SetY( m_pos.y - m_size / 2 );
107  bBox.SetWidth( m_size );
108  bBox.SetHeight( m_size );
109 
110  return bBox;
111 }
112 
113 
114 std::shared_ptr<SHAPE> PCB_TARGET::GetEffectiveShape( PCB_LAYER_ID aLayer ) const
115 {
116  return std::make_shared<SHAPE_CIRCLE>( m_pos, m_size / 2 );
117 }
118 
119 
120 wxString PCB_TARGET::GetSelectMenuText( EDA_UNITS aUnits ) const
121 {
122  // Targets are on *every* layer by definition
123  return _( "Target" );
124 }
125 
126 
128 {
129  return add_pcb_target_xpm;
130 }
131 
132 
134 {
135  return new PCB_TARGET( *this );
136 }
137 
138 
140 {
141  assert( aImage->Type() == PCB_TARGET_T );
142 
143  std::swap( *((PCB_TARGET*) this), *((PCB_TARGET*) aImage) );
144 }
145 
146 
147 void PCB_TARGET::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
148 {
149  EDA_UNITS units = aFrame->GetUserUnits();
150 
151  aList.emplace_back( _( "PCB Target" ), wxEmptyString );
152 
153  aList.emplace_back( _( "Layer" ), GetLayerName() );
154 
155  aList.emplace_back( _( "Size" ), MessageTextFromValue( units, GetSize() ) );
156  aList.emplace_back( _( "Width" ), MessageTextFromValue( units, GetWidth() ) );
157  aList.emplace_back( _( "Shape" ), GetShape() == 0 ? "+" : "X" );
158 }
159 
160 
161 static struct PCB_TARGET_DESC
162 {
164  {
168  propMgr.AddProperty( new PROPERTY<PCB_TARGET, int>( _HKI( "Size" ),
170  propMgr.AddProperty( new PROPERTY<PCB_TARGET, int>( _HKI( "Width" ),
172 
173  auto shape = new PROPERTY<PCB_TARGET, int>( _HKI( "Shape" ),
175  // TODO change the integer to an enum?
176  //shape->SetValues( { { 0, _HKI( "Cross" ) }, { 1, ( "Plus" ) } } );
177  propMgr.AddProperty( shape );
178  }
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:125
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:66
#define TYPE_HASH(x)
Definition: property.h:57
PNG memory record (file in memory).
Definition: bitmap_def.h:29
BITMAP_DEF GetMenuImage() const override
Return a pointer to an image to be used in menus.
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition: board_item.h:194
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:82
void SetSize(int aSize)
Definition: pcb_target.h:66
static struct PCB_TARGET_DESC _PCB_TARGET_DESC
wxString GetSelectMenuText(EDA_UNITS aUnits) const override
Return the text to display to be used in the selection clarification context menu when multiple items...
int GetSize() const
Definition: pcb_target.h:67
PCB_LAYER_ID FlipLayer(PCB_LAYER_ID aLayerId, int aCopperLayersCount)
Definition: lset.cpp:521
#define DEFAULT_COPPER_LINE_WIDTH
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:228
The base class for create windows for drawing purpose.
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.
#define REGISTER_TYPE(x)
Definition: property_mgr.h:249
bool Contains(const wxPoint &aPoint) const
Definition: eda_rect.cpp:57
bool HitTest(const wxPoint &aPosition, int aAccuracy=0) const override
Test if aPosition is contained within or on the bounding box of an item.
int GetWidth() const
Definition: pcb_target.h:70
std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer) const override
Some pad shapes can be complex (rounded/chamfered rectangle), even without considering custom shapes.
PCB_LAYER_ID
A quick note on layer IDs:
Display value expressed in distance units (mm/inch)
Definition: property.h:51
void SetHeight(int val)
Definition: eda_rect.h:181
EDA_ITEM * Clone() const override
Create 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
Flip this object, i.e.
virtual BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
Definition: board_item.cpp:46
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
void SetShape(int aShape)
Definition: pcb_target.h:63
void SetX(int val)
Definition: eda_rect.h:163
void SetWidth(int val)
Definition: eda_rect.h:175
const BITMAP_OPAQUE add_pcb_target_xpm[1]
void SetY(int val)
Definition: eda_rect.h:169
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:104
int m_lineWidth
Definition: pcb_target.h:39
Some functions to handle hotkeys in KiCad.
wxPoint m_pos
Definition: pcb_target.h:40
int m_shape
Definition: pcb_target.h:37
EDA_UNITS
Definition: eda_units.h:38
void Rotate(const wxPoint &aRotCentre, double aAngle) override
Rotate this object.
#define _(s)
Definition: 3d_actions.cpp:33
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:64
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:149
int m_size
Definition: pcb_target.h:38
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:69
PCB_LAYER_ID m_layer
Definition: board_item.h:363
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:63
PCB_TARGET(BOARD_ITEM *aParent)
wxString GetLayerName() const
Return the name of the PCB layer on which the item resides.
Definition: board_item.cpp:60
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:173
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.
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Inflate the rectangle horizontally by dx and vertically by dy.
Definition: eda_rect.cpp:363
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:162