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 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.h>
36
37#include <netlist.h>
47
48
49bool SCH_EDIT_FRAME::WriteNetListFile( int aFormat, const wxString& aFullFileName,
50 unsigned aNetlistOptions, REPORTER* aReporter )
51{
52 // Ensure all power symbols have a valid reference
54
55 if( !ReadyToNetlist( _( "Exporting netlist requires a fully annotated schematic." ) ) )
56 return false;
57
58 // If we are using the new connectivity, make sure that we do a full-rebuild
59 if( ADVANCED_CFG::GetCfg().m_IncrementalConnectivity )
61
62 bool res = true;
63 bool executeCommandLine = false;
64
65 wxString fileName = aFullFileName;
66
67 NETLIST_EXPORTER_BASE *helper = nullptr;
68
69 SCHEMATIC* sch = &Schematic();
70
71 switch( aFormat )
72 {
73 case NET_TYPE_PCBNEW:
74 helper = new NETLIST_EXPORTER_KICAD( sch );
75 break;
76
78 helper = new NETLIST_EXPORTER_ORCADPCB2( sch );
79 break;
80
82 helper = new NETLIST_EXPORTER_CADSTAR( sch );
83 break;
84
85 case NET_TYPE_SPICE:
86 helper = new NETLIST_EXPORTER_SPICE( sch );
87 break;
88
90 helper = new NETLIST_EXPORTER_SPICE_MODEL( sch );
91 break;
92
94 helper = new NETLIST_EXPORTER_ALLEGRO( sch );
95 break;
96
97 case NET_TYPE_PADS:
98 helper = new NETLIST_EXPORTER_PADS( sch );
99 break;
100
101 case NET_TYPE_BOM:
102 // When generating the BOM, we have a bare filename so don't strip
103 // the extension or you might string a '.' from the middle of the filename
104 fileName += wxT( "." GENERIC_INTERMEDIATE_NETLIST_EXT );
105
106 helper = new NETLIST_EXPORTER_XML( sch );
107 executeCommandLine = true;
108 break;
109
110 default:
111 {
112 wxFileName tmpFile = fileName;
113 tmpFile.SetExt( GENERIC_INTERMEDIATE_NETLIST_EXT );
114 fileName = tmpFile.GetFullPath();
115
116 helper = new NETLIST_EXPORTER_XML( sch );
117 executeCommandLine = true;
118 }
119 break;
120 }
121
122 NULL_REPORTER devnull;
123
124 if( aReporter )
125 res = helper->WriteNetlist( fileName, aNetlistOptions, *aReporter );
126 else
127 res = helper->WriteNetlist( fileName, aNetlistOptions, devnull );
128
129 delete helper;
130
131 // If user provided a plugin command line, execute it.
132 if( executeCommandLine && res && !m_netListerCommand.IsEmpty() )
133 {
134 wxString prj_dir = Prj().GetProjectPath();
135
136 // strip trailing '/'
137 prj_dir = prj_dir.SubString( 0, prj_dir.Len() - 2 );
138
139 // build full command line from user's format string.
140 // For instance, "xsltproc -o %O /usr/local/lib/kicad/plugins/netlist_form_pads-pcb.xsl %I"
141 // becomes, after the user selects /tmp/s1.net as the output file from the file dialog:
142 // "xsltproc -o /tmp/s1.net /usr/local/lib/kicad/plugins/netlist_form_pads-pcb.xsl /tmp/s1.xml"
144 fileName, aFullFileName,
145 prj_dir );
146
147 if( aReporter )
148 {
149 wxArrayString output, errors;
150 int diag = wxExecute( commandLine, output, errors, m_exec_flags );
151 wxString msg;
152
153 aReporter->ReportHead( commandLine, RPT_SEVERITY_ACTION );
154
155 if( diag != 0 )
156 {
157 msg.Printf( _( "Command error. Return code %d." ), diag );
158 res = false;
159 aReporter->ReportTail( msg, RPT_SEVERITY_ERROR );
160 }
161 else
162 {
163 aReporter->ReportTail( _( "Success." ), RPT_SEVERITY_INFO );
164 }
165
166 if( output.GetCount() )
167 {
168 for( unsigned ii = 0; ii < output.GetCount(); ii++ )
169 aReporter->Report( output[ii], RPT_SEVERITY_INFO );
170 }
171
172 if( errors.GetCount() )
173 {
174 for( unsigned ii = 0; ii < errors.GetCount(); ii++ )
175 aReporter->Report( errors[ii], RPT_SEVERITY_ERROR );
176 }
177 }
178 else
179 {
180 int diag = wxExecute( commandLine, m_exec_flags );
181 if( diag != 0 )
182 res = false;
183 }
184
185 DefaultExecFlags(); // Reset flags to default after executing
186 }
187
188 return res;
189}
190
191
192bool SCH_EDIT_FRAME::ReadyToNetlist( const wxString& aAnnotateMessage )
193{
194 // Ensure all power symbols have a valid reference
196
197 // Symbols must be annotated
198 if( CheckAnnotate( []( ERCE_T, const wxString&, SCH_REFERENCE*, SCH_REFERENCE* ) {} ) )
199 {
200 // Schematic must be annotated: call Annotate dialog and tell the user why.
201 ModalAnnotate( aAnnotateMessage );
202
203 if( CheckAnnotate( []( ERCE_T, const wxString&, SCH_REFERENCE*, SCH_REFERENCE* ) {} ) )
204 return false;
205 }
206
207 // Test duplicate sheet names:
209
210 if( erc.TestDuplicateSheetNames( false ) > 0 )
211 {
212 if( !IsOK( this, _( "Error: duplicate sheet names. Continue?" ) ) )
213 return false;
214 }
215
216 return true;
217}
218
219
221{
222 std::string packet;
223
224 {
225 NETLIST_EXPORTER_KICAD exporter( &Schematic() );
226 STRING_FORMATTER formatter;
227
228 // @todo : trim GNL_ALL down to minimum for CVPCB
229 exporter.Format( &formatter, GNL_ALL | GNL_OPT_KICAD );
230
231 packet = formatter.GetString(); // an abbreviated "kicad" (s-expr) netlist
232
233 // NETLIST_EXPORTER_KICAD must go out of scope so it can clean up things like the
234 // current sheet setting before sending expressmail
235 }
236
238}
239
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
Definition: erc.h:46
virtual void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=nullptr)
Send aPayload to aDestination from aSource.
Definition: kiway.cpp:553
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:223
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:71
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:108
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:99
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:75
SCH_SHEET_LIST GetSheets() const override
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:100
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:441
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:433
const std::string & GetString()
Definition: richio.h:456
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:360
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:591
KIWAY Kiway
@ 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
VECTOR3I res