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-2018 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 
31 #include <fctsys.h>
32 #include <kicad_string.h>
33 #include <gestfich.h>
34 #include <pgm_base.h>
35 #include <sch_edit_frame.h>
36 #include <reporter.h>
37 #include <confirm.h>
38 #include <kiway.h>
39 
40 #include <netlist.h>
41 #include <netlist_exporter.h>
45 #include <netlist_exporter_kicad.h>
47 
48 #include <invoke_sch_dialog.h>
49 
51  int aFormat, const wxString& aFullFileName,
52  unsigned aNetlistOptions, REPORTER* aReporter )
53 {
54  bool res = true;
55  bool executeCommandLine = false;
56 
57  wxString fileName = aFullFileName;
58 
59  NETLIST_EXPORTER *helper;
60 
61  switch( aFormat )
62  {
63  case NET_TYPE_PCBNEW:
64  helper = new NETLIST_EXPORTER_KICAD( this, aConnectedItemsList );
65  break;
66 
67  case NET_TYPE_ORCADPCB2:
68  helper = new NETLIST_EXPORTER_ORCADPCB2( aConnectedItemsList );
69  break;
70 
71  case NET_TYPE_CADSTAR:
72  helper = new NETLIST_EXPORTER_CADSTAR( aConnectedItemsList );
73  break;
74 
75  case NET_TYPE_SPICE:
76  helper = new NETLIST_EXPORTER_PSPICE( aConnectedItemsList );
77  break;
78 
79  default:
80  {
81  wxFileName tmpFile = fileName;
82  tmpFile.SetExt( GENERIC_INTERMEDIATE_NETLIST_EXT );
83  fileName = tmpFile.GetFullPath();
84 
85  helper = new NETLIST_EXPORTER_GENERIC( this, aConnectedItemsList );
86  executeCommandLine = true;
87  }
88  break;
89  }
90 
91  res = helper->WriteNetlist( fileName, aNetlistOptions );
92  delete helper;
93 
94  // If user provided a plugin command line, execute it.
95  if( executeCommandLine && res && !m_netListerCommand.IsEmpty() )
96  {
97  wxString prj_dir = Prj().GetProjectPath();
98 
99  // build full command line from user's format string, e.g.:
100  // "xsltproc -o %O /usr/local/lib/kicad/plugins/netlist_form_pads-pcb.xsl %I"
101  // becomes, after the user selects /tmp/s1.net as the output file from the file dialog:
102  // "xsltproc -o /tmp/s1.net /usr/local/lib/kicad/plugins/netlist_form_pads-pcb.xsl /tmp/s1.xml"
104  fileName, aFullFileName,
105  prj_dir.SubString( 0, prj_dir.Len() - 2 ) // strip trailing '/'
106  );
107 
108  if( aReporter )
109  {
110  wxArrayString output, errors;
111  int diag = wxExecute( commandLine, output, errors, m_exec_flags );
112 
113  wxString msg;
114 
115  msg << _( "Run command:" ) << wxT( "\n" ) << commandLine << wxT( "\n\n" );
116 
117  aReporter->ReportHead( msg, REPORTER::RPT_ACTION );
118 
119  if( diag != 0 )
120  aReporter->ReportTail( wxString::Format(
121  _("Command error. Return code %d" ), diag ),
123  else
124  aReporter->ReportTail( _( "Success" ), REPORTER::RPT_INFO );
125 
126  if( output.GetCount() )
127  {
128  msg.Empty();
129  msg << wxT( "\n" ) << _( "Info messages:" ) << wxT( "\n" );
130  aReporter->Report( msg, REPORTER::RPT_INFO );
131 
132  for( unsigned ii = 0; ii < output.GetCount(); ii++ )
133  aReporter->Report( output[ii] + wxT( "\n" ), REPORTER::RPT_INFO );
134  }
135 
136  if( errors.GetCount() )
137  {
138  msg.Empty();
139  msg << wxT("\n") << _( "Error messages:" ) << wxT( "\n" );
140  aReporter->Report( msg, REPORTER::RPT_INFO );
141 
142  for( unsigned ii = 0; ii < errors.GetCount(); ii++ )
143  aReporter->Report( errors[ii] + wxT( "\n" ), REPORTER::RPT_ERROR );
144  }
145  }
146  else
147  ProcessExecute( commandLine, m_exec_flags );
148 
149  DefaultExecFlags(); // Reset flags to default after executing
150  }
151 
152  return res;
153 }
154 
155 
156 //Imported function:
157 int TestDuplicateSheetNames( bool aCreateMarker );
158 
159 
161 {
162  SCH_SCREENS schematic;
163 
164  // Ensure all symbol library links for all sheets valid:
165  schematic.UpdateSymbolLinks();
166 
167  // Ensure all power symbols have a valid reference
168  SCH_SHEET_LIST sheets( g_RootSheet );
169  sheets.AnnotatePowerSymbols();
170 
171  // Performs some controls:
173  {
174  // Schematic must be annotated: call Annotate dialog and tell the user why.
175  ModalAnnotate( _( "Exporting the netlist requires a completely annotated schematic." ) );
176 
178  return false;
179  }
180 
181  // Test duplicate sheet names:
182  if( TestDuplicateSheetNames( false ) > 0 )
183  {
184  if( !IsOK( this, _( "Error: duplicate sheet names. Continue?" ) ) )
185  return false;
186  }
187 
188  return true;
189 }
190 
191 
193 {
194  NETLIST_OBJECT_LIST* net_atoms = BuildNetListBase();
195 
196  NETLIST_EXPORTER_KICAD exporter( this, net_atoms );
197 
198  STRING_FORMATTER formatter;
199 
200  // @todo : trim GNL_ALL down to minimum for CVPCB
201  exporter.Format( &formatter, GNL_ALL );
202 
205  formatter.GetString(), // an abbreviated "kicad" (s-expr) netlist
206  this
207  );
208 }
209 
210 
211 bool SCH_EDIT_FRAME::CreateNetlist( int aFormat, const wxString& aFullFileName,
212  unsigned aNetlistOptions, REPORTER* aReporter, bool aSilent )
213 {
214  if( !aSilent ) // checks for errors and invokes annotation dialog as neccessary
215  {
216  if( !prepareForNetlist() )
217  return false;
218  }
219  else // performs similar function as prepareForNetlist but without a dialog.
220  {
221  SCH_SCREENS schematic;
222  schematic.UpdateSymbolLinks();
223  SCH_SHEET_LIST sheets( g_RootSheet );
224  sheets.AnnotatePowerSymbols();
225  }
226 
227  std::unique_ptr<NETLIST_OBJECT_LIST> connectedItemsList( BuildNetListBase() );
228 
229  bool success = WriteNetListFile( connectedItemsList.release(), aFormat,
230  aFullFileName, aNetlistOptions, aReporter );
231 
232  return success;
233 }
234 
235 
237 {
238  // I own this list until I return it to the new owner.
239  std::unique_ptr<NETLIST_OBJECT_LIST> ret( new NETLIST_OBJECT_LIST() );
240 
241  // Creates the flattened sheet list:
242  SCH_SHEET_LIST aSheets( g_RootSheet );
243 
244  // Build netlist info
245  bool success = ret->BuildNetListInfo( aSheets );
246 
247  if( !success )
248  {
249  if( updateStatusText )
250  SetStatusText( _( "No Objects" ) );
251  return ret.release();
252  }
253 
254  wxString msg = wxString::Format( _( "Net count = %d" ), int( ret->size() ) );
255 
256  if( updateStatusText )
257  SetStatusText( msg );
258 
259  return ret.release();
260 }
261 
int TestDuplicateSheetNames(bool aCreateMarker)
Function TestDuplicateSheetNames( ) inside a given sheet, one cannot have sheets with duplicate names...
Definition: erc.cpp:177
Class SCH_SHEET_LIST.
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:206
void sendNetlist()
Send the kicad netlist over to CVPCB.
NETLIST_OBJECT_LIST * BuildNetListBase(bool updateStatusText=true)
Create a flat list which stores all connected objects.
KIWAY & Kiway() const
Function Kiway returns a reference to the KIWAY that this object has an opportunity to participate in...
Definition: kiway_player.h:60
This file is part of the common library TODO brief description.
This file is part of the common library.
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...
Class REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:61
void Format(OUTPUTFORMATTER *aOutputFormatter, int aCtl)
Function Format outputs this s-expression netlist into aOutputFormatter.
Class NETLIST_OBJECT_LIST is a container holding and owning NETLIST_OBJECTs, which are connected item...
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:386
virtual REPORTER & ReportHead(const wxString &aText, SEVERITY aSeverity=RPT_UNDEFINED)
Function ReportHead Places the report at the beginning of the list for objects that support ordering...
Definition: reporter.h:118
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
Class NETLIST_EXPORTER is a abstract class used for the netlist exporters that eeschema supports...
SCH_SHEET * g_RootSheet
Definition: eeschema.cpp:56
Class NETLIST_EXPORTER_GENERIC generates a generic XML based netlist file.
bool prepareForNetlist()
Verify that annotation is complete so that a proper netlist is even possible.
virtual REPORTER & ReportTail(const wxString &aText, SEVERITY aSeverity=RPT_UNDEFINED)
Function ReportTail Places the report at the end of the list, for objects that support report orderin...
Definition: reporter.h:109
Class NETLIST_EXPORTER_KICAD generates the kicad netlist format supported by pcbnew.
const std::string & GetString()
Definition: richio.h:475
int CheckAnnotate(REPORTER &aReporter, bool aOneSheetOnly)
Check for annotation errors.
Definition: annotate.cpp:245
Class NETLIST_EXPORTER_CADSTAR generates a netlist compatible with CADSTAR.
#define GENERIC_INTERMEDIATE_NETLIST_EXT
Class 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
void UpdateSymbolLinks(bool aForce=false)
Initialize or reinitialize the weak reference to the LIB_PART for each SCH_COMPONENT found in the ful...
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
bool CreateNetlist(int aFormat, const wxString &aFullFileName, unsigned aNetlistOptions, REPORTER *aReporter=NULL, bool silent=false)
Create a netlist for the current schematic.
#define GNL_ALL
Class NETLIST_EXPORTER_PSPICE generates a PSPICE compatible netlist.
VTBL_ENTRY const wxString GetProjectPath() const
Function GetProjectPath returns the full path of the project.
Definition: project.cpp:102
virtual bool WriteNetlist(const wxString &aOutFileName, unsigned aNetlistOptions)
Function WriteNetlist writes to specified output file.
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_UNDEFINED)=0
Function Report is a pure virtual function to override in the derived object.
void AnnotatePowerSymbols()
Function AnnotatePowerSymbols Silently annotates the not yet annotated power symbols of the entire hi...
static REPORTER & GetInstance()
Definition: reporter.cpp:104
int m_exec_flags
Flags of the wxExecute() function to call a custom net list generator.
bool WriteNetListFile(NETLIST_OBJECT_LIST *aConnectedItemsList, int aFormat, const wxString &aFullFileName, unsigned aNetlistOptions, REPORTER *aReporter=NULL)
Create a netlist file.
Class STRING_FORMATTER implements OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:445
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Function IsOK displays a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:295
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:523
SCH->CVPCB netlist immediately after launching CVPCB.
Definition: mail_type.h:42