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 <lset.h>
35#include <pcb_group.h>
36#include <pcb_generator.h>
37#include <footprint.h>
38#include <font/font.h>
39
40
42{
43 wxASSERT( m_group == nullptr );
44}
45
46
48{
49 if( Type() == PCB_T )
50 return static_cast<const BOARD*>( this );
51
52 BOARD_ITEM* parent = GetParent();
53
54 if( parent )
55 return parent->GetBoard();
56
57 return nullptr;
58}
59
60
62{
63 if( Type() == PCB_T )
64 return static_cast<BOARD*>( this );
65
66 BOARD_ITEM* parent = GetParent();
67
68 if( parent )
69 return parent->GetBoard();
70
71 return nullptr;
72}
73
74
76{
78 return true;
79
80 const BOARD* board = GetBoard();
81
82 return board && board->GetBoardUse() != BOARD_USE::FPHOLDER && m_isLocked;
83}
84
85
87{
88 wxFAIL_MSG( wxString( "GetStroke() not defined by " ) + GetClass() );
89
91}
92
93
95{
96 wxFAIL_MSG( wxString( "SetStroke() not defined by " ) + GetClass() );
97}
98
99
101{
103}
104
105
107{
108 const BOARD* board = GetBoard();
109
110 if( board )
111 return board->GetLayerSet().count();
112
113 return 64;
114}
115
116
118{
119 const BOARD* board = GetBoard();
120
121 if( board )
122 return board->GetCopperLayerCount();
123
124 return 32;
125}
126
127
129{
130 const BOARD* board = GetBoard();
131
132 if( board )
133 return board->GetLayerSet();
134
135 return LSET::AllLayersMask();
136}
137
138
140{
141 if( const BOARD* board = GetBoard() )
142 return board->GetLayerName( m_layer );
143
144 // If no parent, return standard name
146}
147
148
150{
151 if( ( GetLayerSet() & LSET::SideSpecificMask() ).any() )
152 return true;
153
154 if( const BOARD* board = GetBoard() )
155 {
156 LAYER_T principalLayerType = board->GetLayerType( m_layer );
157
158 if( principalLayerType == LT_FRONT || principalLayerType == LT_BACK )
159 return true;
160 }
161
162 return false;
163}
164
165
167{
168 const BOARD* board = GetBoard();
169 LSET layers = GetLayerSet() & board->GetEnabledLayers();
170
171 LSET copperLayers = layers & LSET::AllCuMask();
172 LSET techLayers = layers & LSET::AllTechMask();
173
174 // Try to be smart and useful. Check all copper first.
175 if( (int) copperLayers.count() == board->GetCopperLayerCount() )
176 return _( "all copper layers" );
177
178 for( LSET testLayers : { copperLayers, techLayers, layers } )
179 {
180 for( int bit = PCBNEW_LAYER_ID_START; bit < PCB_LAYER_ID_COUNT; ++bit )
181 {
182 if( testLayers[ bit ] )
183 {
184 wxString layerInfo = board->GetLayerName( static_cast<PCB_LAYER_ID>( bit ) );
185
186 if( testLayers.count() > 1 )
187 layerInfo << wxS( " " ) + _( "and others" );
188
189 return layerInfo;
190 }
191 }
192 }
193
194 // No copper, no technicals: no layer
195 return _( "no layers" );
196}
197
198
199void BOARD_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
200{
201 // Basic fallback
202 aCount = 1;
203 aLayers[0] = m_layer;
204
205 if( IsLocked() )
206 aLayers[aCount++] = LAYER_LOCKED_ITEM_SHADOW;
207}
208
209
211{
213
214 if( parent )
215 parent->Remove( this );
216
217 delete this;
218}
219
220
222{
223}
224
225
227{
228 if( aImage == nullptr )
229 return;
230
231 EDA_ITEM* parent = GetParent();
233
234 SetParentGroup( nullptr );
235 aImage->SetParentGroup( nullptr );
236 swapData( aImage );
237
238 // Restore pointers to be sure they are not broken
239 SetParent( parent );
241}
242
243
245{
246 BOARD_ITEM* dupe = static_cast<BOARD_ITEM*>( Clone() );
247 const_cast<KIID&>( dupe->m_Uuid ) = KIID();
248
249 if( dupe->GetParentGroup() )
250 dupe->GetParentGroup()->AddItem( dupe );
251
252 return dupe;
253}
254
255
257 int aClearance, int aError, ERROR_LOC aErrorLoc,
258 bool ignoreLineWidth ) const
259{
260 wxASSERT_MSG( false, wxT( "Called TransformShapeToPolygon() on unsupported BOARD_ITEM." ) );
261};
262
263
265{
266 if( a->Type() != b->Type() )
267 return a->Type() < b->Type();
268
269 if( a->GetLayerSet() != b->GetLayerSet() )
270 return a->GetLayerSet().Seq() < b->GetLayerSet().Seq();
271
272 if( a->m_Uuid != b->m_Uuid ) // UUIDs *should* always be unique (for valid boards anyway)
273 return a->m_Uuid < b->m_Uuid;
274
275 return a < b; // But just in case; ptrs are guaranteed to be different
276}
277
278
279std::shared_ptr<SHAPE> BOARD_ITEM::GetEffectiveShape( PCB_LAYER_ID aLayer, FLASHING aFlash ) const
280{
281 static std::shared_ptr<SHAPE> shape;
282
284
285 return shape;
286}
287
288
289std::shared_ptr<SHAPE_SEGMENT> BOARD_ITEM::GetEffectiveHoleShape() const
290{
291 static std::shared_ptr<SHAPE_SEGMENT> slot;
292
294
295 return slot;
296}
297
298
300{
301 // EDA_ITEM::IsType is too slow here.
302 auto isContainer = []( BOARD_ITEM_CONTAINER* aTest )
303 {
304 switch( aTest->Type() )
305 {
306 case PCB_GROUP_T:
307 case PCB_GENERATOR_T:
308 case PCB_TABLE_T:
309 return true;
310
311 default:
312 return false;
313 }
314 };
315
316 BOARD_ITEM_CONTAINER* ancestor = GetParent();
317
318 while( ancestor && isContainer( ancestor ) )
319 ancestor = ancestor->GetParent();
320
321 if( ancestor && ancestor->Type() == PCB_FOOTPRINT_T )
322 return static_cast<FOOTPRINT*>( ancestor );
323
324 return nullptr;
325}
326
327
329{
330 VECTOR2I pos = GetPosition();
331
332 if( FOOTPRINT* parentFP = GetParentFootprint() )
333 {
334 pos -= parentFP->GetPosition();
335 RotatePoint( pos, -parentFP->GetOrientation() );
336 }
337
338 return pos;
339}
340
341
343{
344 VECTOR2I pos( aPos );
345
346 if( FOOTPRINT* parentFP = GetParentFootprint() )
347 {
348 RotatePoint( pos, parentFP->GetOrientation() );
349 pos += parentFP->GetPosition();
350 }
351
352 SetPosition( pos );
353}
354
355
356void BOARD_ITEM::Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle )
357{
358 wxMessageBox( wxT( "virtual BOARD_ITEM::Rotate used, should not occur" ), GetClass() );
359}
360
361
362void BOARD_ITEM::Flip( const VECTOR2I& aCentre, FLIP_DIRECTION aFlipDirection )
363{
364 wxMessageBox( wxT( "virtual BOARD_ITEM::Flip used, should not occur" ), GetClass() );
365}
366
367
368void BOARD_ITEM::Mirror( const VECTOR2I& aCentre, FLIP_DIRECTION aFlipDirection )
369{
370 wxMessageBox( wxT( "virtual BOARD_ITEM::Mirror used, should not occur" ), GetClass() );
371}
372
373
375{
376 if( FOOTPRINT* fp = dynamic_cast<FOOTPRINT*>( m_parent ) )
377 return fp->GetReference();
378
379 return m_parent->m_Uuid.AsString();
380}
381
382
383static struct BOARD_ITEM_DESC
384{
386 {
388
389 if( layerEnum.Choices().GetCount() == 0 )
390 {
391 layerEnum.Undefined( UNDEFINED_LAYER );
392
393 for( PCB_LAYER_ID layer : LSET::AllLayersMask().Seq() )
394 layerEnum.Map( layer, LSET::Name( layer ) );
395 }
396
400
401 propMgr.AddProperty( new PROPERTY<BOARD_ITEM, wxString>( _HKI( "Parent" ),
405
406 propMgr.AddProperty( new PROPERTY<BOARD_ITEM, int>( _HKI( "Position X" ),
407 &BOARD_ITEM::SetX, &BOARD_ITEM::GetX, PROPERTY_DISPLAY::PT_COORD,
409 propMgr.AddProperty( new PROPERTY<BOARD_ITEM, int>( _HKI( "Position Y" ),
410 &BOARD_ITEM::SetY, &BOARD_ITEM::GetY, PROPERTY_DISPLAY::PT_COORD,
414 propMgr.AddProperty( new PROPERTY<BOARD_ITEM, bool>( _HKI( "Locked" ),
417 [=]( INSPECTABLE* aItem ) -> bool
418 {
419 BOARD_ITEM* item = dynamic_cast<BOARD_ITEM*>( aItem );
420 return item && item->GetBoard() && !item->GetBoard()->IsFootprintHolder();
421 } );
422 }
424
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
Definition: approximation.h:32
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:108
LAYER_T
The allowed types of layers, same as Specctra DSN spec.
Definition: board.h:153
@ LT_FRONT
Definition: board.h:160
@ LT_BACK
Definition: board.h:161
#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:79
virtual void Mirror(const VECTOR2I &aCentre, FLIP_DIRECTION aFlipDirection)
Mirror this object relative to a given horizontal axis the layer is not changed.
Definition: board_item.cpp:368
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:237
void SetParentGroup(PCB_GROUP *aGroup)
Definition: board_item.h:89
int GetY() const
Definition: board_item.h:100
void DeleteStructure()
Delete this object after removing from its parent if it has one.
Definition: board_item.cpp:210
void SwapItemData(BOARD_ITEM *aImage)
Swap data between aItem and aImage.
Definition: board_item.cpp:226
virtual STROKE_PARAMS GetStroke() const
Definition: board_item.cpp:86
virtual void SetLocked(bool aLocked)
Definition: board_item.h:328
virtual int BoardLayerCount() const
Return the total number of layers for the board that this item resides on.
Definition: board_item.cpp:106
PCB_GROUP * GetParentGroup() const
Definition: board_item.h:90
PCB_GROUP * m_group
Definition: board_item.h:441
virtual void SetStroke(const STROKE_PARAMS &aStroke)
Definition: board_item.cpp:94
PCB_LAYER_ID m_layer
Definition: board_item.h:436
bool m_isLocked
Definition: board_item.h:439
wxString GetParentAsString() const
For "parent" property.
Definition: board_item.cpp:374
int GetX() const
Definition: board_item.h:94
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:256
virtual void Rotate(const VECTOR2I &aRotCentre, const EDA_ANGLE &aAngle)
Rotate this object.
Definition: board_item.cpp:356
void SetX(int aX)
Definition: board_item.h:116
virtual BOARD_ITEM * Duplicate() const
Create a copy of this BOARD_ITEM.
Definition: board_item.cpp:244
void SetY(int aY)
Definition: board_item.h:122
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition: board_item.h:288
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:279
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
Definition: board_item.cpp:47
FOOTPRINT * GetParentFootprint() const
Definition: board_item.cpp:299
VECTOR2I GetFPRelativePosition() const
Definition: board_item.cpp:328
virtual LSET BoardLayerSet() const
Return the LSET for the board that this item resides on.
Definition: board_item.cpp:128
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
Definition: board_item.h:257
virtual void swapData(BOARD_ITEM *aImage)
Definition: board_item.cpp:221
const KIFONT::METRICS & GetFontMetrics() const
Definition: board_item.cpp:100
void SetFPRelativePosition(const VECTOR2I &aPos)
Definition: board_item.cpp:342
virtual bool IsLocked() const
Definition: board_item.cpp:75
BOARD_ITEM_CONTAINER * GetParent() const
Definition: board_item.h:215
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:199
bool IsSideSpecific() const
Definition: board_item.cpp:149
virtual int BoardCopperLayerCount() const
Return the total number of copper layers for the board that this item resides on.
Definition: board_item.cpp:117
virtual wxString layerMaskDescribe() const
Return a string (to be shown to the user) describing a layer mask.
Definition: board_item.cpp:166
wxString GetLayerName() const
Return the name of the PCB layer on which the item resides.
Definition: board_item.cpp:139
virtual std::shared_ptr< SHAPE_SEGMENT > GetEffectiveHoleShape() const
Definition: board_item.cpp:289
virtual void Flip(const VECTOR2I &aCentre, FLIP_DIRECTION aFlipDirection)
Flip this object, i.e.
Definition: board_item.cpp:362
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:290
bool IsFootprintHolder() const
Find out if the board is being used to hold a single footprint for editing/viewing.
Definition: board.h:320
LSET GetEnabledLayers() const
A proxy function that calls the corresponding function in m_BoardSettings.
Definition: board.cpp:775
BOARD_USE GetBoardUse() const
Get what the board use is.
Definition: board.h:309
static wxString GetStandardLayerName(PCB_LAYER_ID aLayerId)
Return an "English Standard" name of a PCB layer when given aLayerNumber.
Definition: board.h:786
int GetCopperLayerCount() const
Definition: board.cpp:738
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition: board.cpp:579
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
virtual VECTOR2I GetPosition() const
Definition: eda_item.h:243
virtual void SetPosition(const VECTOR2I &aPos)
Definition: eda_item.h:244
const KIID m_Uuid
Definition: eda_item.h:489
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:101
virtual void SetParent(EDA_ITEM *aParent)
Definition: eda_item.h:104
virtual EDA_ITEM * Clone() const
Create a duplicate of this item with linked list members set to NULL.
Definition: eda_item.cpp:85
virtual wxString GetClass() const =0
Return the class name.
EDA_ITEM * m_parent
Linked list: Link (parent struct)
Definition: eda_item.h:500
ENUM_MAP & Map(T aValue, const wxString &aName)
Definition: property.h:669
static ENUM_MAP< T > & Instance()
Definition: property.h:663
ENUM_MAP & Undefined(T aValue)
Definition: property.h:676
wxPGChoices & Choices()
Definition: property.h:712
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:238
LSET is a set of PCB_LAYER_IDs.
Definition: lset.h:36
static LSET AllLayersMask()
Definition: lset.cpp:701
static LSET AllTechMask()
Return a mask holding all technical layers (no CU layer) on both side.
Definition: lset.cpp:734
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:676
static LSET SideSpecificMask()
Definition: lset.cpp:785
LSEQ Seq(const LSEQ &aSequence) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition: lset.cpp:410
static wxString Name(PCB_LAYER_ID aLayerId)
Return the fixed name association with aLayerId.
Definition: lset.cpp:183
A set of BOARD_ITEMs (i.e., without duplicates).
Definition: pcb_group.h:52
virtual bool AddItem(BOARD_ITEM *aItem)
Add item to group.
Definition: pcb_group.cpp:81
PROPERTY_BASE & SetAvailableFunc(std::function< bool(INSPECTABLE *)> aFunc)
Set a callback function to determine whether an object provides this property.
Definition: property.h:257
PROPERTY_BASE & SetIsHiddenFromPropertiesManager(bool aHide=true)
Definition: property.h:300
PROPERTY_BASE & SetIsHiddenFromLibraryEditors(bool aIsHidden=true)
Definition: property.h:314
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:79
#define _HKI(x)
#define _(s)
Some functions to handle hotkeys in KiCad.
constexpr PCB_LAYER_ID PCBNEW_LAYER_ID_START
Definition: layer_ids.h:138
FLASHING
Enum used during connectivity building to ensure we do not query connectivity while building the data...
Definition: layer_ids.h:147
@ 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
@ UNDEFINED_LAYER
Definition: layer_ids.h:61
@ PCB_LAYER_ID_COUNT
Definition: layer_ids.h:135
This file contains miscellaneous commonly used macros and functions.
#define UNIMPLEMENTED_FOR(type)
Definition: macros.h:96
FLIP_DIRECTION
Definition: mirror.h:27
Class to handle a set of BOARD_ITEMs.
#define TYPE_HASH(x)
Definition: property.h:71
#define IMPLEMENT_ENUM_TO_WXANY(type)
Definition: property.h:763
#define NO_SETTER(owner, type)
Definition: property.h:774
#define REGISTER_TYPE(x)
Definition: property_mgr.h:371
bool operator()(const BOARD_ITEM *a, const BOARD_ITEM *b) const
Definition: board_item.cpp:264
constexpr int mmToIU(double mm) const
Definition: base_units.h:88
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_T
Definition: typeinfo.h:82
@ PCB_GENERATOR_T
class PCB_GENERATOR, generator on a layer
Definition: typeinfo.h:91
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:110
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition: typeinfo.h:86
@ PCB_TABLE_T
class PCB_TABLE, table of PCB_TABLECELLs
Definition: typeinfo.h:94