KiCad PCB EDA Suite
LEGACY_NETLIST_READER Class Reference

LEGACY_NETLIST_READER reads the KiCad legacy and the old Orcad netlist formats. More...

#include <netlist_reader.h>

Inheritance diagram for LEGACY_NETLIST_READER:
NETLIST_READER

Public Types

enum  NETLIST_FILE_T { UNKNOWN = -1, ORCAD, LEGACY, KICAD }
 

Public Member Functions

 LEGACY_NETLIST_READER (LINE_READER *aLineReader, NETLIST *aNetlist, CMP_READER *aFootprintLinkReader=NULL)
 
virtual void LoadNetlist () override
 Function LoadNetlist read the netlist file in the legacy format into aNetlist. More...
 
LINE_READERGetLineReader ()
 Function GetLineReader() More...
 

Static Public Member Functions

static NETLIST_FILE_T GuessNetlistFileType (LINE_READER *aLineReader)
 Function GuessNetlistFileType looks at aFileHeaderLine to see if it matches any of the netlist file types it knows about. More...
 
static NETLIST_READERGetNetlistReader (NETLIST *aNetlist, const wxString &aNetlistFileName, const wxString &aCompFootprintFileName=wxEmptyString)
 Function GetNetlistReader attempts to determine the net list file type of aNetlistFileName and return the appropriate NETLIST_READER type. More...
 

Protected Attributes

NETLISTm_netlist
 The net list to read the file(s) into. More...
 
bool m_loadFootprintFilters
 Load the component footprint filters section if true. More...
 
bool m_loadNets
 Load the nets section of the netlist file if true. More...
 
LINE_READERm_lineReader
 The line reader of the netlist. More...
 
CMP_READERm_footprintReader
 The reader used to load the footprint links. If NULL, footprint links are not read. More...
 

Private Member Functions

COMPONENTloadComponent (char *aText)
 Function loadComponent read the aLine containing the description of a component from a legacy format netlist and add it to the netlist. More...
 
void loadFootprintFilters ()
 Function loadFootprintFilters loads the footprint filter section of netlist file. More...
 
void loadNet (char *aText, COMPONENT *aComponent)
 Function loadNet read a component net description from aText. More...
 

Detailed Description

LEGACY_NETLIST_READER reads the KiCad legacy and the old Orcad netlist formats.

The KiCad legacy netlist format was derived directly from an old Orcad netlist format. The primary difference is the header was changed so this reader can read both formats.

Definition at line 208 of file netlist_reader.h.

Member Enumeration Documentation

◆ NETLIST_FILE_T

Enumerator
UNKNOWN 
ORCAD 
LEGACY 
KICAD 

Definition at line 114 of file netlist_reader.h.

115  {
116  UNKNOWN = -1,
117  ORCAD,
118  LEGACY,
119  KICAD,
120 
121  // Add new types here. Don't forget to create the appropriate class derived from
122  // NETCLASS_READER and add the entry to the NETLIST_READER::GetNetlistReader()
123  // function.
124  };

Constructor & Destructor Documentation

◆ LEGACY_NETLIST_READER()

LEGACY_NETLIST_READER::LEGACY_NETLIST_READER ( LINE_READER aLineReader,
NETLIST aNetlist,
CMP_READER aFootprintLinkReader = NULL 
)
inline

Definition at line 257 of file netlist_reader.h.

259  :
260  NETLIST_READER( aLineReader, aNetlist, aFootprintLinkReader )
261  {
262  }
NETLIST_READER(LINE_READER *aLineReader, NETLIST *aNetlist, CMP_READER *aFootprintLinkReader=NULL)
Constructor.

Member Function Documentation

◆ GetLineReader()

LINE_READER* NETLIST_READER::GetLineReader ( )
inherited

Function GetLineReader()

Returns
the LINE_READER associated with the NETLIST_READER.

◆ GetNetlistReader()

NETLIST_READER * NETLIST_READER::GetNetlistReader ( NETLIST aNetlist,
const wxString &  aNetlistFileName,
const wxString &  aCompFootprintFileName = wxEmptyString 
)
staticinherited

