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 <fctsys.h>
27 #include <kicad_string.h>
28 #include <gestfich.h>
29 #include <pgm_base.h>
30 #include <sch_edit_frame.h>
31 #include <schematic.h>
32 #include <reporter.h>
33 #include <confirm.h>
34 #include <kiway.h>
35 #include <erc.h>
36 
37 #include <netlist.h>
38 #include <netlist_exporter.h>
42 #include <netlist_exporter_kicad.h>
44 
45 
46 bool SCH_EDIT_FRAME::WriteNetListFile( int aFormat, const wxString& aFullFileName,
47  unsigned aNetlistOptions, REPORTER* aReporter )
48 {
49  // Ensure all power symbols have a valid reference
51 
52  // Ensure the netlist data is up to date:
54 
55  if( !ReadyToNetlist( false ) )
56  return false;
57 
58  bool res = true;
59  bool executeCommandLine = false;
60 
61  wxString fileName = aFullFileName;
62 
63  NETLIST_EXPORTER *helper;
64 
65  SCHEMATIC* sch = &Schematic();
66 
67  switch( aFormat )
68  {
69  case NET_TYPE_PCBNEW:
70  helper = new NETLIST_EXPORTER_KICAD( sch );
71  break;
72 
73  case NET_TYPE_ORCADPCB2:
74  helper = new NETLIST_EXPORTER_ORCADPCB2( sch );
75  break;
76 
77  case NET_TYPE_CADSTAR:
78  helper = new NETLIST_EXPORTER_CADSTAR( sch );
79  break;
80 
81  case NET_TYPE_SPICE:
82  helper = new NETLIST_EXPORTER_PSPICE( sch );
83  break;
84 
85  default:
86  {
87  wxFileName tmpFile = fileName;
88  tmpFile.SetExt( GENERIC_INTERMEDIATE_NETLIST_EXT );
89  fileName = tmpFile.GetFullPath();
90 
91  helper = new NETLIST_EXPORTER_GENERIC( sch );
92  executeCommandLine = true;
93  }
94  break;
95  }
96 
97  res = helper->WriteNetlist( fileName, aNetlistOptions );
98  delete helper;
99 
100  // If user provided a plugin command line, execute it.
101  if( executeCommandLine && res && !m_netListerCommand.IsEmpty() )
102  {
103  wxString prj_dir = Prj().GetProjectPath();
104 
105  // build full command line from user's format string, e.g.:
106  // "xsltproc -o %O /usr/local/lib/kicad/plugins/netlist_form_pads-pcb.xsl %I"
107  // becomes, after the user selects /tmp/s1.net as the output file from the file dialog:
108  // "xsltproc -o /tmp/s1.net /usr/local/lib/kicad/plugins/netlist_form_pads-pcb.xsl /tmp/s1.xml"
110  fileName, aFullFileName,
111  prj_dir.SubString( 0, prj_dir.Len() - 2 ) // strip trailing '/'
112  );
113 
114  if( aReporter )
115  {
116  wxArrayString output, errors;
117  int diag = wxExecute( commandLine, output, errors, m_exec_flags );
118 
119  wxString msg;
120 
121  msg << _( "Run command:" ) << wxT( "\n" ) << commandLine << wxT( "\n\n" );
122 
123  aReporter->ReportHead( msg, RPT_SEVERITY_ACTION );
124 
125  if( diag != 0 )
126  aReporter->ReportTail( wxString::Format(
127  _("Command error. Return code %d" ), diag ),
129  else
130  aReporter->ReportTail( _( "Success" ), RPT_SEVERITY_INFO );
131 
132  if( output.GetCount() )
133  {
134  msg.Empty();
135  msg << wxT( "\n" ) << _( "Info messages:" ) << wxT( "\n" );
136  aReporter->Report( msg, RPT_SEVERITY_INFO );
137 
138  for( unsigned ii = 0; ii < output.GetCount(); ii++ )
139  aReporter->Report( output[ii] + wxT( "\n" ), RPT_SEVERITY_INFO );
140  }
141 
142  if( errors.GetCount() )
143  {
144  msg.Empty();
145  msg << wxT("\n") << _( "Error messages:" ) << wxT( "\n" );
146  aReporter->Report( msg, RPT_SEVERITY_INFO );
147 
148  for( unsigned ii = 0; ii < errors.GetCount(); ii++ )
149  aReporter->Report( errors[ii] + wxT( "\n" ), RPT_SEVERITY_ERROR );
150  }
151  }
152  else
153  ProcessExecute( commandLine, m_exec_flags );
154 
155  DefaultExecFlags(); // Reset flags to default after executing
156  }
157 
158  return res;
159 }
160 
161 
162 bool SCH_EDIT_FRAME::ReadyToNetlist( bool aSilent, bool aSilentAnnotate )
163 {
164  // Ensure all power symbols have a valid reference
166 
167  // Components must be annotated
168  if( CheckAnnotate( NULL_REPORTER::GetInstance(), false ) )
169  {
170  if( aSilentAnnotate )
171  {
172  AnnotateComponents( true, UNSORTED, INCREMENTAL_BY_REF, 0, false, false, true,
174  }
175  else
176  {
177  if( aSilent )
178  return false;
179 
180  // Schematic must be annotated: call Annotate dialog and tell the user why.
182  _( "Exporting the netlist requires a completely annotated schematic." ) );
183 
184  if( CheckAnnotate( NULL_REPORTER::GetInstance(), false ) )
185  return false;
186  }
187  }
188 
189  // Test duplicate sheet names:
190  ERC_TESTER erc( &Schematic() );
191 
192  if( erc.TestDuplicateSheetNames( false ) > 0 )
193  {
194  if( aSilent || !IsOK( this, _( "Error: duplicate sheet names. Continue?" ) ) )
195  return false;
196  }
197 
198  return true;
199 }
200 
201 
203 {
204  NETLIST_EXPORTER_KICAD exporter( &Schematic() );
205  STRING_FORMATTER formatter;
206 
207  // @todo : trim GNL_ALL down to minimum for CVPCB
208  exporter.Format( &formatter, GNL_ALL );
209 
210  std::string packet = formatter.GetString(); // an abbreviated "kicad" (s-expr) netlist
212 }
213 
214 
216 {
217  // Ensure netlist is up to date
219 
220  // I own this list until I return it to the new owner.
221  std::unique_ptr<NETLIST_OBJECT_LIST> ret( new NETLIST_OBJECT_LIST() );
222 
223  // Creates the flattened sheet list:
224  SCH_SHEET_LIST aSheets = Schematic().GetSheets();
225 
226  // Build netlist info
227  bool success = ret->BuildNetListInfo( aSheets );
228 
229  if( !success )
230  {
231  if( updateStatusText )
232  SetStatusText( _( "No Objects" ) );
233  return ret.release();
234  }
235 
236  wxString msg = wxString::Format( _( "Net count = %d" ), int( ret->size() ) );
237 
238  if( updateStatusText )
239  SetStatusText( msg );
240 
241  return ret.release();
242 }
243 
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:364
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.
SCH_SHEET_LIST GetSheets() const
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:92
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:42
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.
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...
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
NETLIST_OBJECT_LIST is a container holding and owning NETLIST_OBJECTs, which are connected items in a...
NETLIST_EXPORTER is a abstract class used for the netlist exporters that eeschema supports.
Generate a generic XML based netlist file.
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:475
int CheckAnnotate(REPORTER &aReporter, bool aOneSheetOnly)
Check for annotation errors.
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:428
#define GENERIC_INTERMEDIATE_NETLIST_EXT
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 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
#define _(s)
Definition: 3d_actions.cpp:33
#define GNL_ALL
NETLIST_EXPORTER_PSPICE generates a PSPICE compatible netlist.
virtual bool WriteNetlist(const wxString &aOutFileName, unsigned aNetlistOptions)
Function WriteNetlist writes to specified output file.
void AnnotatePowerSymbols()
Function AnnotatePowerSymbols Silently annotates the not yet annotated power symbols of the entire hi...
static REPORTER & GetInstance()
Definition: reporter.cpp:105
int m_exec_flags
Flags of the wxExecute() function to call a custom net list generator.
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:283
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).
SCH->CVPCB netlist immediately after launching CVPCB.
Definition: mail_type.h:42