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 The 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 <common.h>
32#include <confirm.h>
33#include <pgm_base.h>
34#include <env_paths.h>
35#include <richio.h>
38#include <sch_screen.h>
39#include <sch_textbox.h>
40#include <string_utils.h>
41#include <algorithm>
42#include <ki_exception.h>
43
45#include <fmt/core.h>
46#include <paths.h>
47#include <wx/dir.h>
48#include <wx/log.h>
49#include <wx/tokenzr.h>
50#include <locale_io.h>
51#include "markup_parser.h"
52
53
54std::string NAME_GENERATOR::Generate( const std::string& aProposedName )
55{
56 std::string name = aProposedName;
57 int ii = 1;
58
59 while( m_names.contains( name ) )
60 name = fmt::format( "{}#{}", aProposedName, ii++ );
61
62 return name;
63}
64
65
67 NETLIST_EXPORTER_BASE( aSchematic ),
68 m_libMgr( &aSchematic->Project() )
69{
70 std::vector<EMBEDDED_FILES*> embeddedFilesStack;
71 embeddedFilesStack.push_back( aSchematic->GetEmbeddedFiles() );
72 m_libMgr.SetFilesStack( std::move( embeddedFilesStack ) );
73}
74
75
76bool NETLIST_EXPORTER_SPICE::WriteNetlist( const wxString& aOutFileName, unsigned aNetlistOptions,
77 REPORTER& aReporter )
78{
79 FILE_OUTPUTFORMATTER formatter( aOutFileName, wxT( "wt" ), '\'' );
80 return DoWriteNetlist( wxEmptyString, aNetlistOptions, formatter, aReporter );
81}
82
83
84bool NETLIST_EXPORTER_SPICE::DoWriteNetlist( const wxString& aSimCommand, unsigned aSimOptions,
85 OUTPUTFORMATTER& aFormatter, REPORTER& aReporter )
86{
88
89 // Cleanup list to avoid duplicate if the netlist exporter is run more than once.
90 m_rawIncludes.clear();
91
92 bool result = ReadSchematicAndLibraries( aSimOptions, aReporter );
93
94 WriteHead( aFormatter, aSimOptions );
95
96 writeIncludes( aFormatter, aSimOptions );
97 writeModels( aFormatter );
98
99 // Skip this if there is no netlist to avoid an ngspice segfault
100 if( !m_items.empty() )
101 WriteDirectives( aSimCommand, aSimOptions, aFormatter );
102
103 writeItems( aFormatter );
104
105 WriteTail( aFormatter, aSimOptions );
106
107 return result;
108}
109
110
111void NETLIST_EXPORTER_SPICE::WriteHead( OUTPUTFORMATTER& aFormatter, unsigned aNetlistOptions )
112{
113 aFormatter.Print( 0, ".title KiCad schematic\n" );
114}
115
116
117void NETLIST_EXPORTER_SPICE::WriteTail( OUTPUTFORMATTER& aFormatter, unsigned aNetlistOptions )
118{
119 aFormatter.Print( 0, ".end\n" );
120}
121
122
124 REPORTER& aReporter )
125{
126 std::set<std::string> refNames; // Set of reference names to check for duplication.
127 int ncCounter = 1;
128 wxString variant = m_schematic->GetCurrentVariant();
129
130 ReadDirectives( aNetlistOptions );
131
132 m_nets.clear();
133 m_items.clear();
135 m_libParts.clear();
136
137 wxFileName cacheDir;
138 cacheDir.AssignDir( PATHS::GetUserCachePath() );
139 cacheDir.AppendDir( wxT( "ibis" ) );
140
141 if( !cacheDir.DirExists() )
142 {
143 cacheDir.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
144
145 if( !cacheDir.DirExists() )
146 {
147 wxLogTrace( wxT( "IBIS_CACHE:" ),
148 wxT( "%s:%s:%d\n * failed to create ibis cache directory '%s'" ),
149 __FILE__, __FUNCTION__, __LINE__, cacheDir.GetPath() );
150
151 return false;
152 }
153 }
154
155 wxDir dir;
156 wxString dirName = cacheDir.GetFullPath();
157
158 if( !dir.Open( dirName ) )
159 return false;
160
161 wxFileName thisFile;
162 wxArrayString fileList;
163 wxString fileSpec = wxT( "*.cache" );
164
165 thisFile.SetPath( dirName ); // Set the base path to the cache folder
166
167 size_t numFilesFound = wxDir::GetAllFiles( dirName, &fileList, fileSpec );
168
169 for( size_t ii = 0; ii < numFilesFound; ii++ )
170 {
171 // Completes path to specific file so we can get its "last access" date
172 thisFile.SetFullName( fileList[ii] );
173 wxRemoveFile( thisFile.GetFullPath() );
174 }
175
176 for( SCH_SHEET_PATH& sheet : BuildSheetList( aNetlistOptions ) )
177 {
178 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
179 {
180 SCH_SYMBOL* symbol = findNextSymbol( item, sheet );
181
182 if( !symbol || symbol->ResolveExcludedFromSim( &sheet, variant ) )
183 continue;
184
185 try
186 {
187 SPICE_ITEM spiceItem;
188 std::vector<PIN_INFO> pins = CreatePinList( symbol, sheet, true );
189
190 for( const SCH_FIELD& field : symbol->GetFields() )
191 {
192 spiceItem.fields.emplace_back( symbol, FIELD_T::USER, field.GetName() );
193
194 if( field.GetId() == FIELD_T::REFERENCE )
195 spiceItem.fields.back().SetText( symbol->GetRef( &sheet ) );
196 else
197 spiceItem.fields.back().SetText( field.GetShownText( &sheet, false, 0, variant ) );
198 }
199
200 readRefName( sheet, *symbol, spiceItem, refNames );
201 readModel( sheet, *symbol, spiceItem, variant, aReporter );
202 readPinNumbers( *symbol, spiceItem, pins );
203 readPinNetNames( *symbol, spiceItem, pins, ncCounter );
204 readNodePattern( spiceItem );
205 // TODO: transmission line handling?
206
207 m_items.push_back( std::move( spiceItem ) );
208 }
209 catch( IO_ERROR& e )
210 {
211 aReporter.Report( e.What(), RPT_SEVERITY_ERROR );
212 }
213 }
214 }
215
217}
218
219
221{
222 MARKUP::MARKUP_PARSER markupParser( aNetName->ToStdString() );
223 std::unique_ptr<MARKUP::NODE> root = markupParser.Parse();
224
225 std::function<void( const std::unique_ptr<MARKUP::NODE>&)> convertMarkup =
226 [&]( const std::unique_ptr<MARKUP::NODE>& aNode )
227 {
228 if( aNode )
229 {
230 if( !aNode->is_root() )
231 {
232 if( aNode->isOverbar() )
233 {
234 // ~{CLK} is a different signal than CLK
235 *aNetName += '~';
236 }
237 else if( aNode->isSubscript() || aNode->isSuperscript() )
238 {
239 // V_{OUT} is just a pretty-printed version of VOUT
240 }
241
242 if( aNode->has_content() )
243 *aNetName += aNode->string();
244 }
245
246 for( const std::unique_ptr<MARKUP::NODE>& child : aNode->children )
247 convertMarkup( child );
248 }
249 };
250
251 *aNetName = wxEmptyString;
252 convertMarkup( root );
253
254 // Replace all ngspice-disallowed chars in netnames by a '_'
255 aNetName->Replace( '%', '_' );
256 aNetName->Replace( '(', '_' );
257 aNetName->Replace( ')', '_' );
258 aNetName->Replace( ',', '_' );
259 aNetName->Replace( '[', '_' );
260 aNetName->Replace( ']', '_' );
261 aNetName->Replace( '<', '_' );
262 aNetName->Replace( '>', '_' );
263 aNetName->Replace( '~', '_' );
264 aNetName->Replace( ' ', '_' );
265
266 // A net name on the root sheet with a label '/foo' is going to get titled "//foo". This
267 // will trip up ngspice as "//" opens a line comment.
268 if( aNetName->StartsWith( wxS( "//" ) ) )
269 aNetName->Replace( wxS( "//" ), wxS( "/root/" ), false /* replace all */ );
270}
271
272
273wxString NETLIST_EXPORTER_SPICE::GetItemName( const wxString& aRefName ) const
274{
275 if( const SPICE_ITEM* item = FindItem( aRefName ) )
276 return item->model->SpiceGenerator().ItemName( *item );
277
278 return wxEmptyString;
279}
280
281
282const SPICE_ITEM* NETLIST_EXPORTER_SPICE::FindItem( const wxString& aRefName ) const
283{
284 const std::string refName = aRefName.ToStdString();
285 const std::list<SPICE_ITEM>& spiceItems = GetItems();
286
287 auto it = std::find_if( spiceItems.begin(), spiceItems.end(),
288 [&refName]( const SPICE_ITEM& item )
289 {
290 return item.refName == refName;
291 } );
292
293 if( it != spiceItems.end() )
294 return &*it;
295
296 return nullptr;
297}
298
299
300void NETLIST_EXPORTER_SPICE::ReadDirectives( unsigned aNetlistOptions )
301{
302 wxString text;
303
304 m_directives.clear();
305
306 for( const SCH_SHEET_PATH& sheet : BuildSheetList( aNetlistOptions ) )
307 {
308 for( SCH_ITEM* item : sheet.LastScreen()->Items() )
309 {
310 if( item->ResolveExcludedFromSim() )
311 continue;
312
313 if( item->Type() == SCH_TEXT_T )
314 text = static_cast<SCH_TEXT*>( item )->GetShownText( &sheet, false );
315 else if( item->Type() == SCH_TEXTBOX_T )
316 text = static_cast<SCH_TEXTBOX*>( item )->GetShownText( nullptr, &sheet, false );
317 else
318 continue;
319
320 // Send anything that contains directives to SPICE
321 wxStringTokenizer tokenizer( text, "\r\n", wxTOKEN_STRTOK );
322 bool foundDirective = false;
323
324 auto isDirective =
325 []( const wxString& line, const wxString& dir )
326 {
327 return line == dir || line.StartsWith( dir + wxS( " " ) );
328 };
329
330 while( tokenizer.HasMoreTokens() )
331 {
332 wxString line = tokenizer.GetNextToken().Upper();
333
334 if( line.StartsWith( wxT( "." ) ) )
335 {
336 if( isDirective( line, wxS( ".AC" ) )
337 || isDirective( line, wxS( ".CONTROL" ) )
338 || isDirective( line, wxS( ".CSPARAM" ) )
339 || isDirective( line, wxS( ".DISTO" ) )
340 || isDirective( line, wxS( ".DC" ) )
341 || isDirective( line, wxS( ".ELSE" ) )
342 || isDirective( line, wxS( ".ELSEIF" ) )
343 || isDirective( line, wxS( ".END" ) )
344 || isDirective( line, wxS( ".ENDC" ) )
345 || isDirective( line, wxS( ".ENDIF" ) )
346 || isDirective( line, wxS( ".ENDS" ) )
347 || isDirective( line, wxS( ".FOUR" ) )
348 || isDirective( line, wxS( ".FUNC" ) )
349 || isDirective( line, wxS( ".GLOBAL" ) )
350 || isDirective( line, wxS( ".IC" ) )
351 || isDirective( line, wxS( ".IF" ) )
352 || isDirective( line, wxS( ".INCLUDE" ) )
353 || isDirective( line, wxS( ".LIB" ) )
354 || isDirective( line, wxS( ".MEAS" ) )
355 || isDirective( line, wxS( ".MODEL" ) )
356 || isDirective( line, wxS( ".NODESET" ) )
357 || isDirective( line, wxS( ".NOISE" ) )
358 || isDirective( line, wxS( ".OP" ) )
359 || isDirective( line, wxS( ".OPTIONS" ) )
360 || isDirective( line, wxS( ".PARAM" ) )
361 || isDirective( line, wxS( ".PLOT" ) )
362 || isDirective( line, wxS( ".PRINT" ) )
363 || isDirective( line, wxS( ".PROBE" ) )
364 || isDirective( line, wxS( ".PZ" ) )
365 || isDirective( line, wxS( ".SAVE" ) )
366 || isDirective( line, wxS( ".SENS" ) )
367 || isDirective( line, wxS( ".SP" ) )
368 || isDirective( line, wxS( ".SUBCKT" ) )
369 || isDirective( line, wxS( ".TEMP" ) )
370 || isDirective( line, wxS( ".TF" ) )
371 || isDirective( line, wxS( ".TITLE" ) )
372 || isDirective( line, wxS( ".TRAN" ) )
373 || isDirective( line, wxS( ".WIDTH" ) ) )
374 {
375 foundDirective = true;
376 break;
377 }
378 }
379 else if( line.StartsWith( wxT( "K" ) ) )
380 {
381 // Check for mutual inductor declaration
382 wxStringTokenizer line_t( line, " \t", wxTOKEN_STRTOK );
383
384 // Coupling ID
385 if( !line_t.HasMoreTokens() || !line_t.GetNextToken().StartsWith( wxT( "K" ) ) )
386 continue;
387
388 // Inductor 1 ID
389 if( !line_t.HasMoreTokens() || !line_t.GetNextToken().StartsWith( wxT( "L" ) ) )
390 continue;
391
392 // Inductor 2 ID
393 if( !line_t.HasMoreTokens() || !line_t.GetNextToken().StartsWith( wxT( "L" ) ) )
394 continue;
395
396 // That's probably distinctive enough not to bother trying to parse the
397 // coupling value. If there's anything else, assume it's the value.
398 if( line_t.HasMoreTokens() )
399 {
400 foundDirective = true;
401 break;
402 }
403 }
404 }
405
406 if( foundDirective )
407 m_directives.emplace_back( text );
408 }
409 }
410}
411
412
414 const SCH_SHEET_PATH& aSheet )
415{
416 // Only process multi-unit symbols
417 if( !aSymbol.GetLibSymbolRef() || aSymbol.GetLibSymbolRef()->GetUnitCount() <= 1 )
418 return wxEmptyString;
419
420 wxString ref = aSymbol.GetRef( &aSheet );
421 std::vector<std::pair<wxString, wxString>> pinList;
422 std::set<wxString> pinNumbers;
423
424 // Helper to parse and collect pin mappings from a Sim.Pins field value
425 auto parsePins = [&]( const wxString& aPins )
426 {
427 wxStringTokenizer tokenizer( aPins, wxS( " \t\r\n" ), wxTOKEN_STRTOK );
428
429 while( tokenizer.HasMoreTokens() )
430 {
431 wxString token = tokenizer.GetNextToken();
432 int pos = token.Find( wxS( '=' ) );
433
434 if( pos == wxNOT_FOUND )
435 continue;
436
437 wxString pinNumber = token.Left( pos );
438 wxString modelPin = token.Mid( pos + 1 );
439
440 // Only add if we haven't seen this pin number before
441 if( pinNumbers.insert( pinNumber ).second )
442 pinList.emplace_back( pinNumber, modelPin );
443 }
444 };
445
446 // First, parse pins from the current symbol
447 if( SCH_FIELD* pinsField = aSymbol.GetField( SIM_PINS_FIELD ) )
448 parsePins( pinsField->GetShownText( &aSheet, false ) );
449
450 // Then, find all other units with the same reference and collect their Sim.Pins
451 for( const SCH_SHEET_PATH& sheet : m_schematic->Hierarchy() )
452 {
453 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
454 {
455 SCH_SYMBOL* other = static_cast<SCH_SYMBOL*>( item );
456
457 if( other == &aSymbol )
458 continue;
459
460 if( other->GetRef( &sheet ) != ref )
461 continue;
462
463 if( SCH_FIELD* pinsField = other->GetField( SIM_PINS_FIELD ) )
464 parsePins( pinsField->GetShownText( &sheet, false ) );
465 }
466 }
467
468 // If no pins were collected or only from current symbol, return empty
469 // (let the normal processing handle it)
470 if( pinList.empty() )
471 return wxEmptyString;
472
473 // Build the merged Sim.Pins string
474 wxString merged;
475
476 for( const auto& [pinNumber, modelPin] : pinList )
477 {
478 if( !merged.IsEmpty() )
479 merged += wxS( " " );
480
481 merged += pinNumber + wxS( "=" ) + modelPin;
482 }
483
484 return merged;
485}
486
487
489 SPICE_ITEM& aItem, std::set<std::string>& aRefNames )
490{
491 aItem.refName = aSymbol.GetRef( &aSheet );
492
493 if( !aRefNames.insert( aItem.refName ).second )
494 wxASSERT( wxT( "Duplicate refdes encountered; what happened to ReadyToNetlist()?" ) );
495}
496
497
499 const wxString& aVariantName, REPORTER& aReporter )
500{
501 // For multi-unit symbols, collect merged Sim.Pins from all units
502 wxString mergedSimPins = collectMergedSimPins( aSymbol, aSheet );
503
504 const SIM_LIBRARY::MODEL& libModel = m_libMgr.CreateModel( &aSheet, aSymbol, true, 0, aVariantName,
505 aReporter, mergedSimPins );
506
507 aItem.baseModelName = libModel.name;
508 aItem.model = &libModel.model;
509
510 std::string modelName = aItem.model->SpiceGenerator().ModelName( aItem );
511 // Resolve model name collisions.
512 aItem.modelName = m_modelNameGenerator.Generate( modelName );
513
514 // FIXME: Don't have special cases for raw Spice models and KIBIS.
515 if( auto rawSpiceModel = dynamic_cast<const SIM_MODEL_RAW_SPICE*>( aItem.model ) )
516 {
517 int libParamIndex = static_cast<int>( SIM_MODEL_RAW_SPICE::SPICE_PARAM::LIB );
518 wxString path = rawSpiceModel->GetParam( libParamIndex ).value;
519
520 if( !path.IsEmpty() )
521 m_rawIncludes.insert( path );
522 }
523 else if( auto ibisModel = dynamic_cast<const SIM_MODEL_IBIS*>( aItem.model ) )
524 {
525 wxFileName cacheFn;
526 cacheFn.AssignDir( PATHS::GetUserCachePath() );
527 cacheFn.AppendDir( wxT( "ibis" ) );
528 cacheFn.SetFullName( aSymbol.GetRef( &aSheet ) + wxT( ".cache" ) );
529
530 wxFile cacheFile( cacheFn.GetFullPath(), wxFile::write );
531
532 if( !cacheFile.IsOpened() )
533 {
534 wxLogError( _( "Could not open file '%s' to write IBIS model" ),
535 cacheFn.GetFullPath() );
536 }
537
538 auto spiceGenerator = static_cast<const SPICE_GENERATOR_IBIS&>( ibisModel->SpiceGenerator() );
539
540 wxString cacheFilepath = cacheFn.GetPath( wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR );
541 std::string modelData = spiceGenerator.IbisDevice( aItem, m_schematic,
542 cacheFilepath, aReporter );
543
544 cacheFile.Write( wxString( modelData ) );
545 m_rawIncludes.insert( cacheFn.GetFullPath() );
546 }
547}
548
549
551 const std::vector<PIN_INFO>& aPins )
552{
553 for( const PIN_INFO& pin : aPins )
554 aItem.pinNumbers.emplace_back( pin.num.ToStdString() );
555}
556
557
559 const std::vector<PIN_INFO>& aPins, int& aNcCounter )
560{
561 for( const PIN_INFO& pinInfo : aPins )
562 {
563 wxString netName = GenerateItemPinNetName( pinInfo.netName, aNcCounter );
564
565 aItem.pinNetNames.push_back( netName.ToStdString() );
566 m_nets.insert( netName );
567 }
568}
569
570
572 std::vector<std::string>& aModifiers )
573{
574 std::string input = GetFieldValue( &aItem.fields, SIM_NODES_FORMAT_FIELD, true, 0 );
575
576 if( input == "" )
577 return;
578
579 tao::pegtl::string_input<> in( input, "Sim.NodesFormat field" );
580 std::unique_ptr<tao::pegtl::parse_tree::node> root;
581 std::string singleNodeModifier;
582
583 try
584 {
585 root = tao::pegtl::parse_tree::parse<SIM_XSPICE_PARSER_GRAMMAR::nodeSequenceGrammar,
587 tao::pegtl::nothing,
589 for( const auto& node : root->children )
590 {
591 if( node->is_type<SIM_XSPICE_PARSER_GRAMMAR::squareBracketC>() )
592 {
593 //we want ']' to close previous ?
594 aModifiers.back().append( node->string() );
595 }
596 else
597 { //rest goes to the new singleNodeModifier
598 singleNodeModifier.append( node->string() );
599 }
600
601 if( node->is_type<SIM_XSPICE_PARSER_GRAMMAR::nodeName>() )
602 {
603 aModifiers.push_back( singleNodeModifier );
604 singleNodeModifier.erase( singleNodeModifier.begin(), singleNodeModifier.end() );
605 }
606 }
607 }
608 catch( const tao::pegtl::parse_error& e )
609 {
610 THROW_IO_ERROR( wxString::Format( _( "Error in parsing model '%s', error: '%s'" ),
611 aItem.refName, e.what() ) );
612 }
613}
615{
616 std::vector<std::string> xspicePattern;
617 NETLIST_EXPORTER_SPICE::getNodePattern( aItem, xspicePattern );
618
619 if( xspicePattern.empty() )
620 return;
621
622 if( xspicePattern.size() != aItem.pinNetNames.size() )
623 {
624 THROW_IO_ERROR( wxString::Format( _( "Error in parsing model '%s', wrong number of nodes "
625 "'?' in Sim.NodesFormat compared to connections" ),
626 aItem.refName ) );
627 return;
628 }
629
630 auto itNetNames = aItem.pinNetNames.begin();
631
632 for( std::string& pattern : xspicePattern )
633 {
634 // ngspice does not care about aditional spaces, and we make sure that "%d?" is separated
635 const std::string netName = " " + *itNetNames + " ";
636 pattern.replace( pattern.find( "?" ), 1, netName );
637 *itNetNames = pattern;
638 ++itNetNames;
639 }
640}
641
642void NETLIST_EXPORTER_SPICE::writeInclude( OUTPUTFORMATTER& aFormatter, unsigned aNetlistOptions,
643 const wxString& aPath )
644{
645 // First, expand env vars, if any.
646 wxString expandedPath = ExpandEnvVarSubstitutions( aPath, &m_schematic->Project() );
647
648 // Path may have been authored by someone on a Windows box; convert it to UNIX format
649 expandedPath.Replace( '\\', '/' );
650
651 wxString fullPath;
652
653 if( aNetlistOptions & OPTION_ADJUST_INCLUDE_PATHS )
654 {
655 // Look for the library in known search locations.
656 fullPath = ResolveFile( expandedPath, &Pgm().GetLocalEnvVariables(), &m_schematic->Project() );
657
658 if( fullPath.IsEmpty() )
659 {
660 wxLogError( _( "Could not find library file '%s'" ), expandedPath );
661 fullPath = expandedPath;
662 }
663 else if( wxFileName::GetPathSeparator() == '\\' )
664 {
665 // Convert it to UNIX format (again) if ResolveFile() returned a Windows style path
666 fullPath.Replace( '\\', '/' );
667 }
668 }
669 else
670 {
671 fullPath = expandedPath;
672 }
673
674 aFormatter.Print( 0, ".include \"%s\"\n", TO_UTF8( fullPath ) );
675}
676
677
678void NETLIST_EXPORTER_SPICE::writeIncludes( OUTPUTFORMATTER& aFormatter, unsigned aNetlistOptions )
679{
680 for( const auto& [path, library] : m_libMgr.GetLibraries() )
681 {
682 if( dynamic_cast<const SIM_LIBRARY_SPICE*>( &library.get() ) )
683 writeInclude( aFormatter, aNetlistOptions, path );
684 }
685
686 for( const wxString& path : m_rawIncludes )
687 writeInclude( aFormatter, aNetlistOptions, path );
688}
689
690
692{
693 for( const SPICE_ITEM& item : m_items )
694 {
695 if( !item.model->IsEnabled() )
696 continue;
697
698 aFormatter.Print( 0, "%s", item.model->SpiceGenerator().ModelLine( item ).c_str() );
699 }
700}
701
702
704{
705 for( const SPICE_ITEM& item : m_items )
706 {
707 if( !item.model->IsEnabled() )
708 continue;
709
710 aFormatter.Print( 0, "%s", item.model->SpiceGenerator().ItemLine( item ).c_str() );
711 }
712}
713
714
715void NETLIST_EXPORTER_SPICE::WriteDirectives( const wxString& aSimCommand, unsigned aSimOptions,
716 OUTPUTFORMATTER& aFormatter ) const
717{
718 if( aSimOptions & OPTION_SAVE_ALL_VOLTAGES )
719 aFormatter.Print( 0, ".save all\n" );
720
721 if( aSimOptions & OPTION_SAVE_ALL_CURRENTS )
722 aFormatter.Print( 0, ".probe alli\n" );
723
724 if( aSimOptions & OPTION_SAVE_ALL_DISSIPATIONS )
725 {
726 for( const SPICE_ITEM& item : m_items )
727 {
728 // ngspice (v39) does not support power measurement for XSPICE devices
729 // XPSICE devices are marked with 'A'
730 std::string itemName = item.model->SpiceGenerator().ItemName( item );
731
732 if( ( item.model->GetPinCount() >= 2 ) && ( itemName.size() > 0 )
733 && ( itemName.c_str()[0] != 'A' ) )
734 {
735 aFormatter.Print( 0, ".probe p(%s)\n", itemName.c_str() );
736 }
737 }
738 }
739
740 auto isSimCommand =
741 []( const wxString& candidate, const wxString& dir )
742 {
743 return candidate == dir || candidate.StartsWith( dir + wxS( " " ) );
744 };
745
746 for( const wxString& directive : m_directives )
747 {
748 bool simCommand = false;
749
750 if( directive.StartsWith( "." ) )
751 {
752 wxString candidate = directive.Upper();
753
754 simCommand = ( isSimCommand( candidate, wxS( ".AC" ) )
755 || isSimCommand( candidate, wxS( ".DC" ) )
756 || isSimCommand( candidate, wxS( ".TRAN" ) )
757 || isSimCommand( candidate, wxS( ".OP" ) )
758 || isSimCommand( candidate, wxS( ".DISTO" ) )
759 || isSimCommand( candidate, wxS( ".NOISE" ) )
760 || isSimCommand( candidate, wxS( ".PZ" ) )
761 || isSimCommand( candidate, wxS( ".SENS" ) )
762 || isSimCommand( candidate, wxS( ".TF" ) ) );
763 }
764
765 if( !simCommand || ( aSimOptions & OPTION_SIM_COMMAND ) )
766 aFormatter.Print( 0, "%s\n", UTF8( directive ).c_str() );
767 }
768}
769
770
771wxString NETLIST_EXPORTER_SPICE::GenerateItemPinNetName( const wxString& aNetName,
772 int& aNcCounter ) const
773{
774 wxString netName = UnescapeString( aNetName );
775
776 ConvertToSpiceMarkup( &netName );
777
778 if( netName.IsEmpty() )
779 netName.Printf( wxS( "NC-%d" ), aNcCounter++ );
780
781 return netName;
782}
783
784
786{
787 SCH_SHEET_LIST sheets;
788
789 if( aNetlistOptions & OPTION_CUR_SHEET_AS_ROOT )
790 sheets = SCH_SHEET_LIST( m_schematic->CurrentSheet().Last() );
791 else
792 sheets = m_schematic->Hierarchy();
793
794 std::erase_if( sheets,
795 [&]( const SCH_SHEET_PATH& sheet )
796 {
797 return sheet.GetExcludedFromSim();
798 } );
799
800 return sheets;
801}
802
const char * name
Used for text file output.
Definition richio.h:469
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
int GetUnitCount() const override
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:41
std::unique_ptr< NODE > Parse()
std::unordered_set< std::string > m_names
std::string Generate(const std::string &aProposedName)
SCHEMATIC * m_schematic
The schematic we're generating a netlist for.
std::vector< PIN_INFO > CreatePinList(SCH_SYMBOL *aSymbol, const SCH_SHEET_PATH &aSheetPath, bool aKeepUnconnectedPins)
Find a symbol from the DrawList and builds its pin list.
SCH_SYMBOL * findNextSymbol(EDA_ITEM *aItem, const SCH_SHEET_PATH &aSheetPath)
Check if the given symbol should be processed for netlisting.
std::set< LIB_SYMBOL *, LIB_SYMBOL_LESS_THAN > m_libParts
unique library symbols used. LIB_SYMBOL items are sorted by names
NETLIST_EXPORTER_BASE(SCHEMATIC *aSchematic)
UNIQUE_STRINGS m_referencesAlreadyFound
Used for "multiple symbols per package" symbols to avoid processing a lib symbol more than once.
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)
std::list< SPICE_ITEM > m_items
void getNodePattern(SPICE_ITEM &aItem, std::vector< std::string > &aModifiers)
static void ConvertToSpiceMarkup(wxString *aNetName)
Remove formatting wrappers and replace illegal spice net name characters with underscores.
void ReadDirectives(unsigned aNetlistOptions)
SCH_SHEET_LIST BuildSheetList(unsigned aNetlistOptions=0) const
Return the paths of exported sheets (either all or the current one).
virtual wxString GenerateItemPinNetName(const wxString &aNetName, int &aNcCounter) const
std::set< wxString > m_nets
Items representing schematic symbols in Spice world.
void readRefName(SCH_SHEET_PATH &aSheet, SCH_SYMBOL &aSymbol, SPICE_ITEM &aItem, std::set< std::string > &aRefNames)
wxString collectMergedSimPins(SCH_SYMBOL &aSymbol, const SCH_SHEET_PATH &aSheet)
Collect merged Sim.Pins from all units of a multi-unit symbol.
wxString GetItemName(const wxString &aRefName) const
Return name of Spice device corresponding to a schematic symbol.
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).
void writeInclude(OUTPUTFORMATTER &aFormatter, unsigned aNetlistOptions, const wxString &aPath)
virtual void WriteDirectives(const wxString &aSimCommand, unsigned aSimOptions, OUTPUTFORMATTER &candidate) const
NETLIST_EXPORTER_SPICE(SCHEMATIC *aSchematic)
SIM_LIB_MGR m_libMgr
Holds libraries and models.
const SPICE_ITEM * FindItem(const wxString &aRefName) const
Find and return the item corresponding to aRefName.
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.
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.
void readNodePattern(SPICE_ITEM &aItem)
std::set< wxString > m_rawIncludes
include directives found in symbols
void readModel(SCH_SHEET_PATH &aSheet, SCH_SYMBOL &aSymbol, SPICE_ITEM &aItem, const wxString &aVariantName, REPORTER &aReporter)
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:295
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition richio.cpp:422
static wxString GetUserCachePath()
Gets the stock (install) 3d viewer plugins path.
Definition paths.cpp:450
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:102
virtual bool HasMessageOfSeverity(int aSeverityMask) const
Returns true if the reporter has one or more messages matching the specified severity mask.
Definition reporter.h:143
Holds all the data relating to one schematic.
Definition schematic.h:88
EMBEDDED_FILES * GetEmbeddedFiles() override
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:167
bool ResolveExcludedFromSim(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const
Definition sch_item.cpp:282
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...
bool GetExcludedFromSim() const
Schematic symbol object.
Definition sch_symbol.h:76
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly) const override
Populate a std::vector with SCH_FIELDs, sorted in ordinal order.
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition sch_symbol.h:184
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this symbol.
const SPICE_GENERATOR & SpiceGenerator() const
Definition sim_model.h:431
std::string IbisDevice(const SPICE_ITEM &aItem, SCHEMATIC *aSchematic, const wxString &aCacheDir, REPORTER &aReporter) const
virtual std::string ModelName(const SPICE_ITEM &aItem) const
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition utf8.h:71
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition common.cpp:558
The common library.
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.
Helper functions to substitute paths with environmental variables.
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
must_if< error >::control< Rule > control
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_UNDEFINED
wxString GetFieldValue(const std::vector< SCH_FIELD > *aFields, FIELD_T aFieldType)
Definition sch_field.h:403
#define SIM_PINS_FIELD
Definition sim_model.h:54
#define SIM_NODES_FORMAT_FIELD
Definition sim_model.h:58
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.
SIM_MODEL & model
Definition sim_library.h:41
std::string name
Definition sim_library.h:40
Notes: spaces are allowed everywhere in any number ~ can only be before ?
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
@ USER
The field ID hasn't been set yet; field is invalid.
@ REFERENCE
Field Reference of part, i.e. "IC21".
std::string path
KIBIS_PIN * pin
wxString result
Test unit parsing edge cases and error handling.
@ SCH_SYMBOL_T
Definition typeinfo.h:176
@ SCH_TEXT_T
Definition typeinfo.h:155
@ SCH_TEXTBOX_T
Definition typeinfo.h:156