Function GetNetlistReader attempts to determine the net list file type of aNetlistFileName and return the appropriate NETLIST_READER type.

Parameters
aNetlistis the netlist to load aNetlistFileName into.
aNetlistFileNameis the full path and file name of the net list to read.
aCompFootprintFileNameis the full path and file name of the component footprint associations to read. Set to wxEmptyString if loading the footprint association file is not required.
Returns
the appropriate NETLIST_READER if aNetlistFileName is a valid netlist or NULL if aNetlistFileName is not a valid netlist files.

Definition at line 79 of file netlist_reader.cpp.

82 {
83  wxASSERT( aNetlist != NULL );
84 
85  std::unique_ptr< FILE_LINE_READER > file_rdr(new FILE_LINE_READER( aNetlistFileName ) );
86 
87  NETLIST_FILE_T type = GuessNetlistFileType( file_rdr.get() );
88  file_rdr->Rewind();
89 
90  // The component footprint link reader is NULL if no file name was specified.
91  std::unique_ptr<CMP_READER> cmp_rdr( aCompFootprintFileName.IsEmpty() ?
92  NULL :
93  new CMP_READER( new FILE_LINE_READER( aCompFootprintFileName ) ) );
94 
95  switch( type )
96  {
97  case LEGACY:
98  case ORCAD:
99  return new LEGACY_NETLIST_READER( file_rdr.release(), aNetlist, cmp_rdr.release() );
100 
101  case KICAD:
102  return new KICAD_NETLIST_READER( file_rdr.release(), aNetlist, cmp_rdr.release() );
103 
104  default: // Unrecognized format:
105  break;
106  }
107 
108  return NULL;
109 }
static NETLIST_FILE_T GuessNetlistFileType(LINE_READER *aLineReader)
Function GuessNetlistFileType looks at aFileHeaderLine to see if it matches any of the netlist file t...
KICAD_NETLIST_READER read the new s-expression based KiCad netlist format.
FILE_LINE_READER is a LINE_READER that reads from an open file.
Definition: richio.h:181
#define NULL
LEGACY_NETLIST_READER reads the KiCad legacy and the old Orcad netlist formats.
CMP_READER reads a component footprint link file (*.cmp) format.

References NETLIST_READER::GuessNetlistFileType(), NETLIST_READER::KICAD, NETLIST_READER::LEGACY, NULL, and NETLIST_READER::ORCAD.

Referenced by TEST_NETLISTS_FIXTURE::compareNetlists(), and PCB_EDIT_FRAME::ReadNetlistFromFile().

◆ GuessNetlistFileType()

NETLIST_READER::NETLIST_FILE_T NETLIST_READER::GuessNetlistFileType ( LINE_READER aLineReader)
staticinherited

Function GuessNetlistFileType looks at aFileHeaderLine to see if it matches any of the netlist file types it knows about.

Parameters
aLineReaderis the LINE_READER object containing lines from the netlist to test.
Returns
the NETLIST_FILE_T of aLineReader.

Definition at line 48 of file netlist_reader.cpp.

49 {
50  // Orcad Pcb2 netlist format starts by "( {", followed by an unknown comment,
51  // depending on the tool which created the file
52  wxRegEx reOrcad( wxT( "(?i)[ ]*\\([ \t]+{+" ), wxRE_ADVANCED );
53  wxASSERT( reOrcad.IsValid() );
54  // Our legacy netlist format starts by "# EESchema Netlist "
55  wxRegEx reLegacy( wxT( "(?i)#[ \t]+EESchema[ \t]+Netlist[ \t]+" ), wxRE_ADVANCED );
56  wxASSERT( reLegacy.IsValid() );
57  // Our new netlist format starts by "(export (version "
58  wxRegEx reKicad( wxT( "[ ]*\\(export[ ]+" ), wxRE_ADVANCED );
59  wxASSERT( reKicad.IsValid() );
60 
61  wxString line;
62 
63  while( aLineReader->ReadLine() )
64  {
65  line = FROM_UTF8( aLineReader->Line() );
66 
67  if( reLegacy.Matches( line ) )
68  return LEGACY;
69  else if( reKicad.Matches( line ) )
70  return KICAD;
71  else if( reOrcad.Matches( line ) )
72  return ORCAD;
73  }
74 
75  return UNKNOWN;
76 }
char * Line() const
Function Line returns a pointer to the last line that was read in.
Definition: richio.h:140
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:115
virtual char * ReadLine()=0
Function ReadLine reads a line of text into the buffer and increments the line number counter.

