KiCad PCB EDA Suite
Loading...
Searching...
No Matches
netlist_exporter_spice.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-2013 jp.charras at wanadoo.fr
5 * Copyright (C) 2013 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
6 * Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.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 <sim/kibis/kibis.h>
31#include <sim/sim_model_ideal.h>
32#include <common.h>
33#include <confirm.h>
34#include <pgm_base.h>
35#include <env_paths.h>
36#include <sim/sim_library.h>
38#include <sim/sim_model_kibis.h>
39#include <sch_screen.h>
40#include <sch_text.h>
41#include <sch_textbox.h>
42#include <string_utils.h>
43
45#include <fmt/core.h>
46#include <paths.h>
47#include <wx/dir.h>
48#include <wx/log.h>
49#include <locale_io.h>
50#include "markup_parser.h"
51
52#if 0
53
54#include <pegtl.hpp>
55#include <pegtl/contrib/parse_tree.hpp>
56
57namespace NETLIST_EXPORTER_SPICE_PARSER
58{
59 using namespace SPICE_GRAMMAR;
60
61 struct textGrammar : must<spiceSource> {};
62
63 template <typename Rule> struct textSelector : std::false_type {};
64 template <> struct textSelector<modelUnit> : std::true_type {};
65
66 template <> struct textSelector<dotControl> : std::true_type {};
67
68 template <> struct textSelector<dotTitle> : std::true_type {};
69 template <> struct textSelector<dotTitleTitle> : std::true_type {};
70
71 template <> struct textSelector<dotInclude> : std::true_type {};
72 template <> struct textSelector<dotIncludePathWithoutQuotes> : std::true_type {};
73 template <> struct textSelector<dotIncludePathWithoutApostrophes> : std::true_type {};
74 template <> struct textSelector<dotIncludePath> : std::true_type {};
75
76 template <> struct textSelector<kLine> : std::true_type {};
77
78 template <> struct textSelector<dotLine> : std::true_type {};
79}
80#endif
81
82
83std::string NAME_GENERATOR::Generate( const std::string& aProposedName )
84{
85 std::string name = aProposedName;
86 int ii = 1;
87
88 while( m_names.count( name ) )
89 name = fmt::format( "{}#{}", aProposedName, ii++ );
90
91 return name;
92}
93
94
96 wxWindow* aDialogParent ) :
97 NETLIST_EXPORTER_BASE( aSchematic ),
98 m_libMgr( &aSchematic->Prj() ),
99 m_dialogParent( aDialogParent )
100{
101}
102
103
104bool NETLIST_EXPORTER_SPICE::WriteNetlist( const wxString& aOutFileName, unsigned aNetlistOptions,
105 REPORTER& aReporter )
106{
107 FILE_OUTPUTFORMATTER formatter( aOutFileName, wxT( "wt" ), '\'' );
108 return DoWriteNetlist( wxEmptyString, aNetlistOptions, formatter, aReporter );
109}
110
111
112bool NETLIST_EXPORTER_SPICE::DoWriteNetlist( const wxString& aSimCommand, unsigned aSimOptions,
113 OUTPUTFORMATTER& aFormatter, REPORTER& aReporter )
114{
116
117 // Cleanup list to avoid duplicate if the netlist exporter is run more than once.
118 m_rawIncludes.clear();
119
120 bool result = ReadSchematicAndLibraries( aSimOptions, aReporter );
121
122 WriteHead( aFormatter, aSimOptions );
123
124 writeIncludes( aFormatter, aSimOptions );
125 writeModels( aFormatter );
126
127 // Skip this if there is no netlist to avoid an ngspice segfault
128 if( !m_items.empty() )
129 WriteDirectives( aSimCommand, aSimOptions, aFormatter );
130
131 writeItems( aFormatter );
132
133 WriteTail( aFormatter, aSimOptions );
134
135 return result;
136}
137
138
139void NETLIST_EXPORTER_SPICE::WriteHead( OUTPUTFORMATTER& aFormatter, unsigned aNetlistOptions )
140{
141 aFormatter.Print( 0, ".title KiCad schematic\n" );
142}
143
144
145void NETLIST_EXPORTER_SPICE::WriteTail( OUTPUTFORMATTER& aFormatter, unsigned aNetlistOptions )
146{
147 aFormatter.Print( 0, ".end\n" );
148}
149
150
152 REPORTER& aReporter )
153{
154 wxString msg;
155 std::set<std::string> refNames; // Set of reference names to check for duplication.
156 int ncCounter = 1;
157
158 ReadDirectives( aNetlistOptions );
159
160 m_nets.clear();
161 m_items.clear();
163 m_libParts.clear();
164
165 wxFileName cacheDir;
166 cacheDir.AssignDir( PATHS::GetUserCachePath() );
167 cacheDir.AppendDir( wxT( "ibis" ) );
168
169 if( !cacheDir.DirExists() )
170 {
171 cacheDir.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
172
173 if( !cacheDir.DirExists() )
174 {
175 wxLogTrace( wxT( "IBIS_CACHE:" ),
176 wxT( "%s:%s:%d\n * failed to create ibis cache directory '%s'" ),
177 __FILE__, __FUNCTION__, __LINE__, cacheDir.GetPath() );
178
179 return false;
180 }
181 }
182
183 wxDir dir;
184 wxString dirName = cacheDir.GetFullPath();
185
186 if( !dir.Open( dirName ) )
187 return false;
188
189 wxFileName thisFile;
190 wxArrayString fileList;
191 wxString fileSpec = wxT( "*.cache" );
192
193 thisFile.SetPath( dirName ); // Set the base path to the cache folder
194
195 size_t numFilesFound = wxDir::GetAllFiles( dirName, &fileList, fileSpec );
196
197 for( size_t ii = 0; ii < numFilesFound; ii++ )
198 {
199 // Completes path to specific file so we can get its "last access" date
200 thisFile.SetFullName( fileList[ii] );
201 wxRemoveFile( thisFile.GetFullPath() );
202 }
203
204 for( SCH_SHEET_PATH& sheet : GetSheets( aNetlistOptions ) )
205 {
206 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
207 {
208 SCH_SYMBOL* symbol = findNextSymbol( item, &sheet );
209
210 if( !symbol || symbol->GetExcludedFromSim() )
211 continue;
212
213 SPICE_ITEM spiceItem;
214 std::vector<PIN_INFO> pins = CreatePinList( symbol, &sheet, true );
215
216 for( const SCH_FIELD& field : symbol->GetFields() )
217 {
218 spiceItem.fields.emplace_back( VECTOR2I(), -1, symbol, field.GetName() );
219
220 if( field.GetId() == REFERENCE_FIELD )
221 spiceItem.fields.back().SetText( symbol->GetRef( &sheet ) );
222 else
223 spiceItem.fields.back().SetText( field.GetShownText( &sheet, false ) );
224 }
225
226 readRefName( sheet, *symbol, spiceItem, refNames );
227 readModel( sheet, *symbol, spiceItem, aReporter );
228 readPinNumbers( *symbol, spiceItem, pins );
229 readPinNetNames( *symbol, spiceItem, pins, ncCounter );
230
231 // TODO: transmission line handling?
232
233 m_items.push_back( std::move( spiceItem ) );
234 }
235 }
236
237 return !aReporter.HasMessage();
238}
239
240
242{
243 MARKUP::MARKUP_PARSER markupParser( aNetName );
244 std::unique_ptr<MARKUP::NODE> root = markupParser.Parse();
245 std::string converted;
246
247 std::function<void( const std::unique_ptr<MARKUP::NODE>&)> convertMarkup =
248 [&]( const std::unique_ptr<MARKUP::NODE>& aNode )
249 {
250 if( aNode )
251 {
252 if( !aNode->is_root() )
253 {
254 if( aNode->isOverbar() )
255 {
256 // ~{CLK} is a different signal than CLK
257 converted += '~';
258 }
259 else if( aNode->isSubscript() || aNode->isSuperscript() )
260 {
261 // V_{OUT} is just a pretty-printed version of VOUT
262 }
263
264 if( aNode->has_content() )
265 converted += aNode->string();
266 }
267
268 for( const std::unique_ptr<MARKUP::NODE>& child : aNode->children )
269 convertMarkup( child );
270 }
271 };
272
273 convertMarkup( root );
274
275 // Replace all ngspice-disallowed chars in netnames by a '_'
276 std::replace( converted.begin(), converted.end(), '%', '_' );
277 std::replace( converted.begin(), converted.end(), '(', '_' );
278 std::replace( converted.begin(), converted.end(), ')', '_' );
279 std::replace( converted.begin(), converted.end(), ',', '_' );
280 std::replace( converted.begin(), converted.end(), '[', '_' );
281 std::replace( converted.begin(), converted.end(), ']', '_' );
282 std::replace( converted.begin(), converted.end(), '<', '_' );
283 std::replace( converted.begin(), converted.end(), '>', '_' );
284 std::replace( converted.begin(), converted.end(), '~', '_' );
285 std::replace( converted.begin(), converted.end(), ' ', '_' );
286
287 aNetName = converted;
288}
289
290
291std::string NETLIST_EXPORTER_SPICE::GetItemName( const std::string& aRefName ) const
292{
293 const SPICE_ITEM* item = FindItem( aRefName );
294
295 if( !item )
296 return "";
297
298 return item->model->SpiceGenerator().ItemName( *item );
299}
300
301
302const SPICE_ITEM* NETLIST_EXPORTER_SPICE::FindItem( const std::string& aRefName ) const
303{
304 const std::list<SPICE_ITEM>& spiceItems = GetItems();
305
306 auto it = std::find_if( spiceItems.begin(), spiceItems.end(),
307 [aRefName]( const SPICE_ITEM& item )
308 {
309 return item.refName == aRefName;
310 } );
311
312 if( it != spiceItems.end() )
313 return &*it;
314
315 return nullptr;
316}
317
318
319void NETLIST_EXPORTER_SPICE::ReadDirectives( unsigned aNetlistOptions )
320{
321 wxString msg;
322 wxString text;
323
324 m_directives.clear();
325
326 for( const SCH_SHEET_PATH& sheet : GetSheets( aNetlistOptions ) )
327 {
328 for( SCH_ITEM* item : sheet.LastScreen()->Items() )
329 {
330 if( item->GetExcludedFromSim() )
331 continue;
332
333 if( item->Type() == SCH_TEXT_T )
334 text = static_cast<SCH_TEXT*>( item )->GetShownText( &sheet, false );
335 else if( item->Type() == SCH_TEXTBOX_T )
336 text = static_cast<SCH_TEXTBOX*>( item )->GetShownText( &sheet, false );
337 else
338 continue;
339
340 // Send anything that contains directives to SPICE
341 wxStringTokenizer tokenizer( text, wxT( "\r\n" ), wxTOKEN_STRTOK );
342 bool foundDirective = false;
343
344 auto isDirective =
345 []( const wxString& line, const wxString& dir )
346 {
347 return line == dir || line.StartsWith( dir + wxS( " " ) );
348 };
349
350 while( tokenizer.HasMoreTokens() )
351 {
352 wxString line = tokenizer.GetNextToken().Upper();
353
354 if( line.StartsWith( wxT( "." ) ) )
355 {
356 if( isDirective( line, wxS( ".AC" ) )
357 || isDirective( line, wxS( ".CONTROL" ) )
358 || isDirective( line, wxS( ".CSPARAM" ) )
359 || isDirective( line, wxS( ".DISTO" ) )
360 || isDirective( line, wxS( ".DC" ) )
361 || isDirective( line, wxS( ".ELSE" ) )
362 || isDirective( line, wxS( ".ELSEIF" ) )
363 || isDirective( line, wxS( ".END" ) )
364 || isDirective( line, wxS( ".ENDC" ) )
365 || isDirective( line, wxS( ".ENDIF" ) )
366 || isDirective( line, wxS( ".ENDS" ) )
367 || isDirective( line, wxS( ".FOUR" ) )
368 || isDirective( line, wxS( ".FUNC" ) )
369 || isDirective( line, wxS( ".GLOBAL" ) )
370 || isDirective( line, wxS( ".IC" ) )
371 || isDirective( line, wxS( ".IF" ) )
372 || isDirective( line, wxS( ".INCLUDE" ) )
373 || isDirective( line, wxS( ".LIB" ) )
374 || isDirective( line, wxS( ".MEAS" ) )
375 || isDirective( line, wxS( ".MODEL" ) )
376 || isDirective( line, wxS( ".NODESET" ) )
377 || isDirective( line, wxS( ".NOISE" ) )
378 || isDirective( line, wxS( ".OP" ) )
379 || isDirective( line, wxS( ".OPTIONS" ) )
380 || isDirective( line, wxS( ".PARAM" ) )
381 || isDirective( line, wxS( ".PLOT" ) )
382 || isDirective( line, wxS( ".PRINT" ) )
383 || isDirective( line, wxS( ".PROBE" ) )
384 || isDirective( line, wxS( ".PZ" ) )
385 || isDirective( line, wxS( ".SAVE" ) )
386 || isDirective( line, wxS( ".SENS" ) )
387 || isDirective( line, wxS( ".SP" ) )
388 || isDirective( line, wxS( ".SUBCKT" ) )
389 || isDirective( line, wxS( ".TEMP" ) )
390 || isDirective( line, wxS( ".TF" ) )
391 || isDirective( line, wxS( ".TITLE" ) )
392 || isDirective( line, wxS( ".TRAN" ) )
393 || isDirective( line, wxS( ".WIDTH" ) ) )
394 {
395 foundDirective = true;
396 break;
397 }
398 }
399 else if( line.StartsWith( wxT( "K" ) ) )
400 {
401 // Check for mutual inductor declaration
402 wxStringTokenizer line_t( line, wxT( " \t" ), wxTOKEN_STRTOK );
403
404 // Coupling ID
405 if( !line_t.HasMoreTokens() || !line_t.GetNextToken().StartsWith( wxT( "K" ) ) )
406 continue;
407
408 // Inductor 1 ID
409 if( !line_t.HasMoreTokens() || !line_t.GetNextToken().StartsWith( wxT( "L" ) ) )
410 continue;
411
412 // Inductor 2 ID
413 if( !line_t.HasMoreTokens() || !line_t.GetNextToken().StartsWith( wxT( "L" ) ) )
414 continue;
415
416 // That's probably distinctive enough not to bother trying to parse the
417 // coupling value. If there's anything else, assume it's the value.
418 if( line_t.HasMoreTokens() )
419 {
420 foundDirective = true;
421 break;
422 }
423 }
424 }
425
426 if( foundDirective )
427 m_directives.emplace_back( text );
428 }
429 }
430}
431
432
434 SPICE_ITEM& aItem, std::set<std::string>& aRefNames )
435{
436 aItem.refName = aSymbol.GetRef( &aSheet );
437
438 if( !aRefNames.insert( aItem.refName ).second )
439 wxASSERT( wxT( "Duplicate refdes encountered; what happened to ReadyToNetlist()?" ) );
440}
441
442
444 SPICE_ITEM& aItem, REPORTER& aReporter )
445{
446 SIM_LIBRARY::MODEL libModel = m_libMgr.CreateModel( &aSheet, aSymbol, aReporter );
447
448 aItem.baseModelName = libModel.name;
449 aItem.model = &libModel.model;
450
451 std::string modelName = aItem.model->SpiceGenerator().ModelName( aItem );
452 // Resolve model name collisions.
454
455 // FIXME: Don't have special cases for raw Spice models and KIBIS.
456 if( auto rawSpiceModel = dynamic_cast<const SIM_MODEL_RAW_SPICE*>( aItem.model ) )
457 {
458 int libParamIndex = static_cast<int>( SIM_MODEL_RAW_SPICE::SPICE_PARAM::LIB );
459 wxString path = rawSpiceModel->GetParam( libParamIndex ).value;
460
461 if( !path.IsEmpty() )
462 m_rawIncludes.insert( path );
463 }
464 else if( auto kibisModel = dynamic_cast<const SIM_MODEL_KIBIS*>( aItem.model ) )
465 {
466 wxFileName cacheFn;
467 cacheFn.AssignDir( PATHS::GetUserCachePath() );
468 cacheFn.AppendDir( wxT( "ibis" ) );
469 cacheFn.SetFullName( aSymbol.GetRef( &aSheet ) + wxT( ".cache" ) );
470
471 wxFile cacheFile( cacheFn.GetFullPath(), wxFile::write );
472
473 if( !cacheFile.IsOpened() )
474 {
475 DisplayErrorMessage( m_dialogParent, wxString::Format( _( "Could not open file '%s' "
476 "to write IBIS model" ),
477 cacheFn.GetFullPath() ) );
478 }
479
480 auto spiceGenerator = static_cast<const SPICE_GENERATOR_KIBIS&>( kibisModel->SpiceGenerator() );
481 std::string modelData = spiceGenerator.IbisDevice(
482 aItem, m_schematic->Prj(), cacheFn.GetPath( wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR ) );
483
484 cacheFile.Write( wxString( modelData ) );
485 m_rawIncludes.insert( cacheFn.GetFullPath() );
486 }
487}
488
489
491 const std::vector<PIN_INFO>& aPins )
492{
493 for( const PIN_INFO& pin : aPins )
494 aItem.pinNumbers.emplace_back( pin.num.ToStdString() );
495}
496
497
499 const std::vector<PIN_INFO>& aPins, int& aNcCounter )
500{
501 for( const PIN_INFO& pinInfo : aPins )
502 {
503 std::string netName = GenerateItemPinNetName( pinInfo.netName.ToStdString(), aNcCounter );
504
505 aItem.pinNetNames.push_back( netName );
506 m_nets.insert( netName );
507 }
508}
509
510
511void NETLIST_EXPORTER_SPICE::writeInclude( OUTPUTFORMATTER& aFormatter, unsigned aNetlistOptions,
512 const wxString& aPath )
513{
514 // First, expand env vars, if any.
515 wxString expandedPath = ExpandEnvVarSubstitutions( aPath, &m_schematic->Prj() );
516
517 // Path may have been authored by someone on a Windows box; convert it to UNIX format
518 expandedPath.Replace( '\\', '/' );
519
520 wxString fullPath;
521
522 if( aNetlistOptions & OPTION_ADJUST_INCLUDE_PATHS )
523 {
524 // Look for the library in known search locations.
525 fullPath = ResolveFile( expandedPath, &Pgm().GetLocalEnvVariables(), &m_schematic->Prj() );
526
527 if( fullPath.IsEmpty() )
528 {
529 DisplayErrorMessage( m_dialogParent, wxString::Format( _( "Could not find library file "
530 "'%s'" ),
531 expandedPath ) );
532 fullPath = expandedPath;
533 }
534 else if( wxFileName::GetPathSeparator() == '\\' )
535 {
536 // Convert it to UNIX format (again) if ResolveFile() returned a Windows style path
537 fullPath.Replace( '\\', '/' );
538 }
539 }
540 else
541 {
542 fullPath = expandedPath;
543 }
544
545 aFormatter.Print( 0, ".include \"%s\"\n", TO_UTF8( fullPath ) );
546}
547
548
549void NETLIST_EXPORTER_SPICE::writeIncludes( OUTPUTFORMATTER& aFormatter, unsigned aNetlistOptions )
550{
551 for( const auto& [path, library] : m_libMgr.GetLibraries() )
552 {
553 if( dynamic_cast<const SIM_LIBRARY_SPICE*>( &library.get() ) )
554 writeInclude( aFormatter, aNetlistOptions, path );
555 }
556
557 for( const wxString& path : m_rawIncludes )
558 writeInclude( aFormatter, aNetlistOptions, path );
559}
560
561
563{
564 for( const SPICE_ITEM& item : m_items )
565 {
566 if( !item.model->IsEnabled() )
567 continue;
568
569 aFormatter.Print( 0, "%s", item.model->SpiceGenerator().ModelLine( item ).c_str() );
570 }
571}
572
573
575{
576 for( const SPICE_ITEM& item : m_items )
577 {
578 if( !item.model->IsEnabled() )
579 continue;
580
581 aFormatter.Print( 0, "%s", item.model->SpiceGenerator().ItemLine( item ).c_str() );
582 }
583}
584
585
586void NETLIST_EXPORTER_SPICE::WriteDirectives( const wxString& aSimCommand, unsigned aSimOptions,
587 OUTPUTFORMATTER& aFormatter ) const
588{
589 if( aSimOptions & OPTION_SAVE_ALL_VOLTAGES )
590 aFormatter.Print( 0, ".save all\n" );
591
592 if( aSimOptions & OPTION_SAVE_ALL_CURRENTS )
593 aFormatter.Print( 0, ".probe alli\n" );
594
595 if( aSimOptions & OPTION_SAVE_ALL_DISSIPATIONS )
596 {
597 for( const SPICE_ITEM& item : m_items )
598 {
599 // ngspice (v39) does not support power measurement for XSPICE devices
600 // XPSICE devices are marked with 'A'
601 std::string itemName = item.model->SpiceGenerator().ItemName( item );
602
603 if( ( item.model->GetPinCount() >= 2 ) && ( itemName.size() > 0 )
604 && ( itemName.c_str()[0] != 'A' ) )
605 {
606 aFormatter.Print( 0, ".probe p(%s)\n", itemName.c_str() );
607 }
608 }
609 }
610
611 auto isSimCommand =
612 []( const wxString& candidate, const wxString& dir )
613 {
614 return candidate == dir || candidate.StartsWith( dir + wxS( " " ) );
615 };
616
617 for( const wxString& directive : m_directives )
618 {
619 bool simCommand = false;
620
621 if( directive.StartsWith( "." ) )
622 {
623 wxString candidate = directive.Upper();
624
625 simCommand = ( isSimCommand( candidate, wxS( ".AC" ) )
626 || isSimCommand( candidate, wxS( ".DC" ) )
627 || isSimCommand( candidate, wxS( ".TRAN" ) )
628 || isSimCommand( candidate, wxS( ".OP" ) )
629 || isSimCommand( candidate, wxS( ".DISTO" ) )
630 || isSimCommand( candidate, wxS( ".NOISE" ) )
631 || isSimCommand( candidate, wxS( ".PZ" ) )
632 || isSimCommand( candidate, wxS( ".SENS" ) )
633 || isSimCommand( candidate, wxS( ".TF" ) ) );
634 }
635
636 if( !simCommand || ( aSimOptions & OPTION_SIM_COMMAND ) )
637 aFormatter.Print( 0, "%s\n", UTF8( directive ).c_str() );
638 }
639}
640
641
642std::string NETLIST_EXPORTER_SPICE::GenerateItemPinNetName( const std::string& aNetName,
643 int& aNcCounter ) const
644{
645 std::string netName = aNetName;
646
647 ConvertToSpiceMarkup( netName );
648 netName = std::string( UnescapeString( netName ).ToUTF8() );
649
650 if( netName == "" )
651 netName = fmt::format( "NC-{}", aNcCounter++ );
652
653 return netName;
654}
655
656
657SCH_SHEET_LIST NETLIST_EXPORTER_SPICE::GetSheets( unsigned aNetlistOptions ) const
658{
659 if( aNetlistOptions & OPTION_CUR_SHEET_AS_ROOT )
661 else
662 return m_schematic->GetSheets();
663}
664
const char * name
Definition: DXF_plotter.cpp:57
Used for text file output.
Definition: richio.h:475
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:49
std::unique_ptr< NODE > Parse()
std::unordered_set< std::string > m_names
std::string Generate(const std::string &aProposedName)
An abstract class used for the netlist exporters that Eeschema supports.
std::set< LIB_SYMBOL *, LIB_SYMBOL_LESS_THAN > m_libParts
unique library symbols used. LIB_SYMBOL items are sorted by names
std::vector< PIN_INFO > CreatePinList(SCH_SYMBOL *aSymbol, SCH_SHEET_PATH *aSheetPath, bool aKeepUnconnectedPins)
Find a symbol from the DrawList and builds its pin list.
UNIQUE_STRINGS m_referencesAlreadyFound
Used for "multiple symbols per package" symbols to avoid processing a lib symbol more than once.
SCH_SYMBOL * findNextSymbol(EDA_ITEM *aItem, SCH_SHEET_PATH *aSheetPath)
Check if the given symbol should be processed for netlisting.
SCHEMATIC_IFACE * m_schematic
The schematic we're generating a netlist for.
void readPinNetNames(SCH_SYMBOL &aSymbol, SPICE_ITEM &aItem, const std::vector< PIN_INFO > &aPins, int &aNcCounter)
void writeModels(OUTPUTFORMATTER &aFormatter)
void writeIncludes(OUTPUTFORMATTER &aFormatter, unsigned aNetlistOptions)
NETLIST_EXPORTER_SPICE(SCHEMATIC_IFACE *aSchematic, wxWindow *aDialogParent=nullptr)
std::list< SPICE_ITEM > m_items
void ReadDirectives(unsigned aNetlistOptions)
std::string GetItemName(const std::string &aRefName) const
Return name of Spice device corresponding to a schematic symbol.
void readModel(SCH_SHEET_PATH &aSheet, SCH_SYMBOL &aSymbol, SPICE_ITEM &aItem, REPORTER &aReporter)
SCH_SHEET_LIST GetSheets(unsigned aNetlistOptions=0) const
Return the paths of exported sheets (either all or the current one).
void readRefName(SCH_SHEET_PATH &aSheet, SCH_SYMBOL &aSymbol, SPICE_ITEM &aItem, std::set< std::string > &aRefNames)
static void ConvertToSpiceMarkup(std::string &aNetName)
Remove formatting wrappers and replace illegal spice net name characters with underscores.
void writeItems(OUTPUTFORMATTER &aFormatter)
std::vector< wxString > m_directives
Spice directives found in the schematic sheet.
virtual void WriteHead(OUTPUTFORMATTER &aFormatter, unsigned aNetlistOptions)
Write the netlist head (title and so on).
virtual std::string GenerateItemPinNetName(const std::string &aNetName, int &aNcCounter) const
const SPICE_ITEM * FindItem(const std::string &aRefName) const
Find and return the item corresponding to aRefName.
void writeInclude(OUTPUTFORMATTER &aFormatter, unsigned aNetlistOptions, const wxString &aPath)
virtual void WriteDirectives(const wxString &aSimCommand, unsigned aSimOptions, OUTPUTFORMATTER &candidate) const
SIM_LIB_MGR m_libMgr
Holds libraries and models.
virtual bool ReadSchematicAndLibraries(unsigned aNetlistOptions, REPORTER &aReporter)
Process the schematic and Spice libraries to create net mapping and a list of SPICE_ITEMs.
bool DoWriteNetlist(const wxString &aSimCommand, unsigned aSimOptions, OUTPUTFORMATTER &aFormatter, REPORTER &aReporter)
Write the netlist in aFormatter.
std::set< std::string > m_nets
Items representing schematic symbols in Spice world.
void readPinNumbers(SCH_SYMBOL &aSymbol, SPICE_ITEM &aItem, const std::vector< PIN_INFO > &aPins)
const std::list< SPICE_ITEM > & GetItems() const
Return the list of items representing schematic symbols in the Spice world.
std::set< wxString > m_rawIncludes
include directives found in symbols
virtual void WriteTail(OUTPUTFORMATTER &aFormatter, unsigned aNetlistOptions)
Write the tail (.end).
NAME_GENERATOR m_modelNameGenerator
Generates unique model names.
bool WriteNetlist(const wxString &aOutFileName, unsigned aNetlistOptions, REPORTER &aReporter) override
Write to specified output file.
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:322
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:458
static wxString GetUserCachePath()
Gets the stock (install) 3d viewer plugins path.
Definition: paths.cpp:351
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:71
virtual bool HasMessage() const =0
Returns true if the reporter client is non-empty.
virtual SCH_SHEET_LIST GetSheets() const =0
virtual SCH_SHEET_PATH & CurrentSheet() const =0
virtual PROJECT & Prj() const =0
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:52
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:151
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
SCH_SHEET * Last() const
Return a pointer to the last SCH_SHEET of the list.
Schematic symbol object.
Definition: sch_symbol.h:109
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const
Return the reference for the given sheet path.
Definition: sch_symbol.cpp:748
bool GetExcludedFromSim() const override
Definition: sch_symbol.h:854
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly)
Populate a std::vector with SCH_FIELDs.
Definition: sch_symbol.cpp:998
virtual wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const
SIM_MODEL & CreateModel(SIM_MODEL::TYPE aType, const std::vector< LIB_PIN * > &aPins, REPORTER &aReporter)
std::map< wxString, std::reference_wrapper< const SIM_LIBRARY > > GetLibraries() const
const SPICE_GENERATOR & SpiceGenerator() const
Definition: sim_model.h:439
std::string IbisDevice(const SPICE_ITEM &aItem, const PROJECT &aProject, const wxString &aCacheDir) const
virtual std::string ItemName(const SPICE_ITEM &aItem) const
virtual std::string ModelName(const SPICE_ITEM &aItem) const
void Clear()
Erase the record.
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition: utf8.h:71
const char * c_str() const
Definition: utf8.h:102
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:334
The common library.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:305
This file is part of the common library.
#define _(s)
wxString ResolveFile(const wxString &aFileName, const ENV_VAR_MAP *aEnvVars, const PROJECT *aProject)
Search the default paths trying to find one with the requested file.
Definition: env_paths.cpp:164
PROJECT & Prj()
Definition: kicad.cpp:591
see class PGM_BASE
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:119
std::vector< FAB_LAYER_COLOR > dummy
wxString UnescapeString(const wxString &aSource)
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:391
SIM_MODEL & model
Definition: sim_library.h:41
std::string name
Definition: sim_library.h:40
std::string refName
std::vector< SCH_FIELD > fields
std::string modelName
const SIM_MODEL * model
std::vector< std::string > pinNetNames
std::string baseModelName
std::vector< std::string > pinNumbers
@ REFERENCE_FIELD
Field Reference of part, i.e. "IC21".
@ SCH_SYMBOL_T
Definition: typeinfo.h:156
@ SCH_TEXT_T
Definition: typeinfo.h:150
@ SCH_TEXTBOX_T
Definition: typeinfo.h:149
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588