KiCad PCB EDA Suite
LEGACY_NETLIST_READER Class Reference

Read 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=nullptr)
 
virtual void LoadNetlist () override
 Read the netlist file in the legacy format into aNetlist. More...
 
LINE_READERGetLineReader ()
 

Static Public Member Functions

static NETLIST_FILE_T GuessNetlistFileType (LINE_READER *aLineReader)
 Look 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)
 Attempt 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)
 Read the aLine containing the description of a component from a legacy format netlist and add it to the netlist. More...
 
void loadFootprintFilters ()
 Load 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

Read 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 197 of file netlist_reader.h.

Member Enumeration Documentation

◆ NETLIST_FILE_T

Enumerator
UNKNOWN 
ORCAD 
LEGACY 
KICAD 

Definition at line 110 of file netlist_reader.h.

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

Constructor & Destructor Documentation

◆ LEGACY_NETLIST_READER()

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

Definition at line 244 of file netlist_reader.h.

246  :
247  NETLIST_READER( aLineReader, aNetlist, aFootprintLinkReader )
248  {
249  }
NETLIST_READER(LINE_READER *aLineReader, NETLIST *aNetlist, CMP_READER *aFootprintLinkReader=nullptr)

Member Function Documentation

◆ GetLineReader()

LINE_READER* NETLIST_READER::GetLineReader ( )
inherited
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

Attempt 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 81 of file netlist_reader.cpp.

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

References NETLIST_READER::GuessNetlistFileType(), NETLIST_READER::KICAD, NETLIST_READER::LEGACY, 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

Look 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 
55  // Our legacy netlist format starts by "# EESchema Netlist "
56  wxRegEx reLegacy( wxT( "(?i)#[ \t]+EESchema[ \t]+Netlist[ \t]+" ), wxRE_ADVANCED );
57  wxASSERT( reLegacy.IsValid() );
58 
59  // Our new netlist format starts by "(export (version "
60  wxRegEx reKicad( wxT( "[ ]*\\(export[ ]+" ), wxRE_ADVANCED );
61  wxASSERT( reKicad.IsValid() );
62 
63  wxString line;
64 
65  while( aLineReader->ReadLine() )
66  {
67  line = FROM_UTF8( aLineReader->Line() );
68 
69  if( reLegacy.Matches( line ) )
70  return LEGACY;
71  else if( reKicad.Matches( line ) )
72  return KICAD;
73  else if( reOrcad.Matches( line ) )
74  return ORCAD;
75  }
76 
77  return UNKNOWN;
78 }
char * Line() const
Return a pointer to the last line that was read in.
Definition: richio.h:117
static wxString FROM_UTF8(const char *cstring)
Convert a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:110
virtual char * ReadLine()=0
Read 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

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" ) ) == nullptr )
117  {
118  msg = _( "Cannot parse time stamp in symbol section of netlist." );
120  m_lineReader->Length() );
121  }
122 
124 
125  // Read footprint name (second word)
126  if( ( text = strtok( nullptr, " ()\t\n" ) ) == nullptr )
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( nullptr, " ()\t\n" ) ) == nullptr )
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( nullptr, " ()\t\n" ) ) == nullptr )
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( nullptr, " ()\t\n" ) ) != nullptr )
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)
Convert a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:110
virtual const wxString & GetSource() const
Returns the name of the source of the lines in an abstract sense.
Definition: richio.h:109
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)
Add aComponent to the NETLIST.
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
Definition: ki_exception.h:164
virtual unsigned LineNumber() const
Return the line number of the last line read from this LINE_READER.
Definition: richio.h:135
#define _(s)
int SetLibItemName(const UTF8 &aLibItemName)
Override the library item name portion of the LIB_ID to aLibItemName.
Definition: lib_id.cpp:108
Store all of the related footprint information found in a netlist.
Definition: pcb_netlist.h:84
unsigned Length() const
Return the number of bytes in the last line read from this LINE_READER.
Definition: richio.h:143
const char * name
Definition: DXF_plotter.cpp:56
void SetName(const wxString &aName)
Definition: pcb_netlist.h:117
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, path, LIB_ID::SetLibItemName(), COMPONENT::SetName(), text, and THROW_PARSE_ERROR.

