KiCad PCB EDA Suite
Loading...
Searching...
No Matches
board_item.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, [email protected]
5 * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
6 * Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#include <pybind11/pybind11.h>
27
28#include <wx/debug.h>
29#include <wx/msgdlg.h>
30#include <i18n_utility.h>
31#include <macros.h>
32#include <board.h>
34#include <pcb_group.h>
35#include <footprint.h>
36#include <font/font.h>
37
38
40{
41 wxASSERT( m_group == nullptr );
42}
43
44
46{
47 if( Type() == PCB_T )
48 return static_cast<const BOARD*>( this );
49
50 BOARD_ITEM* parent = GetParent();
51
52 if( parent )
53 return parent->GetBoard();
54
55 return nullptr;
56}
57
58
60{
61 if( Type() == PCB_T )
62 return static_cast<BOARD*>( this );
63
64 BOARD_ITEM* parent = GetParent();
65
66 if( parent )
67 return parent->GetBoard();
68
69 return nullptr;
70}
71
72
74{
75 if( GetParentGroup() )
76 return GetParentGroup()->IsLocked();
77
78 const BOARD* board = GetBoard();
79
80 return board && board->GetBoardUse() != BOARD_USE::FPHOLDER && m_isLocked;
81}
82
83
85{
86 wxCHECK( false, STROKE_PARAMS( pcbIUScale.mmToIU( DEFAULT_LINE_WIDTH ) ) );
87}
88
89
91{
92 wxCHECK( false, /* void */ );
93}
94
95
97{
99}
100
101
103{
104 const BOARD* board = GetBoard();
105
106 if( board )
107 return board->GetLayerName( m_layer );
108
109 // If no parent, return standard name
111}
112
113
115{
116 const BOARD* board = GetBoard();
117 LSET layers = GetLayerSet() & board->GetEnabledLayers();
118
119 LSET copperLayers = layers & LSET::AllCuMask();
120 LSET techLayers = layers & LSET::AllTechMask();
121
122 // Try to be smart and useful. Check all copper first.
123 if( (int) copperLayers.count() == board->GetCopperLayerCount() )
124 return _( "all copper layers" );
125
126 for( LSET testLayers : { copperLayers, techLayers, layers } )
127 {
128 for( int bit = PCBNEW_LAYER_ID_START; bit < PCB_LAYER_ID_COUNT; ++bit )
129 {
130 if( testLayers[ bit ] )
131 {
132 wxString layerInfo = board->GetLayerName( static_cast<PCB_LAYER_ID>( bit ) );
133
134 if( testLayers.count() > 1 )
135 layerInfo << wxS( " " ) + _( "and others" );
136
137 return layerInfo;
138 }
139 }
140 }
141
142 // No copper, no technicals: no layer
143 return _( "no layers" );
144}
145
146
147void BOARD_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
148{
149 // Basic fallback
150 aCount = 1;
151 aLayers[0] = m_layer;
152
153 if( IsLocked() )
154 aLayers[aCount++] = LAYER_LOCKED_ITEM_SHADOW;
155}
156
157
159{
161
162 if( parent )
163 parent->Remove( this );
164
165 delete this;
166}
167
168
170{
171}
172
173
175{
176 if( aImage == nullptr )
177 return;
178
179 EDA_ITEM* parent = GetParent();
181
182 SetParentGroup( nullptr );
183 aImage->SetParentGroup( nullptr );
184 swapData( aImage );
185
186 // Restore pointers to be sure they are not broken
187 SetParent( parent );
189}
190
191
193{
194 BOARD_ITEM* dupe = static_cast<BOARD_ITEM*>( Clone() );
195 const_cast<KIID&>( dupe->m_Uuid ) = KIID();
196
197 if( dupe->GetParentGroup() )
198 dupe->GetParentGroup()->AddItem( dupe );
199
200 return dupe;
201}
202
203
205 int aClearance, int aError, ERROR_LOC aErrorLoc,
206 bool ignoreLineWidth ) const
207{
208 wxASSERT_MSG( false, wxT( "Called TransformShapeToPolygon() on unsupported BOARD_ITEM." ) );
209};
210
211
213{
214 if( a->Type() != b->Type() )
215 return a->Type() < b->Type();
216
217 if( a->GetLayerSet() != b->GetLayerSet() )
218 return a->GetLayerSet().Seq() < b->GetLayerSet().Seq();
219
220 if( a->m_Uuid != b->m_Uuid ) // UUIDs *should* always be unique (for valid boards anyway)
221 return a->m_Uuid < b->m_Uuid;
222
223 return a < b; // But just in case; ptrs are guaranteed to be different
224}
225
226
227std::shared_ptr<SHAPE> BOARD_ITEM::GetEffectiveShape( PCB_LAYER_ID aLayer, FLASHING aFlash ) const
228{
229 static std::shared_ptr<SHAPE> shape;
230
232
233 return shape;
234}
235
236
237std::shared_ptr<SHAPE_SEGMENT> BOARD_ITEM::GetEffectiveHoleShape() const
238{
239 static std::shared_ptr<SHAPE_SEGMENT> slot;
240
242
243 return slot;
244}
245
246
248{
249 BOARD_ITEM_CONTAINER* ancestor = GetParent();
250
251 while( ancestor && ancestor->Type() == PCB_GROUP_T )
252 ancestor = ancestor->GetParent();
253
254 if( ancestor && ancestor->Type() == PCB_FOOTPRINT_T )
255 return static_cast<FOOTPRINT*>( ancestor );
256
257 return nullptr;
258}
259
260
262{
263 VECTOR2I pos = GetPosition();
264
265 if( FOOTPRINT* parentFP = GetParentFootprint() )
266 {
267 pos -= parentFP->GetPosition();
268 RotatePoint( pos, -parentFP->GetOrientation() );
269 }
270
271 return pos;
272}
273
274
276{
277 VECTOR2I pos( aPos );
278
279 if( FOOTPRINT* parentFP = GetParentFootprint() )
280 {
281 RotatePoint( pos, parentFP->GetOrientation() );
282 pos += parentFP->GetPosition();
283 }
284
285 SetPosition( pos );
286}
287
288
289void BOARD_ITEM::Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle )
290{
291 wxMessageBox( wxT( "virtual BOARD_ITEM::Rotate used, should not occur" ), GetClass() );
292}
293
294
295void BOARD_ITEM::Flip( const VECTOR2I& aCentre, bool aFlipLeftRight )
296{
297 wxMessageBox( wxT( "virtual BOARD_ITEM::Flip used, should not occur" ), GetClass() );
298}
299
300
302{
303 if( FOOTPRINT* fp = dynamic_cast<FOOTPRINT*>( m_parent ) )
304 return fp->GetReference();
305
306 return m_parent->m_Uuid.AsString();
307}
308
309
310static struct BOARD_ITEM_DESC
311{
313 {
315
316 if( layerEnum.Choices().GetCount() == 0 )
317 {
318 layerEnum.Undefined( UNDEFINED_LAYER );
319
320 for( LSEQ seq = LSET::AllLayersMask().Seq(); seq; ++seq )
321 layerEnum.Map( *seq, LSET::Name( *seq ) );
322 }
323
327
328 propMgr.AddProperty( new PROPERTY<BOARD_ITEM, wxString>( _HKI( "Parent" ),
331
332 propMgr.AddProperty( new PROPERTY<BOARD_ITEM, int>( _HKI( "Position X" ),
333 &BOARD_ITEM::SetX, &BOARD_ITEM::GetX, PROPERTY_DISPLAY::PT_COORD,
335 propMgr.AddProperty( new PROPERTY<BOARD_ITEM, int>( _HKI( "Position Y" ),
336 &BOARD_ITEM::SetY, &BOARD_ITEM::GetY, PROPERTY_DISPLAY::PT_COORD,
340 propMgr.AddProperty( new PROPERTY<BOARD_ITEM, bool>( _HKI( "Locked" ),
343 [=]( INSPECTABLE* aItem ) -> bool
344 {
345 BOARD_ITEM* item = dynamic_cast<BOARD_ITEM*>( aItem );
346 return item && item->GetBoard() && !item->GetBoard()->IsFootprintHolder();
347 } );
348 }
350
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:109
#define DEFAULT_LINE_WIDTH
static struct BOARD_ITEM_DESC _BOARD_ITEM_DESC
Abstract interface for BOARD_ITEMs capable of storing other items inside.
virtual void Remove(BOARD_ITEM *aItem, REMOVE_MODE aMode=REMOVE_MODE::NORMAL)=0
Removes an item from the container.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:77
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:225
void SetParentGroup(PCB_GROUP *aGroup)
Definition: board_item.h:90
int GetY() const
Definition: board_item.h:101
void DeleteStructure()
Delete this object after removing from its parent if it has one.
Definition: board_item.cpp:158
void SwapItemData(BOARD_ITEM *aImage)
Swap data between aItem and aImage.
Definition: board_item.cpp:174
virtual void Flip(const VECTOR2I &aCentre, bool aFlipLeftRight)
Flip this object, i.e.
Definition: board_item.cpp:295
virtual STROKE_PARAMS GetStroke() const
Definition: board_item.cpp:84
virtual void SetLocked(bool aLocked)
Definition: board_item.h:299
PCB_GROUP * GetParentGroup() const
Definition: board_item.h:91
PCB_GROUP * m_group
Definition: board_item.h:392
virtual void SetStroke(const STROKE_PARAMS &aStroke)
Definition: board_item.cpp:90
PCB_LAYER_ID m_layer
Definition: board_item.h:387
bool m_isLocked
Definition: board_item.h:390
wxString GetParentAsString() const
For "parent" property.
Definition: board_item.cpp:301
int GetX() const
Definition: board_item.h:95
virtual void TransformShapeToPolygon(SHAPE_POLY_SET &aBuffer, PCB_LAYER_ID aLayer, int aClearance, int aError, ERROR_LOC aErrorLoc, bool ignoreLineWidth=false) const
Convert the item shape to a closed polygon.
Definition: board_item.cpp:204
virtual void Rotate(const VECTOR2I &aRotCentre, const EDA_ANGLE &aAngle)
Rotate this object.
Definition: board_item.cpp:289
void SetX(int aX)
Definition: board_item.h:117
virtual BOARD_ITEM * Duplicate() const
Create a copy of this BOARD_ITEM.
Definition: board_item.cpp:192
void SetY(int aY)
Definition: board_item.h:123
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition: board_item.h:259
virtual std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER, FLASHING aFlash=FLASHING::DEFAULT) const
Some pad shapes can be complex (rounded/chamfered rectangle), even without considering custom shapes.
Definition: board_item.cpp:227
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
Definition: board_item.cpp:45
FOOTPRINT * GetParentFootprint() const
Definition: board_item.cpp:247
VECTOR2I GetFPRelativePosition() const
Definition: board_item.cpp:261
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
Definition: board_item.h:230
virtual void swapData(BOARD_ITEM *aImage)
Definition: board_item.cpp:169
const KIFONT::METRICS & GetFontMetrics() const
Definition: board_item.cpp:96
void SetFPRelativePosition(const VECTOR2I &aPos)
Definition: board_item.cpp:275
virtual bool IsLocked() const
Definition: board_item.cpp:73
BOARD_ITEM_CONTAINER * GetParent() const
Definition: board_item.h:203
virtual void ViewGetLayers(int aLayers[], int &aCount) const override
Return the all the layers within the VIEW the object is painted on.
Definition: board_item.cpp:147
virtual wxString layerMaskDescribe() const
Return a string (to be shown to the user) describing a layer mask.
Definition: board_item.cpp:114
wxString GetLayerName() const
Return the name of the PCB layer on which the item resides.
Definition: board_item.cpp:102
virtual std::shared_ptr< SHAPE_SEGMENT > GetEffectiveHoleShape() const
Definition: board_item.cpp:237
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:276
bool IsFootprintHolder() const
Find out if the board is being used to hold a single footprint for editing/viewing.
Definition: board.h:306
LSET GetEnabledLayers() const
A proxy function that calls the corresponding function in m_BoardSettings.
Definition: board.cpp:649
BOARD_USE GetBoardUse() const
Get what the board use is.
Definition: board.h:295
static wxString GetStandardLayerName(PCB_LAYER_ID aLayerId)
Return an "English Standard" name of a PCB layer when given aLayerNumber.
Definition: board.h:742
int GetCopperLayerCount() const
Definition: board.cpp:625
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition: board.cpp:536
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
virtual VECTOR2I GetPosition() const
Definition: eda_item.h:239
virtual void SetPosition(const VECTOR2I &aPos)
Definition: eda_item.h:240
const KIID m_Uuid
Definition: eda_item.h:482
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:97
virtual void SetParent(EDA_ITEM *aParent)
Definition: eda_item.h:100
virtual EDA_ITEM * Clone() const
Create a duplicate of this item with linked list members set to NULL.
Definition: eda_item.cpp:82
virtual wxString GetClass() const =0
Return the class name.
EDA_ITEM * m_parent
Linked list: Link (parent struct)
Definition: eda_item.h:485
ENUM_MAP & Map(T aValue, const wxString &aName)
Definition: property.h:646
static ENUM_MAP< T > & Instance()
Definition: property.h:640
ENUM_MAP & Undefined(T aValue)
Definition: property.h:653
wxPGChoices & Choices()
Definition: property.h:689
Class that other classes need to inherit from, in order to be inspectable.
Definition: inspectable.h:36
static const METRICS & Default()
Definition: font.cpp:52
Definition: kiid.h:49
wxString AsString() const
Definition: kiid.cpp:257
LSEQ is a sequence (and therefore also a set) of PCB_LAYER_IDs.
Definition: layer_ids.h:513
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:556
static LSET AllLayersMask()
Definition: lset.cpp:817
LSEQ Seq(const PCB_LAYER_ID *aWishListSequence, unsigned aCount) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition: lset.cpp:418
static LSET AllTechMask()
Return a mask holding all technical layers (no CU layer) on both side.
Definition: lset.cpp:850
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:782
static const wxChar * Name(PCB_LAYER_ID aLayerId)
Return the fixed name association with aLayerId.
Definition: lset.cpp:89
A set of BOARD_ITEMs (i.e., without duplicates).
Definition: pcb_group.h:51
virtual bool AddItem(BOARD_ITEM *aItem)
Add item to group.
Definition: pcb_group.cpp:79
PROPERTY_BASE & SetAvailableFunc(std::function< bool(INSPECTABLE *)> aFunc)
Set a callback function to determine whether an object provides this property.
Definition: property.h:249
PROPERTY_BASE & SetIsHiddenFromLibraryEditors(bool aIsHidden=true)
Definition: property.h:306
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:85
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:87
PROPERTY_BASE & AddProperty(PROPERTY_BASE *aProperty, const wxString &aGroup=wxEmptyString)
Register a property.
Represent a set of closed polygons.
Simple container to manage line stroke parameters.
Definition: stroke_params.h:81
#define _HKI(x)
#define _(s)
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
Some functions to handle hotkeys in KiCad.
FLASHING
Enum used during connectivity building to ensure we do not query connectivity while building the data...
Definition: layer_ids.h:148
@ LAYER_LOCKED_ITEM_SHADOW
shadow layer for locked items
Definition: layer_ids.h:240
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
@ PCBNEW_LAYER_ID_START
Definition: layer_ids.h:64
@ UNDEFINED_LAYER
Definition: layer_ids.h:61
@ PCB_LAYER_ID_COUNT
Definition: layer_ids.h:138
This file contains miscellaneous commonly used macros and functions.
#define UNIMPLEMENTED_FOR(type)
Definition: macros.h:96
Class to handle a set of BOARD_ITEMs.
#define TYPE_HASH(x)
Definition: property.h:64
#define IMPLEMENT_ENUM_TO_WXANY(type)
Definition: property.h:740
#define NO_SETTER(owner, type)
Definition: property.h:751
#define REGISTER_TYPE(x)
Definition: property_mgr.h:366
bool operator()(const BOARD_ITEM *a, const BOARD_ITEM *b) const
Definition: board_item.cpp:212
constexpr int mmToIU(double mm) const
Definition: base_units.h:89
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:228
@ PCB_T
Definition: typeinfo.h:82
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:108
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition: typeinfo.h:86