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 The 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 switch ( Type() )
44 {
45 case PCB_FOOTPRINT_T:
46 case PCB_PAD_T:
47 case PCB_SHAPE_T:
49 case PCB_FIELD_T:
50 case PCB_TEXT_T:
51 case PCB_TEXTBOX_T:
52 case PCB_TABLE_T:
53 case PCB_GROUP_T:
54 case PCB_GENERATOR_T:
55 case PCB_TRACE_T:
56 case PCB_VIA_T:
57 case PCB_ARC_T:
58 case PCB_DIMENSION_T:
64 case PCB_ZONE_T:
65 case PCB_BARCODE_T:
66 return true;
67 default:
68 return false;
69 }
70}
71
72
73void BOARD_ITEM::CopyFrom( const BOARD_ITEM* aOther )
74{
75 wxCHECK( aOther, /* void */ );
76 *this = *aOther;
77}
78
79
81{
82 if( Type() == PCB_T )
83 return static_cast<const BOARD*>( this );
84
85 return static_cast<const BOARD*>( findParent( PCB_T ) );
86}
87
88
90{
91 if( Type() == PCB_T )
92 return static_cast<BOARD*>( this );
93
94 return static_cast<BOARD*>( findParent( PCB_T ) );
95}
96
97
99{
100 return static_cast<FOOTPRINT*>( findParent( PCB_FOOTPRINT_T ) );
101}
102
103
105{
107 {
108 if( group->AsEdaItem()->IsLocked() )
109 return true;
110 }
111
112 if( !GetBoard() || GetBoard()->GetBoardUse() == BOARD_USE::FPHOLDER )
113 return false;
114
115 return m_isLocked;
116}
117
118
120{
121 wxFAIL_MSG( wxString( "GetStroke() not defined by " ) + GetClass() );
122
123 return STROKE_PARAMS( pcbIUScale.mmToIU( DEFAULT_LINE_WIDTH ) );
124}
125
126
128{
129 wxFAIL_MSG( wxString( "SetStroke() not defined by " ) + GetClass() );
130}
131
132
137
138
140{
141 if( const BOARD* board = GetBoard() )
142 return board->GetDesignSettings().m_MaxError;
143
144 return ARC_HIGH_DEF;
145}
146
147
149{
150 const BOARD* board = GetBoard();
151
152 if( board )
153 return board->GetLayerSet().count();
154
155 return 64;
156}
157
158
160{
161 const BOARD* board = GetBoard();
162
163 if( board )
164 return board->GetCopperLayerCount();
165
166 return 32;
167}
168
169
171{
172 const BOARD* board = GetBoard();
173
174 if( board )
175 return board->GetEnabledLayers();
176
177 return LSET::AllLayersMask();
178}
179
180
182{
183 if( const BOARD* board = GetBoard() )
184 return board->GetLayerName( m_layer );
185
186 // If no parent, return standard name
188}
189
190
192{
193 if( ( GetLayerSet() & LSET::SideSpecificMask() ).any() )
194 return true;
195
196 if( const BOARD* board = GetBoard() )
197 {
198 LAYER_T principalLayerType = board->GetLayerType( m_layer );
199
200 if( principalLayerType == LT_FRONT || principalLayerType == LT_BACK )
201 return true;
202 }
203
204 return false;
205}
206
207
209{
210 const BOARD* board = GetBoard();
211 LSET layers = GetLayerSet();
212
213 if( board )
214 layers &= board->GetEnabledLayers();
215
216 LSET copperLayers = layers & LSET::AllCuMask();
217 LSET techLayers = layers & LSET::AllTechMask();
218
219 // Try to be smart and useful. Check all copper first.
220 if( (int) copperLayers.count() == board->GetCopperLayerCount() )
221 return _( "all copper layers" );
222
223 for( LSET testLayers : { copperLayers, techLayers, layers } )
224 {
225 for( int bit = PCBNEW_LAYER_ID_START; bit < PCB_LAYER_ID_COUNT; ++bit )
226 {
227 if( testLayers[ bit ] )
228 {
229 wxString layerInfo = board->GetLayerName( static_cast<PCB_LAYER_ID>( bit ) );
230
231 if( testLayers.count() > 1 )
232 layerInfo << wxS( " " ) + _( "and others" );
233
234 return layerInfo;
235 }
236 }
237 }
238
239 // No copper, no technicals: no layer
240 return _( "no layers" );
241}
242
243
244std::vector<int> BOARD_ITEM::ViewGetLayers() const
245{
246 // Basic fallback
247 if( IsLocked() )
249
250 return { m_layer };
251}
252
253
255{
257
258 if( parent )
259 parent->Remove( this );
260
261 delete this;
262}
263
264
266{
268}
269
270
272{
273 if( aImage == nullptr )
274 return;
275
276 EDA_ITEM* parent = GetParent();
277
278 swapData( aImage );
279
280 SetParent( parent );
281}
282
283
284BOARD_ITEM* BOARD_ITEM::Duplicate( bool addToParentGroup, BOARD_COMMIT* aCommit ) const
285{
286 BOARD_ITEM* dupe = static_cast<BOARD_ITEM*>( Clone() );
287 const_cast<KIID&>( dupe->m_Uuid ) = KIID();
288
289 if( addToParentGroup )
290 {
291 wxCHECK_MSG( aCommit, dupe, "Must supply a commit to update parent group" );
292
293 if( EDA_GROUP* group = dupe->GetParentGroup() )
294 {
295 aCommit->Modify( group->AsEdaItem(), nullptr, RECURSE_MODE::NO_RECURSE );
296 group->AddItem( dupe );
297 }
298 }
299
300 return dupe;
301}
302
303
305 int aClearance, int aError, ERROR_LOC aErrorLoc,
306 bool ignoreLineWidth ) const
307{
308 wxFAIL_MSG( wxString::Format( wxT( "%s doesn't implement TransformShapeToPolygon()" ), GetClass() ) );
309};
310
311
313{
314 if( a->Type() != b->Type() )
315 return a->Type() < b->Type();
316
317 if( a->GetLayerSet() != b->GetLayerSet() )
318 return a->GetLayerSet().Seq() < b->GetLayerSet().Seq();
319
320 if( a->m_Uuid != b->m_Uuid ) // UUIDs *should* always be unique (for valid boards anyway)
321 return a->m_Uuid < b->m_Uuid;
322
323 return a < b; // But just in case; ptrs are guaranteed to be different
324}
325
326
327std::shared_ptr<SHAPE> BOARD_ITEM::GetEffectiveShape( PCB_LAYER_ID aLayer, FLASHING aFlash ) const
328{
329 static std::shared_ptr<SHAPE> shape;
330
332
333 return shape;
334}
335
336
337std::shared_ptr<SHAPE_SEGMENT> BOARD_ITEM::GetEffectiveHoleShape() const
338{
339 static std::shared_ptr<SHAPE_SEGMENT> slot;
340
342
343 return slot;
344}
345
346
348{
349 VECTOR2I pos = GetPosition();
350
351 if( FOOTPRINT* parentFP = GetParentFootprint() )
352 {
353 pos -= parentFP->GetPosition();
354 RotatePoint( pos, -parentFP->GetOrientation() );
355 }
356
357 return pos;
358}
359
360
362{
363 VECTOR2I pos( aPos );
364
365 if( FOOTPRINT* parentFP = GetParentFootprint() )
366 {
367 RotatePoint( pos, parentFP->GetOrientation() );
368 pos += parentFP->GetPosition();
369 }
370
371 SetPosition( pos );
372}
373
374
375void BOARD_ITEM::Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle )
376{
377 wxMessageBox( wxT( "virtual BOARD_ITEM::Rotate used, should not occur" ), GetClass() );
378}
379
380
381void BOARD_ITEM::Flip( const VECTOR2I& aCentre, FLIP_DIRECTION aFlipDirection )
382{
383 wxMessageBox( wxT( "virtual BOARD_ITEM::Flip used, should not occur" ), GetClass() );
384}
385
386
387void BOARD_ITEM::Mirror( const VECTOR2I& aCentre, FLIP_DIRECTION aFlipDirection )
388{
389 wxMessageBox( wxT( "virtual BOARD_ITEM::Mirror used, should not occur" ), GetClass() );
390}
391
392
394{
395 if( FOOTPRINT* fp = dynamic_cast<FOOTPRINT*>( m_parent ) )
396 return fp->GetReference();
397
398 return m_parent->m_Uuid.AsString();
399}
400
401
402const std::vector<wxString>* BOARD_ITEM::GetEmbeddedFonts()
403{
404 if( BOARD* board = GetBoard() )
405 return board->GetFontFiles();
406
407 return nullptr;
408}
409
410
411static struct BOARD_ITEM_DESC
412{
414 {
416
417 if( layerEnum.Choices().GetCount() == 0 )
418 {
419 layerEnum.Undefined( UNDEFINED_LAYER );
420
421 for( PCB_LAYER_ID layer : LSET::AllLayersMask() )
422 layerEnum.Map( layer, LSET::Name( layer ) );
423 }
424
428
429 propMgr.AddProperty( new PROPERTY<BOARD_ITEM, wxString>( _HKI( "Parent" ),
433
434 auto isNotFootprintHolder =
435 []( INSPECTABLE* aItem ) -> bool
436 {
437 BOARD_ITEM* item = dynamic_cast<BOARD_ITEM*>( aItem );
438 return item && item->GetBoard() && !item->GetBoard()->IsFootprintHolder();
439 };
440
441 propMgr.AddProperty( new PROPERTY<BOARD_ITEM, int>( _HKI( "Position X" ),
444 .SetAvailableFunc( isNotFootprintHolder );
445 propMgr.AddProperty( new PROPERTY<BOARD_ITEM, int>( _HKI( "Position Y" ),
448 .SetAvailableFunc( isNotFootprintHolder );
451 .SetAvailableFunc( isNotFootprintHolder );
452 propMgr.AddProperty( new PROPERTY<BOARD_ITEM, bool>( _HKI( "Locked" ),
454 .SetAvailableFunc( isNotFootprintHolder );
455 }
457
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
constexpr int ARC_HIGH_DEF
Definition base_units.h:129
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:112
@ FPHOLDER
Definition board.h:314
LAYER_T
The allowed types of layers, same as Specctra DSN spec.
Definition board.h:185
@ LT_FRONT
Definition board.h:192
@ LT_BACK
Definition board.h:193
#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
BOARD_ITEM(BOARD_ITEM *aParent, KICAD_T idtype, PCB_LAYER_ID aLayer=F_Cu)
Definition board_item.h:81
virtual void Mirror(const VECTOR2I &aCentre, FLIP_DIRECTION aFlipDirection)
Mirror this object relative to a given horizontal axis the layer is not changed.
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition board_item.h:232
int GetY() const
Definition board_item.h:101
bool IsGroupableType() const
virtual BOARD_ITEM * Duplicate(bool addToParentGroup, BOARD_COMMIT *aCommit=nullptr) const
Create a copy of this BOARD_ITEM.
void DeleteStructure()
Delete this object after removing from its parent if it has one.
const std::vector< wxString > * GetEmbeddedFonts() override
void SwapItemData(BOARD_ITEM *aImage)
Swap data between aItem and aImage.
void SetLocked(bool aLocked) override
Definition board_item.h:323
virtual STROKE_PARAMS GetStroke() const
virtual int BoardLayerCount() const
Return the total number of layers for the board that this item resides on.
virtual void SetStroke(const STROKE_PARAMS &aStroke)
PCB_LAYER_ID m_layer
Definition board_item.h:453
bool m_isLocked
Definition board_item.h:456
wxString GetParentAsString() const
For "parent" property.
int GetX() const
Definition board_item.h:95
bool IsLocked() const override
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.
virtual void Rotate(const VECTOR2I &aRotCentre, const EDA_ANGLE &aAngle)
Rotate this object.
void SetX(int aX)
Definition board_item.h:117
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:280
virtual std::vector< int > ViewGetLayers() const override
Return the all the layers within the VIEW the object is painted on.
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.
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
FOOTPRINT * GetParentFootprint() const
VECTOR2I GetFPRelativePosition() const
virtual LSET BoardLayerSet() const
Return the LSET for the board that this item resides on.
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
Definition board_item.h:252
virtual void swapData(BOARD_ITEM *aImage)
const KIFONT::METRICS & GetFontMetrics() const
void SetFPRelativePosition(const VECTOR2I &aPos)
virtual wxString LayerMaskDescribe() const
Return a string (to be shown to the user) describing a layer mask.
virtual void CopyFrom(const BOARD_ITEM *aOther)
BOARD_ITEM_CONTAINER * GetParent() const
Definition board_item.h:210
bool IsSideSpecific() const
virtual int BoardCopperLayerCount() const
Return the total number of copper layers for the board that this item resides on.
wxString GetLayerName() const
Return the name of the PCB layer on which the item resides.
int GetMaxError() const
virtual std::shared_ptr< SHAPE_SEGMENT > GetEffectiveHoleShape() const
virtual void Flip(const VECTOR2I &aCentre, FLIP_DIRECTION aFlipDirection)
Flip this object, i.e.
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
bool IsFootprintHolder() const
Find out if the board is being used to hold a single footprint for editing/viewing.
Definition board.h:352
static wxString GetStandardLayerName(PCB_LAYER_ID aLayerId)
Return an "English Standard" name of a PCB layer when given aLayerNumber.
Definition board.h:858
LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition board.h:655
int GetCopperLayerCount() const
Definition board.cpp:879
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition board.cpp:695
const LSET & GetEnabledLayers() const
A proxy function that calls the corresponding function in m_BoardSettings.
Definition board.cpp:927
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
Definition commit.h:106
A set of EDA_ITEMs (i.e., without duplicates).
Definition eda_group.h:46
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
virtual VECTOR2I GetPosition() const
Definition eda_item.h:272
virtual void SetPosition(const VECTOR2I &aPos)
Definition eda_item.h:273
const KIID m_Uuid
Definition eda_item.h:516
virtual EDA_GROUP * GetParentGroup() const
Definition eda_item.h:116
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
virtual void SetParent(EDA_ITEM *aParent)
Definition eda_item.h:113
virtual EDA_ITEM * Clone() const
Create a duplicate of this item with linked list members set to NULL.
Definition eda_item.cpp:118
EDA_ITEM * m_parent
Owner.
Definition eda_item.h:528
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition eda_item.cpp:39
EDA_ITEM * findParent(KICAD_T aType) const
Definition eda_item.cpp:75
ENUM_MAP & Map(T aValue, const wxString &aName)
Definition property.h:727
static ENUM_MAP< T > & Instance()
Definition property.h:721
ENUM_MAP & Undefined(T aValue)
Definition property.h:734
wxPGChoices & Choices()
Definition property.h:770
Class that other classes need to inherit from, in order to be inspectable.
Definition inspectable.h:37
static const METRICS & Default()
Definition font.cpp:52
virtual wxString GetClass() const =0
Return the class name.
Definition kiid.h:49
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
static const LSET & SideSpecificMask()
Definition lset.cpp:719
LSEQ Seq(const LSEQ &aSequence) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition lset.cpp:296
static const LSET & AllTechMask()
Return a mask holding all technical layers (no CU layer) on both side.
Definition lset.cpp:659
static const LSET & AllLayersMask()
Definition lset.cpp:624
static LSET AllCuMask()
return AllCuMask( MAX_CU_LAYERS );
Definition lset.cpp:591
static wxString Name(PCB_LAYER_ID aLayerId)
Return the fixed name association with aLayerId.
Definition lset.cpp:188
PROPERTY_BASE & SetAvailableFunc(std::function< bool(INSPECTABLE *)> aFunc)
Set a callback function to determine whether an object provides this property.
Definition property.h:262
PROPERTY_BASE & SetIsHiddenFromPropertiesManager(bool aHide=true)
Definition property.h:319
PROPERTY_BASE & SetIsHiddenFromLibraryEditors(bool aIsHidden=true)
Definition property.h:333
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.
Simple container to manage line stroke parameters.
A type-safe container of any type.
Definition ki_any.h:93
#define _(s)
@ NO_RECURSE
Definition eda_item.h:52
Some functions to handle hotkeys in KiCad.
constexpr PCB_LAYER_ID PCBNEW_LAYER_ID_START
Definition layer_ids.h:174
FLASHING
Enum used during connectivity building to ensure we do not query connectivity while building the data...
Definition layer_ids.h:184
@ LAYER_LOCKED_ITEM_SHADOW
Shadow layer for locked items.
Definition layer_ids.h:307
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:171
This file contains miscellaneous commonly used macros and functions.
#define UNIMPLEMENTED_FOR(type)
Definition macros.h:96
FLIP_DIRECTION
Definition mirror.h:27
#define _HKI(x)
Definition page_info.cpp:44
Class to handle a set of BOARD_ITEMs.
#define TYPE_HASH(x)
Definition property.h:74
#define IMPLEMENT_ENUM_TO_WXANY(type)
Definition property.h:821
#define NO_SETTER(owner, type)
Definition property.h:828
@ PT_COORD
Coordinate expressed in distance units (mm/inch)
Definition property.h:65
#define REGISTER_TYPE(x)
bool operator()(const BOARD_ITEM *a, const BOARD_ITEM *b) const
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_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:88
@ PCB_DIM_ORTHOGONAL_T
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition typeinfo.h:106
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition typeinfo.h:103
@ PCB_GENERATOR_T
class PCB_GENERATOR, generator on a layer
Definition typeinfo.h:91
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:97
@ PCB_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition typeinfo.h:104
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition typeinfo.h:111
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition typeinfo.h:93
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition typeinfo.h:108
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition typeinfo.h:92
@ PCB_REFERENCE_IMAGE_T
class PCB_REFERENCE_IMAGE, bitmap on a layer
Definition typeinfo.h:89
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition typeinfo.h:90
@ PCB_BARCODE_T
class PCB_BARCODE, a barcode (graphic item)
Definition typeinfo.h:101
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition typeinfo.h:86
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition typeinfo.h:102
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:87
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition typeinfo.h:98
@ PCB_DIMENSION_T
class PCB_DIMENSION_BASE: abstract dimension meta-type
Definition typeinfo.h:100
@ PCB_TABLE_T
class PCB_TABLE, table of PCB_TABLECELLs
Definition typeinfo.h:94
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:96
@ PCB_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition typeinfo.h:105
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695