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-2020 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 <kicad_string.h>
27 #include <gestfich.h>
28 #include <pgm_base.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( false ) )
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, e.g.:
108  // "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 
120  wxString msg;
121 
122  msg << _( "Run command:" ) << wxT( "\n" ) << commandLine << wxT( "\n\n" );
123 
124  aReporter->ReportHead( msg, RPT_SEVERITY_ACTION );
125 
126  if( diag != 0 )
127  aReporter->ReportTail( wxString::Format(
128  _("Command error. Return code %d" ), diag ),
130  else
131  aReporter->ReportTail( _( "Success" ), RPT_SEVERITY_INFO );
132 
133  if( output.GetCount() )
134  {
135  msg.Empty();
136  msg << wxT( "\n" ) << _( "Info messages:" ) << wxT( "\n" );
137  aReporter->Report( msg, RPT_SEVERITY_INFO );
138 
139  for( unsigned ii = 0; ii < output.GetCount(); ii++ )
140  aReporter->Report( output[ii] + wxT( "\n" ), RPT_SEVERITY_INFO );
141  }
142 
143  if( errors.GetCount() )
144  {
145  msg.Empty();
146  msg << wxT("\n") << _( "Error messages:" ) << wxT( "\n" );
147  aReporter->Report( msg, RPT_SEVERITY_INFO );
148 
149  for( unsigned ii = 0; ii < errors.GetCount(); ii++ )
150  aReporter->Report( errors[ii] + wxT( "\n" ), RPT_SEVERITY_ERROR );
151  }
152  }
153  else
154  ProcessExecute( commandLine, m_exec_flags );
155 
156  DefaultExecFlags(); // Reset flags to default after executing
157  }
158 
159  return res;
160 }
161 
162 
163 bool SCH_EDIT_FRAME::ReadyToNetlist( bool aSilent, bool aSilentAnnotate )
164 {
165  // Ensure all power symbols have a valid reference
167 
168  // Components must be annotated
169  if( CheckAnnotate( NULL_REPORTER::GetInstance(), false ) )
170  {
171  if( aSilentAnnotate )
172  {
173  AnnotateComponents( true, UNSORTED, INCREMENTAL_BY_REF, 0, false, false, true,
175  }
176  else
177  {
178  if( aSilent )
179  return false;
180 
181  // Schematic must be annotated: call Annotate dialog and tell the user why.
183  _( "Exporting the netlist requires a completely annotated schematic." ) );
184 
185  if( CheckAnnotate( NULL_REPORTER::GetInstance(), false ) )
186  return false;
187  }
188  }
189 
190  // Test duplicate sheet names:
191  ERC_TESTER erc( &Schematic() );
192 
193  if( erc.TestDuplicateSheetNames( false ) > 0 )
194  {
195  if( aSilent || !IsOK( this, _( "Error: duplicate sheet names. Continue?" ) ) )
196  return false;
197  }
198 
199  return true;
200 }
201 
202 
204 {
205  NETLIST_EXPORTER_KICAD exporter( &Schematic() );
206  STRING_FORMATTER formatter;
207 
208  // @todo : trim GNL_ALL down to minimum for CVPCB
209  exporter.Format( &formatter, GNL_ALL );
210 
211  std::string packet = formatter.GetString(); // an abbreviated "kicad" (s-expr) netlist
213 }
214 
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:40
virtual bool WriteNetlist(const wxString &aOutFileName, unsigned aNetlistOptions)
Function WriteNetlist writes to specified output file.
KIWAY & Kiway() const
Function Kiway returns a reference to the KIWAY that this object has an opportunity to participate in...
Definition: kiway_holder.h:56
SCH_SHEET_LIST GetSheets() const
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:97
bool WriteNetListFile(int aFormat, const wxString &aFullFileName, unsigned aNetlistOptions, REPORTER *aReporter=NULL)
Create a netlist file.
This file is part of the common library TODO brief description.
void RecalculateConnections(SCH_CLEANUP_FLAGS aCleanupFlags)
Generates the connection data for the entire schematic hierarchy.
Holds all the data relating to one schematic A schematic may consist of one or more sheets (and one r...
Definition: schematic.h:44
This file is part of the common library.
void AnnotateComponents(bool aAnnotateSchematic, ANNOTATE_ORDER_T aSortOption, ANNOTATE_OPTION_T aAlgoOption, int aStartNumber, bool aResetAnnotation, bool aRepairTimestamps, bool aLockUnits, REPORTER &aReporter)
Annotate the components in the schematic that are not currently annotated.
virtual REPORTER & ReportTail(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Function ReportTail Places the report at the end of the list, for objects that support report orderin...
Definition: reporter.h:94
Annotate incrementally using the first free reference number.
Definition: erc.h:51
bool ReadyToNetlist(bool aSilent=false, bool aSilentAnnotate=false)
Checks if we are ready to write a netlist file for the current schematic.
REPORTER is 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.
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Function Report is a pure virtual function to override in the derived object.
VTBL_ENTRY const wxString GetProjectPath() const
Function GetProjectPath returns 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)
Function MakeCommandLine builds up a string that describes a command line for executing a child proce...
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
Annotate by position of component in the schematic sheet object list.
Generate the KiCad netlist format supported by Pcbnew.
SCHEMATIC & Schematic() const
const std::string & GetString()
Definition: richio.h:476
#define GNL_ALL
NETLIST_EXPORTER_BASE is a abstract class used for the netlist exporters that eeschema supports.
int CheckAnnotate(REPORTER &aReporter, bool aOneSheetOnly)
Check for annotation errors.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
virtual REPORTER & ReportHead(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Function ReportHead Places the report at the beginning of the list for objects that support ordering.
Definition: reporter.h:103
NETLIST_EXPORTER_CADSTAR generates a netlist compatible with CADSTAR.
VTBL_ENTRY void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=NULL)
Function ExpressMail send aPayload to aDestination from aSource.
Definition: kiway.cpp:439
NETLIST_EXPORTER_ORCADPCB2 generates a netlist compatible with OrCAD.
int ModalAnnotate(const wxString &aMessage)
Run a modal version of the Annotate dialog for a specific purpose.
see class PGM_BASE
#define _(s)
Definition: 3d_actions.cpp:33
NETLIST_EXPORTER_PSPICE generates a PSPICE compatible netlist.
void AnnotatePowerSymbols()
Silently annotate the not yet annotated power symbols of the entire hierarchy of the sheet path list.
#define GENERIC_INTERMEDIATE_NETLIST_EXT
static REPORTER & GetInstance()
Definition: reporter.cpp:105
int m_exec_flags
Flags of the wxExecute() function to call a custom net list generator.
Generate a generic XML based netlist file.
STRING_FORMATTER implements OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:446
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:284
void sendNetlistToCvpcb()
Send the kicad netlist over to CVPCB.
int TestDuplicateSheetNames(bool aCreateMarker)
Inside a given sheet, one cannot have sheets with duplicate names (file names can be duplicated).