KiCad PCB EDA Suite
Loading...
Searching...
No Matches
common/netlist_reader/netlist_reader.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24
25
26#include <string_utils.h>
27#include <reporter.h>
28#include <richio.h>
29
31#include <netlist_reader/netlist_reader.h>
32
33#include <wx/regex.h>
34
35
41
42
44{
45 // Orcad Pcb2 netlist format starts by "( {", followed by an unknown comment,
46 // depending on the tool which created the file
47 wxRegEx reOrcad( wxT( "(?i)[ ]*\\([ \t]+{+" ), wxRE_ADVANCED );
48 wxASSERT( reOrcad.IsValid() );
49
50 // Our legacy netlist format starts by "# EESchema Netlist "
51 wxRegEx reLegacy( wxT( "(?i)#[ \t]+EESchema[ \t]+Netlist[ \t]+" ), wxRE_ADVANCED );
52 wxASSERT( reLegacy.IsValid() );
53
54 // Our new netlist format starts by "(export (version "
55 wxRegEx reKicad( wxT( "[ ]*\\(export(\\s+\\(version)?" ), wxRE_ADVANCED );
56 wxASSERT( reKicad.IsValid() );
57
58 wxString line;
59
60 while( aLineReader->ReadLine() )
61 {
62 line = From_UTF8( aLineReader->Line() );
63
64 if( reLegacy.Matches( line ) )
65 return LEGACY;
66 else if( reKicad.Matches( line ) )
67 return KICAD;
68 else if( reOrcad.Matches( line ) )
69 return ORCAD;
70 }
71
72 return UNKNOWN;
73}
74
75
77 const wxString& aNetlistFileName,
78 const wxString& aCompFootprintFileName )
79{
80 wxASSERT( aNetlist != nullptr );
81
82 std::unique_ptr<FILE_LINE_READER> file_rdr =
83 std::make_unique<FILE_LINE_READER>( aNetlistFileName );
84
85 NETLIST_FILE_T type = GuessNetlistFileType( file_rdr.get() );
86 file_rdr->Rewind();
87
88 // The component footprint link reader is NULL if no file name was specified.
89 std::unique_ptr<CMP_READER> cmp_rdr( aCompFootprintFileName.IsEmpty() ?
90 nullptr :
91 new CMP_READER( new FILE_LINE_READER( aCompFootprintFileName ) ) );
92
93 switch( type )
94 {
95 case LEGACY:
96 case ORCAD:
97 return new LEGACY_NETLIST_READER( file_rdr.release(), aNetlist, cmp_rdr.release() );
98
99 case KICAD:
100 return new KICAD_NETLIST_READER( file_rdr.release(), aNetlist, cmp_rdr.release() );
101
102 default: // Unrecognized format:
103 break;
104 }
105
106 return nullptr;
107}
108
109
111{
112 if( m_lineReader )
113 {
114 delete m_lineReader;
115 m_lineReader = nullptr;
116 }
117}
118
119
120bool CMP_READER::Load( NETLIST* aNetlist )
121{
122 wxCHECK_MSG( aNetlist != nullptr, true, wxT( "No netlist passed to CMP_READER::Load()" ) );
123
124 wxString reference; // Stores value read from line like Reference = BUS1;
125 wxString timestamp; // Stores value read from line like TimeStamp = /32307DE2/AA450F67;
126 wxString footprint; // Stores value read from line like IdModule = CP6;
127 wxString buffer;
128 wxString value;
129
130 bool ok = true;
131
132 while( m_lineReader->ReadLine() )
133 {
134 buffer = From_UTF8( m_lineReader->Line() );
135
136 if( !buffer.StartsWith( wxT( "BeginCmp" ) ) )
137 continue;
138
139 // Begin component description.
140 reference.Empty();
141 footprint.Empty();
142 timestamp.Empty();
143
144 while( m_lineReader->ReadLine() )
145 {
146 buffer = From_UTF8( m_lineReader->Line() );
147
148 if( buffer.StartsWith( wxT( "EndCmp" ) ) )
149 break;
150
151 // store string value, stored between '=' and ';' delimiters.
152 value = buffer.AfterFirst( '=' );
153 value = value.BeforeLast( ';' );
154 value.Trim( true );
155 value.Trim( false );
156
157 if( buffer.StartsWith( wxT( "Reference" ) ) )
158 {
159 reference = value;
160 continue;
161 }
162
163 if( buffer.StartsWith( wxT( "IdModule =" ) ) )
164 {
165 footprint = value;
166 continue;
167 }
168
169 if( buffer.StartsWith( wxT( "TimeStamp =" ) ) )
170 {
171 timestamp = value;
172 continue;
173 }
174 }
175
176 // Find the corresponding item in component list:
177 COMPONENT* component = aNetlist->GetComponentByReference( reference );
178
179 // The corresponding component could no longer existing in the netlist. This
180 // can happen when it is removed from schematic and still exists in footprint
181 // assignment list. This is an usual case during the life of a design.
182 if( component )
183 {
184 LIB_ID fpid;
185
186 if( !footprint.IsEmpty() && fpid.Parse( footprint, true ) >= 0 )
187 {
188 wxString error;
189 error.Printf( _( "Invalid footprint ID in\nfile: '%s'\nline: %d" ),
190 m_lineReader->GetSource(), m_lineReader->LineNumber() );
191
192 THROW_IO_ERROR( error );
193 }
194
195 // For checking purpose, store the existing LIB_ID (if any) in the alternate fpid copy
196 // if this existing LIB_ID differs from the LIB_ID read from the .cmp file.
197 // CvPcb can ask for user to chose the right LIB_ID.
198 // It happens if the LIB_ID was modified outside CvPcb.
199 if( fpid != component->GetFPID() && !component->GetFPID().empty() )
200 component->SetAltFPID( component->GetFPID() );
201
202 component->SetFPID( fpid );
203 }
204 else
205 {
206 ok = false; // can be used to display a warning in Pcbnew.
207 }
208 }
209
210 return ok;
211}
Read a component footprint link file (*.cmp) format.
LINE_READER * m_lineReader
The line reader to read.
bool Load(NETLIST *aNetlist)
Read the *.cmp file format contains the component footprint assignments created by CvPcb into aNetlis...
Store all of the related component information found in a netlist.
void SetAltFPID(const LIB_ID &aFPID)
void SetFPID(const LIB_ID &aFPID)
const LIB_ID & GetFPID() const
A LINE_READER that reads from an open file.
Definition richio.h:186
Read the new s-expression based KiCad netlist format.
Read the KiCad legacy and the old Orcad netlist formats.
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:49
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition lib_id.cpp:52
bool empty() const
Definition lib_id.h:193
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition richio.h:94
virtual char * ReadLine()=0
Read a line of text into the buffer and increments the line number counter.
char * Line() const
Return a pointer to the last line that was read in.
Definition richio.h:130
static NETLIST_READER * GetNetlistReader(NETLIST *aNetlist, const wxString &aNetlistFileName, const wxString &aCompFootprintFileName=wxEmptyString)
Attempt to determine the net list file type of aNetlistFileName and return the appropriate NETLIST_RE...
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.
CMP_READER * m_footprintReader
The reader used to load the footprint links. If NULL, footprint links are not read.
LINE_READER * m_lineReader
The line reader of the netlist.
NETLIST_READER(LINE_READER *aLineReader, NETLIST *aNetlist, CMP_READER *aFootprintLinkReader=nullptr)
Store information read from a netlist along with the flags used to update the NETLIST in the BOARD.
COMPONENT * GetComponentByReference(const wxString &aReference)
Return a COMPONENT by aReference.
#define _(s)
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
wxString From_UTF8(const char *cstring)