KiCad PCB EDA Suite
netform.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-2015 jp.charras at wanadoo.fr
5  * Copyright (C) 2013 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
6  * Copyright (C) 1992-2015 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 <schframe.h>
36 #include <reporter.h>
37 
38 #include <netlist.h>
39 #include <netlist_exporter.h>
43 #include <netlist_exporter_kicad.h>
45 
47  int aFormat, const wxString& aFullFileName,
48  unsigned aNetlistOptions, REPORTER* aReporter )
49 {
50  bool res = true;
51  bool executeCommandLine = false;
52 
53  wxString fileName = aFullFileName;
54 
55  NETLIST_EXPORTER *helper;
56 
57  switch( aFormat )
58  {
59  case NET_TYPE_PCBNEW:
60  helper = new NETLIST_EXPORTER_KICAD( aConnectedItemsList, Prj().SchLibs() );
61  break;
62 
63  case NET_TYPE_ORCADPCB2:
64  helper = new NETLIST_EXPORTER_ORCADPCB2( aConnectedItemsList, Prj().SchLibs() );
65  break;
66 
67  case NET_TYPE_CADSTAR:
68  helper = new NETLIST_EXPORTER_CADSTAR( aConnectedItemsList, Prj().SchLibs() );
69  break;
70 
71  case NET_TYPE_SPICE:
72  helper = new NETLIST_EXPORTER_PSPICE( aConnectedItemsList, Prj().SchLibs() );
73  break;
74 
75  default:
76  {
77  wxFileName tmpFile = fileName;
78  tmpFile.SetExt( GENERIC_INTERMEDIATE_NETLIST_EXT );
79  fileName = tmpFile.GetFullPath();
80 
81  helper = new NETLIST_EXPORTER_GENERIC( aConnectedItemsList, Prj().SchLibs() );
82  executeCommandLine = true;
83  }
84  break;
85  }
86 
87  res = helper->WriteNetlist( fileName, aNetlistOptions );
88  delete helper;
89 
90  // If user provided a plugin command line, execute it.
91  if( executeCommandLine && res && !m_netListerCommand.IsEmpty() )
92  {
93  wxString prj_dir = Prj().GetProjectPath();
94 
95  // build full command line from user's format string, e.g.:
96  // "xsltproc -o %O /usr/local/lib/kicad/plugins/netlist_form_pads-pcb.xsl %I"
97  // becomes, after the user selects /tmp/s1.net as the output file from the file dialog:
98  // "xsltproc -o /tmp/s1.net /usr/local/lib/kicad/plugins/netlist_form_pads-pcb.xsl /tmp/s1.xml"
100  fileName, aFullFileName,
101  prj_dir.SubString( 0, prj_dir.Len() - 2 ) // strip trailing '/'
102  );
103 
104  if( aReporter )
105  {
106  wxArrayString output, errors;
107  int diag = wxExecute( commandLine, output, errors, m_exec_flags );
108 
109  wxString msg;
110 
111  msg << _( "Run command:" ) << wxT( "\n" ) << commandLine << wxT( "\n\n" );
112 
113  aReporter->Report( msg, REPORTER::RPT_ACTION );
114 
115  if( diag != 0 )
116  aReporter->Report( wxString::Format(
117  _("Command error. Return code %d" ), diag ),
119  else
120  aReporter->Report( _( "Success" ), REPORTER::RPT_INFO );
121 
122  *aReporter << wxT("\n");
123 
124  if( output.GetCount() )
125  {
126  msg.Empty();
127  msg << wxT( "\n" ) << _( "Info messages:" ) << wxT( "\n" );
128  aReporter->Report( msg, REPORTER::RPT_INFO );
129 
130  for( unsigned ii = 0; ii < output.GetCount(); ii++ )
131  aReporter->Report( output[ii] + wxT( "\n" ), REPORTER::RPT_INFO );
132  }
133 
134  if( errors.GetCount() )
135  {
136  msg.Empty();
137  msg << wxT("\n") << _( "Error messages:" ) << wxT( "\n" );
138  aReporter->Report( msg, REPORTER::RPT_INFO );
139 
140  for( unsigned ii = 0; ii < errors.GetCount(); ii++ )
141  aReporter->Report( errors[ii] + wxT( "\n" ), REPORTER::RPT_ERROR );
142  }
143  }
144  else
145  ProcessExecute( commandLine, m_exec_flags );
146 
147  DefaultExecFlags(); // Reset flags to default after executing
148  }
149 
150  return res;
151 }
wxString m_netListerCommand
Command line to call a custom net list generator.
Definition: schframe.h:147
void DefaultExecFlags()
Function DefaultExecFlags resets the execution flags to defaults for external netlist and bom generat...
Definition: schframe.h:1387
int ProcessExecute(const wxString &aCommandLine, int aFlags, wxProcess *callback)
Function ProcessExecute runs a child process.
Definition: common.cpp:160
This file is part of the common library TODO brief description.
Class REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:61
static wxString MakeCommandLine(const wxString &aFormatString, const wxString &aTempfile, const wxString &aFinalFile, const wxString &aProjectDirectory)
Function MakeCommandLine builds up a string that describes a command line for executing a child proce...
Class NETLIST_OBJECT_LIST is a container holding and owning NETLIST_OBJECTs, which are connected item...
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...
Class NETLIST_EXPORTER_GENERIC generates a generic XML based netlist file.
Class NETLIST_EXPORTER_KICAD generates the kicad netlist format supported by pcbnew.
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.
see class PGM_BASE
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
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.
int m_exec_flags
Flags of the wxExecute() function to call a custom net list generator.
Definition: schframe.h:149
bool WriteNetListFile(NETLIST_OBJECT_LIST *aConnectedItemsList, int aFormat, const wxString &aFullFileName, unsigned aNetlistOptions, REPORTER *aReporter=NULL)
Function WriteNetListFile Create the netlist file.
Definition: netform.cpp:46