Referenced by LoadNetlist().

◆ loadFootprintFilters()

void LEGACY_NETLIST_READER::loadFootprintFilters ( )
private

Load 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 = nullptr; // Suppress compile warning
218 
219  while( ( line = m_lineReader->ReadLine() ) != nullptr )
220  {
221  if( strncasecmp( line, "$endlist", 8 ) == 0 ) // end of list for the current component
222  {
223  wxASSERT( component != nullptr );
224  component->SetFootprintFilters( filters );
225  component = nullptr;
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 == nullptr )
244  {
245  wxString msg;
246  msg.Printf( _( "Cannot find symbol %s in footprint filter section of netlist." ),
247  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)
Convert a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:110
virtual const wxString & GetSource() const
Returns the name of the source of the lines in an abstract sense.
Definition: richio.h:109
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:164
virtual unsigned LineNumber() const
Return the line number of the last line read from this LINE_READER.
Definition: richio.h:135
#define _(s)
Store all of the related footprint information found in a netlist.
Definition: pcb_netlist.h:84
unsigned Length() const
Return the number of bytes in the last line read from this LINE_READER.
Definition: richio.h:143
void SetFootprintFilters(const wxArrayString &aFilters)
Definition: pcb_netlist.h:142
virtual char * ReadLine()=0
Read a line of text into the buffer and increments the line number counter.
COMPONENT * GetComponentByReference(const wxString &aReference)
Return 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, 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" ) ) == nullptr )
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( nullptr, " ()\t\n" ) ) == nullptr )
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, wxEmptyString );
209 }
static wxString FROM_UTF8(const char *cstring)
Convert a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:110
virtual const wxString & GetSource() const
Returns the name of the source of the lines in an abstract sense.
Definition: richio.h:109
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
Definition: ki_exception.h:164
virtual unsigned LineNumber() const
Return the line number of the last line read from this LINE_READER.
Definition: richio.h:135
#define _(s)
unsigned Length() const
Return the number of bytes in the last line read from this LINE_READER.
Definition: richio.h:143
void AddNet(const wxString &aPinName, const wxString &aNetName, const wxString &aPinFunction, const wxString &aPinType)
Definition: pcb_netlist.h:103
LINE_READER * m_lineReader
The line reader of the netlist.

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

Referenced by LoadNetlist().

◆ LoadNetlist()

void LEGACY_NETLIST_READER::LoadNetlist ( )
overridevirtual

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 = nullptr;
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, '}' ) ) == nullptr )
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, '}' ) ) == nullptr )
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 != nullptr );
85 
86  loadNet( line, component );
87  state--;
88  }
89  }
90 
91  if( m_footprintReader )
92  {
94  }
95 }
char * Line() const
Return a pointer to the last line that was read in.
Definition: richio.h:117
NETLIST * m_netlist
The net list to read the file(s) into.
bool Load(NETLIST *aNetlist)
Read the *.cmp file format contains the component footprint assignments created by CvPcb into aNetlis...
char * StrPurge(char *text)
Remove leading and training spaces, tabs and end of line chars in text.
void loadNet(char *aText, COMPONENT *aComponent)
Function loadNet read a component net description from aText.
Store all of the related footprint information found in a netlist.
Definition: pcb_netlist.h:84
bool m_loadFootprintFilters
Load the component footprint filters section if true.
virtual char * ReadLine()=0
Read a line of text into the buffer and increments the line number counter.
void loadFootprintFilters()
Load the footprint filter section of netlist file.
LINE_READER * m_lineReader
The line reader of the netlist.
COMPONENT * loadComponent(char *aText)
Read the aLine containing the description of a component from a legacy format netlist and add it to t...
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, 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 187 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 182 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 183 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 181 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: