KiCad PCB EDA Suite
class_netlist_object.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) 2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2013 Wayne Stambaugh <stambaughw@verizon.net>
6  * Copyright (C) 1992-2016 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 
31 #include <fctsys.h>
32 #include <macros.h>
33 #include <schframe.h>
34 
35 #include <sch_component.h>
36 #include <class_netlist_object.h>
37 
38 #include <wx/regex.h>
39 
40 
46 static wxRegEx busLabelRe( wxT( "^([^[:space:]]+)(\\[[\\d]+\\.+[\\d]+\\])$" ), wxRE_ADVANCED );
47 
48 
49 bool IsBusLabel( const wxString& aLabel )
50 {
51  wxCHECK_MSG( busLabelRe.IsValid(), false,
52  wxT( "Invalid regular expression in IsBusLabel()." ) );
53 
54  return busLabelRe.Matches( aLabel );
55 }
56 
57 
58 #if defined(DEBUG)
59 
60 #include <iostream>
61 const char* ShowType( NETLIST_ITEM_T aType )
62 {
63  const char* ret;
64 
65  switch( aType )
66  {
67  case NET_SEGMENT:
68  ret = "segment"; break;
69 
70  case NET_BUS:
71  ret = "bus"; break;
72 
73  case NET_JUNCTION:
74  ret = "junction"; break;
75 
76  case NET_LABEL:
77  ret = "label"; break;
78 
79  case NET_HIERLABEL:
80  ret = "hierlabel"; break;
81 
82  case NET_GLOBLABEL:
83  ret = "glabel"; break;
84 
85  case NET_BUSLABELMEMBER:
86  ret = "buslblmember"; break;
87 
89  ret = "hierbuslblmember"; break;
90 
92  ret = "gbuslblmember"; break;
93 
95  ret = "sbuslblmember"; break;
96 
97  case NET_SHEETLABEL:
98  ret = "sheetlabel"; break;
99 
100  case NET_PINLABEL:
101  ret = "pinlabel"; break;
102 
103  case NET_PIN:
104  ret = "pin"; break;
105 
106  case NET_NOCONNECT:
107  ret = "noconnect"; break;
108 
109  default:
110  ret = "??"; break;
111  }
112 
113  return ret;
114 }
115 
116 
117 void NETLIST_OBJECT::Show( std::ostream& out, int ndx ) const
118 {
119  wxString path = m_SheetPath.PathHumanReadable();
120 
121  out << "<netItem ndx=\"" << ndx << '"' <<
122  " type=\"" << ShowType( m_Type ) << '"' <<
123  " netCode=\"" << GetNet() << '"' <<
124  " sheet=\"" << TO_UTF8( path ) << '"' <<
125  ">\n";
126 
127  out << " <start " << m_Start << "/> <end " << m_End << "/>\n";
128 
129  if( !m_Label.IsEmpty() )
130  out << " <label>" << m_Label.mb_str() << "</label>\n";
131 
132  out << " <sheetpath>" << m_SheetPath.PathHumanReadable().mb_str() << "</sheetpath>\n";
133 
134  switch( m_Type )
135  {
136  case NET_PIN:
137  /* GetRef() needs to be const
138  out << " <refOfComp>" << GetComponentParent()->GetRef(&m_SheetPath).mb_str()
139  << "</refOfComp>\n";
140  */
141 
142  if( m_Comp )
143  m_Comp->Show( 1, out );
144 
145  break;
146 
147  default:
148  // not all the m_Comp classes have working Show functions.
149  ;
150  }
151 
152 /* was segfault-ing
153  if( m_Comp )
154  m_Comp->Show( 1, out ); // labels may not have good Show() funcs?
155  else
156  out << " m_Comp==NULL\n";
157 */
158 
159  out << "</netItem>\n";
160 }
161 
162 #endif
163 
164 
166 {
167  m_Type = NET_ITEM_UNSPECIFIED; /* Type of this item (see NETLIST_ITEM_T enum) */
168  m_Comp = NULL; /* Pointer on the library item that created this net object
169  * (the parent)*/
170  m_Link = NULL; /* For SCH_SHEET_PIN:
171  * Pointer to the hierarchy sheet that contains this
172  * SCH_SHEET_PIN For Pins: pointer to the component that
173  * contains this pin
174  */
175  m_Flag = 0; /* flag used in calculations */
176  m_ElectricalPinType = PIN_INPUT; /* Has meaning only for Pins: electrical type of the pin
177  * used to detect conflicts between pins in ERC
178  */
179  m_netCode = 0; /* net code for all items except BUS labels because a BUS
180  * label has as many net codes as bus members
181  */
182  m_BusNetCode = 0; /* Used for BUS connections */
183  m_Member = 0; /* for labels type NET_BUSLABELMEMBER ( bus member created
184  * from the BUS label ) member number
185  */
187  m_netNameCandidate = NULL; /* a pointer to a NETLIST_OBJECT type label connected to this
188  * object used to give a name to the net
189  */
190 }
191 
192 
193 // Copy constructor
195 {
196  *this = aSource;
197 }
198 
199 
201 {
202 }
203 
204 
205 // return true if the object is a label of any type
207 {
208  return m_Type == NET_LABEL
212  || m_Type == NET_PINLABEL;
213 }
214 
216 {
217  if( aNetItem == this ) // Don't compare the same net list object.
218  return false;
219 
220  int at = m_Type;
221  int bt = aNetItem->m_Type;
222 
223  if( ( at == NET_HIERLABEL || at == NET_HIERBUSLABELMEMBER )
224  && ( bt == NET_SHEETLABEL || bt == NET_SHEETBUSLABELMEMBER ) )
225  {
226  if( m_SheetPath == aNetItem->m_SheetPathInclude )
227  {
228  return true; //connected!
229  }
230  }
231  else if( ( at == NET_GLOBLABEL ) && ( bt == NET_GLOBLABEL ) )
232  {
233  if( m_Label == aNetItem->m_Label )
234  return true; //connected!
235  }
236 
237  return false; //these two are unconnected
238 }
239 
240 
242 {
243  wxCHECK_RET( IsBusLabel( m_Label ),
244  wxT( "<" ) + m_Label + wxT( "> is not a valid bus label." ) );
245 
246  if( m_Type == NET_HIERLABEL )
248  else if( m_Type == NET_GLOBLABEL )
250  else if( m_Type == NET_SHEETLABEL )
252  else if( m_Type == NET_LABEL )
254  else
255  wxCHECK_RET( false, wxT( "Net list object type is not valid." ) );
256 
257  unsigned i;
258  wxString tmp, busName, busNumber;
259  long begin, end, member;
260 
261  busName = busLabelRe.GetMatch( m_Label, 1 );
262  busNumber = busLabelRe.GetMatch( m_Label, 2 );
263 
264  /* Search for '[' because a bus label is like "busname[nn..mm]" */
265  i = busNumber.Find( '[' );
266  i++;
267 
268  while( i < busNumber.Len() && busNumber[i] != '.' )
269  {
270  tmp.Append( busNumber[i] );
271  i++;
272  }
273 
274  tmp.ToLong( &begin );
275 
276  while( i < busNumber.Len() && busNumber[i] == '.' )
277  i++;
278 
279  tmp.Empty();
280 
281  while( i < busNumber.Len() && busNumber[i] != ']' )
282  {
283  tmp.Append( busNumber[i] );
284  i++;
285  }
286 
287  tmp.ToLong( &end );
288 
289  if( begin < 0 )
290  begin = 0;
291 
292  if( end < 0 )
293  end = 0;
294 
295  if( begin > end )
296  std::swap( begin, end );
297 
298  member = begin;
299  tmp = busName;
300  tmp << member;
301  m_Label = tmp;
302  m_Member = member;
303 
304  for( member++; member <= end; member++ )
305  {
306  NETLIST_OBJECT* item = new NETLIST_OBJECT( *this );
307 
308  // Conversion of bus label to the root name + the current member id.
309  tmp = busName;
310  tmp << member;
311  item->m_Label = tmp;
312  item->m_Member = member;
313 
314  aNetListItems.push_back( item );
315  }
316 }
317 
318 
320 {
321  // return true if the object is a global label
322  // * a actual global label
323  // * a pin label coming from a invisible power pin
324  return ( m_Type == NET_PINLABEL ) ||
325  ( m_Type == NET_GLOBLABEL ) ||
327 }
328 
329 
331 {
332  // return true if the object is a bus label member build from a
333  // schematic bus label (like label[xx..yy)
334  // They are labels with very specific properties, especially for connection
335  // between them: 2 bus label members can be connected only
336  // if they have the same member value.
337  return ( m_Type == NET_SHEETBUSLABELMEMBER ) ||
338  ( m_Type == NET_BUSLABELMEMBER ) ||
341 }
342 
343 
344 /*
345  * return the net name of the item
346  */
347 wxString NETLIST_OBJECT::GetNetName( bool adoptTimestamp ) const
348 {
349  if( m_netNameCandidate == NULL )
350  return wxEmptyString;
351 
352  wxString netName;
353 
355  return GetShortNetName( adoptTimestamp );
356 
358  {
359  // usual net name, prefix it by the sheet path
361  }
362 
363  netName += m_netNameCandidate->m_Label;
364 
365  return netName;
366 }
367 
373 wxString NETLIST_OBJECT::GetShortNetName( bool adoptTimestamp ) const
374 {
375  if( m_netNameCandidate == NULL )
376  return wxEmptyString;
377 
378  wxString netName;
379 
381  {
383  if( link ) // Should be always true
384  {
385  netName = wxT("Net-(");
386  netName << link->GetRef( &m_netNameCandidate->m_SheetPath );
387 
388  if( adoptTimestamp && netName.Last() == '?' )
389  netName << link->GetTimeStamp();
390 
391  netName << wxT("-Pad") << m_netNameCandidate->m_PinNum << wxT(")");
392  }
393  }
394  else
395  netName = m_netNameCandidate->m_Label;
396 
397  return netName;
398 }
399 
409 {
410  switch( aCandidate->m_Type )
411  {
412  case NET_HIERLABEL:
413  case NET_LABEL:
414  case NET_PINLABEL:
415  case NET_GLOBLABEL:
418  case NET_PIN:
419  m_netNameCandidate = aCandidate;
420  break;
421 
422  default:
423  break;
424  }
425 }
bool IsBusLabel(const wxString &aLabel)
Function IsBusLabel test if aLabel has a bus notation.
bool IsLabelGlobal() const
Function IsLabelGlobal.
NETLIST_ITEM_T
wxString GetNetName(bool adoptTimestamp=false) const
Function GetNetName.
SCH_SHEET_PATH m_SheetPathInclude
NETLIST_OBJECT * m_netNameCandidate
NET_CONNECTION_T m_ConnectionType
SCH_SHEET_PATH m_SheetPath
ELECTRICAL_PINTYPE m_ElectricalPinType
NETLIST_ITEM_T m_Type
This file contains miscellaneous commonly used macros and functions.
Class NETLIST_OBJECT_LIST is a container holding and owning NETLIST_OBJECTs, which are connected item...
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes...
Definition: macros.h:47
bool IsLabelType() const
Function IsLabelType.
bool IsLabelBusMemberType() const
Function IsLabelBusMemberType.
wxString PathHumanReadable() const
Function PathHumanReadable returns the sheet path in a human readable form, i.e.
static wxRegEx busLabelRe(wxT("^([^[:space:]]+)(\\[[\\d]+\\.+[\\d]+\\])$"), wxRE_ADVANCED)
The regular expression string for label bus notation.
const wxString GetRef(const SCH_SHEET_PATH *aSheet)
Return the reference for the given sheet path.
SCH_COMPONENT * GetComponentParent() const
For Pins (NET_PINS):
time_t GetTimeStamp() const
Definition: base_struct.h:218
bool IsLabelConnected(NETLIST_OBJECT *aNetItem)
Function IsLabelConnected tests if the net list object is a hierarchical label or sheet label and is ...
Definition of the NETLIST_OBJECT class.
Definition the SCH_COMPONENT class for Eeschema.
void SetNetNameCandidate(NETLIST_OBJECT *aCandidate)
Set m_netNameCandidate to a connected item which will be used to calcule the net name of the item Obv...
Class SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:69
wxString GetShortNetName(bool adoptTimestamp=false) const
Function GetShortNetName.
void ConvertBusToNetListItems(NETLIST_OBJECT_LIST &aNetListItems)
Function ConvertBusToNetListItems breaks the text of a bus label type net list object into as many me...