KiCad PCB EDA Suite
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 <dick@softplc.com>
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 <common.h> // for ProcessExecute
27 #include <kicad_string.h>
28 #include <gestfich.h>
29 #include <sch_edit_frame.h>
30 #include <schematic.h>
31 #include <reporter.h>
32 #include <confirm.h>
33 #include <kiway.h>
34 #include <erc.h>
35 
36 #include <netlist.h>
37 #include <netlist_exporter_base.h>
41 #include <netlist_exporter_kicad.h>
42 #include <netlist_exporter_xml.h>
43 
44 
45 bool SCH_EDIT_FRAME::WriteNetListFile( int aFormat, const wxString& aFullFileName,
46  unsigned aNetlistOptions, REPORTER* aReporter )
47 {
48  // Ensure all power symbols have a valid reference
50 
51  // Ensure the netlist data is up to date:
53 
54  if( !ReadyToNetlist( _( "Exporting netlist requires a fully annotated schematic." ) ) )
55  return false;
56 
57  bool res = true;
58  bool executeCommandLine = false;
59 
60  wxString fileName = aFullFileName;
61 
62  NETLIST_EXPORTER_BASE *helper;
63 
64  SCHEMATIC* sch = &Schematic();
65 
66  switch( aFormat )
67  {
68  case NET_TYPE_PCBNEW:
69  helper = new NETLIST_EXPORTER_KICAD( sch );
70  break;
71 
72  case NET_TYPE_ORCADPCB2:
73  helper = new NETLIST_EXPORTER_ORCADPCB2( sch );
74  break;
75 
76  case NET_TYPE_CADSTAR:
77  helper = new NETLIST_EXPORTER_CADSTAR( sch );
78  break;
79 
80  case NET_TYPE_SPICE:
81  helper = new NETLIST_EXPORTER_PSPICE( sch );
82  break;
83 
84  default:
85  {
86  wxFileName tmpFile = fileName;
87  tmpFile.SetExt( GENERIC_INTERMEDIATE_NETLIST_EXT );
88  fileName = tmpFile.GetFullPath();
89 
90  helper = new NETLIST_EXPORTER_XML( sch );
91  executeCommandLine = true;
92  }
93  break;
94  }
95 
96  res = helper->WriteNetlist( fileName, aNetlistOptions );
97  delete helper;
98 
99  // If user provided a plugin command line, execute it.
100  if( executeCommandLine && res && !m_netListerCommand.IsEmpty() )
101  {
102  wxString prj_dir = Prj().GetProjectPath();
103 
104  // strip trailing '/'
105  prj_dir = prj_dir.SubString( 0, prj_dir.Len() - 2 );
106 
107  // build full command line from user's format string.
108  // For instance, "xsltproc -o %O /usr/local/lib/kicad/plugins/netlist_form_pads-pcb.xsl %I"
109  // becomes, after the user selects /tmp/s1.net as the output file from the file dialog:
110  // "xsltproc -o /tmp/s1.net /usr/local/lib/kicad/plugins/netlist_form_pads-pcb.xsl /tmp/s1.xml"
112  fileName, aFullFileName,
113  prj_dir );
114 
115  if( aReporter )
116  {
117  wxArrayString output, errors;
118  int diag = wxExecute( commandLine, output, errors, m_exec_flags );
119  wxString msg;
120 
121  aReporter->ReportHead( commandLine, RPT_SEVERITY_ACTION );
122 
123  if( diag != 0 )
124  {
125  msg.Printf( _( "Command error. Return code %d." ), diag );
126  aReporter->ReportTail( msg, RPT_SEVERITY_ERROR );
127  }
128  else
129  {
130  aReporter->ReportTail( _( "Success." ), RPT_SEVERITY_INFO );
131  }
132 
133  if( output.GetCount() )
134  {
135  for( unsigned ii = 0; ii < output.GetCount(); ii++ )
136  aReporter->Report( output[ii], RPT_SEVERITY_INFO );
137  }
138 
139  if( errors.GetCount() )
140  {
141  for( unsigned ii = 0; ii < errors.GetCount(); ii++ )
142  aReporter->Report( errors[ii], RPT_SEVERITY_ERROR );
143  }
144  }
145  else
146  {
147  ProcessExecute( commandLine, m_exec_flags );
148  }
149 
150  DefaultExecFlags(); // Reset flags to default after executing
151  }
152 
153  return res;
154 }
155 
156 
157 bool SCH_EDIT_FRAME::ReadyToNetlist( const wxString& aAnnotateMessage )
158 {
159  // Ensure all power symbols have a valid reference
161 
162  // Symbols must be annotated
163  if( CheckAnnotate( []( ERCE_T, const wxString&, SCH_REFERENCE*, SCH_REFERENCE* ) {} ) )
164  {
165  // Schematic must be annotated: call Annotate dialog and tell the user why.
166  ModalAnnotate( aAnnotateMessage );
167 
168  if( CheckAnnotate( []( ERCE_T, const wxString&, SCH_REFERENCE*, SCH_REFERENCE* ) {} ) )
169  return false;
170  }
171 
172  // Test duplicate sheet names:
173  ERC_TESTER erc( &Schematic() );
174 
175  if( erc.TestDuplicateSheetNames( false ) > 0 )
176  {
177  if( !IsOK( this, _( "Error: duplicate sheet names. Continue?" ) ) )
178  return false;
179  }
180 
181  return true;
182 }
183 
184 
186 {
187  std::string packet;
188 
189  {
190  NETLIST_EXPORTER_KICAD exporter( &Schematic() );
191  STRING_FORMATTER formatter;
192 
193  // @todo : trim GNL_ALL down to minimum for CVPCB
194  exporter.Format( &formatter, GNL_ALL );
195 
196  packet = formatter.GetString(); // an abbreviated "kicad" (s-expr) netlist
197 
198  // NETLIST_EXPORTER_KICAD must go out of scope so it can clean up things like the
199  // current sheet setting before sending expressmail
200  }
201 
203 }
204 
bool ReadyToNetlist(const wxString &aAnnotateMessage)
Check if we are ready to write a netlist file for the current schematic.
wxString m_netListerCommand
Command line to call a custom net list generator.
void DefaultExecFlags()
Reset the execution flags to defaults for external netlist and bom generators.
int ProcessExecute(const wxString &aCommandLine, int aFlags, wxProcess *callback)
Run a command in a child process.
Definition: common.cpp:46
virtual bool WriteNetlist(const wxString &aOutFileName, unsigned aNetlistOptions)
Write to specified output file.
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
Definition: kiway_holder.h:53
This file is part of the common library TODO brief description.
void RecalculateConnections(SCH_CLEANUP_FLAGS aCleanupFlags)
Generate the connection data for the entire schematic hierarchy.
Holds all the data relating to one schematic.
Definition: schematic.h:59
This file is part of the common library.
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:93
Definition: erc.h:47
virtual void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=nullptr)
Send aPayload to aDestination from aSource.
Definition: kiway.cpp:476
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:64
void Format(OUTPUTFORMATTER *aOutputFormatter, int aCtl)
Output this s-expression netlist into aOutputFormatter.
int CheckAnnotate(ANNOTATION_ERROR_HANDLER aErrorHandler, ANNOTATE_SCOPE_T aAnnotateScope=ANNOTATE_ALL)
Check for annotation errors.
Definition: annotate.cpp:342
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:122
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.
bool WriteNetListFile(int aFormat, const wxString &aFullFileName, unsigned aNetlistOptions, REPORTER *aReporter=nullptr)
Create a netlist file.
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
Generate the KiCad netlist format supported by Pcbnew.
SCHEMATIC & Schematic() const
#define _(s)
const std::string & GetString()
Definition: richio.h:438
#define GNL_ALL
ERCE_T
ERC error codes.
Definition: erc_settings.h:36
An abstract class used for the netlist exporters that Eeschema supports.
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:102
Generate a netlist compatible with CADSTAR.
SCH_SHEET_LIST GetSheets() const override
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:87
Generate a netlist compatible with OrCAD.
int ModalAnnotate(const wxString &aMessage)
Run a modal version of the annotate dialog for a specific purpose.
Generate a PSPICE compatible netlist.
The common library.
void AnnotatePowerSymbols()
Silently annotate the not yet annotated power symbols of the entire hierarchy of the sheet path list.
#define GENERIC_INTERMEDIATE_NETLIST_EXT
int m_exec_flags
Flags of the wxExecute() function to call a custom net list generator.
Generate a generic XML based netlist file.
Implement an OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:414
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:296
A helper to define a symbol's reference designator in a schematic.
void sendNetlistToCvpcb()
Send the KiCad netlist over to CVPCB.