KiCad PCB EDA Suite
Loading...
Searching...
No Matches
netlist_generator.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) 1992-2018 jp.charras at wanadoo.fr
5 * Copyright (C) 2013 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
6 * Copyright The 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, see <https://www.gnu.org/licenses/>.
20 */
21
22#include <advanced_config.h>
23#include <common.h> // for ProcessExecute
24#include <string_utils.h>
25#include <gestfich.h>
26#include <sch_edit_frame.h>
27#include <schematic.h>
28#include <reporter.h>
29#include <confirm.h>
30#include <kiway.h>
31#include <erc/erc.h>
32#include <richio.h>
33#include <wx/utils.h>
34
35#include <netlist.h>
45
46
47bool SCH_EDIT_FRAME::WriteNetListFile( int aFormat, const wxString& aFullFileName,
48 unsigned aNetlistOptions, REPORTER* aReporter )
49{
50 // Ensure all power symbols have a valid reference
52
53 if( !ReadyToNetlist( _( "Exporting netlist requires a fully annotated schematic." ) ) )
54 return false;
55
57
58 bool res = true;
59 bool executeCommandLine = false;
60
61 wxString fileName = aFullFileName;
62
63 NETLIST_EXPORTER_BASE *helper = nullptr;
64
65 SCHEMATIC* sch = &Schematic();
66
67 switch( aFormat )
68 {
69 case NET_TYPE_PCBNEW:
70 helper = new NETLIST_EXPORTER_KICAD( sch );
71 break;
72
74 helper = new NETLIST_EXPORTER_ORCADPCB2( sch );
75 break;
76
78 helper = new NETLIST_EXPORTER_CADSTAR( sch );
79 break;
80
81 case NET_TYPE_SPICE:
82 helper = new NETLIST_EXPORTER_SPICE( sch );
83 break;
84
86 helper = new NETLIST_EXPORTER_SPICE_MODEL( sch );
87 break;
88
90 helper = new NETLIST_EXPORTER_ALLEGRO( sch );
91 break;
92
93 case NET_TYPE_PADS:
94 helper = new NETLIST_EXPORTER_PADS( sch );
95 break;
96
97 case NET_TYPE_BOM:
98 // When generating the BOM, we have a bare filename so don't strip
99 // the extension or you might string a '.' from the middle of the filename
100 fileName += wxT( "." GENERIC_INTERMEDIATE_NETLIST_EXT );
101
102 helper = new NETLIST_EXPORTER_XML( sch );
103 executeCommandLine = true;
104 break;
105
106 default:
107 {
108 wxFileName tmpFile = fileName;
109 tmpFile.SetExt( GENERIC_INTERMEDIATE_NETLIST_EXT );
110 fileName = tmpFile.GetFullPath();
111
112 helper = new NETLIST_EXPORTER_XML( sch );
113 executeCommandLine = true;
114 }
115 break;
116 }
117
118 NULL_REPORTER devnull;
119
120 if( aReporter )
121 res = helper->WriteNetlist( fileName, aNetlistOptions, *aReporter );
122 else
123 res = helper->WriteNetlist( fileName, aNetlistOptions, devnull );
124
125 delete helper;
126
127 RefreshConnectivity( true );
128
129 // If user provided a plugin command line, execute it.
130 if( executeCommandLine && res && !m_netListerCommand.IsEmpty() )
131 {
132 wxString prj_dir = Prj().GetProjectPath();
133
134 // strip trailing '/'
135 prj_dir = prj_dir.SubString( 0, prj_dir.Len() - 2 );
136
137 // build full command line from user's format string.
138 // For instance, "xsltproc -o %O /usr/local/lib/kicad/plugins/netlist_form_pads-pcb.xsl %I"
139 // becomes, after the user selects /tmp/s1.net as the output file from the file dialog:
140 // "xsltproc -o /tmp/s1.net /usr/local/lib/kicad/plugins/netlist_form_pads-pcb.xsl /tmp/s1.xml"
142 fileName, aFullFileName,
143 prj_dir );
144
145 // Clear AppImage / embedded Python env so system interpreters used by BOM
146 // generators (e.g. /usr/bin/python3) do not inherit PYTHONHOME/PYTHONPATH
147 // that point into the AppImage and fail with ModuleNotFoundError: encodings.
148 wxExecuteEnv env;
149 wxGetEnvMap( &env.env );
150 env.env.erase( wxS( "PYTHONHOME" ) );
151 env.env.erase( wxS( "PYTHONPATH" ) );
152
153 if( aReporter )
154 {
155 wxArrayString output, errors;
156 int diag = wxExecute( commandLine, output, errors, m_exec_flags, &env );
157 wxString msg;
158
159 aReporter->ReportHead( commandLine, RPT_SEVERITY_ACTION );
160
161 if( diag != 0 )
162 {
163 msg.Printf( _( "Command error. Return code %d." ), diag );
164 res = false;
165 aReporter->ReportTail( msg, RPT_SEVERITY_ERROR );
166 }
167 else
168 {
169 aReporter->ReportTail( _( "Done." ), RPT_SEVERITY_INFO );
170 }
171
172 if( output.GetCount() )
173 {
174 for( unsigned ii = 0; ii < output.GetCount(); ii++ )
175 aReporter->Report( output[ii], RPT_SEVERITY_INFO );
176 }
177
178 if( errors.GetCount() )
179 {
180 for( unsigned ii = 0; ii < errors.GetCount(); ii++ )
181 aReporter->Report( errors[ii], RPT_SEVERITY_ERROR );
182 }
183 }
184 else
185 {
186 int diag = wxExecute( commandLine, m_exec_flags, nullptr, &env );
187 if( diag != 0 )
188 res = false;
189 }
190
191 DefaultExecFlags(); // Reset flags to default after executing
192 }
193
194 return res;
195}
196
197
198bool SCH_EDIT_FRAME::ReadyToNetlist( const wxString& aAnnotateMessage, bool* aUserCancelled )
199{
200 if( aUserCancelled )
201 *aUserCancelled = false;
202
203 // Ensure all power symbols have a valid reference
205
206 // Symbols must be annotated
207 if( CheckAnnotate( []( ERCE_T, const wxString&, SCH_REFERENCE*, SCH_REFERENCE* ) {},
209 {
210 // Schematic must be annotated: call Annotate dialog and tell the user why.
211 ModalAnnotate( aAnnotateMessage );
212
213 if( CheckAnnotate( []( ERCE_T, const wxString&, SCH_REFERENCE*, SCH_REFERENCE* ) {},
215 return false;
216 }
217
218 // Test duplicate sheet names:
220
221 if( erc.TestDuplicateSheetNames( false ) > 0 )
222 {
223 if( !IsOK( this, _( "Error: duplicate sheet names. Continue?" ) ) )
224 {
225 if( aUserCancelled )
226 *aUserCancelled = true;
227
228 return false;
229 }
230 }
231
232 return true;
233}
234
235
237{
239 std::string packet;
240
241 {
242 NETLIST_EXPORTER_KICAD exporter( &Schematic() );
243 STRING_FORMATTER formatter;
244
245 // @todo : trim GNL_ALL down to minimum for CVPCB
246 exporter.Format( &formatter, GNL_ALL | GNL_OPT_KICAD );
247
248 packet = formatter.GetString(); // an abbreviated "kicad" (s-expr) netlist
249
250 // NETLIST_EXPORTER_KICAD must go out of scope so it can clean up things like the
251 // current sheet setting before sending expressmail
252 }
253
254 RefreshConnectivity( true );
256}
virtual void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=nullptr, bool aFromOtherThread=false)
Send aPayload to aDestination from aSource.
Definition kiway.cpp:486
Generate a netlist compatible with Allegro.
An abstract class used for the netlist exporters that Eeschema supports.
bool WriteNetlist(const wxString &aOutFileName, unsigned aNetlistOptions, REPORTER &aReporter)
Write to specified output file.
static wxString MakeCommandLine(const wxString &aFormatString, const wxString &aNetlistFile, const wxString &aFinalFile, const wxString &aProjectDirectory)
Build up a string that describes a command line for executing a child process.
Generate a netlist compatible with CADSTAR.
Generate the KiCad netlist format supported by Pcbnew.
void Format(OUTPUTFORMATTER *aOutputFormatter, int aCtl)
Output this s-expression netlist into aOutputFormatter.
Generate a netlist compatible with OrCAD.
Generate a netlist compatible with PADS.
Generate a generic XML based netlist file.
A singleton reporter that reports to nowhere.
Definition reporter.h:267
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition project.cpp:183
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
virtual REPORTER & ReportHead(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Places the report at the beginning of the list for objects that support ordering.
Definition reporter.h:130
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:102
virtual REPORTER & ReportTail(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Places the report at the end of the list, for objects that support report ordering.
Definition reporter.h:121
Holds all the data relating to one schematic.
Definition schematic.h:148
SCH_SHEET_LIST Hierarchy() const
Return the full schematic flattened hierarchical sheet list.
void DefaultExecFlags()
Reset the execution flags to defaults for external netlist and bom generators.
void RefreshConnectivity(bool aForce=false)
Refresh connectivity-dependent display state after a model rebuild.
bool ReadyToNetlist(const wxString &aAnnotateMessage, bool *aUserCancelled=nullptr)
Check if we are ready to write a netlist file for the current schematic.
bool WriteNetListFile(int aFormat, const wxString &aFullFileName, unsigned aNetlistOptions, REPORTER *aReporter=nullptr)
Create a netlist file.
SCHEMATIC & Schematic() const
void PrepareForNetlist()
Commit source cleanup before the exporter's full connectivity rebuild.
int m_exec_flags
Flags of the wxExecute() function to call a custom net list generator.
void sendNetlistToCvpcb()
Send the KiCad netlist over to CVPCB.
int CheckAnnotate(ANNOTATION_ERROR_HANDLER aErrorHandler, ANNOTATE_SCOPE_T aAnnotateScope, bool aRecursive, SYMBOL_FILTER aSymbolFilter)
Check for annotation errors.
Definition annotate.cpp:510
int ModalAnnotate(const wxString &aMessage)
Run a modal version of the annotate dialog for a specific purpose.
wxString m_netListerCommand
Command line to call a custom net list generator.
A helper to define a symbol's reference designator in a schematic.
void AnnotatePowerSymbols()
Silently annotate the not yet annotated power symbols of the entire hierarchy of the sheet path list.
Implement an OUTPUTFORMATTER to a memory buffer.
Definition richio.h:430
const std::string & GetString()
Definition richio.h:453
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition confirm.cpp:274
This file is part of the common library.
#define _(s)
ERCE_T
ERC error codes.
@ FRAME_CVPCB
Definition frame_type.h:48
PROJECT & Prj()
Definition kicad.cpp:727
@ MAIL_EESCHEMA_NETLIST
Definition mail_type.h:40
#define GNL_ALL
#define GENERIC_INTERMEDIATE_NETLIST_EXT
@ GNL_OPT_KICAD
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_INFO
@ RPT_SEVERITY_ACTION
@ ANNOTATE_ALL
Annotate the full schematic.
@ SYMBOL_FILTER_NON_POWER
KIWAY Kiway(KFCTL_STANDALONE)
VECTOR3I res