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-2021 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>
33 #include <pcb_group.h>
34 
35 
37 {
38  if( Type() == PCB_T )
39  return static_cast<const BOARD*>( this );
40 
41  BOARD_ITEM* parent = GetParent();
42 
43  if( parent )
44  return parent->GetBoard();
45 
46  return nullptr;
47 }
48 
49 
51 {
52  if( Type() == PCB_T )
53  return static_cast<BOARD*>( this );
54 
55  BOARD_ITEM* parent = GetParent();
56 
57  if( parent )
58  return parent->GetBoard();
59 
60  return nullptr;
61 }
62 
63 
65 {
66  if( GetParentGroup() )
67  return GetParentGroup()->IsLocked();
68 
69  const BOARD* board = GetBoard();
70 
71  return board && board->GetBoardUse() != BOARD_USE::FPHOLDER && GetState( LOCKED );
72 }
73 
74 
75 wxString BOARD_ITEM::GetLayerName() const
76 {
77  const BOARD* board = GetBoard();
78 
79  if( board )
80  return board->GetLayerName( m_layer );
81 
82  // If no parent, return standard name
84 }
85 
86 
88 {
89  const BOARD* board = GetBoard();
90  LSET layers = GetLayerSet();
91 
92  // Try to be smart and useful. Check all copper first.
93  if( layers[F_Cu] && layers[B_Cu] )
94  return _( "all copper layers" );
95 
96  LSET copperLayers = layers & board->GetEnabledLayers().AllCuMask();
97  LSET techLayers = layers & board->GetEnabledLayers().AllTechMask();
98 
99  for( LSET testLayers : { copperLayers, techLayers, layers } )
100  {
101  for( int bit = PCBNEW_LAYER_ID_START; bit < PCB_LAYER_ID_COUNT; ++bit )
102  {
103  if( testLayers[ bit ] )
104  {
105  wxString layerInfo = board->GetLayerName( static_cast<PCB_LAYER_ID>( bit ) );
106 
107  if( testLayers.count() > 1 )
108  layerInfo << wxS( " " ) + _( "and others" );
109 
110  return layerInfo;
111  }
112  }
113  }
114 
115  // No copper, no technicals: no layer
116  return _( "no layers" );
117 }
118 
119 
120 void BOARD_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
121 {
122  // Basic fallback
123  aCount = 1;
124  aLayers[0] = m_layer;
125 }
126 
127 
129 {
130  BOARD_ITEM_CONTAINER* parent = GetParent();
131 
132  if( parent )
133  parent->Remove( this );
134 
135  delete this;
136 }
137 
138 
140 {
141 }
142 
143 
145 {
146  BOARD_ITEM* dupe = static_cast<BOARD_ITEM*>( Clone() );
147  const_cast<KIID&>( dupe->m_Uuid ) = KIID();
148 
149  if( dupe->GetParentGroup() )
150  dupe->GetParentGroup()->AddItem( dupe );
151 
152  return static_cast<BOARD_ITEM*>( dupe );
153 }
154 
155 
157  PCB_LAYER_ID aLayer, int aClearanceValue,
158  int aError, ERROR_LOC aErrorLoc,
159  bool ignoreLineWidth ) const
160 {
161  wxASSERT_MSG( false, wxT( "Called TransformShapeWithClearanceToPolygon() on unsupported "
162  "BOARD_ITEM." ) );
163 };
164 
165 
167 {
168  if( a->Type() != b->Type() )
169  return a->Type() < b->Type();
170 
171  if( a->GetLayer() != b->GetLayer() )
172  return a->GetLayer() < b->GetLayer();
173 
174  if( a->m_Uuid != b->m_Uuid ) // UUIDs *should* always be unique (for valid boards anyway)
175  return a->m_Uuid < b->m_Uuid;
176 
177  return a < b; // But just in case; ptrs are guaranteed to be different
178 }
179 
180 
181 std::shared_ptr<SHAPE> BOARD_ITEM::GetEffectiveShape( PCB_LAYER_ID aLayer, FLASHING aFlash ) const
182 {
183  std::shared_ptr<SHAPE> shape;
184 
186 
187  return shape;
188 }
189 
190 
192 {
193  BOARD_ITEM_CONTAINER* ancestor = GetParent();
194 
195  while( ancestor && ancestor->Type() == PCB_GROUP_T )
196  ancestor = ancestor->GetParent();
197 
198  return ( ancestor && ancestor->Type() == PCB_FOOTPRINT_T ) ? ancestor : nullptr;
199 }
200 
201 
202 void BOARD_ITEM::Rotate( const wxPoint& aRotCentre, double aAngle )
203 {
204  wxMessageBox( wxT( "virtual BOARD_ITEM::Rotate used, should not occur" ), GetClass() );
205 }
206 
207 
208 void BOARD_ITEM::Flip( const wxPoint& aCentre, bool aFlipLeftRight )
209 {
210  wxMessageBox( wxT( "virtual BOARD_ITEM::Flip used, should not occur" ), GetClass() );
211 }
212 
213 
214 static struct BOARD_ITEM_DESC
215 {
217  {
219 
220  if( layerEnum.Choices().GetCount() == 0 )
221  {
222  layerEnum.Undefined( UNDEFINED_LAYER );
223 
224  for( LSEQ seq = LSET::AllLayersMask().Seq(); seq; ++seq )
225  layerEnum.Map( *seq, LSET::Name( *seq ) );
226  }
227 
231 
232  propMgr.AddProperty( new PROPERTY<BOARD_ITEM, int>( _HKI( "Position X" ),
234  propMgr.AddProperty( new PROPERTY<BOARD_ITEM, int>( _HKI( "Position Y" ),
238  propMgr.AddProperty( new PROPERTY<BOARD_ITEM, bool>( _HKI( "Locked" ),
240  }
242 
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:759
PCB_GROUP * GetParentGroup() const
Definition: board_item.h:60
bool AddItem(BOARD_ITEM *aItem)
Add item to group.
Definition: pcb_group.cpp:39
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:65
#define TYPE_HASH(x)
Definition: property.h:59
#define IMPLEMENT_ENUM_TO_WXANY(type)
Definition: property.h:610
Definition: typeinfo.h:84
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition: board.cpp:362
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition: board_item.h:164
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:49
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:108
virtual void Rotate(const wxPoint &aRotCentre, double aAngle)
Rotate this object.
Definition: board_item.cpp:202
ENUM_MAP & Undefined(T aValue)
Definition: property.h:523
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:120
static ENUM_MAP< T > & Instance()
Definition: property.h:510
void DeleteStructure()
Delete this object after removing from its parent if it has one.
Definition: board_item.cpp:128
LSET GetEnabledLayers() const
A proxy function that calls the corresponding function in m_BoardSettings.
Definition: board.cpp:467
virtual void SetLocked(bool aLocked)
Modify the 'lock' status for of the item.
Definition: board_item.h:218
wxPGChoices & Choices()
Definition: property.h:559
static LSET AllTechMask()
Return a mask holding all technical layers (no CU layer) on both side.
Definition: lset.cpp:829
static struct BOARD_ITEM_DESC _BOARD_ITEM_DESC
virtual bool IsLocked() const
Definition: board_item.cpp:64
#define REGISTER_TYPE(x)
Definition: property_mgr.h:248
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:181
virtual EDA_ITEM * Clone() const
Create a duplicate of this item with linked list members set to NULL.
Definition: eda_item.cpp:83
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
This file contains miscellaneous commonly used macros and functions.
virtual void SwapData(BOARD_ITEM *aImage)
Swap data between aItem and aImage.
Definition: board_item.cpp:139
Definition: kiid.h:44
virtual wxString layerMaskDescribe() const
Return a string (to be shown to the user) describing a layer mask.
Definition: board_item.cpp:87
virtual void Flip(const wxPoint &aCentre, bool aFlipLeftRight)
Flip this object, i.e.
Definition: board_item.cpp:208
Display value expressed in distance units (mm/inch)
Definition: property.h:53
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:516
int GetY() const
Definition: board_item.h:70
static const wxChar * Name(PCB_LAYER_ID aLayerId)
Return the fixed name association with aLayerId.
Definition: lset.cpp:82
Represent a set of closed polygons.
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
#define UNIMPLEMENTED_FOR(type)
Definition: macros.h:120
#define LOCKED
Pcbnew: locked from movement and deletion.
void SetY(int aY)
Definition: board_item.h:92
#define _(s)
static LSET AllLayersMask()
Definition: lset.cpp:796
int GetState(EDA_ITEM_FLAGS type) const
Definition: eda_item.h:136
void SetX(int aX)
Definition: board_item.h:86
LSEQ is a sequence (and therefore also a set) of PCB_LAYER_IDs.
Definition: layer_ids.h:477
FLASHING
Enum used during connectivity building to ensure we do not query connectivity while building the data...
Definition: layer_ids.h:153
class FOOTPRINT, a footprint
Definition: typeinfo.h:88
const KIID m_Uuid
Definition: eda_item.h:474
Some functions to handle hotkeys in KiCad.
virtual wxString GetClass() const =0
Return the class name.
int GetX() const
Definition: board_item.h:64
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:191
bool operator()(const BOARD_ITEM *a, const BOARD_ITEM *b) const
Definition: board_item.cpp:166
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:65
Definition: layer_ids.h:71
void AddProperty(PROPERTY_BASE *aProperty)
Register a property.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:99
PCB_LAYER_ID m_layer
Definition: board_item.h:313
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
Definition: board_item.cpp:36
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:62
virtual void TransformShapeWithClearanceToPolygon(SHAPE_POLY_SET &aCornerBuffer, PCB_LAYER_ID aLayer, int aClearanceValue, int aError, ERROR_LOC aErrorLoc, bool ignoreLineWidth=false) const
Convert the item shape to a closed polygon.
Definition: board_item.cpp:156
virtual void Remove(BOARD_ITEM *aItem, REMOVE_MODE aMode=REMOVE_MODE::NORMAL)=0
Removes an item from the container.
Abstract interface for BOARD_ITEMs capable of storing other items inside.
ENUM_MAP & Map(T aValue, const wxString &aName)
Definition: property.h:516
BOARD_ITEM_CONTAINER * GetParent() const
Definition: board_item.h:136
BOARD_USE GetBoardUse() const
Get what the board use is.
Definition: board.h:211
wxString GetLayerName() const
Return the name of the PCB layer on which the item resides.
Definition: board_item.cpp:75
#define _HKI(x)
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:143
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
Definition: board_item.h:148
virtual BOARD_ITEM * Duplicate() const
Create a copy of this BOARD_ITEM.
Definition: board_item.cpp:144
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:112
BOARD_ITEM_CONTAINER * GetParentFootprint() const
Definition: board_item.cpp:191
static wxString GetStandardLayerName(PCB_LAYER_ID aLayerId)
Return an "English Standard" name of a PCB layer when given aLayerNumber.
Definition: board.h:606