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, 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 <board.h>
29#include <drc/drc_engine.h>
31#include <lset.h>
33#include <string_utils.h>
34#include <i18n_utility.h>
35#include <netinfo.h>
36#include <api/board/board_types.pb.h>
37#include <shared_mutex>
38
39using namespace std::placeholders;
40
42 BOARD_ITEM( aParent, idtype ),
43 m_netinfo( NETINFO_LIST::OrphanedItem() )
44{
46}
47
48
50{
51 BOARD_ITEM::SetLayer( aLayer );
52
53 // Invalidate clearance cache since layer can affect clearance rules
54 if( BOARD* board = GetBoard() )
55 board->InvalidateClearanceCache( m_Uuid );
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 m_netinfo = net;
68 }
69 else
70 {
71 NETINFO_ITEM* newnet = new NETINFO_ITEM( board, name, 0 );
72 board->Add( newnet );
73 m_netinfo = 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 // Invalidate clearance cache since net can affect clearance rules
104 if( board )
105 {
107
108 std::unique_lock<std::shared_mutex> writeLock( board->m_CachesMutex );
109 board->m_ItemNetclassCache.erase( this );
110 }
111
112 return ( m_netinfo != nullptr );
113}
114
115
116int BOARD_CONNECTED_ITEM::GetOwnClearance( PCB_LAYER_ID aLayer, wxString* aSource ) const
117{
118 if( GetBoard() && GetBoard()->GetDesignSettings().m_DRCEngine )
119 {
121 return bds.m_DRCEngine->GetCachedOwnClearance( this, aLayer, aSource );
122 }
123
124 return 0;
125}
126
127
129{
130 return m_netinfo ? m_netinfo->GetNetCode() : -1;
131}
132
133
134// Note: do NOT return a std::shared_ptr from this. It is used heavily in DRC, and the
135// std::shared_ptr stuff shows up large in performance profiling.
137{
138 // Static fallback netclass for items without a valid board (e.g., during DRC evaluation
139 // of dummy items, or items not yet added to a board).
140 static std::shared_ptr<NETCLASS> fallbackNetclass = std::make_shared<NETCLASS>( NETCLASS::Default );
141
142 if( m_netinfo && m_netinfo->GetNetClass() )
143 return m_netinfo->GetNetClass();
144
145 if( const BOARD* board = GetBoard() )
146 {
147 if( board->GetDesignSettings().m_NetSettings )
148 return board->GetDesignSettings().m_NetSettings->GetDefaultNetclass().get();
149 }
150
151 return fallbackNetclass.get();
152}
153
154
156{
157 return GetEffectiveNetClass()->GetName();
158}
159
160
162{
163 return m_netinfo ? m_netinfo->GetNetname() : wxString();
164}
165
166
168{
169 if( !GetBoard() )
170 return wxT( "[** NO BOARD DEFINED **]" );
171
172 wxString netname = GetNetname();
173
174 if( !netname.length() )
175 return wxT( "[<no net>]" );
176 else if( GetNetCode() < 0 )
177 return wxT( "[" ) + UnescapeString( netname ) + wxT( "](" ) + _( "Not Found" ) + wxT( ")" );
178 else
179 return wxT( "[" ) + UnescapeString( netname ) + wxT( "]" );
180}
181
182
184{
185 static wxString emptyString;
186
187 return m_netinfo ? m_netinfo->GetShortNetname() : emptyString;
188}
189
190
192{
193 static wxString emptyString;
194
195 if( !m_netinfo )
196 return emptyString;
197
198 if( const BOARD* board = GetBoard() )
199 {
200 if( board->GetNetInfo().m_DisplayNetnamesDirty )
201 board->GetNetInfo().RebuildDisplayNetnames();
202 }
203
204 return m_netinfo->GetDisplayNetname();
205}
206
207
209{
211 {
213
214 if( layerEnum.Choices().GetCount() == 0 )
215 {
216 layerEnum.Undefined( UNDEFINED_LAYER );
217
218 for( PCB_LAYER_ID layer : LSET::AllLayersMask() )
219 layerEnum.Map( layer, LSET::Name( layer ) );
220 }
221
225
226 // Replace layer property as the properties panel will set a restriction for copper layers
227 // only for BOARD_CONNECTED_ITEM that we don't want to apply to BOARD_ITEM
229 _HKI( "Layer" ),
231 layer->SetChoices( layerEnum.Choices() );
232 propMgr.ReplaceProperty( TYPE_HASH( BOARD_ITEM ), _HKI( "Layer" ), layer );
233
239
247 propMgr.AddProperty( new PROPERTY<BOARD_CONNECTED_ITEM, wxString>( _HKI( "Net Class" ),
248 NO_SETTER( BOARD_CONNECTED_ITEM, wxString ),
253
254 // Compatibility alias for DRC engine
256 NO_SETTER( BOARD_CONNECTED_ITEM, wxString ),
260
261 // Used only in DRC engine
263 NO_SETTER( BOARD_CONNECTED_ITEM, wxString ),
267
268 auto supportsTeardrops =
269 []( INSPECTABLE* aItem ) -> bool
270 {
271 if( BOARD_CONNECTED_ITEM* bci = dynamic_cast<BOARD_CONNECTED_ITEM*>( aItem ) )
272 {
273 if( !bci->GetBoard() || bci->GetBoard()->LegacyTeardrops() )
274 return false;
275
276 return bci->Type() == PCB_PAD_T || bci->Type() == PCB_VIA_T;
277 }
278
279 return false;
280 };
281
282 auto supportsTeardropPreferZoneSetting =
283 []( INSPECTABLE* aItem ) -> bool
284 {
285 if( BOARD_CONNECTED_ITEM* bci = dynamic_cast<BOARD_CONNECTED_ITEM*>( aItem ) )
286 {
287 if( !bci->GetBoard() || bci->GetBoard()->LegacyTeardrops() )
288 return false;
289
290 return bci->Type() == PCB_PAD_T;
291 }
292
293 return false;
294 };
295
296 const wxString groupTeardrops = _HKI( "Teardrops" );
297
298 auto enableTeardrops = new PROPERTY<BOARD_CONNECTED_ITEM, bool>( _HKI( "Enable Teardrops" ),
301 enableTeardrops->SetAvailableFunc( supportsTeardrops );
302 propMgr.AddProperty( enableTeardrops, groupTeardrops );
303
304 auto bestLength = new PROPERTY<BOARD_CONNECTED_ITEM, double>( _HKI( "Best Length Ratio" ),
307 bestLength->SetAvailableFunc( supportsTeardrops );
308 bestLength->SetValidator( PROPERTY_VALIDATORS::PositiveRatioValidator );
309 propMgr.AddProperty( bestLength, groupTeardrops );
310
311 auto maxLength = new PROPERTY<BOARD_CONNECTED_ITEM, int>( _HKI( "Max Length" ),
314 maxLength->SetAvailableFunc( supportsTeardrops );
315 propMgr.AddProperty( maxLength, groupTeardrops );
316
317 auto bestWidth = new PROPERTY<BOARD_CONNECTED_ITEM, double>( _HKI( "Best Width Ratio" ),
320 bestWidth->SetAvailableFunc( supportsTeardrops );
321 bestWidth->SetValidator( PROPERTY_VALIDATORS::PositiveRatioValidator );
322 propMgr.AddProperty( bestWidth, groupTeardrops );
323
324 auto maxWidth = new PROPERTY<BOARD_CONNECTED_ITEM, int>( _HKI( "Max Width" ),
327 maxWidth->SetAvailableFunc( supportsTeardrops );
328 propMgr.AddProperty( maxWidth, groupTeardrops );
329
330 auto curvePts = new PROPERTY<BOARD_CONNECTED_ITEM, bool>( _HKI( "Curved Teardrops" ),
333 curvePts->SetAvailableFunc( supportsTeardrops );
334 propMgr.AddProperty( curvePts, groupTeardrops );
335
336 auto preferZones = new PROPERTY<BOARD_CONNECTED_ITEM, bool>( _HKI( "Prefer Zone Connections" ),
339 preferZones->SetAvailableFunc( supportsTeardropPreferZoneSetting );
340 propMgr.AddProperty( preferZones, groupTeardrops );
341
342 auto twoTracks = new PROPERTY<BOARD_CONNECTED_ITEM, bool>( _HKI( "Allow Teardrops To Span Two Tracks" ),
345 twoTracks->SetAvailableFunc( supportsTeardrops );
346 propMgr.AddProperty( twoTracks, groupTeardrops );
347
348 auto maxTrackWidth = new PROPERTY<BOARD_CONNECTED_ITEM, double>( _HKI( "Max Width Ratio" ),
351 maxTrackWidth->SetAvailableFunc( supportsTeardrops );
352 propMgr.AddProperty( maxTrackWidth, groupTeardrops );
353 }
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
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
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:83
BOARD_ITEM(BOARD_ITEM *aParent, KICAD_T idtype, PCB_LAYER_ID aLayer=F_Cu)
Definition board_item.h:85
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition board_item.h:284
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:155
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
std::unordered_map< const BOARD_ITEM *, wxString > m_ItemNetclassCache
Definition board.h:1474
NETINFO_ITEM * FindNet(int aNetcode) const
Search for a net with the given netcode.
Definition board.cpp:2362
void InvalidateClearanceCache(const KIID &aUuid)
Invalidate the clearance cache for a specific item.
Definition board.cpp:1096
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition board.cpp:1084
std::shared_mutex m_CachesMutex
Definition board.h:1462
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:521
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 LSET & AllLayersMask()
Definition lset.cpp:641
static wxString Name(PCB_LAYER_ID aLayerId)
Return the fixed name association with aLayerId.
Definition lset.cpp:188
A collection of nets and the parameters used to route or test these nets.
Definition netclass.h:45
static const char Default[]
the name of the default NETCLASS
Definition netclass.h:47
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:328
Handle the data for a net.
Definition netinfo.h:54
Container for NETINFO_ITEM elements, which are the nets.
Definition netinfo.h:212
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:255
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)
Some functions to handle hotkeys in KiCad.
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
@ UNDEFINED_LAYER
Definition layer_ids.h:61
#define _HKI(x)
Definition page_info.cpp:44
#define TYPE_HASH(x)
Definition property.h:74
#define NO_SETTER(owner, type)
Definition property.h:828
@ 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:78
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:97
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:87