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_PinNum = 0; /* pin number ( 1 long = 4 bytes -> 4 ascii codes) */
188  m_netNameCandidate = NULL; /* a pointer to a NETLIST_OBJECT type label connected to this
189  * object used to give a name to the net
190  */
191 }
192 
193 
194 // Copy constructor
196 {
197  *this = aSource;
198 }
199 
200 
202 {
203 }
204 
205 
206 // return true if the object is a label of any type
208 {
209  return m_Type == NET_LABEL
213  || m_Type == NET_PINLABEL;
214 }
215 
217 {
218  if( aNetItem == this ) // Don't compare the same net list object.
219  return false;
220 
221  int at = m_Type;
222  int bt = aNetItem->m_Type;
223 
224  if( ( at == NET_HIERLABEL || at == NET_HIERBUSLABELMEMBER )
225  && ( bt == NET_SHEETLABEL || bt == NET_SHEETBUSLABELMEMBER ) )
226  {
227  if( m_SheetPath == aNetItem->m_SheetPathInclude )
228  {
229  return true; //connected!
230  }
231  }
232  else if( ( at == NET_GLOBLABEL ) && ( bt == NET_GLOBLABEL ) )
233  {
234  if( m_Label == aNetItem->m_Label )
235  return true; //connected!
236  }
237 
238  return false; //these two are unconnected
239 }
240 
241 
243 {
244  wxCHECK_RET( IsBusLabel( m_Label ),
245  wxT( "<" ) + m_Label + wxT( "> is not a valid bus label." ) );
246 
247  if( m_Type == NET_HIERLABEL )
249  else if( m_Type == NET_GLOBLABEL )
251  else if( m_Type == NET_SHEETLABEL )
253  else if( m_Type == NET_LABEL )
255  else
256  wxCHECK_RET( false, wxT( "Net list object type is not valid." ) );
257 
258  unsigned i;
259  wxString tmp, busName, busNumber;
260  long begin, end, member;
261 
262  busName = busLabelRe.GetMatch( m_Label, 1 );
263  busNumber = busLabelRe.GetMatch( m_Label, 2 );
264 
265  /* Search for '[' because a bus label is like "busname[nn..mm]" */
266  i = busNumber.Find( '[' );
267  i++;
268 
269  while( i < busNumber.Len() && busNumber[i] != '.' )
270  {
271  tmp.Append( busNumber[i] );
272  i++;
273  }
274 
275  tmp.ToLong( &begin );
276 
277  while( i < busNumber.Len() && busNumber[i] == '.' )
278  i++;
279 
280  tmp.Empty();
281 
282  while( i < busNumber.Len() && busNumber[i] != ']' )
283  {
284  tmp.Append( busNumber[i] );
285  i++;
286  }
287 
288  tmp.ToLong( &end );
289 
290  if( begin < 0 )
291  begin = 0;
292 
293  if( end < 0 )
294  end = 0;
295 
296  if( begin > end )
297  std::swap( begin, end );
298 
299  member = begin;
300  tmp = busName;
301  tmp << member;
302  m_Label = tmp;
303  m_Member = member;
304 
305  for( member++; member <= end; member++ )
306  {
307  NETLIST_OBJECT* item = new NETLIST_OBJECT( *this );
308 
309  // Conversion of bus label to the root name + the current member id.
310  tmp = busName;
311  tmp << member;
312  item->m_Label = tmp;
313  item->m_Member = member;
314 
315  aNetListItems.push_back( item );
316  }
317 }
318 
319 
321 {
322  // return true if the object is a global label
323  // * a actual global label
324  // * a pin label coming from a invisible power pin
325  return ( m_Type == NET_PINLABEL ) ||
326  ( m_Type == NET_GLOBLABEL ) ||
328 }
329 
330 
332 {
333  // return true if the object is a bus label member build from a
334  // schematic bus label (like label[xx..yy)
335  // They are labels with very specific properties, especially for connection
336  // between them: 2 bus label members can be connected only
337  // if they have the same member value.
338  return ( m_Type == NET_SHEETBUSLABELMEMBER ) ||
339  ( m_Type == NET_BUSLABELMEMBER ) ||
342 }
343 
344 
345 /*
346  * return the net name of the item
347  */
348 wxString NETLIST_OBJECT::GetNetName( bool adoptTimestamp ) const
349 {
350  if( m_netNameCandidate == NULL )
351  return wxEmptyString;
352 
353  wxString netName;
354 
356  return GetShortNetName( adoptTimestamp );
357 
359  {
360  // usual net name, prefix it by the sheet path
362  }
363 
364  netName += m_netNameCandidate->m_Label;
365 
366  return netName;
367 }
368 
374 wxString NETLIST_OBJECT::GetShortNetName( bool adoptTimestamp ) const
375 {
376  if( m_netNameCandidate == NULL )
377  return wxEmptyString;
378 
379  wxString netName;
380 
382  {
384  if( link ) // Should be always true
385  {
386  netName = wxT("Net-(");
387  netName << link->GetRef( &m_netNameCandidate->m_SheetPath );
388 
389  if( adoptTimestamp && netName.Last() == '?' )
390  netName << link->GetTimeStamp();
391 
392  netName << wxT("-Pad")
394  << wxT(")");
395  }
396  }
397  else
398  netName = m_netNameCandidate->m_Label;
399 
400  return netName;
401 }
402 
412 {
413  switch( aCandidate->m_Type )
414  {
415  case NET_HIERLABEL:
416  case NET_LABEL:
417  case NET_PINLABEL:
418  case NET_GLOBLABEL:
421  case NET_PIN:
422  m_netNameCandidate = aCandidate;
423  break;
424 
425  default:
426  break;
427  }
428 }
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.
SCH_COMPONENT * GetComponentParent() const
For Pins (NET_PINS):
time_t GetTimeStamp() const
Definition: base_struct.h:204
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.
const wxString GetRef(const SCH_SHEET_PATH *sheet)
Function GetRef returns the reference, for the given sheet path.
Definition the SCH_COMPONENT class for Eeschema.
void PinStringNum(wxString &aStringBuffer) const
Fill a string buffer with pin number.
Definition: lib_pin.cpp:1886
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:68
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...