KiCad PCB EDA Suite
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-2022 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
36
38{
39 wxASSERT( m_group == nullptr );
40}
41
42
44{
45 if( Type() == PCB_T )
46 return static_cast<const BOARD*>( this );
47
48 BOARD_ITEM* parent = GetParent();
49
50 if( parent )
51 return parent->GetBoard();
52
53 return nullptr;
54}
55
56
58{
59 if( Type() == PCB_T )
60 return static_cast<BOARD*>( this );
61
62 BOARD_ITEM* parent = GetParent();
63
64 if( parent )
65 return parent->GetBoard();
66
67 return nullptr;
68}
69
70
72{
73 if( GetParentGroup() )
74 return GetParentGroup()->IsLocked();
75
76 const BOARD* board = GetBoard();
77
78 return board && board->GetBoardUse() != BOARD_USE::FPHOLDER && m_isLocked;
79}
80
81
83{
84 wxCHECK( false, STROKE_PARAMS( pcbIUScale.mmToIU( DEFAULT_LINE_WIDTH ) ) );
85}
86
87
89{
90 wxCHECK( false, /* void */ );
91}
92
93
95{
96 const BOARD* board = GetBoard();
97
98 if( board )
99 return board->GetLayerName( m_layer );
100
101 // If no parent, return standard name
103}
104
105
107{
108 const BOARD* board = GetBoard();
109 LSET layers = GetLayerSet() & board->GetEnabledLayers();
110
111 LSET copperLayers = layers & LSET::AllCuMask();
112 LSET techLayers = layers & LSET::AllTechMask();
113
114 // Try to be smart and useful. Check all copper first.
115 if( (int) copperLayers.count() == board->GetCopperLayerCount() )
116 return _( "all copper layers" );
117
118 for( LSET testLayers : { copperLayers, techLayers, layers } )
119 {
120 for( int bit = PCBNEW_LAYER_ID_START; bit < PCB_LAYER_ID_COUNT; ++bit )
121 {
122 if( testLayers[ bit ] )
123 {
124 wxString layerInfo = board->GetLayerName( static_cast<PCB_LAYER_ID>( bit ) );
125
126 if( testLayers.count() > 1 )
127 layerInfo << wxS( " " ) + _( "and others" );
128
129 return layerInfo;
130 }
131 }
132 }
133
134 // No copper, no technicals: no layer
135 return _( "no layers" );
136}
137
138
139void BOARD_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
140{
141 // Basic fallback
142 aCount = 1;
143 aLayers[0] = m_layer;
144
145 if( IsLocked() )
146 aLayers[aCount++] = LAYER_LOCKED_ITEM_SHADOW;
147}
148
149
151{
153
154 if( parent )
155 parent->Remove( this );
156
157 delete this;
158}
159
160
162{
163}
164
165
167{
168 if( aImage == nullptr )
169 return;
170
171 EDA_ITEM* parent = GetParent();
173
174 SetParentGroup( nullptr );
175 aImage->SetParentGroup( nullptr );
176 swapData( aImage );
177
178 // Restore pointers to be sure they are not broken
179 SetParent( parent );
181}
182
183
185{
186 BOARD_ITEM* dupe = static_cast<BOARD_ITEM*>( Clone() );
187 const_cast<KIID&>( dupe->m_Uuid ) = KIID();
188
189 if( dupe->GetParentGroup() )
190 dupe->GetParentGroup()->AddItem( dupe );
191
192 return static_cast<BOARD_ITEM*>( dupe );
193}
194
195
197 int aClearance, int aError, ERROR_LOC aErrorLoc,
198 bool ignoreLineWidth ) const
199{
200 wxASSERT_MSG( false, wxT( "Called TransformShapeToPolygon() on unsupported BOARD_ITEM." ) );
201};
202
203
205{
206 if( a->Type() != b->Type() )
207 return a->Type() < b->Type();
208
209 if( a->GetLayerSet() != b->GetLayerSet() )
210 return a->GetLayerSet().Seq() < b->GetLayerSet().Seq();
211
212 if( a->m_Uuid != b->m_Uuid ) // UUIDs *should* always be unique (for valid boards anyway)
213 return a->m_Uuid < b->m_Uuid;
214
215 return a < b; // But just in case; ptrs are guaranteed to be different
216}
217
218
219std::shared_ptr<SHAPE> BOARD_ITEM::GetEffectiveShape( PCB_LAYER_ID aLayer, FLASHING aFlash ) const
220{
221 static std::shared_ptr<SHAPE> shape;
222
224
225 return shape;
226}
227
228
229std::shared_ptr<SHAPE_SEGMENT> BOARD_ITEM::GetEffectiveHoleShape() const
230{
231 static std::shared_ptr<SHAPE_SEGMENT> slot;
232
234
235 return slot;
236}
237
238
240{
241 BOARD_ITEM_CONTAINER* ancestor = GetParent();
242
243 while( ancestor && ancestor->Type() == PCB_GROUP_T )
244 ancestor = ancestor->GetParent();
245
246 return ( ancestor && ancestor->Type() == PCB_FOOTPRINT_T ) ? ancestor : nullptr;
247}
248
249
250void BOARD_ITEM::Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle )
251{
252 wxMessageBox( wxT( "virtual BOARD_ITEM::Rotate used, should not occur" ), GetClass() );
253}
254
255
256void BOARD_ITEM::Flip( const VECTOR2I& aCentre, bool aFlipLeftRight )
257{
258 wxMessageBox( wxT( "virtual BOARD_ITEM::Flip used, should not occur" ), GetClass() );
259}
260
261
262static struct BOARD_ITEM_DESC
263{
265 {
267
268 if( layerEnum.Choices().GetCount() == 0 )
269 {
270 layerEnum.Undefined( UNDEFINED_LAYER );
271
272 for( LSEQ seq = LSET::AllLayersMask().Seq(); seq; ++seq )
273 layerEnum.Map( *seq, LSET::Name( *seq ) );
274 }
275
279
280 propMgr.AddProperty( new PROPERTY<BOARD_ITEM, int>( _HKI( "Position X" ),
283 propMgr.AddProperty( new PROPERTY<BOARD_ITEM, int>( _HKI( "Position Y" ),
288 propMgr.AddProperty( new PROPERTY<BOARD_ITEM, bool>( _HKI( "Locked" ),
290 }
292
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:58
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:180
void SetParentGroup(PCB_GROUP *aGroup)
Definition: board_item.h:71
int GetY() const
Definition: board_item.h:82
void DeleteStructure()
Delete this object after removing from its parent if it has one.
Definition: board_item.cpp:150
void SwapItemData(BOARD_ITEM *aImage)
Swap data between aItem and aImage.
Definition: board_item.cpp:166
virtual void Flip(const VECTOR2I &aCentre, bool aFlipLeftRight)
Flip this object, i.e.
Definition: board_item.cpp:256
virtual STROKE_PARAMS GetStroke() const
Definition: board_item.cpp:82
virtual void SetLocked(bool aLocked)
Definition: board_item.h:254
PCB_GROUP * GetParentGroup() const
Definition: board_item.h:72
PCB_GROUP * m_group
Definition: board_item.h:334
virtual void SetStroke(const STROKE_PARAMS &aStroke)
Definition: board_item.cpp:88
PCB_LAYER_ID m_layer
Definition: board_item.h:329
bool m_isLocked
Definition: board_item.h:332
int GetX() const
Definition: board_item.h:76
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:196
virtual void Rotate(const VECTOR2I &aRotCentre, const EDA_ANGLE &aAngle)
Rotate this object.
Definition: board_item.cpp:250
void SetX(int aX)
Definition: board_item.h:98
virtual BOARD_ITEM * Duplicate() const
Create a copy of this BOARD_ITEM.
Definition: board_item.cpp:184
void SetY(int aY)
Definition: board_item.h:104
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition: board_item.h:214
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:219
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
Definition: board_item.cpp:43
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
Definition: board_item.h:185
virtual void swapData(BOARD_ITEM *aImage)
Definition: board_item.cpp:161
virtual bool IsLocked() const
Definition: board_item.cpp:71
BOARD_ITEM_CONTAINER * GetParentFootprint() const
Definition: board_item.cpp:239
BOARD_ITEM_CONTAINER * GetParent() const
Definition: board_item.h:163
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:139
virtual wxString layerMaskDescribe() const
Return a string (to be shown to the user) describing a layer mask.
Definition: board_item.cpp:106
wxString GetLayerName() const
Return the name of the PCB layer on which the item resides.
Definition: board_item.cpp:94
virtual std::shared_ptr< SHAPE_SEGMENT > GetEffectiveHoleShape() const
Definition: board_item.cpp:229
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:265
LSET GetEnabledLayers() const
A proxy function that calls the corresponding function in m_BoardSettings.
Definition: board.cpp:569
BOARD_USE GetBoardUse() const
Get what the board use is.
Definition: board.h:284
static wxString GetStandardLayerName(PCB_LAYER_ID aLayerId)
Return an "English Standard" name of a PCB layer when given aLayerNumber.
Definition: board.h:696
int GetCopperLayerCount() const
Definition: board.cpp:545
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition: board.cpp:456
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
const KIID m_Uuid
Definition: eda_item.h:492
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.
ENUM_MAP & Map(T aValue, const wxString &aName)
Definition: property.h:579
static ENUM_MAP< T > & Instance()
Definition: property.h:573
ENUM_MAP & Undefined(T aValue)
Definition: property.h:586
wxPGChoices & Choices()
Definition: property.h:622
Definition: kiid.h:48
LSEQ is a sequence (and therefore also a set) of PCB_LAYER_IDs.
Definition: layer_ids.h:491
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:530
static LSET AllLayersMask()
Definition: lset.cpp:808
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:411
static LSET AllTechMask()
Return a mask holding all technical layers (no CU layer) on both side.
Definition: lset.cpp:841
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:773
static const wxChar * Name(PCB_LAYER_ID aLayerId)
Return the fixed name association with aLayerId.
Definition: lset.cpp:82
A set of BOARD_ITEMs (i.e., without duplicates).
Definition: pcb_group.h:51
bool AddItem(BOARD_ITEM *aItem)
Add item to group.
Definition: pcb_group.cpp:80
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:74
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:76
void 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:88
#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:147
@ LAYER_LOCKED_ITEM_SHADOW
shadow layer for locked items
Definition: layer_ids.h:239
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:59
@ PCBNEW_LAYER_ID_START
Definition: layer_ids.h:63
@ UNDEFINED_LAYER
Definition: layer_ids.h:60
@ PCB_LAYER_ID_COUNT
Definition: layer_ids.h:137
This file contains miscellaneous commonly used macros and functions.
#define UNIMPLEMENTED_FOR(type)
Definition: macros.h:120
#define TYPE_HASH(x)
Definition: property.h:62
#define IMPLEMENT_ENUM_TO_WXANY(type)
Definition: property.h:673
@ PT_COORD
Coordinate expressed in distance units (mm/inch)
Definition: property.h:56
#define REGISTER_TYPE(x)
Definition: property_mgr.h:328
bool operator()(const BOARD_ITEM *a, const BOARD_ITEM *b) const
Definition: board_item.cpp:204
constexpr int mmToIU(double mm) const
Definition: base_units.h:89
@ PCB_T
Definition: typeinfo.h:82
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:115
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition: typeinfo.h:86