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 (C) 1992-2021, 2024 KiCad Developers, see change_log.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, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#include <advanced_config.h>
27#include <common.h> // for ProcessExecute
28#include <string_utils.h>
29#include <gestfich.h>
30#include <sch_edit_frame.h>
31#include <schematic.h>
32#include <reporter.h>
33#include <confirm.h>
34#include <kiway.h>
35#include <erc/erc.h>
36#include <richio.h>
37
38#include <netlist.h>
48
49
50bool SCH_EDIT_FRAME::WriteNetListFile( int aFormat, const wxString& aFullFileName,
51 unsigned aNetlistOptions, REPORTER* aReporter )
52{
53 // Ensure all power symbols have a valid reference
55
56 if( !ReadyToNetlist( _( "Exporting netlist requires a fully annotated schematic." ) ) )
57 return false;
58
59 // If we are using the new connectivity, make sure that we do a full-rebuild
60 if( ADVANCED_CFG::GetCfg().m_IncrementalConnectivity )
62
63 bool res = true;
64 bool executeCommandLine = false;
65
66 wxString fileName = aFullFileName;
67
68 NETLIST_EXPORTER_BASE *helper = nullptr;
69
70 SCHEMATIC* sch = &Schematic();
71
72 switch( aFormat )
73 {
74 case NET_TYPE_PCBNEW:
75 helper = new NETLIST_EXPORTER_KICAD( sch );
76 break;
77
79 helper = new NETLIST_EXPORTER_ORCADPCB2( sch );
80 break;
81
83 helper = new NETLIST_EXPORTER_CADSTAR( sch );
84 break;
85
86 case NET_TYPE_SPICE:
87 helper = new NETLIST_EXPORTER_SPICE( sch );
88 break;
89
91 helper = new NETLIST_EXPORTER_SPICE_MODEL( sch );
92 break;
93
95 helper = new NETLIST_EXPORTER_ALLEGRO( sch );
96 break;
97
98 case NET_TYPE_PADS:
99 helper = new NETLIST_EXPORTER_PADS( sch );
100 break;
101
102 case NET_TYPE_BOM:
103 // When generating the BOM, we have a bare filename so don't strip
104 // the extension or you might string a '.' from the middle of the filename
105 fileName += wxT( "." GENERIC_INTERMEDIATE_NETLIST_EXT );
106
107 helper = new NETLIST_EXPORTER_XML( sch );
108 executeCommandLine = true;
109 break;
110
111 default:
112 {
113 wxFileName tmpFile = fileName;
114 tmpFile.SetExt( GENERIC_INTERMEDIATE_NETLIST_EXT );
115 fileName = tmpFile.GetFullPath();
116
117 helper = new NETLIST_EXPORTER_XML( sch );
118 executeCommandLine = true;
119 }
120 break;
121 }
122
123 NULL_REPORTER devnull;
124
125 if( aReporter )
126 res = helper->WriteNetlist( fileName, aNetlistOptions, *aReporter );
127 else
128 res = helper->WriteNetlist( fileName, aNetlistOptions, devnull );
129
130 delete helper;
131
132 // If user provided a plugin command line, execute it.
133 if( executeCommandLine && res && !m_netListerCommand.IsEmpty() )
134 {
135 wxString prj_dir = Prj().GetProjectPath();
136
137 // strip trailing '/'
138 prj_dir = prj_dir.SubString( 0, prj_dir.Len() - 2 );
139
140 // build full command line from user's format string.
141 // For instance, "xsltproc -o %O /usr/local/lib/kicad/plugins/netlist_form_pads-pcb.xsl %I"
142 // becomes, after the user selects /tmp/s1.net as the output file from the file dialog:
143 // "xsltproc -o /tmp/s1.net /usr/local/lib/kicad/plugins/netlist_form_pads-pcb.xsl /tmp/s1.xml"
145 fileName, aFullFileName,
146 prj_dir );
147
148 if( aReporter )
149 {
150 wxArrayString output, errors;
151 int diag = wxExecute( commandLine, output, errors, m_exec_flags );
152 wxString msg;
153
154 aReporter->ReportHead( commandLine, RPT_SEVERITY_ACTION );
155
156 if( diag != 0 )
157 {
158 msg.Printf( _( "Command error. Return code %d." ), diag );
159 res = false;
160 aReporter->ReportTail( msg, RPT_SEVERITY_ERROR );
161 }
162 else
163 {
164 aReporter->ReportTail( _( "Success." ), RPT_SEVERITY_INFO );
165 }
166
167 if( output.GetCount() )
168 {
169 for( unsigned ii = 0; ii < output.GetCount(); ii++ )
170 aReporter->Report( output[ii], RPT_SEVERITY_INFO );
171 }
172
173 if( errors.GetCount() )
174 {
175 for( unsigned ii = 0; ii < errors.GetCount(); ii++ )
176 aReporter->Report( errors[ii], RPT_SEVERITY_ERROR );
177 }
178 }
179 else
180 {
181 int diag = wxExecute( commandLine, m_exec_flags );
182 if( diag != 0 )
183 res = false;
184 }
185
186 DefaultExecFlags(); // Reset flags to default after executing
187 }
188
189 return res;
190}
191
192
193bool SCH_EDIT_FRAME::ReadyToNetlist( const wxString& aAnnotateMessage )
194{
195 // Ensure all power symbols have a valid reference
197
198 // Symbols must be annotated
199 if( CheckAnnotate( []( ERCE_T, const wxString&, SCH_REFERENCE*, SCH_REFERENCE* ) {} ) )
200 {
201 // Schematic must be annotated: call Annotate dialog and tell the user why.
202 ModalAnnotate( aAnnotateMessage );
203
204 if( CheckAnnotate( []( ERCE_T, const wxString&, SCH_REFERENCE*, SCH_REFERENCE* ) {} ) )
205 return false;
206 }
207
208 // Test duplicate sheet names:
210
211 if( erc.TestDuplicateSheetNames( false ) > 0 )
212 {
213 if( !IsOK( this, _( "Error: duplicate sheet names. Continue?" ) ) )
214 return false;
215 }
216
217 return true;
218}
219
220
222{
223 std::string packet;
224
225 {
226 NETLIST_EXPORTER_KICAD exporter( &Schematic() );
227 STRING_FORMATTER formatter;
228
229 // @todo : trim GNL_ALL down to minimum for CVPCB
230 exporter.Format( &formatter, GNL_ALL | GNL_OPT_KICAD );
231
232 packet = formatter.GetString(); // an abbreviated "kicad" (s-expr) netlist
233
234 // NETLIST_EXPORTER_KICAD must go out of scope so it can clean up things like the
235 // current sheet setting before sending expressmail
236 }
237
239}
240
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
Definition: erc.h:52
virtual void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=nullptr)
Send aPayload to aDestination from aSource.
Definition: kiway.cpp:527
Generate a netlist compatible with Allegro.
An abstract class used for the netlist exporters that Eeschema supports.
virtual 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:203
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:135
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:72
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:109
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:100
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
Holds all the data relating to one schematic.
Definition: schematic.h:77
SCH_SHEET_LIST Hierarchy() const override
Return the full schematic flattened hierarchical sheet list.
Definition: schematic.cpp:214
void DefaultExecFlags()
Reset the execution flags to defaults for external netlist and bom generators.
bool ReadyToNetlist(const wxString &aAnnotateMessage)
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
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.
void RecalculateConnections(SCH_COMMIT *aCommit, SCH_CLEANUP_FLAGS aCleanupFlags)
Generate the connection data for the entire schematic hierarchy.
int CheckAnnotate(ANNOTATION_ERROR_HANDLER aErrorHandler, ANNOTATE_SCOPE_T aAnnotateScope=ANNOTATE_ALL, bool aRecursive=true)
Check for annotation errors.
Definition: annotate.cpp:460
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:436
const std::string & GetString()
Definition: richio.h:459
The common library.
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:250
This file is part of the common library.
#define _(s)
ERCE_T
ERC error codes.
Definition: erc_settings.h:37
@ FRAME_CVPCB
Definition: frame_type.h:52
PROJECT & Prj()
Definition: kicad.cpp:595
@ MAIL_EESCHEMA_NETLIST
Definition: mail_type.h:44
@ NET_TYPE_BOM
Definition: netlist.h:37
@ NET_TYPE_CADSTAR
Definition: netlist.h:40
@ NET_TYPE_ORCADPCB2
Definition: netlist.h:39
@ NET_TYPE_SPICE
Definition: netlist.h:41
@ NET_TYPE_ALLEGRO
Definition: netlist.h:43
@ NET_TYPE_SPICE_MODEL
Definition: netlist.h:42
@ NET_TYPE_PCBNEW
Definition: netlist.h:38
@ NET_TYPE_PADS
Definition: netlist.h:44
#define GNL_ALL
#define GENERIC_INTERMEDIATE_NETLIST_EXT
@ GNL_OPT_KICAD
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_INFO
@ RPT_SEVERITY_ACTION
@ GLOBAL_CLEANUP
KIWAY Kiway(KFCTL_STANDALONE)
VECTOR3I res