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  if( aConnectedItemsList == nullptr ) // Schematic netlist not available.
55  return false;
56 
57  bool res = true;
58  bool executeCommandLine = false;
59 
60  wxString fileName = aFullFileName;
61 
62  NETLIST_EXPORTER *helper;
63 
64  switch( aFormat )
65  {
66  case NET_TYPE_PCBNEW:
67  helper = new NETLIST_EXPORTER_KICAD( this, aConnectedItemsList,
69  break;
70 
71  case NET_TYPE_ORCADPCB2:
72  helper = new NETLIST_EXPORTER_ORCADPCB2( aConnectedItemsList );
73  break;
74 
75  case NET_TYPE_CADSTAR:
76  helper = new NETLIST_EXPORTER_CADSTAR( aConnectedItemsList );
77  break;
78 
79  case NET_TYPE_SPICE:
80  helper = new NETLIST_EXPORTER_PSPICE( aConnectedItemsList );
81  break;
82 
83  default:
84  {
85  wxFileName tmpFile = fileName;
86  tmpFile.SetExt( GENERIC_INTERMEDIATE_NETLIST_EXT );
87  fileName = tmpFile.GetFullPath();
88 
89  helper = new NETLIST_EXPORTER_GENERIC( this, aConnectedItemsList,
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  // build full command line from user's format string, e.g.:
105  // "xsltproc -o %O /usr/local/lib/kicad/plugins/netlist_form_pads-pcb.xsl %I"
106  // becomes, after the user selects /tmp/s1.net as the output file from the file dialog:
107  // "xsltproc -o /tmp/s1.net /usr/local/lib/kicad/plugins/netlist_form_pads-pcb.xsl /tmp/s1.xml"
109  fileName, aFullFileName,
110  prj_dir.SubString( 0, prj_dir.Len() - 2 ) // strip trailing '/'
111  );
112 
113  if( aReporter )
114  {
115  wxArrayString output, errors;
116  int diag = wxExecute( commandLine, output, errors, m_exec_flags );
117 
118  wxString msg;
119 
120  msg << _( "Run command:" ) << wxT( "\n" ) << commandLine << wxT( "\n\n" );
121 
122  aReporter->ReportHead( msg, REPORTER::RPT_ACTION );
123 
124  if( diag != 0 )
125  aReporter->ReportTail( wxString::Format(
126  _("Command error. Return code %d" ), diag ),
128  else
129  aReporter->ReportTail( _( "Success" ), REPORTER::RPT_INFO );
130 
131  if( output.GetCount() )
132  {
133  msg.Empty();
134  msg << wxT( "\n" ) << _( "Info messages:" ) << wxT( "\n" );
135  aReporter->Report( msg, REPORTER::RPT_INFO );
136 
137  for( unsigned ii = 0; ii < output.GetCount(); ii++ )
138  aReporter->Report( output[ii] + wxT( "\n" ), REPORTER::RPT_INFO );
139  }
140 
141  if( errors.GetCount() )
142  {
143  msg.Empty();
144  msg << wxT("\n") << _( "Error messages:" ) << wxT( "\n" );
145  aReporter->Report( msg, REPORTER::RPT_INFO );
146 
147  for( unsigned ii = 0; ii < errors.GetCount(); ii++ )
148  aReporter->Report( errors[ii] + wxT( "\n" ), REPORTER::RPT_ERROR );
149  }
150  }
151  else
152  ProcessExecute( commandLine, m_exec_flags );
153 
154  DefaultExecFlags(); // Reset flags to default after executing
155  }
156 
157  return res;
158 }
159 
160 
161 //Imported function:
162 int TestDuplicateSheetNames( bool aCreateMarker );
163 
164 
166 {
167  SCH_SCREENS schematic;
168 
169  // Ensure all symbol library links for all sheets valid:
170  schematic.UpdateSymbolLinks();
171 
172  // Ensure all power symbols have a valid reference
173  SCH_SHEET_LIST sheets( g_RootSheet );
174  sheets.AnnotatePowerSymbols();
175 
176  // Performs some controls:
178  {
179  // Schematic must be annotated: call Annotate dialog and tell the user why.
180  ModalAnnotate( _( "Exporting the netlist requires a completely annotated schematic." ) );
181 
183  return false;
184  }
185 
186  // Test duplicate sheet names:
187  if( TestDuplicateSheetNames( false ) > 0 )
188  {
189  if( !IsOK( this, _( "Error: duplicate sheet names. Continue?" ) ) )
190  return false;
191  }
192 
193  return true;
194 }
195 
196 
198 {
199  NETLIST_OBJECT_LIST* net_atoms = BuildNetListBase();
200  NETLIST_EXPORTER_KICAD exporter( this, net_atoms, g_ConnectionGraph );
201  STRING_FORMATTER formatter;
202 
203  // @todo : trim GNL_ALL down to minimum for CVPCB
204  exporter.Format( &formatter, GNL_ALL );
205 
206  std::string packet = formatter.GetString(); // an abbreviated "kicad" (s-expr) netlist
208 }
209 
210 
212  bool aSilentAnnotate )
213 {
214  if( !aSilent ) // checks for errors and invokes annotation dialog as neccessary
215  {
216  if( !prepareForNetlist() )
217  return nullptr;
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  if( aSilentAnnotate )
227  AnnotateComponents( true, UNSORTED, INCREMENTAL_BY_REF, 0, false, false, true,
229  }
230 
231  // TODO(JE) This is really going to turn into "PrepareForNetlist"
232  // when the old netlister (BuildNetListBase) is removed
233 
234  return BuildNetListBase();
235 }
236 
237 
239 {
240  // Ensure netlist is up to date
242 
243  // I own this list until I return it to the new owner.
244  std::unique_ptr<NETLIST_OBJECT_LIST> ret( new NETLIST_OBJECT_LIST() );
245 
246  // Creates the flattened sheet list:
247  SCH_SHEET_LIST aSheets( g_RootSheet );
248 
249  // Build netlist info
250  bool success = ret->BuildNetListInfo( aSheets );
251 
252  if( !success )
253  {
254  if( updateStatusText )
255  SetStatusText( _( "No Objects" ) );
256  return ret.release();
257  }
258 
259  wxString msg = wxString::Format( _( "Net count = %d" ), int( ret->size() ) );
260 
261  if( updateStatusText )
262  SetStatusText( msg );
263 
264  return ret.release();
265 }
266 
int TestDuplicateSheetNames(bool aCreateMarker)
Function TestDuplicateSheetNames( ) inside a given sheet, one cannot have sheets with duplicate names...
Definition: erc.cpp:174
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:211
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
NETLIST_OBJECT_LIST * BuildNetListBase(bool updateStatusText=true)
Create a flat list which stores all connected objects.
This file is part of the common library TODO brief description.
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.
Definition: annotate.cpp:82
Annotate incrementally using the first free reference number.
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.
CONNECTION_GRAPH * g_ConnectionGraph
This also wants to live in the eventual SCHEMATIC object.
VTBL_ENTRY const wxString GetProjectPath() const
Function GetProjectPath returns the full path of the project.
Definition: project.cpp:102
Class NETLIST_OBJECT_LIST is a container holding and owning NETLIST_OBJECTs, which are connected item...
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:120
Class NETLIST_EXPORTER is a abstract class used for the netlist exporters that eeschema supports.
SCH_SHEET * g_RootSheet
Definition: eeschema.cpp:47
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:111
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.
Class NETLIST_EXPORTER_KICAD generates the kicad netlist format supported by pcbnew.
const std::string & GetString()
Definition: richio.h:475
#define _(s)
int CheckAnnotate(REPORTER &aReporter, bool aOneSheetOnly)
Check for annotation errors.
Definition: annotate.cpp:245
Class 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:407
#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
NETLIST_OBJECT_LIST * CreateNetlist(bool aSilent=false, bool aSilentAnnotate=false)
Create a netlist for the current schematic.
#define GNL_ALL
Class NETLIST_EXPORTER_PSPICE generates a PSPICE compatible netlist.
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)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:294
void RecalculateConnections(bool aDoCleanup=true)
Generates the connection data for the entire schematic hierarchy.
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:486
void sendNetlistToCvpcb()
Send the kicad netlist over to CVPCB.
SCH->CVPCB netlist immediately after launching CVPCB.
Definition: mail_type.h:42