KiCad PCB EDA Suite
Loading...
Searching...
No Matches
board_connected_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) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
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, see <https://www.gnu.org/licenses/>.
20 */
21
22#include <board.h>
26#include <drc/drc_engine.h>
28#include <lset.h>
30#include <properties/property.h>
32#include <string_utils.h>
33#include <i18n_utility.h>
34#include <netinfo.h>
35#include <api/board/board_types.pb.h>
36
37using namespace std::placeholders;
38
40 BOARD_ITEM( aParent, idtype ),
41 m_netinfo( NETINFO_LIST::OrphanedItem() )
42{
44}
45
46
48{
49 BOARD_ITEM::SetLayer( aLayer );
50
51 if( !( GetFlags() & ROUTER_TRANSIENT ) )
52 {
53 if( BOARD* board = GetBoard() )
54 board->InvalidateClearanceCache( m_Uuid );
55 }
56}
57
58
59void BOARD_CONNECTED_ITEM::UnpackNet( const kiapi::board::types::Net& aProto )
60{
61 if( BOARD* board = GetBoard() )
62 {
63 wxString name = wxString::FromUTF8( aProto.name() );
64
65 if( NETINFO_ITEM* net = board->FindNet( name ) )
66 {
67 SetNet( net );
68 }
69 else
70 {
71 NETINFO_ITEM* newnet = new NETINFO_ITEM( board, name, 0 );
72 board->Add( newnet );
73 SetNet( newnet );
74 }
75 }
76}
77
78
79void BOARD_CONNECTED_ITEM::PackNet( kiapi::board::types::Net* aProto ) const
80{
81 aProto->set_name( GetNetname().ToUTF8() );
82}
83
84
85bool BOARD_CONNECTED_ITEM::SetNetCode( int aNetCode, bool aNoAssert )
86{
87 if( !IsOnCopperLayer() )
88 aNetCode = 0;
89
90 // if aNetCode < 0 (typically NETINFO_LIST::FORCE_ORPHANED) or no parent board,
91 // set the m_netinfo to the dummy NETINFO_LIST::ORPHANED
92
93 BOARD* board = GetBoard();
94
95 if( ( aNetCode >= 0 ) && board )
96 m_netinfo = board->FindNet( aNetCode );
97 else
99
100 if( !aNoAssert )
101 wxASSERT( m_netinfo );
102
103 if( board && !( GetFlags() & ROUTER_TRANSIENT ) )
105
106 return ( m_netinfo != nullptr );
107}
108
109
110int BOARD_CONNECTED_ITEM::GetOwnClearance( PCB_LAYER_ID aLayer, wxString* aSource ) const
111{
112 if( GetBoard() && GetBoard()->GetDesignSettings().m_DRCEngine )
113 {
115 return bds.m_DRCEngine->GetCachedOwnClearance( this, aLayer, aSource );
116 }
117
118 return 0;
119}
120
121
123{
124 return m_netinfo ? m_netinfo->GetNetCode() : -1;
125}
126
127
128// Note: do NOT return a std::shared_ptr from this. It is used heavily in DRC, and the
129// std::shared_ptr stuff shows up large in performance profiling.
131{
132 // Static fallback netclass for items without a valid board (e.g., during DRC evaluation
133 // of dummy items, or items not yet added to a board).
134 static std::shared_ptr<NETCLASS> fallbackNetclass = std::make_shared<NETCLASS>( NETCLASS::Default );
135
136 if( m_netinfo && m_netinfo->GetNetClass() )
137 return m_netinfo->GetNetClass();
138
139 if( const BOARD* board = GetBoard() )
140 {
141 if( board->GetDesignSettings().m_NetSettings )
142 return board->GetDesignSettings().m_NetSettings->GetDefaultNetclass().get();
143 }
144
145 return fallbackNetclass.get();
146}
147
148
150{
151 return GetEffectiveNetClass()->GetName();
152}
153
154
156{
157 return m_netinfo ? m_netinfo->GetNetname() : wxString();
158}
159
160
162{
163 if( !GetBoard() )
164 return wxT( "[** NO BOARD DEFINED **]" );
165
166 wxString netname = GetNetname();
167
168 if( !netname.length() )
169 return wxT( "[<no net>]" );
170 else if( GetNetCode() < 0 )
171 return wxT( "[" ) + UnescapeString( netname ) + wxT( "](" ) + _( "Not Found" ) + wxT( ")" );
172 else
173 return wxT( "[" ) + UnescapeString( netname ) + wxT( "]" );
174}
175
176
178{
179 static wxString emptyString;
180
181 return m_netinfo ? m_netinfo->GetShortNetname() : emptyString;
182}
183
184
186{
187 static wxString emptyString;
188
189 if( !m_netinfo )
190 return emptyString;
191
192 if( const BOARD* board = GetBoard() )
193 {
194 if( board->GetNetInfo().m_DisplayNetnamesDirty )
195 board->GetNetInfo().RebuildDisplayNetnames();
196 }
197
198 return m_netinfo->GetDisplayNetname();
199}
200
201
203{
205 {
207
208 if( layerEnum.Choices().GetCount() == 0 )
209 {
210 layerEnum.Undefined( UNDEFINED_LAYER );
211
212 for( PCB_LAYER_ID layer : LSET::AllLayersMask() )
213 layerEnum.Map( layer, LSET::Name( layer ) );
214 }
215
219
220 // Replace layer property as the properties panel will set a restriction for copper layers
221 // only for BOARD_CONNECTED_ITEM that we don't want to apply to BOARD_ITEM
222 propMgr.ReplaceProperty( TYPE_HASH( BOARD_ITEM ), _HKI( "Layer" ),
225 .SetIsCopyable().SetChoices( layerEnum.Choices() );
226
231
239 propMgr.AddProperty( new PROPERTY<BOARD_CONNECTED_ITEM, wxString>( _HKI( "Net Class" ),
244
245 // Compatibility alias for DRC engine
250
251 // Used only in DRC engine
256
257 auto supportsTeardrops =
258 []( INSPECTABLE* aItem ) -> bool
259 {
260 if( BOARD_CONNECTED_ITEM* bci = dynamic_cast<BOARD_CONNECTED_ITEM*>( aItem ) )
261 {
262 if( !bci->GetBoard() || bci->GetBoard()->LegacyTeardrops() )
263 return false;
264
265 return bci->Type() == PCB_PAD_T || bci->Type() == PCB_VIA_T;
266 }
267
268 return false;
269 };
270
271 auto supportsTeardropPreferZoneSetting =
272 []( INSPECTABLE* aItem ) -> bool
273 {
274 if( BOARD_CONNECTED_ITEM* bci = dynamic_cast<BOARD_CONNECTED_ITEM*>( aItem ) )
275 {
276 if( !bci->GetBoard() || bci->GetBoard()->LegacyTeardrops() )
277 return false;
278
279 return bci->Type() == PCB_PAD_T;
280 }
281
282 return false;
283 };
284
285 const wxString groupTeardrops = _HKI( "Teardrops" );
286
287 propMgr.AddProperty( new PROPERTY<BOARD_CONNECTED_ITEM, bool>( _HKI( "Enable Teardrops" ),
289 groupTeardrops )
290 .SetAvailableFunc( supportsTeardrops );
291
292 propMgr.AddProperty( new PROPERTY<BOARD_CONNECTED_ITEM, double>( _HKI( "Best Length Ratio" ),
295 groupTeardrops )
296 .SetAvailableFunc( supportsTeardrops )
298
299 propMgr.AddProperty( new PROPERTY<BOARD_CONNECTED_ITEM, int>( _HKI( "Max Length" ),
302 groupTeardrops )
303 .SetAvailableFunc( supportsTeardrops );
304
305 propMgr.AddProperty( new PROPERTY<BOARD_CONNECTED_ITEM, double>( _HKI( "Best Width Ratio" ),
308 groupTeardrops )
309 .SetAvailableFunc( supportsTeardrops )
311
312 propMgr.AddProperty( new PROPERTY<BOARD_CONNECTED_ITEM, int>( _HKI( "Max Width" ),
315 groupTeardrops )
316 .SetAvailableFunc( supportsTeardrops );
317
318 propMgr.AddProperty( new PROPERTY<BOARD_CONNECTED_ITEM, bool>( _HKI( "Curved Teardrops" ),
320 groupTeardrops )
321 .SetAvailableFunc( supportsTeardrops );
322
323 propMgr.AddProperty( new PROPERTY<BOARD_CONNECTED_ITEM, bool>( _HKI( "Prefer Zone Connections" ),
326 groupTeardrops )
327 .SetAvailableFunc( supportsTeardropPreferZoneSetting );
328
329 propMgr.AddProperty( new PROPERTY<BOARD_CONNECTED_ITEM, bool>( _HKI( "Allow Teardrops To Span Two Tracks" ),
332 groupTeardrops )
333 .SetAvailableFunc( supportsTeardrops );
334
335 propMgr.AddProperty( new PROPERTY<BOARD_CONNECTED_ITEM, double>( _HKI( "Max Width Ratio" ),
337 groupTeardrops )
338 .SetAvailableFunc( supportsTeardrops );
339 }
const char * name
static struct BOARD_CONNECTED_ITEM_DESC _BOARD_CONNECTED_ITEM_DESC
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
bool GetTeardropPreferZoneConnections() const
virtual NETCLASS * GetEffectiveNetClass() const
Return the NETCLASS for this item.
double GetTeardropBestLengthRatio() const
virtual bool SetNetCode(int aNetCode, bool aNoAssert)
Set net using a net code.
void SetTeardropBestWidthRatio(double aRatio)
void SetTeardropMaxTrackWidth(double aRatio)
PCB_LAYER_ID GetLayer() const override
Return the primary layer this item is on.
BOARD_CONNECTED_ITEM(BOARD_ITEM *aParent, KICAD_T idtype)
void PackNet(kiapi::board::types::Net *aProto) const
double GetTeardropMaxTrackWidth() const
virtual void SetNet(NETINFO_ITEM *aNetInfo)
Set a NET_INFO object for the item.
void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
void SetTeardropAllowSpanTwoTracks(bool aAllow)
const wxString & GetDisplayNetname() const
const wxString & GetShortNetname() const
double GetTeardropBestWidthRatio() const
wxString GetNetClassName() const
Returns the name of the effective netclass.
void SetTeardropCurved(bool aCurve)
NETINFO_ITEM * m_netinfo
Store all information about the net that item belongs to.
virtual int GetOwnClearance(PCB_LAYER_ID aLayer, wxString *aSource=nullptr) const
Return an item's "own" clearance in internal units.
void SetTeardropMaxWidth(int aMaxWidth)
void SetTeardropPreferZoneConnections(bool aPrefer)
void SetTeardropBestLengthRatio(double aRatio)
void SetTeardropMaxLength(int aMaxLength)
void UnpackNet(const kiapi::board::types::Net &aProto)
Assigns a net to this item from an API message.
bool GetTeardropAllowSpanTwoTracks() const
void SetTeardropsEnabled(bool aEnable)
Container for design settings for a BOARD object.
std::shared_ptr< DRC_ENGINE > m_DRCEngine
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
BOARD_ITEM(BOARD_ITEM *aParent, KICAD_T idtype, PCB_LAYER_ID aLayer=F_Cu)
Definition board_item.h:86
friend class BOARD
Definition board_item.h:593
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition board_item.h:374
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
virtual bool IsOnCopperLayer() const
Definition board_item.h:189
NETINFO_ITEM * FindNet(int aNetcode) const
Search for a net with the given netcode.
Definition board.cpp:3001
void InvalidateClearanceCache(const KIID &aUuid)
Invalidate the clearance cache for a specific item.
Definition board.cpp:1316
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition board.cpp:1301
int GetCachedOwnClearance(const BOARD_ITEM *aItem, PCB_LAYER_ID aLayer, wxString *aSource=nullptr)
Get the cached own clearance for an item on a specific layer.
const KIID m_Uuid
Definition eda_item.h:599
EDA_ITEM_FLAGS GetFlags() const
Definition eda_item.h:167
ENUM_MAP & Map(T aValue, const wxString &aName)
Definition property.h:789
static ENUM_MAP< T > & Instance()
Definition property.h:783
ENUM_MAP & Undefined(T aValue)
Definition property.h:796
wxPGChoices & Choices()
Definition property.h:834
Class that other classes need to inherit from, in order to be inspectable.
Definition inspectable.h:39
static const LSET & AllLayersMask()
Definition lset.cpp:637
static wxString Name(PCB_LAYER_ID aLayerId)
Return the fixed name association with aLayerId.
Definition lset.cpp:184
A collection of nets and the parameters used to route or test these nets.
Definition netclass.h:43
static const char Default[]
the name of the default NETCLASS
Definition netclass.h:45
const wxString GetName() const
Gets the name of this (maybe aggregate) netclass in a format for internal usage or for export to exte...
Definition netclass.cpp:368
Handle the data for a net.
Definition netinfo.h:50
Container for NETINFO_ITEM elements, which are the nets.
Definition netinfo.h:232
static NETINFO_ITEM * OrphanedItem()
NETINFO_ITEM meaning that there was no net assigned for an item, as there was no board storing net li...
Definition netinfo.h:289
PROPERTY_BASE & SetAvailableFunc(std::function< bool(INSPECTABLE *)> aFunc)
Set a callback function to determine whether an object provides this property.
Definition property.h:263
PROPERTY_BASE & SetValidator(PROPERTY_VALIDATOR_FN &&aValidator)
Definition property.h:368
PROPERTY_BASE & SetIsCopyable(bool aIsCopyable=true)
Definition property.h:359
PROPERTY_BASE & SetIsHiddenFromPropertiesManager(bool aHide=true)
Definition property.h:325
virtual void SetChoices(const wxPGChoices &aChoices)
Set the possible values for for the property.
Definition property.h:238
PROPERTY_BASE & SetIsHiddenFromRulesEditor(bool aHide=true)
Definition property.h:332
PROPERTY_BASE & SetIsHiddenFromLibraryEditors(bool aIsHidden=true)
Definition property.h:339
Provide class metadata.
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.
PROPERTY_BASE & ReplaceProperty(size_t aBase, const wxString &aName, PROPERTY_BASE *aNew, const wxString &aGroup=wxEmptyString)
Replace an existing property for a specific type.
static VALIDATOR_RESULT PositiveRatioValidator(const wxAny &&aValue, EDA_ITEM *aItem)
#define _(s)
#define ROUTER_TRANSIENT
transient items that should NOT be cached
Some functions to handle hotkeys in KiCad.
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ UNDEFINED_LAYER
Definition layer_ids.h:57
#define _HKI(x)
Definition page_info.cpp:40
#define TYPE_HASH(x)
Macro to generate unique identifier for a type.
Definition property.h:74
#define NO_SETTER(owner, type)
Macro to define read-only fields (no setter method available)
Definition property.h:895
@ PT_SIZE
Size expressed in distance units (mm/inch)
Definition property.h:63
@ PT_NET
Net selection property.
Definition property.h:70
#define REGISTER_TYPE(x)
Helper macro to map type hashes to names.
static const char * emptyString
wxString UnescapeString(const wxString &aSource)
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition typeinfo.h:70
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:89
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:79