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#include <shared_mutex>
37
38using namespace std::placeholders;
39
41 BOARD_ITEM( aParent, idtype ),
42 m_netinfo( NETINFO_LIST::OrphanedItem() )
43{
45}
46
47
49{
50 BOARD_ITEM::SetLayer( aLayer );
51
52 if( !( GetFlags() & ROUTER_TRANSIENT ) )
53 {
54 if( BOARD* board = GetBoard() )
55 board->InvalidateClearanceCache( m_Uuid );
56 }
57}
58
59
60void BOARD_CONNECTED_ITEM::UnpackNet( const kiapi::board::types::Net& aProto )
61{
62 if( BOARD* board = GetBoard() )
63 {
64 wxString name = wxString::FromUTF8( aProto.name() );
65
66 if( NETINFO_ITEM* net = board->FindNet( name ) )
67 {
68 SetNet( net );
69 }
70 else
71 {
72 NETINFO_ITEM* newnet = new NETINFO_ITEM( board, name, 0 );
73 board->Add( newnet );
74 SetNet( newnet );
75 }
76 }
77}
78
79
80void BOARD_CONNECTED_ITEM::PackNet( kiapi::board::types::Net* aProto ) const
81{
82 aProto->set_name( GetNetname().ToUTF8() );
83}
84
85
86bool BOARD_CONNECTED_ITEM::SetNetCode( int aNetCode, bool aNoAssert )
87{
88 if( !IsOnCopperLayer() )
89 aNetCode = 0;
90
91 // if aNetCode < 0 (typically NETINFO_LIST::FORCE_ORPHANED) or no parent board,
92 // set the m_netinfo to the dummy NETINFO_LIST::ORPHANED
93
94 BOARD* board = GetBoard();
95
96 if( ( aNetCode >= 0 ) && board )
97 m_netinfo = board->FindNet( aNetCode );
98 else
100
101 if( !aNoAssert )
102 wxASSERT( m_netinfo );
103
104 if( board )
105 {
106 if( !( GetFlags() & ROUTER_TRANSIENT ) )
108
109 std::unique_lock<std::shared_mutex> writeLock( board->m_CachesMutex );
110 board->m_ItemNetclassCache.erase( this );
111 }
112
113 return ( m_netinfo != nullptr );
114}
115
116
117int BOARD_CONNECTED_ITEM::GetOwnClearance( PCB_LAYER_ID aLayer, wxString* aSource ) const
118{
119 if( GetBoard() && GetBoard()->GetDesignSettings().m_DRCEngine )
120 {
122 return bds.m_DRCEngine->GetCachedOwnClearance( this, aLayer, aSource );
123 }
124
125 return 0;
126}
127
128
130{
131 return m_netinfo ? m_netinfo->GetNetCode() : -1;
132}
133
134
135// Note: do NOT return a std::shared_ptr from this. It is used heavily in DRC, and the
136// std::shared_ptr stuff shows up large in performance profiling.
138{
139 // Static fallback netclass for items without a valid board (e.g., during DRC evaluation
140 // of dummy items, or items not yet added to a board).
141 static std::shared_ptr<NETCLASS> fallbackNetclass = std::make_shared<NETCLASS>( NETCLASS::Default );
142
143 if( m_netinfo && m_netinfo->GetNetClass() )
144 return m_netinfo->GetNetClass();
145
146 if( const BOARD* board = GetBoard() )
147 {
148 if( board->GetDesignSettings().m_NetSettings )
149 return board->GetDesignSettings().m_NetSettings->GetDefaultNetclass().get();
150 }
151
152 return fallbackNetclass.get();
153}
154
155
157{
158 return GetEffectiveNetClass()->GetName();
159}
160
161
163{
164 return m_netinfo ? m_netinfo->GetNetname() : wxString();
165}
166
167
169{
170 if( !GetBoard() )
171 return wxT( "[** NO BOARD DEFINED **]" );
172
173 wxString netname = GetNetname();
174
175 if( !netname.length() )
176 return wxT( "[<no net>]" );
177 else if( GetNetCode() < 0 )
178 return wxT( "[" ) + UnescapeString( netname ) + wxT( "](" ) + _( "Not Found" ) + wxT( ")" );
179 else
180 return wxT( "[" ) + UnescapeString( netname ) + wxT( "]" );
181}
182
183
185{
186 static wxString emptyString;
187
188 return m_netinfo ? m_netinfo->GetShortNetname() : emptyString;
189}
190
191
193{
194 static wxString emptyString;
195
196 if( !m_netinfo )
197 return emptyString;
198
199 if( const BOARD* board = GetBoard() )
200 {
201 if( board->GetNetInfo().m_DisplayNetnamesDirty )
202 board->GetNetInfo().RebuildDisplayNetnames();
203 }
204
205 return m_netinfo->GetDisplayNetname();
206}
207
208
210{
212 {
214
215 if( layerEnum.Choices().GetCount() == 0 )
216 {
217 layerEnum.Undefined( UNDEFINED_LAYER );
218
219 for( PCB_LAYER_ID layer : LSET::AllLayersMask() )
220 layerEnum.Map( layer, LSET::Name( layer ) );
221 }
222
226
227 // Replace layer property as the properties panel will set a restriction for copper layers
228 // only for BOARD_CONNECTED_ITEM that we don't want to apply to BOARD_ITEM
230 _HKI( "Layer" ),
232 layer->SetChoices( layerEnum.Choices() );
233 propMgr.ReplaceProperty( TYPE_HASH( BOARD_ITEM ), _HKI( "Layer" ), layer );
234
240
248 propMgr.AddProperty( new PROPERTY<BOARD_CONNECTED_ITEM, wxString>( _HKI( "Net Class" ),
249 NO_SETTER( BOARD_CONNECTED_ITEM, wxString ),
254
255 // Compatibility alias for DRC engine
257 NO_SETTER( BOARD_CONNECTED_ITEM, wxString ),
261
262 // Used only in DRC engine
264 NO_SETTER( BOARD_CONNECTED_ITEM, wxString ),
268
269 auto supportsTeardrops =
270 []( INSPECTABLE* aItem ) -> bool
271 {
272 if( BOARD_CONNECTED_ITEM* bci = dynamic_cast<BOARD_CONNECTED_ITEM*>( aItem ) )
273 {
274 if( !bci->GetBoard() || bci->GetBoard()->LegacyTeardrops() )
275 return false;
276
277 return bci->Type() == PCB_PAD_T || bci->Type() == PCB_VIA_T;
278 }
279
280 return false;
281 };
282
283 auto supportsTeardropPreferZoneSetting =
284 []( INSPECTABLE* aItem ) -> bool
285 {
286 if( BOARD_CONNECTED_ITEM* bci = dynamic_cast<BOARD_CONNECTED_ITEM*>( aItem ) )
287 {
288 if( !bci->GetBoard() || bci->GetBoard()->LegacyTeardrops() )
289 return false;
290
291 return bci->Type() == PCB_PAD_T;
292 }
293
294 return false;
295 };
296
297 const wxString groupTeardrops = _HKI( "Teardrops" );
298
299 auto enableTeardrops = new PROPERTY<BOARD_CONNECTED_ITEM, bool>( _HKI( "Enable Teardrops" ),
302 enableTeardrops->SetAvailableFunc( supportsTeardrops );
303 propMgr.AddProperty( enableTeardrops, groupTeardrops );
304
305 auto bestLength = new PROPERTY<BOARD_CONNECTED_ITEM, double>( _HKI( "Best Length Ratio" ),
308 bestLength->SetAvailableFunc( supportsTeardrops );
309 bestLength->SetValidator( PROPERTY_VALIDATORS::PositiveRatioValidator );
310 propMgr.AddProperty( bestLength, groupTeardrops );
311
312 auto maxLength = new PROPERTY<BOARD_CONNECTED_ITEM, int>( _HKI( "Max Length" ),
315 maxLength->SetAvailableFunc( supportsTeardrops );
316 propMgr.AddProperty( maxLength, groupTeardrops );
317
318 auto bestWidth = new PROPERTY<BOARD_CONNECTED_ITEM, double>( _HKI( "Best Width Ratio" ),
321 bestWidth->SetAvailableFunc( supportsTeardrops );
322 bestWidth->SetValidator( PROPERTY_VALIDATORS::PositiveRatioValidator );
323 propMgr.AddProperty( bestWidth, groupTeardrops );
324
325 auto maxWidth = new PROPERTY<BOARD_CONNECTED_ITEM, int>( _HKI( "Max Width" ),
328 maxWidth->SetAvailableFunc( supportsTeardrops );
329 propMgr.AddProperty( maxWidth, groupTeardrops );
330
331 auto curvePts = new PROPERTY<BOARD_CONNECTED_ITEM, bool>( _HKI( "Curved Teardrops" ),
334 curvePts->SetAvailableFunc( supportsTeardrops );
335 propMgr.AddProperty( curvePts, groupTeardrops );
336
337 auto preferZones = new PROPERTY<BOARD_CONNECTED_ITEM, bool>( _HKI( "Prefer Zone Connections" ),
340 preferZones->SetAvailableFunc( supportsTeardropPreferZoneSetting );
341 propMgr.AddProperty( preferZones, groupTeardrops );
342
343 auto twoTracks = new PROPERTY<BOARD_CONNECTED_ITEM, bool>( _HKI( "Allow Teardrops To Span Two Tracks" ),
346 twoTracks->SetAvailableFunc( supportsTeardrops );
347 propMgr.AddProperty( twoTracks, groupTeardrops );
348
349 auto maxTrackWidth = new PROPERTY<BOARD_CONNECTED_ITEM, double>( _HKI( "Max Width Ratio" ),
352 maxTrackWidth->SetAvailableFunc( supportsTeardrops );
353 propMgr.AddProperty( maxTrackWidth, groupTeardrops );
354 }
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:81
BOARD_ITEM(BOARD_ITEM *aParent, KICAD_T idtype, PCB_LAYER_ID aLayer=F_Cu)
Definition board_item.h:83
friend class BOARD
Definition board_item.h:512
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition board_item.h:313
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:172
std::unordered_map< const BOARD_ITEM *, wxString > m_ItemNetclassCache
Definition board.h:1680
NETINFO_ITEM * FindNet(int aNetcode) const
Search for a net with the given netcode.
Definition board.cpp:2657
void InvalidateClearanceCache(const KIID &aUuid)
Invalidate the clearance cache for a specific item.
Definition board.cpp:1161
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition board.cpp:1149
std::shared_mutex m_CachesMutex
Definition board.h:1660
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:531
EDA_ITEM_FLAGS GetFlags() const
Definition eda_item.h:155
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:772
Class that other classes need to inherit from, in order to be inspectable.
Definition inspectable.h:38
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:38
static const char Default[]
the name of the default NETCLASS
Definition netclass.h:40
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:354
Handle the data for a net.
Definition netinfo.h:46
Container for NETINFO_ITEM elements, which are the nets.
Definition netinfo.h:221
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:264
PROPERTY_BASE & SetIsHiddenFromPropertiesManager(bool aHide=true)
Definition property.h:319
PROPERTY_BASE & SetIsHiddenFromRulesEditor(bool aHide=true)
Definition property.h:326
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.
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)
Definition property.h:74
#define NO_SETTER(owner, type)
Definition property.h:833
@ 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)
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:71
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:90
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:80