KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_point.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, see <https://www.gnu.org/licenses/>.
21 */
22#include "pcb_point.h"
23
24#include <api/api_enums.h>
25#include <api/api_utils.h>
26#include <api/board/board_types.pb.h>
27#include <base_units.h>
28#include <bitmaps.h>
29#include <board.h>
30#include <eda_draw_frame.h>
31#include <footprint.h>
34#include <geometry/shape_rect.h>
35#include <pcb_shape.h>
38#include <trigo.h>
39#include <view/view.h>
40#include <properties/property.h>
42
43
44static const double DEFAULT_PT_SIZE_MM = 1.0;
45
46
48 BOARD_ITEM( aParent, PCB_POINT_T ),
49 m_libPos( 0, 0 ),
50 m_pos( 0, 0 ),
52{
54}
55
56
57PCB_POINT::PCB_POINT( BOARD_ITEM* aParent, const VECTOR2I& aPos, int aSize ) :
58 BOARD_ITEM( aParent, PCB_POINT_T ),
59 m_libPos( 0, 0 ),
60 m_pos( 0, 0 ),
61 m_size( aSize )
62{
63 SetPosition( aPos );
64}
65
66
68{
69 if( const FOOTPRINT* fp = GetParentFootprint() )
70 m_pos = fp->GetTransform().Apply( m_libPos );
71 else
73}
74
75
77{
78 if( const FOOTPRINT* fp = GetParentFootprint() )
79 m_libPos = fp->GetTransform().InverseApply( aPos );
80 else
81 m_libPos = aPos;
82
84}
85
86
88{
89 BOARD_ITEM::SetParent( aParent );
91}
92
93
98
99
101{
102 if( a->GetLayer() != b->GetLayer() )
103 return a->GetLayer() < b->GetLayer();
104
105 if( a->GetPosition().x != b->GetPosition().x )
106 return a->GetPosition().x < b->GetPosition().x;
107
108 if( a->GetPosition().y != b->GetPosition().y )
109 return a->GetPosition().y < b->GetPosition().y;
110
111 if( a->GetSize() != b->GetSize() )
112 return a->GetSize() < b->GetSize();
113
114 if( a->m_Uuid != b->m_Uuid )
115 return a->m_Uuid < b->m_Uuid;
116
117 return a < b;
118}
119
120
121bool PCB_POINT::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
122{
123 // Compute the hit on the bars of the X
124 const int size = GetSize() / 2;
125 const VECTOR2I pos = GetPosition();
126 const SEG loc = SEG( aPosition, aPosition );
127
128 const SEG seg1 = SEG( pos - VECTOR2I{ size, size }, pos + VECTOR2I{ size, size } );
129 const SEG seg2 = SEG( pos - VECTOR2I{ size, -size }, pos + VECTOR2I{ size, -size } );
130 const SHAPE_CIRCLE circle( pos, size / 2 );
131
132 bool hit = seg1.Collide( loc, aAccuracy ) || seg2.Collide( loc, aAccuracy ) || circle.Collide( loc, aAccuracy );
133
134 return hit;
135}
136
137
138bool PCB_POINT::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
139{
140 return KIGEOM::BoxHitTest( aRect, GetBoundingBox(), aContained, aAccuracy );
141}
142
143
144std::vector<int> PCB_POINT::ViewGetLayers() const
145{
146 const PCB_LAYER_ID layer = GetLayer();
147
148 std::vector<int> layers = {
149 POINT_LAYER_FOR( layer ),
150 };
151
152 if( IsLocked() )
153 layers.push_back( LAYER_LOCKED_ITEM_SHADOW );
154
155 return layers;
156}
157
158
159double PCB_POINT::ViewGetLOD( int aLayer, const KIGFX::VIEW* aView ) const
160{
161 // All points hidden
162 if( !aView->IsLayerVisible( LAYER_POINTS ) )
163 return LOD_HIDE;
164
165 // Hide if the "main" layer is not shown
166 if( !aView->IsLayerVisible( m_layer ) )
167 return LOD_HIDE;
168
169 return LOD_SHOW;
170}
171
172
173void PCB_POINT::Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle )
174{
175 VECTOR2I newPos = GetPosition();
176 RotatePoint( newPos, aRotCentre, aAngle );
177 SetPosition( newPos );
178}
179
180
181void PCB_POINT::Flip( const VECTOR2I& aCentre, FLIP_DIRECTION aFlipDirection )
182{
183 if( const FOOTPRINT* fp = GetParentFootprint() )
184 {
185 // Mirror the library-frame position (rotation-independent).
186 const VECTOR2I libAxis = fp->GetTransform().InverseApply( aCentre );
187 MIRROR( m_libPos, libAxis, aFlipDirection );
189 }
190 else
191 {
192 VECTOR2I newPos = GetPosition();
193 MIRROR( newPos, aCentre, aFlipDirection );
194 SetPosition( newPos );
195 }
196
198}
199
200
205
206
208{
209 return std::make_shared<SHAPE_RECT>( GetBoundingBox() );
210}
211
212
213wxString PCB_POINT::GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const
214{
215 return _( "Point" );
216}
217
218
223
224
226{
227 return new PCB_POINT( *this );
228}
229
230
231void PCB_POINT::CopyFrom( const BOARD_ITEM* aOther )
232{
233 wxCHECK( aOther && aOther->Type() == PCB_POINT_T, /* void */ );
234 *this = *static_cast<const PCB_POINT*>( aOther );
235}
236
237
239{
240 assert( aOther->Type() == PCB_POINT_T );
241
242 std::swap( *((PCB_POINT*) this), *((PCB_POINT*) aOther) );
243}
244
245
246void PCB_POINT::Serialize( google::protobuf::Any& aContainer ) const
247{
248 kiapi::board::types::ReferencePoint point;
249
250 point.mutable_id()->set_value( m_Uuid.AsStdString() );
252 kiapi::common::PackVector2( *point.mutable_position(), m_pos );
253 point.mutable_size()->set_value_nm( m_size );
254 point.set_locked( IsLocked() ? kiapi::common::types::LockedState::LS_LOCKED
255 : kiapi::common::types::LockedState::LS_UNLOCKED );
256
257 if( FOOTPRINT* parent = GetParentFootprint() )
258 point.mutable_parent()->set_value( parent->m_Uuid.AsStdString() );
259 else if( const BOARD* board = GetBoard() )
260 point.mutable_parent()->set_value( board->m_Uuid.AsStdString() );
261
262 kiapi::common::PackCustomProperties( point.mutable_custom_properties(), *this );
263 aContainer.PackFrom( point );
264}
265
266
267bool PCB_POINT::Deserialize( const google::protobuf::Any& aContainer )
268{
269 kiapi::board::types::ReferencePoint point;
270
271 if( !aContainer.UnpackTo( &point ) )
272 return false;
273
274 SetUuidDirect( KIID( point.id().value() ) );
276 SetPosition( kiapi::common::UnpackVector2( point.position() ) );
277 SetSize( point.size().value_nm() );
278 SetLocked( point.locked() == kiapi::common::types::LockedState::LS_LOCKED );
279 kiapi::common::UnpackCustomProperties( point.custom_properties(), *this );
280
281 return true;
282}
283
284
285void PCB_POINT::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
286{
287 aList.emplace_back( _( "PCB Point" ), wxEmptyString );
288
289 aList.emplace_back( _( "Position X" ), aFrame->MessageTextFromValue( GetPosition().x ) );
290 aList.emplace_back( _( "Position Y" ), aFrame->MessageTextFromValue( GetPosition().y ) );
291 aList.emplace_back( _( "Size" ), aFrame->MessageTextFromValue( GetSize() ) );
292 aList.emplace_back( _( "Layer" ), GetLayerName() );
293}
294
295
297 int aClearance, int aError, ERROR_LOC aErrorLoc,
298 bool ignoreLineWidth ) const
299{
300}
301
302
303bool PCB_POINT::operator==( const BOARD_ITEM& aBoardItem ) const
304{
305 if( aBoardItem.Type() != Type() )
306 return false;
307
308 const PCB_POINT& other = static_cast<const PCB_POINT&>( aBoardItem );
309
310 return *this == other;
311}
312
313
314bool PCB_POINT::operator==( const PCB_POINT& aOther ) const
315{
316 return GetPosition() == aOther.GetPosition() && m_size == aOther.m_size;
317}
318
319
320double PCB_POINT::Similarity( const BOARD_ITEM& aOther ) const
321{
322 if( aOther.Type() != Type() )
323 return 0.0;
324
325 const PCB_POINT& other = static_cast<const PCB_POINT&>( aOther );
326
327 double similarity = 1.0;
328
329 if( GetPosition() == other.GetPosition() )
330 similarity *= 0.9;
331
332 if( m_size != other.m_size )
333 similarity *= 0.9;
334
335 if( GetLayer() != other.GetLayer() )
336 similarity *= 0.9;
337
338 return similarity;
339}
340
341static struct PCB_POINT_DESC
342{
343 // clang-format off
345 {
347 // PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
350
351 propMgr.AddProperty( new PROPERTY<PCB_POINT, int>( _HKI( "Size" ),
353 }
354 // clang-format on
KICOMMON_API types::KiCadObjectType ToProtoEnum(KICAD_T aValue)
KICOMMON_API KICAD_T FromProtoEnum(types::KiCadObjectType aValue)
Definition api_enums.cpp:55
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:121
BITMAPS
A list of all bitmap identifiers.
BOX2< VECTOR2I > BOX2I
Definition box2.h:927
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
BOARD_ITEM(BOARD_ITEM *aParent, KICAD_T idtype, PCB_LAYER_ID aLayer=F_Cu)
Definition board_item.h:86
friend class BOARD
Definition board_item.h:578
void SetUuidDirect(const KIID &aUuid)
Raw UUID assignment.
void SetLocked(bool aLocked) override
Definition board_item.h:417
PCB_LAYER_ID m_layer
Definition board_item.h:571
bool IsLocked() const override
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition board_item.h:374
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
FOOTPRINT * GetParentFootprint() const
wxString GetLayerName() const
Return the name of the PCB layer on which the item resides.
static constexpr BOX2< VECTOR2I > ByCenter(const VECTOR2I &aCenter, const SizeVec &aSize)
Definition box2.h:72
The base class for create windows for drawing purpose.
const KIID m_Uuid
Definition eda_item.h:597
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
virtual void SetParent(EDA_ITEM *aParent)
Definition eda_item.cpp:153
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition eda_item.cpp:84
static constexpr double LOD_HIDE
Return this constant from ViewGetLOD() to hide the item unconditionally.
Definition view_item.h:176
static constexpr double LOD_SHOW
Return this constant from ViewGetLOD() to show the item unconditionally.
Definition view_item.h:181
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition view.h:63
bool IsLayerVisible(int aLayer) const
Return information about visibility of a particular layer.
Definition view.h:427
Definition kiid.h:46
A PCB_POINT is a 0-dimensional point that is used to mark a position on a PCB, or more usually a foot...
Definition pcb_point.h:39
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.
PCB_POINT(BOARD_ITEM *aParent)
Definition pcb_point.cpp:47
std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER, FLASHING aFlash=FLASHING::DEFAULT, DRC_CONSTRAINT_T aUsage=NULL_CONSTRAINT) const override
Some pad shapes can be complex (rounded/chamfered rectangle), even without considering custom shapes.
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
void Flip(const VECTOR2I &aCentre, FLIP_DIRECTION aFlipDirection) override
Flip this object, i.e.
VECTOR2I m_pos
Definition pcb_point.h:143
std::vector< int > ViewGetLayers() const override
Return the all the layers within the VIEW the object is painted on.
bool operator==(const PCB_POINT &aOther) const
int GetSize() const
Definition pcb_point.h:69
void recomputePosition()
Recompute the cached board position from the library position and parent transform.
Definition pcb_point.cpp:67
void SetParent(EDA_ITEM *aParent) override
Definition pcb_point.cpp:87
double ViewGetLOD(int aLayer, const KIGFX::VIEW *aView) const override
Return the level of detail (LOD) of the item.
void SetPosition(const VECTOR2I &aPos) override
Definition pcb_point.cpp:76
bool HitTest(const VECTOR2I &aPosition, int aAccuracy=0) const override
Test if aPosition is inside or on the boundary of this item.
wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider, bool aFull) const override
Return a user-visible description string of this item.
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
void Rotate(const VECTOR2I &aRotCentre, const EDA_ANGLE &aAngle) override
Rotate this object.
virtual void swapData(BOARD_ITEM *aImage) override
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.
void SetSize(const int aSize)
Definition pcb_point.h:68
void OnFootprintTransformed() override
Refresh the cached board position after the parent footprint's transform changed.
Definition pcb_point.cpp:94
double Similarity(const BOARD_ITEM &aBoardItem) const override
Return a measure of how likely the other object is to represent the same object.
void CopyFrom(const BOARD_ITEM *aOther) override
VECTOR2I m_libPos
Definition pcb_point.h:141
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
VECTOR2I GetPosition() const override
Definition pcb_point.h:60
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.
Definition seg.h:38
bool Collide(const SEG &aSeg, int aClearance, int *aActual=nullptr) const
Definition seg.cpp:538
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().
DRC_CONSTRAINT_T
Definition drc_rule.h:49
#define _(s)
a few functions useful in geometry calculations.
PCB_LAYER_ID FlipLayer(PCB_LAYER_ID aLayerId, int aCopperLayersCount)
Definition layer_id.cpp:179
FLASHING
Enum used during connectivity building to ensure we do not query connectivity while building the data...
Definition layer_ids.h:180
#define POINT_LAYER_FOR(boardLayer)
Definition layer_ids.h:394
@ LAYER_POINTS
PCB reference/manual snap points visibility.
Definition layer_ids.h:317
@ LAYER_LOCKED_ITEM_SHADOW
Shadow layer for locked items.
Definition layer_ids.h:303
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
constexpr void MIRROR(T &aPoint, const T &aMirrorRef)
Updates aPoint with the mirror of aPoint relative to the aMirrorRef.
Definition mirror.h:41
FLIP_DIRECTION
Definition mirror.h:23
bool BoxHitTest(const VECTOR2I &aHitPoint, const BOX2I &aHittee, int aAccuracy)
Perform a point-to-box hit test.
KICOMMON_API void PackCustomProperties(google::protobuf::RepeatedPtrField< types::CustomProperty > *aOutput, const EDA_ITEM &aItem)
KICOMMON_API VECTOR2I UnpackVector2(const types::Vector2 &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void PackVector2(types::Vector2 &aOutput, const VECTOR2I &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void UnpackCustomProperties(const google::protobuf::RepeatedPtrField< types::CustomProperty > &aInput, EDA_ITEM &aItem)
#define _HKI(x)
Definition page_info.cpp:40
static const double DEFAULT_PT_SIZE_MM
Definition pcb_point.cpp:44
static struct PCB_POINT_DESC _PCB_POINT_DESC
#define TYPE_HASH(x)
Definition property.h:74
@ PT_SIZE
Size expressed in distance units (mm/inch)
Definition property.h:63
#define REGISTER_TYPE(x)
bool operator()(const PCB_POINT *a, const PCB_POINT *b) const
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:225
@ PCB_POINT_T
class PCB_POINT, a 0-dimensional point
Definition typeinfo.h:105
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683