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, jean-pierre.charras@ujf-grenoble.fr
5  * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
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 <i18n_utility.h>
30 #include <board.h>
31 #include <pcb_group.h>
32 #include <string>
33 #include <wx/msgdlg.h>
34 
35 
36 wxString BOARD_ITEM::ShowShape( SHAPE_T aShape )
37 {
38  switch( aShape )
39  {
40  case SHAPE_T::SEGMENT: return _( "Line" );
41  case SHAPE_T::RECT: return _( "Rect" );
42  case SHAPE_T::ARC: return _( "Arc" );
43  case SHAPE_T::CIRCLE: return _( "Circle" );
44  case SHAPE_T::BEZIER: return _( "Bezier Curve" );
45  case SHAPE_T::POLY: return _( "Polygon" );
46  default: return wxT( "??" );
47  }
48 }
49 
50 
52 {
53  if( Type() == PCB_T )
54  return (BOARD*) this;
55 
56  BOARD_ITEM* parent = GetParent();
57 
58  if( parent )
59  return parent->GetBoard();
60 
61  return nullptr;
62 }
63 
64 
66 {
67  if( GetParentGroup() )
68  return GetParentGroup()->IsLocked();
69 
70  return GetState( LOCKED );
71 }
72 
73 
74 wxString BOARD_ITEM::GetLayerName() const
75 {
76  BOARD* board = GetBoard();
77 
78  if( board )
79  return board->GetLayerName( m_layer );
80 
81  // If no parent, return standard name
83 }
84 
85 
87 {
88  BOARD* board = GetBoard();
89  LSET layers = GetLayerSet();
90 
91  // Try to be smart and useful. Check all copper first.
92  if( layers[F_Cu] && layers[B_Cu] )
93  return _( "all copper layers" );
94 
95  LSET copperLayers = layers & board->GetEnabledLayers().AllCuMask();
96  LSET techLayers = layers & board->GetEnabledLayers().AllTechMask();
97 
98  for( LSET testLayers : { copperLayers, techLayers, layers } )
99  {
100  for( int bit = PCBNEW_LAYER_ID_START; bit < PCB_LAYER_ID_COUNT; ++bit )
101  {
102  if( testLayers[ bit ] )
103  {
104  wxString layerInfo = board->GetLayerName( static_cast<PCB_LAYER_ID>( bit ) );
105 
106  if( testLayers.count() > 1 )
107  layerInfo << wxS( " " ) + _( "and others" );
108 
109  return layerInfo;
110  }
111  }
112  }
113 
114  // No copper, no technicals: no layer
115  return _( "no layers" );
116 }
117 
118 
119 void BOARD_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
120 {
121  // Basic fallback
122  aCount = 1;
123  aLayers[0] = m_layer;
124 }
125 
126 
128 {
129  BOARD_ITEM_CONTAINER* parent = GetParent();
130 
131  if( parent )
132  parent->Remove( this );
133 
134  delete this;
135 }
136 
137 
139 {
140 }
141 
142 
144  PCB_LAYER_ID aLayer, int aClearanceValue,
145  int aError, ERROR_LOC aErrorLoc,
146  bool ignoreLineWidth ) const
147 {
148  wxASSERT_MSG( false,
149  "Called TransformShapeWithClearanceToPolygon() on unsupported BOARD_ITEM." );
150 };
151 
152 
154 {
155  if( a->Type() != b->Type() )
156  return a->Type() < b->Type();
157 
158  if( a->GetLayer() != b->GetLayer() )
159  return a->GetLayer() < b->GetLayer();
160 
161  if( a->m_Uuid != b->m_Uuid ) // should be always the case for valid boards
162  return a->m_Uuid < b->m_Uuid;
163 
164  return a < b;
165 }
166 
167 
168 std::shared_ptr<SHAPE> BOARD_ITEM::GetEffectiveShape( PCB_LAYER_ID aLayer ) const
169 {
170  std::shared_ptr<SHAPE> shape;
171 
172  wxFAIL_MSG( "GetEffectiveShape() not implemented for " + GetClass() );
173 
174  return shape;
175 }
176 
177 
178 void BOARD_ITEM::Rotate( const wxPoint& aRotCentre, double aAngle )
179 {
180  wxMessageBox( wxT( "virtual BOARD_ITEM::Rotate used, should not occur" ), GetClass() );
181 }
182 
183 
184 void BOARD_ITEM::Flip( const wxPoint& aCentre, bool aFlipLeftRight )
185 {
186  wxMessageBox( wxT( "virtual BOARD_ITEM::Flip used, should not occur" ), GetClass() );
187 }
188 
189 
190 static struct BOARD_ITEM_DESC
191 {
193  {
195 
196  if( layerEnum.Choices().GetCount() == 0 )
197  {
198  layerEnum.Undefined( UNDEFINED_LAYER );
199 
200  for( LSEQ seq = LSET::AllLayersMask().Seq(); seq; ++seq )
201  layerEnum.Map( *seq, LSET::Name( *seq ) );
202  }
203 
207 
208  propMgr.AddProperty( new PROPERTY<BOARD_ITEM, int>( _HKI( "Position X" ),
210  propMgr.AddProperty( new PROPERTY<BOARD_ITEM, int>( _HKI( "Position Y" ),
214  propMgr.AddProperty( new PROPERTY<BOARD_ITEM, bool>( _HKI( "Locked" ),
216  }
218 
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:750
PCB_GROUP * GetParentGroup() const
Definition: board_item.h:91
Arcs (with rounded ends)
Bezier Curve.
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:360
polygon (not yet used for tracks, but could be in microwave apps)
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition: board_item.h:192
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:80
virtual void Rotate(const wxPoint &aRotCentre, double aAngle)
Rotate this object.
Definition: board_item.cpp:178
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:119
usual segment : line with rounded ends
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:127
LSET GetEnabledLayers() const
A proxy function that calls the corresponding function in m_BoardSettings.
Definition: board.cpp:465
virtual void SetLocked(bool aLocked)
Modify the 'lock' status for of the item.
Definition: board_item.h:252
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:820
static struct BOARD_ITEM_DESC _BOARD_ITEM_DESC
virtual bool IsLocked() const
Definition: board_item.cpp:65
#define REGISTER_TYPE(x)
Definition: property_mgr.h:248
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
SHAPE_T
The set of shapes for PCB graphics and tracks and footprint graphics in the .m_Shape member.
Definition: board_item.h:46
virtual void SwapData(BOARD_ITEM *aImage)
Swap data between aItem and aImage.
Definition: board_item.cpp:138
virtual wxString layerMaskDescribe() const
Return a string (to be shown to the user) describing a layer mask.
Definition: board_item.cpp:86
PCB_LAYER_ID
A quick note on layer IDs:
virtual void Flip(const wxPoint &aCentre, bool aFlipLeftRight)
Flip this object, i.e.
Definition: board_item.cpp:184
Display value expressed in distance units (mm/inch)
Definition: property.h:53
LSET is a set of PCB_LAYER_IDs.
int GetY() const
Definition: board_item.h:101
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.
virtual BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
Definition: board_item.cpp:51
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
#define LOCKED
Pcbnew: locked from movement and deletion.
void SetY(int aY)
Definition: board_item.h:123
#define _(s)
static LSET AllLayersMask()
Definition: lset.cpp:787
int GetState(EDA_ITEM_FLAGS type) const
Definition: eda_item.h:137
void SetX(int aX)
Definition: board_item.h:117
LSEQ is a sequence (and therefore also a set) of PCB_LAYER_IDs.
const KIID m_Uuid
Definition: eda_item.h:475
Some functions to handle hotkeys in KiCad.
virtual wxString GetClass() const =0
Return the class name.
int GetX() const
Definition: board_item.h:95
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
bool operator()(const BOARD_ITEM *a, const BOARD_ITEM *b) const
Definition: board_item.cpp:153
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:100
PCB_LAYER_ID m_layer
Definition: board_item.h:352
virtual std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER) const
Some pad shapes can be complex (rounded/chamfered rectangle), even without considering custom shapes.
Definition: board_item.cpp:168
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:143
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
segment with non rounded ends
static wxString ShowShape(SHAPE_T aShape)
Convert the enum #PCB_SHAPE_TYPE_T integer value to a wxString.
Definition: board_item.cpp:36
BOARD_ITEM_CONTAINER * GetParent() const
Definition: board_item.h:166
wxString GetLayerName() const
Return the name of the PCB layer on which the item resides.
Definition: board_item.cpp:74
#define _HKI(x)
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:171
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
Definition: board_item.h:176
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:113
static wxString GetStandardLayerName(PCB_LAYER_ID aLayerId)
Return an "English Standard" name of a PCB layer when given aLayerNumber.
Definition: board.h:605