References FROM_UTF8(), NETLIST_READER::KICAD, NETLIST_READER::LEGACY, LINE_READER::Line(), NETLIST_READER::ORCAD, LINE_READER::ReadLine(), and NETLIST_READER::UNKNOWN.

Referenced by NETLIST_READER::GetNetlistReader().

◆ loadComponent()

COMPONENT * LEGACY_NETLIST_READER::loadComponent ( char *  aText)
private

Function loadComponent read the aLine containing the description of a component from a legacy format netlist and add it to the netlist.

Analyze the first line of a component description in netlist: ( /40C08647 $noname R20 4.7K {Lib=R}

Parameters
aTextcontains the first line of description
Returns
the new component created by parsing aLine
Exceptions
PARSE_ERRORwhen aLine is not a valid component description.

Definition at line 98 of file legacy_netlist_reader.cpp.

99 {
100  char* text;
101  wxString msg;
102  wxString footprintName; // the footprint name read from netlist
103  wxString value; // the component value read from netlist
104  wxString reference; // the component schematic reference designator read from netlist
105  wxString name; // the name of component that was placed in the schematic
106  char line[1024];
107 
108  strncpy( line, aText, sizeof(line)-1 );
109  line[sizeof(line)-1] = '\0';
110 
111  value = wxT( "~" );
112 
113  // Sample component line: /68183921-93a5-49ac-91b0-49d05a0e1647 $noname R20 4.7K {Lib=R}
114 
115  // Read time stamp (first word)
116  if( ( text = strtok( line, " ()\t\n" ) ) == NULL )
117  {
118  msg = _( "Cannot parse time stamp in symbol section of netlist." );
120  m_lineReader->Length() );
121  }
122 
123  KIID_PATH path( FROM_UTF8( text ) );
124 
125  // Read footprint name (second word)
126  if( ( text = strtok( NULL, " ()\t\n" ) ) == NULL )
127  {
128  msg = _( "Cannot parse footprint name in symbol section of netlist." );
130  m_lineReader->Length() );
131  }
132 
133  footprintName = FROM_UTF8( text );
134 
135  // The footprint name will have to be looked up in the *.cmp file.
136  if( footprintName == wxT( "$noname" ) )
137  footprintName = wxEmptyString;
138 
139  // Read schematic reference designator (third word)
140  if( ( text = strtok( NULL, " ()\t\n" ) ) == NULL )
141  {
142  msg = _( "Cannot parse reference designator in symbol section of netlist." );
144  m_lineReader->Length() );
145  }
146 
147  reference = FROM_UTF8( text );
148 
149  // Read schematic value (forth word)
150  if( ( text = strtok( NULL, " ()\t\n" ) ) == NULL )
151  {
152  msg = _( "Cannot parse value in symbol section of netlist." );
154  m_lineReader->Length() );
155  }
156 
157  value = FROM_UTF8( text );
158 
159  // Read component name (fifth word) {Lib=C}
160  // This is an optional field (a comment), which does not always exists
161  if( ( text = strtok( NULL, " ()\t\n" ) ) != NULL )
162  {
163  name = FROM_UTF8( text ).AfterFirst( wxChar( '=' ) ).BeforeLast( wxChar( '}' ) );
164  }
165 
166  LIB_ID fpid;
167 
168  if( !footprintName.IsEmpty() )
169  fpid.SetLibItemName( footprintName );
170 
171  COMPONENT* component = new COMPONENT( fpid, reference, value, path );
172  component->SetName( name );
173  m_netlist->AddComponent( component );
174  return component;
175 }
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:115
virtual const wxString & GetSource() const
Function GetSource returns the name of the source of the lines in an abstract sense.
Definition: richio.h:131
NETLIST * m_netlist
The net list to read the file(s) into.
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
void AddComponent(COMPONENT *aComponent)
Function AddComponent adds aComponent to the NETLIST.
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
Definition: ki_exception.h:171
#define NULL
virtual unsigned LineNumber() const
Function Line Number returns the line number of the last line read from this LINE_READER.
Definition: richio.h:160
int SetLibItemName(const UTF8 &aLibItemName, bool aTestForRev=true)
Override the library item name portion of the LIB_ID to aLibItemName.
Definition: lib_id.cpp:206
COMPONENT is used to store components and all of their related information found in a netlist.
Definition: pcb_netlist.h:80
unsigned Length() const
Function Length returns the number of bytes in the last line read from this LINE_READER.
Definition: richio.h:169
const char * name
Definition: DXF_plotter.cpp:59
#define _(s)
Definition: 3d_actions.cpp:33
void SetName(const wxString &aName)
Definition: pcb_netlist.h:141
LINE_READER * m_lineReader
The line reader of the netlist.

