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
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 // Invalidate clearance cache since layer can affect clearance rules
53 if( BOARD* board = GetBoard() )
54 board->InvalidateClearanceCache( m_Uuid );
55}
56
57
58void BOARD_CONNECTED_ITEM::UnpackNet( const kiapi::board::types::Net& aProto )
59{
60 if( BOARD* board = GetBoard() )
61 {
62 wxString name = wxString::FromUTF8( aProto.name() );
63
64 if( NETINFO_ITEM* net = board->FindNet( name ) )
65 {
66 m_netinfo = net;
67 }
68 else
69 {
70 NETINFO_ITEM* newnet = new NETINFO_ITEM( board, name, 0 );
71 board->Add( newnet );
72 m_netinfo = newnet;
73 }
74 }
75}
76
77
78void BOARD_CONNECTED_ITEM::PackNet( kiapi::board::types::Net* aProto ) const
79{
80 aProto->set_name( GetNetname().ToUTF8() );
81}
82
83
84bool BOARD_CONNECTED_ITEM::SetNetCode( int aNetCode, bool aNoAssert )
85{
86 if( !IsOnCopperLayer() )
87 aNetCode = 0;
88
89 // if aNetCode < 0 (typically NETINFO_LIST::FORCE_ORPHANED) or no parent board,
90 // set the m_netinfo to the dummy NETINFO_LIST::ORPHANED
91
92 BOARD* board = GetBoard();
93
94 if( ( aNetCode >= 0 ) && board )
95 m_netinfo = board->FindNet( aNetCode );
96 else
98
99 if( !aNoAssert )
100 wxASSERT( m_netinfo );
101
102 // Invalidate clearance cache since net can affect clearance rules
103 if( board )
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
223 _HKI( "Layer" ),
225 layer->SetChoices( layerEnum.Choices() );
226 propMgr.ReplaceProperty( TYPE_HASH( BOARD_ITEM ), _HKI( "Layer" ), layer );
227
233
241 propMgr.AddProperty( new PROPERTY<BOARD_CONNECTED_ITEM, wxString>( _HKI( "Net Class" ),
242 NO_SETTER( BOARD_CONNECTED_ITEM, wxString ),
247
248 // Compatibility alias for DRC engine
250 NO_SETTER( BOARD_CONNECTED_ITEM, wxString ),
254
255 // Used only in DRC engine
257 NO_SETTER( BOARD_CONNECTED_ITEM, wxString ),
261
262 auto supportsTeardrops =
263 []( INSPECTABLE* aItem ) -> bool
264 {
265 if( BOARD_CONNECTED_ITEM* bci = dynamic_cast<BOARD_CONNECTED_ITEM*>( aItem ) )
266 {
267 if( !bci->GetBoard() || bci->GetBoard()->LegacyTeardrops() )
268 return false;
269
270 return bci->Type() == PCB_PAD_T || bci->Type() == PCB_VIA_T;
271 }
272
273 return false;
274 };
275
276 auto supportsTeardropPreferZoneSetting =
277 []( INSPECTABLE* aItem ) -> bool
278 {
279 if( BOARD_CONNECTED_ITEM* bci = dynamic_cast<BOARD_CONNECTED_ITEM*>( aItem ) )
280 {
281 if( !bci->GetBoard() || bci->GetBoard()->LegacyTeardrops() )
282 return false;
283
284 return bci->Type() == PCB_PAD_T;
285 }
286
287 return false;
288 };
289
290 const wxString groupTeardrops = _HKI( "Teardrops" );
291
292 auto enableTeardrops = new PROPERTY<BOARD_CONNECTED_ITEM, bool>( _HKI( "Enable Teardrops" ),
295 enableTeardrops->SetAvailableFunc( supportsTeardrops );
296 propMgr.AddProperty( enableTeardrops, groupTeardrops );
297
298 auto bestLength = new PROPERTY<BOARD_CONNECTED_ITEM, double>( _HKI( "Best Length Ratio" ),
301 bestLength->SetAvailableFunc( supportsTeardrops );
302 bestLength->SetValidator( PROPERTY_VALIDATORS::PositiveRatioValidator );
303 propMgr.AddProperty( bestLength, groupTeardrops );
304
305 auto maxLength = new PROPERTY<BOARD_CONNECTED_ITEM, int>( _HKI( "Max Length" ),
308 maxLength->SetAvailableFunc( supportsTeardrops );
309 propMgr.AddProperty( maxLength, groupTeardrops );
310
311 auto bestWidth = new PROPERTY<BOARD_CONNECTED_ITEM, double>( _HKI( "Best Width Ratio" ),
314 bestWidth->SetAvailableFunc( supportsTeardrops );
315 bestWidth->SetValidator( PROPERTY_VALIDATORS::PositiveRatioValidator );
316 propMgr.AddProperty( bestWidth, groupTeardrops );
317
318 auto maxWidth = new PROPERTY<BOARD_CONNECTED_ITEM, int>( _HKI( "Max Width" ),
321 maxWidth->SetAvailableFunc( supportsTeardrops );
322 propMgr.AddProperty( maxWidth, groupTeardrops );
323
324 auto curvePts = new PROPERTY<BOARD_CONNECTED_ITEM, bool>( _HKI( "Curved Teardrops" ),
327 curvePts->SetAvailableFunc( supportsTeardrops );
328 propMgr.AddProperty( curvePts, groupTeardrops );
329
330 auto preferZones = new PROPERTY<BOARD_CONNECTED_ITEM, bool>( _HKI( "Prefer Zone Connections" ),
333 preferZones->SetAvailableFunc( supportsTeardropPreferZoneSetting );
334 propMgr.AddProperty( preferZones, groupTeardrops );
335
336 auto twoTracks = new PROPERTY<BOARD_CONNECTED_ITEM, bool>( _HKI( "Allow Teardrops To Span Two Tracks" ),
339 twoTracks->SetAvailableFunc( supportsTeardrops );
340 propMgr.AddProperty( twoTracks, groupTeardrops );
341
342 auto maxTrackWidth = new PROPERTY<BOARD_CONNECTED_ITEM, double>( _HKI( "Max Width Ratio" ),
345 maxTrackWidth->SetAvailableFunc( supportsTeardrops );
346 propMgr.AddProperty( maxTrackWidth, groupTeardrops );
347 }
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
NETINFO_ITEM * FindNet(int aNetcode) const
Search for a net with the given netcode.
Definition board.cpp:2358
void InvalidateClearanceCache(const KIID &aUuid)
Invalidate the clearance cache for a specific item.
Definition board.cpp:1094
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition board.cpp:1082
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:522
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:624
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