KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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#include "pcb_target.h"
27
28#include <bitmaps.h>
29#include <board.h>
31#include <base_units.h>
34#include <trigo.h>
35#include <i18n_utility.h>
37#include <eda_draw_frame.h>
38#include <pcb_shape.h>
39
40
42 BOARD_ITEM( aParent, PCB_TARGET_T )
43{
44 m_shape = 0;
45 m_size = pcbIUScale.mmToIU( 5 ); // Gives a decent size
47 m_layer = Edge_Cuts; // a target is on all layers
48}
49
50PCB_TARGET::PCB_TARGET( BOARD_ITEM* aParent, int aShape, PCB_LAYER_ID aLayer, const VECTOR2I& aPos,
51 int aSize, int aWidth ) :
52 BOARD_ITEM( aParent, PCB_TARGET_T )
53{
54 m_shape = aShape;
55 m_layer = aLayer;
56 m_pos = aPos;
57 m_size = aSize;
58 m_lineWidth = aWidth;
59}
60
61
65
66
67bool PCB_TARGET::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
68{
69 int dX = aPosition.x - m_pos.x;
70 int dY = aPosition.y - m_pos.y;
71 int radius = aAccuracy + ( m_size / 2 );
72 return abs( dX ) <= radius && abs( dY ) <= radius;
73}
74
75
76bool PCB_TARGET::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
77{
78 BOX2I arect = aRect;
79 arect.Inflate( aAccuracy );
80
81 if( aContained )
82 return arect.Contains( GetBoundingBox() );
83 else
84 return GetBoundingBox().Intersects( arect );
85}
86
87
88void PCB_TARGET::Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle )
89{
90 RotatePoint( m_pos, aRotCentre, aAngle );
91}
92
93
94void PCB_TARGET::Flip( const VECTOR2I& aCentre, FLIP_DIRECTION aFlipDirection )
95{
96 if( aFlipDirection == FLIP_DIRECTION::LEFT_RIGHT )
97 m_pos.x = aCentre.x - ( m_pos.x - aCentre.x );
98 else
99 m_pos.y = aCentre.y - ( m_pos.y - aCentre.y );
100
102}
103
104
106{
107 BOX2I bBox;
108 bBox.SetX( m_pos.x - m_size / 2 );
109 bBox.SetY( m_pos.y - m_size / 2 );
110 bBox.SetWidth( m_size );
111 bBox.SetHeight( m_size );
112
113 return bBox;
114}
115
116
117std::shared_ptr<SHAPE> PCB_TARGET::GetEffectiveShape( PCB_LAYER_ID aLayer, FLASHING aFlash ) const
118{
119 return std::make_shared<SHAPE_CIRCLE>( m_pos, m_size / 2 );
120}
121
122
123wxString PCB_TARGET::GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const
124{
125 // Targets are on *every* layer by definition
126 return _( "Target" );
127}
128
129
134
135
137{
138 return new PCB_TARGET( *this );
139}
140
141
143{
144 assert( aImage->Type() == PCB_TARGET_T );
145
146 std::swap( *((PCB_TARGET*) this), *((PCB_TARGET*) aImage) );
147}
148
149
150void PCB_TARGET::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
151{
152 aList.emplace_back( _( "PCB Target" ), wxEmptyString );
153
154 aList.emplace_back( _( "Layer" ), GetLayerName() );
155
156 aList.emplace_back( _( "Size" ), aFrame->MessageTextFromValue( GetSize() ) );
157 aList.emplace_back( _( "Width" ), aFrame->MessageTextFromValue( GetWidth() ) );
158 aList.emplace_back( _( "Shape" ), GetShape() == 0 ? wxT( "+" ) : wxT( "X" ) );
159}
160
161
163 int aClearance, int aError, ERROR_LOC aErrorLoc,
164 bool ignoreLineWidth ) const
165{
166 int size = GetShape() ? GetSize() / 1.5 : GetSize() / 2.0;
167 int radius = GetShape() ? GetSize() / 2.0 : GetSize() / 3.0;
168
169 PCB_SHAPE line1, line2;
170 PCB_SHAPE circle( nullptr, SHAPE_T::CIRCLE );
171 line1.SetStart( VECTOR2I( -size, 0.0 ) );
172 line1.SetEnd( VECTOR2I( size, 0.0 ) );
173 line2.SetStart( VECTOR2I( 0.0, -size ) );
174 line2.SetEnd( VECTOR2I( 0.0, size ) );
175 circle.SetEndX( radius );
176
177 if( GetShape() ) // shape x
178 {
179 line1.Rotate( VECTOR2I(0,0), EDA_ANGLE( 45.0, DEGREES_T ) );
180 line2.Rotate( VECTOR2I(0,0), EDA_ANGLE( 45.0, DEGREES_T ) );
181 }
182
183 for( PCB_SHAPE* item: { &line1, &line2, &circle } )
184 {
185 item->SetWidth( GetWidth() );
186 item->Move( GetPosition() );
187 item->TransformShapeToPolygon( aBuffer, aLayer, aClearance, aError, aErrorLoc,
188 ignoreLineWidth );
189 }
190}
191
192
193bool PCB_TARGET::operator==( const BOARD_ITEM& aBoardItem ) const
194{
195 if( aBoardItem.Type() != Type() )
196 return false;
197
198 const PCB_TARGET& other = static_cast<const PCB_TARGET&>( aBoardItem );
199
200 return *this == other;
201}
202
203
204bool PCB_TARGET::operator==( const PCB_TARGET& aOther ) const
205{
206 return m_shape == aOther.m_shape
207 && m_size == aOther.m_size
208 && m_lineWidth == aOther.m_lineWidth
209 && m_layer == aOther.m_layer
210 && m_pos == aOther.m_pos;
211}
212
213
214double PCB_TARGET::Similarity( const BOARD_ITEM& aOther ) const
215{
216 if( aOther.Type() != Type() )
217 return 0.0;
218
219 const PCB_TARGET& other = static_cast<const PCB_TARGET&>( aOther );
220
221 double similarity = 1.0;
222
223 if( GetShape() != other.GetShape() )
224 similarity *= 0.9;
225
226 if( GetSize() != other.GetSize() )
227 similarity *= 0.9;
228
229 if( GetWidth() != other.GetWidth() )
230 similarity *= 0.9;
231
232 if( GetLayer() != other.GetLayer() )
233 similarity *= 0.9;
234
235 if( GetPosition() != other.GetPosition() )
236 similarity *= 0.9;
237
238 return similarity;
239}
240
241static struct PCB_TARGET_DESC
242{
244 {
248 propMgr.AddProperty( new PROPERTY<PCB_TARGET, int>( _HKI( "Size" ),
250 propMgr.AddProperty( new PROPERTY<PCB_TARGET, int>( _HKI( "Width" ),
252
253 auto shape = new PROPERTY<PCB_TARGET, int>( _HKI( "Shape" ),
255 // TODO change the integer to an enum?
256 //shape->SetValues( { { 0, _HKI( "Cross" ) }, { 1, ( "Plus" ) } } );
257 propMgr.AddProperty( shape );
258 }
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:112
BITMAPS
A list of all bitmap identifiers.
@ add_pcb_target
#define DEFAULT_COPPER_LINE_WIDTH
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:79
BOARD_ITEM(BOARD_ITEM *aParent, KICAD_T idtype, PCB_LAYER_ID aLayer=F_Cu)
Definition board_item.h:81
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition board_item.h:232
PCB_LAYER_ID m_layer
Definition board_item.h:453
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition board_item.h:280
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
wxString GetLayerName() const
Return the name of the PCB layer on which the item resides.
constexpr BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition box2.h:558
constexpr void SetHeight(size_type val)
Definition box2.h:292
constexpr bool Contains(const Vec &aPoint) const
Definition box2.h:168
constexpr void SetWidth(size_type val)
Definition box2.h:287
constexpr void SetX(coord_type val)
Definition box2.h:277
constexpr void SetY(coord_type val)
Definition box2.h:282
constexpr bool Intersects(const BOX2< Vec > &aRect) const
Definition box2.h:311
The base class for create windows for drawing purpose.
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition eda_item.cpp:39
void SetStart(const VECTOR2I &aStart)
Definition eda_shape.h:177
void SetEnd(const VECTOR2I &aEnd)
Definition eda_shape.h:219
void Rotate(const VECTOR2I &aRotCentre, const EDA_ANGLE &aAngle) override
Rotate this object.
int m_lineWidth
Definition pcb_target.h:128
wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider, bool aFull) const override
Return a user-visible description string of this item.
int GetShape() const
Definition pcb_target.h:58
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
int GetWidth() const
Definition pcb_target.h:64
PCB_TARGET(BOARD_ITEM *aParent)
void SetWidth(int aWidth)
Definition pcb_target.h:63
void Rotate(const VECTOR2I &aRotCentre, const EDA_ANGLE &aAngle) override
Rotate this object.
void Flip(const VECTOR2I &aCentre, FLIP_DIRECTION aFlipDirection) override
Flip this object, i.e.
void TransformShapeToPolygon(SHAPE_POLY_SET &aBuffer, PCB_LAYER_ID aLayer, int aClearance, int aError, ERROR_LOC aErrorLoc, bool ignoreLineWidth=false) const override
Convert the shape to a closed polygon.
void SetSize(int aSize)
Definition pcb_target.h:60
bool operator==(const PCB_TARGET &aOther) const
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.
void SetShape(int aShape)
Definition pcb_target.h:57
int GetSize() const
Definition pcb_target.h:61
bool HitTest(const VECTOR2I &aPosition, int aAccuracy=0) const override
Test if aPosition is inside or on the boundary of this item.
VECTOR2I m_pos
Definition pcb_target.h:129
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
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.
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
virtual void swapData(BOARD_ITEM *aImage) override
double Similarity(const BOARD_ITEM &aBoardItem) const override
Return a measure of how likely the other object is to represent the same object.
VECTOR2I GetPosition() const override
Definition pcb_target.h:55
Provide class metadata.Helper macro to map type hashes to names.
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
static PROPERTY_MANAGER & Instance()
PROPERTY_BASE & AddProperty(PROPERTY_BASE *aProperty, const wxString &aGroup=wxEmptyString)
Register a property.
Represent a set of closed polygons.
wxString MessageTextFromValue(double aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
A lower-precision version of StringFromValue().
#define _(s)
@ DEGREES_T
Definition eda_angle.h:31
Some functions to handle hotkeys in KiCad.
PCB_LAYER_ID FlipLayer(PCB_LAYER_ID aLayerId, int aCopperLayersCount)
Definition layer_id.cpp:170
FLASHING
Enum used during connectivity building to ensure we do not query connectivity while building the data...
Definition layer_ids.h:184
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
@ Edge_Cuts
Definition layer_ids.h:112
FLIP_DIRECTION
Definition mirror.h:27
@ LEFT_RIGHT
Flip left to right (around the Y axis)
Definition mirror.h:28
#define _HKI(x)
Definition page_info.cpp:44
static struct PCB_TARGET_DESC _PCB_TARGET_DESC
#define TYPE_HASH(x)
Definition property.h:73
@ PT_SIZE
Size expressed in distance units (mm/inch)
Definition property.h:62
#define REGISTER_TYPE(x)
int radius
SHAPE_CIRCLE circle(c.m_circle_center, c.m_circle_radius)
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition trigo.cpp:229
@ PCB_TARGET_T
class PCB_TARGET, a target (graphic item)
Definition typeinfo.h:106
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695