KiCad PCB EDA Suite
netlist_reader.cpp
Go to the documentation of this file.
1 
4 /*
5  * This program source code file is part of KiCad, a free EDA CAD application.
6  *
7  * Copyright (C) 1992-2011 Jean-Pierre Charras.
8  * Copyright (C) 2013-2016 Wayne Stambaugh <stambaughw@verizon.net>.
9  * Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.txt for contributors.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, you may find one here:
23  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
24  * or you may search the http://www.gnu.org website for the version 2 license,
25  * or you may write to the Free Software Foundation, Inc.,
26  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27  */
28 
29 
30 
31 #include <kicad_string.h>
32 #include <reporter.h>
33 
34 #include <pcb_netlist.h>
35 #include <netlist_reader.h>
36 #include <class_module.h>
37 
38 #include <wx/regex.h>
39 
40 
42 {
43  delete m_lineReader;
44  delete m_footprintReader;
45 }
46 
47 
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 }
77 
78 
80  const wxString& aNetlistFileName,
81  const wxString& aCompFootprintFileName )
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 }
110 
111 
112 bool CMP_READER::Load( NETLIST* aNetlist )
113 {
114  wxCHECK_MSG( aNetlist != NULL,true, wxT( "No netlist passed to CMP_READER::Load()" ) );
115 
116  wxString reference; // Stores value read from line like Reference = BUS1;
117  wxString timestamp; // Stores value read from line like TimeStamp = /32307DE2/AA450F67;
118  wxString footprint; // Stores value read from line like IdModule = CP6;
119  wxString buffer;
120  wxString value;
121 
122  bool ok = true;
123 
124  while( m_lineReader->ReadLine() )
125  {
126  buffer = FROM_UTF8( m_lineReader->Line() );
127 
128  if( !buffer.StartsWith( wxT( "BeginCmp" ) ) )
129  continue;
130 
131  // Begin component description.
132  reference.Empty();
133  footprint.Empty();
134  timestamp.Empty();
135 
136  while( m_lineReader->ReadLine() )
137  {
138  buffer = FROM_UTF8( m_lineReader->Line() );
139 
140  if( buffer.StartsWith( wxT( "EndCmp" ) ) )
141  break;
142 
143  // store string value, stored between '=' and ';' delimiters.
144  value = buffer.AfterFirst( '=' );
145  value = value.BeforeLast( ';' );
146  value.Trim( true );
147  value.Trim( false );
148 
149  if( buffer.StartsWith( wxT( "Reference" ) ) )
150  {
151  reference = value;
152  continue;
153  }
154 
155  if( buffer.StartsWith( wxT( "IdModule =" ) ) )
156  {
157  footprint = value;
158  continue;
159  }
160 
161  if( buffer.StartsWith( wxT( "TimeStamp =" ) ) )
162  {
163  timestamp = value;
164  continue;
165  }
166  }
167 
168  // Find the corresponding item in component list:
169  COMPONENT* component = aNetlist->GetComponentByReference( reference );
170 
171  // The corresponding component could no longer existing in the netlist. This
172  // can happen when it is removed from schematic and still exists in footprint
173  // assignment list. This is an usual case during the life of a design.
174  if( component )
175  {
176  LIB_ID fpid;
177 
178  if( !footprint.IsEmpty() && fpid.Parse( TO_UTF8( footprint ) ) >= 0 )
179  {
180  wxString error;
181  error.Printf( _( "invalid footprint ID in\nfile: <%s>\nline: %d" ),
184 
185  THROW_IO_ERROR( error );
186  }
187 
188  // For checking purpose, store the existing LIB_ID (if any) in the alternate fpid copy
189  // if this existing LIB_ID differs from the LIB_ID read from the .cmp file.
190  // CvPcb can ask for user to chose the right LIB_ID.
191  // It happens if the LIB_ID was modified outside CvPcb.
192  if( fpid != component->GetFPID() && !component->GetFPID().empty() )
193  component->SetAltFPID( component->GetFPID() );
194 
195  component->SetFPID( fpid );
196  }
197  else
198  {
199  ok = false; // can be used to display a warning in Pcbnew.
200  }
201  }
202 
203  return ok;
204 }
void SetAltFPID(const LIB_ID &aFPID)
Definition: pcb_netlist.h:159
Class LINE_READER is an abstract class from which implementation specific LINE_READERs may be derived...
Definition: richio.h:81
static NETLIST_FILE_T GuessNetlistFileType(LINE_READER *aLineReader)
Function GuessNetlistFileType looks at aFileHeaderLine to see if it matches any of the netlist file t...
bool empty() const
Function empty.
Definition: lib_id.h:198
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:164
virtual unsigned LineNumber() const
Function Line Number returns the line number of the last line read from this LINE_READER.
Definition: richio.h:159
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:53
Class KICAD_NETLIST_READER read the new s-expression based KiCad netlist format.
void SetFPID(const LIB_ID &aFPID)
Definition: pcb_netlist.h:153
LINE_READER * m_lineReader
The line reader to read.
static NETLIST_READER * GetNetlistReader(NETLIST *aNetlist, const wxString &aNetlistFileName, const wxString &aCompFootprintFileName=wxEmptyString)
Function GetNetlistReader attempts to determine the net list file type of aNetlistFileName and return...
int Parse(const UTF8 &aId)
Function Parse.
Definition: lib_id.cpp:122
Class LIB_ID.
Definition: lib_id.h:56
#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 Load(NETLIST *aNetlist)
Function Load read the *.cmp file format contains the component footprint assignments created by CvPc...
Class FILE_LINE_READER is a LINE_READER that reads from an open file.
Definition: richio.h:180
Class NETLIST stores all of information read from a netlist along with the flags used to update the N...
Definition: pcb_netlist.h:205
char * Line() const
Function Line returns a pointer to the last line that was read in.
Definition: richio.h:139
virtual ~NETLIST_READER()
Class NETLIST_READER is a pure virtual class to derive a specific type of netlist reader from...
#define THROW_IO_ERROR(x)
Definition: utf8.cpp:60
Class LEGACY_NETLIST_READER reads the KiCad legacy and the old Orcad netlist formats.
Class COMPONENT is used to store components and all of their related information found in a netlist...
Definition: pcb_netlist.h:83
Class CMP_READER reads a component footprint link file (*.cmp) format.
virtual char * ReadLine()=0
Function ReadLine reads a line of text into the buffer and increments the line number counter...
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
virtual const wxString & GetSource() const
Function GetSource returns the name of the source of the lines in an abstract sense.
Definition: richio.h:130
COMPONENT * GetComponentByReference(const wxString &aReference)
Function GetComponentByReference returns a COMPONENT by aReference.
LINE_READER * m_lineReader
The line reader of the netlist.
Module description (excepted pads)
CMP_READER * m_footprintReader
The reader used to load the footprint links. If NULL, footprint links are not read.