References _, NETLIST::AddComponent(), COMPONENT, FROM_UTF8(), LINE_READER::GetSource(), LINE_READER::Length(), LINE_READER::LineNumber(), NETLIST_READER::m_lineReader, NETLIST_READER::m_netlist, name, NULL, LIB_ID::SetLibItemName(), COMPONENT::SetName(), and THROW_PARSE_ERROR.

Referenced by LoadNetlist().

◆ loadFootprintFilters()

void LEGACY_NETLIST_READER::loadFootprintFilters ( )
private

Function loadFootprintFilters loads the footprint filter section of netlist file.

Sample legacy footprint filter section: { Allowed footprints by component: $component R11 R? SM0603 SM0805 R?-* SM1206 $endlist $endfootprintlist }

Exceptions
IO_ERRORif a file IO error occurs.
PARSE_ERRORif an error occurs while parsing the file.

Definition at line 212 of file legacy_netlist_reader.cpp.

213 {
214  wxArrayString filters;
215  wxString cmpRef;
216  char* line;
217  COMPONENT* component = NULL; // Suppress compil warning
218 
219  while( ( line = m_lineReader->ReadLine() ) != NULL )
220  {
221  if( strncasecmp( line, "$endlist", 8 ) == 0 ) // end of list for the current component
222  {
223  wxASSERT( component != NULL );
224  component->SetFootprintFilters( filters );
225  component = NULL;
226  filters.Clear();
227  continue;
228  }
229 
230  if( strncasecmp( line, "$endfootprintlist", 4 ) == 0 )
231  // End of this section
232  return;
233 
234  if( strncasecmp( line, "$component", 10 ) == 0 ) // New component reference found
235  {
236  cmpRef = FROM_UTF8( line + 11 );
237  cmpRef.Trim( true );
238  cmpRef.Trim( false );
239 
240  component = m_netlist->GetComponentByReference( cmpRef );
241 
242  // Cannot happen if the netlist is valid.
243  if( component == NULL )
244  {
245  wxString msg;
246  msg.Printf( _( "Cannot find symbol \"%s\" in footprint filter section "
247  "of netlist." ), cmpRef );
249  m_lineReader->Length() );
250  }
251  }
252  else
253  {
254  // Add new filter to list
255  wxString fp = FROM_UTF8( line + 1 );
256  fp.Trim( false );
257  fp.Trim( true );
258  filters.Add( fp );
259  }
260  }
261 }
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:115
virtual const wxString & GetSource() const
Function GetSource returns the name of the source of the lines in an abstract sense.
Definition: richio.h:131
NETLIST * m_netlist
The net list to read the file(s) into.
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
Definition: ki_exception.h:171
#define NULL
virtual unsigned LineNumber() const
Function Line Number returns the line number of the last line read from this LINE_READER.
Definition: richio.h:160
COMPONENT is used to store components and all of their related information found in a netlist.
Definition: pcb_netlist.h:80
unsigned Length() const
Function Length returns the number of bytes in the last line read from this LINE_READER.
Definition: richio.h:169
void SetFootprintFilters(const wxArrayString &aFilters)
Definition: pcb_netlist.h:164
virtual char * ReadLine()=0
Function ReadLine reads a line of text into the buffer and increments the line number counter.
#define _(s)
Definition: 3d_actions.cpp:33
COMPONENT * GetComponentByReference(const wxString &aReference)
Function GetComponentByReference returns a COMPONENT by aReference.
LINE_READER * m_lineReader
The line reader of the netlist.

References _, FROM_UTF8(), NETLIST::GetComponentByReference(), LINE_READER::GetSource(), LINE_READER::Length(), LINE_READER::LineNumber(), NETLIST_READER::m_lineReader, NETLIST_READER::m_netlist, NULL, LINE_READER::ReadLine(), COMPONENT::SetFootprintFilters(), and THROW_PARSE_ERROR.

Referenced by LoadNetlist().

◆ loadNet()

void LEGACY_NETLIST_READER::loadNet ( char *  aText,
COMPONENT aComponent 
)
private

Function loadNet read a component net description from aText.

Parameters
aTextis current line read from the netlist.
aComponentis the component to add the net to.
Exceptions
PARSE_ERRORif a error occurs reading aText.

Definition at line 178 of file legacy_netlist_reader.cpp.

179 {
180  wxString msg;
181  char* p;
182  char line[256];
183 
184  strncpy( line, aText, sizeof( line ) );
185  line[ sizeof(line) - 1 ] = '\0';
186 
187  if( ( p = strtok( line, " ()\t\n" ) ) == NULL )
188  {
189  msg = _( "Cannot parse pin name in symbol net section of netlist." );
191  m_lineReader->Length() );
192  }
193 
194  wxString pinName = FROM_UTF8( p );
195 
196  if( ( p = strtok( NULL, " ()\t\n" ) ) == NULL )
197  {
198  msg = _( "Cannot parse net name in symbol net section of netlist." );
200  m_lineReader->Length() );
201  }
202 
203  wxString netName = FROM_UTF8( p );
204 
205  if( (char) netName[0] == '?' ) // ? indicates no net connected to pin.
206  netName = wxEmptyString;
207 
208  aComponent->AddNet( pinName, netName, wxEmptyString );
209 }
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:115
virtual const wxString & GetSource() const
Function GetSource returns the name of the source of the lines in an abstract sense.
Definition: richio.h:131
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
Definition: ki_exception.h:171
#define NULL
virtual unsigned LineNumber() const
Function Line Number returns the line number of the last line read from this LINE_READER.
Definition: richio.h:160
unsigned Length() const
Function Length returns the number of bytes in the last line read from this LINE_READER.
Definition: richio.h:169
#define _(s)
Definition: 3d_actions.cpp:33
LINE_READER * m_lineReader
The line reader of the netlist.
void AddNet(const wxString &aPinName, const wxString &aNetName, const wxString &aPinFunction)
Definition: pcb_netlist.h:128

References _, COMPONENT::AddNet(), FROM_UTF8(), LINE_READER::GetSource(), LINE_READER::Length(), LINE_READER::LineNumber(), NETLIST_READER::m_lineReader, NULL, and THROW_PARSE_ERROR.

Referenced by LoadNetlist().

◆ LoadNetlist()

void LEGACY_NETLIST_READER::LoadNetlist ( )
overridevirtual

Function LoadNetlist read the netlist file in the legacy format into aNetlist.

The legacy netlist format is: # EESchema Netlist Version 1.0 generee le 18/5/2005-12:30:22 ( ( 40C08647 $noname R20 4,7K {Lib=R} ( 1 VCC ) ( 2 MODB_1 ) ) ( 40C0863F $noname R18 4,7_k {Lib=R} ( 1 VCC ) ( 2 MODA_1 ) ) } #End

Exceptions
IO_ERRORif a file IO error occurs.
PARSE_ERRORif an error occurs while parsing the file.

Implements NETLIST_READER.

Definition at line 36 of file legacy_netlist_reader.cpp.

