54 std::string
name = aProposedName;
58 name = fmt::format(
"{}#{}", aProposedName, ii++ );
66 m_libMgr( &aSchematic->
Prj(), aSchematic )
75 return DoWriteNetlist( wxEmptyString, aNetlistOptions, formatter, aReporter );
108 aFormatter.
Print( 0,
".title KiCad schematic\n" );
114 aFormatter.
Print( 0,
".end\n" );
122 std::set<std::string> refNames;
134 cacheDir.AppendDir( wxT(
"ibis" ) );
136 if( !cacheDir.DirExists() )
138 cacheDir.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
140 if( !cacheDir.DirExists() )
142 wxLogTrace( wxT(
"IBIS_CACHE:" ),
143 wxT(
"%s:%s:%d\n * failed to create ibis cache directory '%s'" ),
144 __FILE__, __FUNCTION__, __LINE__, cacheDir.GetPath() );
151 wxString dirName = cacheDir.GetFullPath();
153 if( !dir.Open( dirName ) )
157 wxArrayString fileList;
158 wxString fileSpec = wxT(
"*.cache" );
160 thisFile.SetPath( dirName );
162 size_t numFilesFound = wxDir::GetAllFiles( dirName, &fileList, fileSpec );
164 for(
size_t ii = 0; ii < numFilesFound; ii++ )
167 thisFile.SetFullName( fileList[ii] );
168 wxRemoveFile( thisFile.GetFullPath() );
183 std::vector<PIN_INFO> pins =
CreatePinList( symbol, sheet,
true );
190 if( field.GetId() == FIELD_T::REFERENCE )
191 spiceItem.
fields.back().SetText( symbol->
GetRef( &sheet ) );
193 spiceItem.
fields.back().SetText( field.GetShownText( &sheet,
false ) );
196 readRefName( sheet, *symbol, spiceItem, refNames );
197 readModel( sheet, *symbol, spiceItem, aReporter );
203 m_items.push_back( std::move( spiceItem ) );
219 std::unique_ptr<MARKUP::NODE> root = markupParser.
Parse();
221 std::function<void(
const std::unique_ptr<MARKUP::NODE>&)> convertMarkup =
222 [&](
const std::unique_ptr<MARKUP::NODE>& aNode )
226 if( !aNode->is_root() )
228 if( aNode->isOverbar() )
233 else if( aNode->isSubscript() || aNode->isSuperscript() )
238 if( aNode->has_content() )
239 *aNetName += aNode->string();
242 for(
const std::unique_ptr<MARKUP::NODE>& child : aNode->children )
243 convertMarkup( child );
247 *aNetName = wxEmptyString;
248 convertMarkup( root );
251 aNetName->Replace(
'%',
'_' );
252 aNetName->Replace(
'(',
'_' );
253 aNetName->Replace(
')',
'_' );
254 aNetName->Replace(
',',
'_' );
255 aNetName->Replace(
'[',
'_' );
256 aNetName->Replace(
']',
'_' );
257 aNetName->Replace(
'<',
'_' );
258 aNetName->Replace(
'>',
'_' );
259 aNetName->Replace(
'~',
'_' );
260 aNetName->Replace(
' ',
'_' );
264 if( aNetName->StartsWith( wxS(
"//" ) ) )
265 aNetName->Replace( wxS(
"//" ), wxS(
"/root/" ),
false );
272 return item->model->SpiceGenerator().ItemName( *item );
274 return wxEmptyString;
280 const std::string refName = aRefName.ToStdString();
281 const std::list<SPICE_ITEM>& spiceItems =
GetItems();
283 auto it = std::find_if( spiceItems.begin(), spiceItems.end(),
286 return item.refName == refName;
289 if( it != spiceItems.end() )
305 for(
SCH_ITEM* item : sheet.LastScreen()->Items() )
307 if( item->GetExcludedFromSim() )
311 text =
static_cast<SCH_TEXT*
>( item )->GetShownText( &sheet,
false );
318 wxStringTokenizer tokenizer(
text, wxT(
"\r\n" ), wxTOKEN_STRTOK );
319 bool foundDirective =
false;
322 [](
const wxString& line,
const wxString& dir )
324 return line == dir || line.StartsWith( dir + wxS(
" " ) );
327 while( tokenizer.HasMoreTokens() )
329 wxString line = tokenizer.GetNextToken().Upper();
331 if( line.StartsWith( wxT(
"." ) ) )
333 if( isDirective( line, wxS(
".AC" ) )
334 || isDirective( line, wxS(
".CONTROL" ) )
335 || isDirective( line, wxS(
".CSPARAM" ) )
336 || isDirective( line, wxS(
".DISTO" ) )
337 || isDirective( line, wxS(
".DC" ) )
338 || isDirective( line, wxS(
".ELSE" ) )
339 || isDirective( line, wxS(
".ELSEIF" ) )
340 || isDirective( line, wxS(
".END" ) )
341 || isDirective( line, wxS(
".ENDC" ) )
342 || isDirective( line, wxS(
".ENDIF" ) )
343 || isDirective( line, wxS(
".ENDS" ) )
344 || isDirective( line, wxS(
".FOUR" ) )
345 || isDirective( line, wxS(
".FUNC" ) )
346 || isDirective( line, wxS(
".GLOBAL" ) )
347 || isDirective( line, wxS(
".IC" ) )
348 || isDirective( line, wxS(
".IF" ) )
349 || isDirective( line, wxS(
".INCLUDE" ) )
350 || isDirective( line, wxS(
".LIB" ) )
351 || isDirective( line, wxS(
".MEAS" ) )
352 || isDirective( line, wxS(
".MODEL" ) )
353 || isDirective( line, wxS(
".NODESET" ) )
354 || isDirective( line, wxS(
".NOISE" ) )
355 || isDirective( line, wxS(
".OP" ) )
356 || isDirective( line, wxS(
".OPTIONS" ) )
357 || isDirective( line, wxS(
".PARAM" ) )
358 || isDirective( line, wxS(
".PLOT" ) )
359 || isDirective( line, wxS(
".PRINT" ) )
360 || isDirective( line, wxS(
".PROBE" ) )
361 || isDirective( line, wxS(
".PZ" ) )
362 || isDirective( line, wxS(
".SAVE" ) )
363 || isDirective( line, wxS(
".SENS" ) )
364 || isDirective( line, wxS(
".SP" ) )
365 || isDirective( line, wxS(
".SUBCKT" ) )
366 || isDirective( line, wxS(
".TEMP" ) )
367 || isDirective( line, wxS(
".TF" ) )
368 || isDirective( line, wxS(
".TITLE" ) )
369 || isDirective( line, wxS(
".TRAN" ) )
370 || isDirective( line, wxS(
".WIDTH" ) ) )
372 foundDirective =
true;
376 else if( line.StartsWith( wxT(
"K" ) ) )
379 wxStringTokenizer line_t( line, wxT(
" \t" ), wxTOKEN_STRTOK );
382 if( !line_t.HasMoreTokens() || !line_t.GetNextToken().StartsWith( wxT(
"K" ) ) )
386 if( !line_t.HasMoreTokens() || !line_t.GetNextToken().StartsWith( wxT(
"L" ) ) )
390 if( !line_t.HasMoreTokens() || !line_t.GetNextToken().StartsWith( wxT(
"L" ) ) )
395 if( line_t.HasMoreTokens() )
397 foundDirective =
true;
411 SPICE_ITEM& aItem, std::set<std::string>& aRefNames )
415 if( !aRefNames.insert( aItem.
refName ).second )
416 wxASSERT( wxT(
"Duplicate refdes encountered; what happened to ReadyToNetlist()?" ) );
435 int libParamIndex =
static_cast<int>( SIM_MODEL_RAW_SPICE::SPICE_PARAM::LIB );
436 wxString
path = rawSpiceModel->GetParam( libParamIndex ).value;
438 if( !
path.IsEmpty() )
445 cacheFn.AppendDir( wxT(
"ibis" ) );
446 cacheFn.SetFullName( aSymbol.
GetRef( &aSheet ) + wxT(
".cache" ) );
448 wxFile cacheFile( cacheFn.GetFullPath(), wxFile::write );
450 if( !cacheFile.IsOpened() )
452 wxLogError(
_(
"Could not open file '%s' to write IBIS model" ),
453 cacheFn.GetFullPath() );
458 wxString cacheFilepath = cacheFn.GetPath( wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR );
460 cacheFilepath, aReporter );
462 cacheFile.Write( wxString( modelData ) );
469 const std::vector<PIN_INFO>& aPins )
477 const std::vector<PIN_INFO>& aPins,
int& aNcCounter )
479 for(
const PIN_INFO& pinInfo : aPins )
483 aItem.
pinNetNames.push_back( netName.ToStdString() );
490 std::vector<std::string>& aModifiers )
497 tao::pegtl::string_input<> in( input,
"Sim.NodesFormat field" );
498 std::unique_ptr<tao::pegtl::parse_tree::node> root;
499 std::string singleNodeModifier;
507 for(
const auto& node : root->children )
512 aModifiers.back().append( node->string() );
516 singleNodeModifier.append( node->string() );
521 aModifiers.push_back( singleNodeModifier );
522 singleNodeModifier.erase( singleNodeModifier.begin(), singleNodeModifier.end() );
526 catch(
const tao::pegtl::parse_error& e )
528 THROW_IO_ERROR( wxString::Format(
_(
"Error in parsing model '%s', error: '%s'" ),
534 std::vector<std::string> xspicePattern;
537 if( xspicePattern.empty() )
540 if( xspicePattern.size() != aItem.
pinNetNames.size() )
542 THROW_IO_ERROR( wxString::Format(
_(
"Error in parsing model '%s', wrong number of nodes "
543 "'?' in Sim.NodesFormat compared to connections" ),
550 for( std::string& pattern : xspicePattern )
553 const std::string netName =
" " + *itNetNames +
" ";
554 pattern.replace( pattern.find(
"?" ), 1, netName );
555 *itNetNames = pattern;
561 const wxString& aPath )
567 expandedPath.Replace(
'\\',
'/' );
576 if( fullPath.IsEmpty() )
578 wxLogError(
_(
"Could not find library file '%s'" ), expandedPath );
579 fullPath = expandedPath;
581 else if( wxFileName::GetPathSeparator() ==
'\\' )
584 fullPath.Replace(
'\\',
'/' );
589 fullPath = expandedPath;
592 aFormatter.
Print( 0,
".include \"%s\"\n",
TO_UTF8( fullPath ) );
613 if( !item.model->IsEnabled() )
616 aFormatter.
Print( 0,
"%s", item.model->SpiceGenerator().ModelLine( item ).c_str() );
625 if( !item.model->IsEnabled() )
628 aFormatter.
Print( 0,
"%s", item.model->SpiceGenerator().ItemLine( item ).c_str() );
637 aFormatter.
Print( 0,
".save all\n" );
640 aFormatter.
Print( 0,
".probe alli\n" );
648 std::string itemName = item.model->SpiceGenerator().ItemName( item );
650 if( ( item.model->GetPinCount() >= 2 ) && ( itemName.size() > 0 )
651 && ( itemName.c_str()[0] !=
'A' ) )
653 aFormatter.
Print( 0,
".probe p(%s)\n", itemName.c_str() );
659 [](
const wxString& candidate,
const wxString& dir )
661 return candidate == dir || candidate.StartsWith( dir + wxS(
" " ) );
666 bool simCommand =
false;
668 if( directive.StartsWith(
"." ) )
670 wxString candidate = directive.Upper();
672 simCommand = ( isSimCommand( candidate, wxS(
".AC" ) )
673 || isSimCommand( candidate, wxS(
".DC" ) )
674 || isSimCommand( candidate, wxS(
".TRAN" ) )
675 || isSimCommand( candidate, wxS(
".OP" ) )
676 || isSimCommand( candidate, wxS(
".DISTO" ) )
677 || isSimCommand( candidate, wxS(
".NOISE" ) )
678 || isSimCommand( candidate, wxS(
".PZ" ) )
679 || isSimCommand( candidate, wxS(
".SENS" ) )
680 || isSimCommand( candidate, wxS(
".TF" ) ) );
690 int& aNcCounter )
const
696 if( netName.IsEmpty() )
697 netName.Printf( wxS(
"NC-%d" ), aNcCounter++ );
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()
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
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.
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
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)
@ OPTION_SAVE_ALL_CURRENTS
@ OPTION_SAVE_ALL_VOLTAGES
@ OPTION_SAVE_ALL_DISSIPATIONS
@ OPTION_ADJUST_INCLUDE_PATHS
@ OPTION_CUR_SHEET_AS_ROOT
SCH_SHEET_LIST BuildSheetList(unsigned aNetlistOptions=0) const
Return the paths of exported sheets (either all or the current one).
void readModel(SCH_SHEET_PATH &aSheet, SCH_SYMBOL &aSymbol, SPICE_ITEM &aItem, REPORTER &aReporter)
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 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
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.
static wxString GetUserCachePath()
Gets the stock (install) 3d viewer plugins path.
A pure virtual class used to derive REPORTER objects from.
virtual bool HasMessageOfSeverity(int aSeverityMask) const
Returns true if the reporter has one or more messages matching the specified severity mask.
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
Holds all the data relating to one schematic.
PROJECT & Prj() const
Return a reference to the project this schematic is part of.
SCH_SHEET_LIST Hierarchy() const
Return the full schematic flattened hierarchical sheet list.
SCH_SHEET_PATH & CurrentSheet() const
Base class for any item which can be embedded within the SCHEMATIC container class,...
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
SCH_SHEET * Last() const
Return a pointer to the last SCH_SHEET of the list.
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly) const override
Populate a std::vector with SCH_FIELDs, sorted in ordinal order.
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
virtual wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const
SIM_MODEL & CreateModel(SIM_MODEL::TYPE aType, const std::vector< SCH_PIN * > &aPins, REPORTER &aReporter)
std::map< wxString, std::reference_wrapper< const SIM_LIBRARY > > GetLibraries() const
const SPICE_GENERATOR & SpiceGenerator() const
std::string IbisDevice(const SPICE_ITEM &aItem, SCHEMATIC *aSchematic, const wxString &aCacheDir, REPORTER &aReporter) const
virtual std::string ModelName(const SPICE_ITEM &aItem) const
bool GetExcludedFromSim() const override
void Clear()
Erase the record.
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
const char * c_str() const
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
This file is part of the common library.
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)
must_if< error >::control< Rule > control
void delete_if(_Container &__c, _Function &&__f)
Deletes all values from __c for which __f returns true.
PGM_BASE & Pgm()
The global program "get" accessor.
wxString GetFieldValue(const std::vector< SCH_FIELD > *aFields, FIELD_T aFieldType)
#define SIM_NODES_FORMAT_FIELD
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.
Notes: spaces are allowed everywhere in any number ~ can only be before ? ~~ is not allowed [] can en...
std::vector< SCH_FIELD > fields
std::vector< std::string > pinNetNames
std::string baseModelName
std::vector< std::string > pinNumbers
VECTOR2< int32_t > VECTOR2I