KiCad PCB EDA Suite
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 <dick@softplc.com>
6  * Copyright (C) 1992-2020 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 <fctsys.h>
27 #include <pcbnew.h>
28 
29 #include <class_board.h>
30 #include <class_board_item.h>
31 
33 
34 
36  BOARD_ITEM( aParent, idtype ), m_netinfo( NETINFO_LIST::OrphanedItem() )
37 {
39 }
40 
41 
42 bool BOARD_CONNECTED_ITEM::SetNetCode( int aNetCode, bool aNoAssert )
43 {
44  if( !IsOnCopperLayer() )
45  aNetCode = 0;
46 
47  // if aNetCode < 0 ( typically NETINFO_LIST::FORCE_ORPHANED )
48  // or no parent board,
49  // set the m_netinfo to the dummy NETINFO_LIST::ORPHANED
50 
51  BOARD* board = GetBoard();
52  //auto connectivity = board ? board->GetConnectivity() : nullptr;
53  //bool addRatsnest = false;
54 
55  //if( connectivity )
56  //addRatsnest = connectivity->Remove( this );
57 
58  if( ( aNetCode >= 0 ) && board )
59  m_netinfo = board->FindNet( aNetCode );
60  else
62 
63  if( !aNoAssert )
64  wxASSERT( m_netinfo );
65 
66  // Add only if it was previously added to the ratsnest
67  //if( addRatsnest )
68  // connectivity->Add( this );
69 
70  return ( m_netinfo != NULL );
71 }
72 
73 
74 // This method returns the Default netclass for nets which don't have their own.
76 {
77  // NB: we must check the net first, as when it is 0 GetNetClass() will return the
78  // orphaned net netclass, not the default netclass.
79  if( m_netinfo->GetNet() == 0 )
81  else
82  return GetNetClass();
83 }
84 
85 
86 /*
87  * Clearances exist in a hiearchy. If a given level is specified then the remaining levels
88  * are NOT consulted.
89  *
90  * LEVEL 1: (highest priority) local overrides (pad, footprint, etc.)
91  * LEVEL 2: Rules
92  * LEVEL 3: Accumulated local settings, netclass settings, & board design settings
93  */
94 int BOARD_CONNECTED_ITEM::GetClearance( BOARD_ITEM* aItem, wxString* aSource ) const
95 {
96  BOARD* board = GetBoard();
97  int clearance = 0;
98  wxString source;
99  wxString* localSource = aSource ? &source : nullptr;
100  BOARD_CONNECTED_ITEM* second = dynamic_cast<BOARD_CONNECTED_ITEM*>( aItem );
101 
102  // No clearance if "this" is not (yet) linked to a board therefore no available netclass
103  if( !board )
104  return clearance;
105 
106  // LEVEL 1: local overrides (pad, footprint, etc.)
107  //
108  if( GetLocalClearanceOverrides() > clearance )
109  clearance = GetLocalClearanceOverrides( localSource );
110 
111  if( second && second->GetLocalClearanceOverrides() > clearance )
112  clearance = second->GetLocalClearanceOverrides( localSource );
113 
114  if( clearance )
115  {
116  if( aSource )
117  *aSource = *localSource;
118 
119  return clearance;
120  }
121 
122  // LEVEL 2: Rules
123  //
124  if( GetRuleClearance( aItem, &clearance, aSource ) )
125  return clearance;
126 
127  // LEVEL 3: Accumulated local settings, netclass settings, & board design settings
128  //
130  NETCLASS* netclass = GetEffectiveNetclass();
131  NETCLASS* secondNetclass = second ? second->GetEffectiveNetclass() : nullptr;
132 
133  if( bds.m_MinClearance > clearance )
134  {
135  if( aSource )
136  *aSource = _( "board minimum" );
137 
138  clearance = bds.m_MinClearance;
139  }
140 
141  if( netclass && netclass->GetClearance() > clearance )
142  clearance = netclass->GetClearance( aSource );
143 
144  if( secondNetclass && secondNetclass->GetClearance() > clearance )
145  clearance = secondNetclass->GetClearance( aSource );
146 
147  if( aItem && aItem->GetLayer() == Edge_Cuts && bds.m_CopperEdgeClearance > clearance )
148  {
149  if( aSource )
150  *aSource = _( "board edge" );
151 
152  clearance = bds.m_CopperEdgeClearance;
153  }
154 
155  if( GetLocalClearance() > clearance )
156  clearance = GetLocalClearance( aSource );
157 
158  if( second && second->GetLocalClearance() > clearance )
159  clearance = second->GetLocalClearance( aSource );
160 
161  return clearance;
162 }
163 
164 
166  wxString* aSource ) const
167 {
168  DRC_RULE* rule = GetRule( this, aItem, CLEARANCE_CONSTRAINT );
169 
170  if( rule )
171  {
172  if( aSource )
173  *aSource = wxString::Format( _( "'%s' rule clearance" ), rule->m_Name );
174 
175  *aClearance = rule->m_Clearance.Min;
176  return true;
177  }
178 
179  return false;
180 }
181 
182 
183 // Note: do NOT return a std::shared_ptr from this. It is used heavily in DRC, and the
184 // std::shared_ptr stuff shows up large in performance profiling.
186 {
187  NETCLASS* netclass = m_netinfo->GetNetClass();
188 
189  if( netclass )
190  return netclass;
191  else
192  return GetBoard()->GetDesignSettings().GetDefault();
193 }
194 
195 
197 {
198  return m_netinfo->GetClassName();
199 }
NETCLASS * GetNetClass()
Function GetNetClass.
Definition: netinfo.h:128
NETINFO_ITEM * FindNet(int aNetcode) const
Function FindNet searches for a net with the given netcode.
virtual int GetLocalClearance(wxString *aSource=nullptr) const
Function GetLocalClearance returns any local clearances set in the "classic" (ie: pre-rule) system.
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:469
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
wxString m_Name
Definition: drc_rule.h:73
Classes BOARD_ITEM and BOARD_CONNECTED_ITEM.
NETCLASS * GetNetClass() const
Function GetNetClassPtr returns the NETCLASS for this item.
BOARD_CONNECTED_ITEM(BOARD_ITEM *aParent, KICAD_T idtype)
#define CLEARANCE_CONSTRAINT
Definition: drc_rule.h:35
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Function GetDesignSettings.
Definition: class_board.h:542
int GetClearance(wxString *aSource=nullptr) const
Definition: netclass.h:162
BOARD_CONNECTED_ITEM is a base class derived from BOARD_ITEM for items that can be connected and have...
wxString GetNetClassName() const
Function GetNetClassName returns a pointer to the netclass of the zone.
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:78
DRC_RULE * GetRule(const BOARD_ITEM *aItem, const BOARD_ITEM *bItem, int aConstraint)
Definition: drc_rule.cpp:67
#define NULL
MINOPTMAX m_Clearance
Definition: drc_rule.h:79
NETINFO_LIST is a container class for NETINFO_ITEM elements, which are the nets.
Definition: netinfo.h:407
NETCLASS handles a collection of nets and the parameters used to route or test these nets.
Definition: netclass.h:55
virtual BOARD * GetBoard() const
Function GetBoard returns the BOARD in which this BOARD_ITEM resides, or NULL if none.
bool SetNetCode(int aNetCode, bool aNoAssert=false)
Sets net using a net code.
virtual int GetClearance(BOARD_ITEM *aItem=nullptr, wxString *aSource=nullptr) const
Function GetClearance returns the clearance in internal units.
virtual int GetLocalClearanceOverrides(wxString *aSource=nullptr) const
Function GetLocalClearanceOverrides returns any local clearance overrides set in the "classic" (ie: p...
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
int GetNet() const
Function GetNet.
Definition: netinfo.h:223
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:181
#define _(s)
Definition: 3d_actions.cpp:33
NETINFO_ITEM * m_netinfo
Stores all informations about the net that item belongs to.
NETCLASS * GetDefault() const
Function GetDefault.
NETCLASS * GetEffectiveNetclass() const
Function GetEffectiveNetclass returns the NETCLASS for this item, or the default netclass if none is ...
wxString GetClassName() const
Function GetClassName returns the class name.
Definition: netinfo.h:137
virtual PCB_LAYER_ID GetLayer() const
Function GetLayer returns the primary layer this item is on.
virtual bool GetRuleClearance(BOARD_ITEM *aItem, int *aClearance, wxString *aSource) const
Function GetRuleClearance returns any rule-based clearance.
virtual bool IsOnCopperLayer() const
BOARD_DESIGN_SETTINGS contains design settings for a BOARD object.