37 {
38  int state = 0;
39  bool is_comment = false;
40  COMPONENT* component = NULL;
41 
42  while( m_lineReader->ReadLine() )
43  {
44  char* line = StrPurge( m_lineReader->Line() );
45 
46  if( is_comment ) // Comments in progress
47  {
48  // Test for end of the current comment
49  if( ( line = strchr( line, '}' ) ) == NULL )
50  continue;
51 
52  is_comment = false;
53  }
54 
55  if( *line == '{' ) // Start Comment or Pcbnew info section
56  {
57  is_comment = true;
58 
59  if( m_loadFootprintFilters && state == 0
60  && (strncasecmp( line, "{ Allowed footprints", 20 ) == 0) )
61  {
63  continue;
64  }
65 
66  if( ( line = strchr( line, '}' ) ) == NULL )
67  continue;
68  }
69 
70  if( *line == '(' )
71  state++;
72 
73  if( *line == ')' )
74  state--;
75 
76  if( state == 2 )
77  {
78  component = loadComponent( line );
79  continue;
80  }
81 
82  if( state >= 3 ) // Pad descriptions are read here.
83  {
84  wxASSERT( component != NULL );
85 
86  loadNet( line, component );
87  state--;
88  }
89  }
90 
91  if( m_footprintReader )
92  {
94  }
95 }
char * Line() const
Function Line returns a pointer to the last line that was read in.
Definition: richio.h:140
char * StrPurge(char *text)
Remove leading and training spaces, tabs and end of line chars in text.
Definition: string.cpp:364
NETLIST * m_netlist
The net list to read the file(s) into.
bool Load(NETLIST *aNetlist)
Function Load read the *.cmp file format contains the component footprint assignments created by CvPc...
#define NULL
void loadNet(char *aText, COMPONENT *aComponent)
Function loadNet read a component net description from aText.
COMPONENT is used to store components and all of their related information found in a netlist.
Definition: pcb_netlist.h:80
bool m_loadFootprintFilters
Load the component footprint filters section if true.
virtual char * ReadLine()=0
Function ReadLine reads a line of text into the buffer and increments the line number counter.
void loadFootprintFilters()
Function loadFootprintFilters loads the footprint filter section of netlist file.
LINE_READER * m_lineReader
The line reader of the netlist.
COMPONENT * loadComponent(char *aText)
Function loadComponent read the aLine containing the description of a component from a legacy format ...
CMP_READER * m_footprintReader
The reader used to load the footprint links. If NULL, footprint links are not read.

References LINE_READER::Line(), CMP_READER::Load(), loadComponent(), loadFootprintFilters(), loadNet(), NETLIST_READER::m_footprintReader, NETLIST_READER::m_lineReader, NETLIST_READER::m_loadFootprintFilters, NETLIST_READER::m_netlist, NULL, LINE_READER::ReadLine(), and StrPurge().

Member Data Documentation

◆ m_footprintReader

CMP_READER* NETLIST_READER::m_footprintReader
protectedinherited

The reader used to load the footprint links. If NULL, footprint links are not read.

Definition at line 197 of file netlist_reader.h.

Referenced by LoadNetlist(), NETLIST_READER::NETLIST_READER(), and NETLIST_READER::~NETLIST_READER().

◆ m_lineReader

LINE_READER* NETLIST_READER::m_lineReader
protectedinherited

◆ m_loadFootprintFilters

bool NETLIST_READER::m_loadFootprintFilters
protectedinherited

Load the component footprint filters section if true.

Definition at line 192 of file netlist_reader.h.

Referenced by LoadNetlist(), and NETLIST_READER::NETLIST_READER().

◆ m_loadNets

bool NETLIST_READER::m_loadNets
protectedinherited

Load the nets section of the netlist file if true.

Definition at line 193 of file netlist_reader.h.

Referenced by NETLIST_READER::NETLIST_READER().

◆ m_netlist

NETLIST* NETLIST_READER::m_netlist
protectedinherited

The net list to read the file(s) into.

Definition at line 191 of file netlist_reader.h.

Referenced by loadComponent(), loadFootprintFilters(), LoadNetlist(), and NETLIST_READER::NETLIST_READER().


The documentation for this class was generated from the following files: