KiCad PCB EDA Suite
SIM_LIB_MGR Class Reference

#include <sim_lib_mgr.h>

Public Member Functions

 SIM_LIB_MGR (const PROJECT *aPrj, REPORTER *aReporter=nullptr)
 
virtual ~SIM_LIB_MGR ()=default
 
void SetReporter (REPORTER *aReporter)
 
void Clear ()
 
void SetLibrary (const wxString &aLibraryPath)
 
SIM_MODELCreateModel (SIM_MODEL::TYPE aType, const std::vector< LIB_PIN * > &aPins)
 
SIM_MODELCreateModel (const SIM_MODEL *aBaseModel, const std::vector< LIB_PIN * > &aPins)
 
template<typename T >
SIM_MODELCreateModel (const SIM_MODEL *aBaseModel, const std::vector< LIB_PIN * > &aPins, const std::vector< T > &aFields)
 
SIM_LIBRARY::MODEL CreateModel (const SCH_SHEET_PATH *aSheetPath, SCH_SYMBOL &aSymbol)
 
template<typename T >
SIM_LIBRARY::MODEL CreateModel (const std::vector< T > &aFields, const std::vector< LIB_PIN * > &aPins, bool aResolved)
 
template<typename T >
SIM_LIBRARY::MODEL CreateModel (const wxString &aLibraryPath, const std::string &aBaseModelName, const std::vector< T > &aFields, const std::vector< LIB_PIN * > &aPins)
 
void SetModel (int aIndex, std::unique_ptr< SIM_MODEL > aModel)
 
std::map< wxString, std::reference_wrapper< const SIM_LIBRARY > > GetLibraries () const
 
std::vector< std::reference_wrapper< SIM_MODEL > > GetModels () const
 
std::string ResolveEmbeddedLibraryPath (const std::string &aLibPath, const std::string &aRelativeLib)
 

Static Public Member Functions

static wxString ResolveLibraryPath (const wxString &aLibraryPath, const PROJECT *aProject)
 

Private Attributes

const PROJECTm_project
 
REPORTERm_reporter
 
std::map< wxString, std::unique_ptr< SIM_LIBRARY > > m_libraries
 
std::vector< std::unique_ptr< SIM_MODEL > > m_models
 

Detailed Description

Definition at line 40 of file sim_lib_mgr.h.

Constructor & Destructor Documentation

◆ SIM_LIB_MGR()

SIM_LIB_MGR::SIM_LIB_MGR ( const PROJECT aPrj,
REPORTER aReporter = nullptr 
)

Definition at line 42 of file sim_lib_mgr.cpp.

42 :
43 m_project( aPrj ),
44 m_reporter( aReporter )
45{
46}
const PROJECT * m_project
Definition: sim_lib_mgr.h:84
REPORTER * m_reporter
Definition: sim_lib_mgr.h:85

◆ ~SIM_LIB_MGR()

virtual SIM_LIB_MGR::~SIM_LIB_MGR ( )
virtualdefault

Member Function Documentation

◆ Clear()

void SIM_LIB_MGR::Clear ( )

Definition at line 49 of file sim_lib_mgr.cpp.

50{
51 m_libraries.clear();
52 m_models.clear();
53}
std::vector< std::unique_ptr< SIM_MODEL > > m_models
Definition: sim_lib_mgr.h:87
std::map< wxString, std::unique_ptr< SIM_LIBRARY > > m_libraries
Definition: sim_lib_mgr.h:86

References m_libraries, and m_models.

Referenced by SetLibrary().

◆ CreateModel() [1/6]

SIM_LIBRARY::MODEL SIM_LIB_MGR::CreateModel ( const SCH_SHEET_PATH aSheetPath,
SCH_SYMBOL aSymbol 
)

Definition at line 183 of file sim_lib_mgr.cpp.

184{
185 // Note: currently this creates a resolved model (all Kicad variables references are resolved
186 // before building the model).
187 //
188 // That's not what we want if this is ever called from the Simulation Model Editor (or other
189 // editors, but it is what we want if called to generate a netlist or other exported items.
190
191
192 std::vector<SCH_FIELD> fields;
193
194 for( int i = 0; i < aSymbol.GetFieldCount(); ++i )
195 {
196 fields.emplace_back( VECTOR2I(), i, &aSymbol, aSymbol.GetFields()[ i ].GetName() );
197
198 if( i == REFERENCE_FIELD )
199 fields.back().SetText( aSymbol.GetRef( aSheetPath ) );
200 else
201 fields.back().SetText( aSymbol.GetFields()[ i ].GetShownText( 0, false ) );
202 }
203
204 wxString deviceType;
205 wxString modelType;
206 wxString modelParams;
207 wxString pinMap;
208 bool storeInValue = false;
209
210 // Infer RLC and VI models if they aren't specified
212 &deviceType, &modelType, &modelParams, &pinMap ) )
213 {
214 fields.emplace_back( &aSymbol, -1, SIM_DEVICE_TYPE_FIELD );
215 fields.back().SetText( deviceType );
216
217 if( !modelType.IsEmpty() )
218 {
219 fields.emplace_back( &aSymbol, -1, SIM_TYPE_FIELD );
220 fields.back().SetText( modelType );
221 }
222
223 fields.emplace_back( &aSymbol, -1, SIM_PARAMS_FIELD );
224 fields.back().SetText( modelParams );
225
226 fields.emplace_back( &aSymbol, -1, SIM_PINS_FIELD );
227 fields.back().SetText( pinMap );
228
229 storeInValue = true;
230 }
231
232 std::vector<LIB_PIN*> sourcePins = aSymbol.GetAllLibPins();
233
234 std::sort( sourcePins.begin(), sourcePins.end(),
235 []( const LIB_PIN* lhs, const LIB_PIN* rhs )
236 {
237 return StrNumCmp( lhs->GetNumber(), rhs->GetNumber(), true ) < 0;
238 } );
239
240 SIM_LIBRARY::MODEL model = CreateModel( fields, sourcePins, true );
241
242 model.model.SetIsStoredInValue( storeInValue );
243
244 return model;
245}
int GetFieldCount() const
Return the number of fields in this symbol.
Definition: sch_symbol.h:486
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const
Return the reference for the given sheet path.
Definition: sch_symbol.cpp:674
std::vector< LIB_PIN * > GetAllLibPins() const
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly)
Populate a std::vector with SCH_FIELDs.
Definition: sch_symbol.cpp:901
SIM_MODEL & CreateModel(SIM_MODEL::TYPE aType, const std::vector< LIB_PIN * > &aPins)
static bool InferSimModel(T_symbol &aSymbol, std::vector< T_field > *aFields, bool aResolve, SIM_VALUE_GRAMMAR::NOTATION aNotation, wxString *aDeviceType, wxString *aModelType, wxString *aModelParams, wxString *aPinMap)
Definition: sim_model.cpp:1096
void SetIsStoredInValue(bool aIsStoredInValue)
Definition: sim_model.h:491
#define SIM_PINS_FIELD
Definition: sim_model.h:54
#define SIM_DEVICE_TYPE_FIELD
Definition: sim_model.h:52
#define SIM_TYPE_FIELD
Definition: sim_model.h:53
#define SIM_PARAMS_FIELD
Definition: sim_model.h:55
SIM_MODEL & model
Definition: sim_library.h:41
@ REFERENCE_FIELD
Field Reference of part, i.e. "IC21".
VECTOR2< int > VECTOR2I
Definition: vector2d.h:590

References CreateModel(), SCH_SYMBOL::GetAllLibPins(), SCH_SYMBOL::GetFieldCount(), SCH_SYMBOL::GetFields(), SCH_SYMBOL::GetRef(), SIM_MODEL::InferSimModel(), SIM_LIBRARY::MODEL::model, REFERENCE_FIELD, SIM_MODEL::SetIsStoredInValue(), SIM_VALUE_GRAMMAR::SI, SIM_DEVICE_TYPE_FIELD, SIM_PARAMS_FIELD, SIM_PINS_FIELD, and SIM_TYPE_FIELD.

◆ CreateModel() [2/6]

SIM_MODEL & SIM_LIB_MGR::CreateModel ( const SIM_MODEL aBaseModel,
const std::vector< LIB_PIN * > &  aPins 
)

Definition at line 158 of file sim_lib_mgr.cpp.

160{
161 m_models.push_back( SIM_MODEL::Create( aBaseModel, aPins, m_reporter ) );
162 return *m_models.back();
163}
static std::unique_ptr< SIM_MODEL > Create(TYPE aType, const std::vector< LIB_PIN * > &aPins, REPORTER *aReporter)
Definition: sim_model.cpp:446

References SIM_MODEL::Create(), m_models, and m_reporter.

◆ CreateModel() [3/6]

template<typename T >
SIM_MODEL & SIM_LIB_MGR::CreateModel ( const SIM_MODEL aBaseModel,
const std::vector< LIB_PIN * > &  aPins,
const std::vector< T > &  aFields 
)

Definition at line 167 of file sim_lib_mgr.cpp.

170{
171 m_models.push_back( SIM_MODEL::Create( aBaseModel, aPins, aFields, m_reporter ) );
172 return *m_models.back();
173}

References SIM_MODEL::Create(), m_models, and m_reporter.

◆ CreateModel() [4/6]

template<typename T >
SIM_LIBRARY::MODEL SIM_LIB_MGR::CreateModel ( const std::vector< T > &  aFields,
const std::vector< LIB_PIN * > &  aPins,
bool  aResolved 
)

Definition at line 249 of file sim_lib_mgr.cpp.

251{
252 std::string libraryPath = SIM_MODEL::GetFieldValue( &aFields, SIM_LIBRARY::LIBRARY_FIELD );
253 std::string baseModelName = SIM_MODEL::GetFieldValue( &aFields, SIM_LIBRARY::NAME_FIELD );
254
255 if( libraryPath != "" )
256 {
257 return CreateModel( libraryPath, baseModelName, aFields, aPins );
258 }
259 else
260 {
261 m_models.push_back( SIM_MODEL::Create( aFields, aPins, aResolved, m_reporter ) );
262 return { baseModelName, *m_models.back() };
263 }
264}
static constexpr auto LIBRARY_FIELD
Definition: sim_library.h:35
static constexpr auto NAME_FIELD
Definition: sim_library.h:36
static std::string GetFieldValue(const std::vector< T > *aFields, const wxString &aFieldName, bool aResolve=true)
Definition: sim_model.cpp:612

References SIM_MODEL::Create(), CreateModel(), SIM_MODEL::GetFieldValue(), SIM_LIBRARY::LIBRARY_FIELD, m_models, m_reporter, and SIM_LIBRARY::NAME_FIELD.

◆ CreateModel() [5/6]

template<typename T >
SIM_LIBRARY::MODEL SIM_LIB_MGR::CreateModel ( const wxString &  aLibraryPath,
const std::string &  aBaseModelName,
const std::vector< T > &  aFields,
const std::vector< LIB_PIN * > &  aPins 
)

Definition at line 275 of file sim_lib_mgr.cpp.

279{
280 wxString path;
281 wxString msg;
282 SIM_LIBRARY* library = nullptr;
283 SIM_MODEL* baseModel = nullptr;
284 std::string modelName;
285
286 try
287 {
288 path = ResolveLibraryPath( aLibraryPath, m_project );
289
290 std::function<std::string( const std::string&, const std::string& )> f2 =
291 std::bind( &SIM_LIB_MGR::ResolveEmbeddedLibraryPath, this, _1, _2 );
292
293 auto it = m_libraries.try_emplace( path, SIM_LIBRARY::Create( path, m_reporter, &f2 ) ).first;
294 library = &*it->second;
295 }
296 catch( const IO_ERROR& e )
297 {
298 if( m_reporter )
299 {
300 msg.Printf( _( "Error loading simulation model library '%s': %s" ),
301 path,
302 e.What() );
303
305 }
306 }
307
308 if( aBaseModelName == "" )
309 {
310 if( m_reporter )
311 {
312 msg.Printf( _( "Error loading simulation model: no '%s' field" ),
314
316 }
317
318 modelName = _( "unknown" ).ToStdString();
319 }
320 else if( library )
321 {
322 baseModel = library->FindModel( aBaseModelName );
323 modelName = aBaseModelName;
324
325 if( !baseModel )
326 {
327 if( m_reporter )
328 {
329 msg.Printf( _( "Error loading simulation model: could not find base model '%s' "
330 "in library '%s'" ),
331 aBaseModelName,
332 path );
333
335 }
336 }
337 }
338
339 m_models.push_back( SIM_MODEL::Create( baseModel, aPins, aFields, m_reporter ) );
340
341 return { modelName, *m_models.back() };
342}
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:76
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
static std::unique_ptr< SIM_LIBRARY > Create(const wxString &aFilePath, REPORTER *aReporter=nullptr, std::function< std::string(const std::string &, const std::string &)> *aResolver=nullptr)
Read library from a source file (e.g.
Definition: sim_library.cpp:32
std::string ResolveEmbeddedLibraryPath(const std::string &aLibPath, const std::string &aRelativeLib)
Definition: sim_lib_mgr.cpp:91
static wxString ResolveLibraryPath(const wxString &aLibraryPath, const PROJECT *aProject)
Definition: sim_lib_mgr.cpp:56
#define _(s)
@ RPT_SEVERITY_ERROR

References _, SIM_LIBRARY::Create(), SIM_MODEL::Create(), library, m_libraries, m_models, m_project, m_reporter, SIM_LIBRARY::NAME_FIELD, path, REPORTER::Report(), ResolveEmbeddedLibraryPath(), ResolveLibraryPath(), RPT_SEVERITY_ERROR, and IO_ERROR::What().

◆ CreateModel() [6/6]

◆ GetLibraries()

std::map< wxString, std::reference_wrapper< const SIM_LIBRARY > > SIM_LIB_MGR::GetLibraries ( ) const

Definition at line 351 of file sim_lib_mgr.cpp.

352{
353 std::map<wxString, std::reference_wrapper<const SIM_LIBRARY>> libraries;
354
355 for( auto& [path, library] : m_libraries )
356 libraries.try_emplace( path, *library );
357
358 return libraries;
359}

References library, m_libraries, and path.

Referenced by NETLIST_EXPORTER_SPICE::writeIncludes().

◆ GetModels()

std::vector< std::reference_wrapper< SIM_MODEL > > SIM_LIB_MGR::GetModels ( ) const

Definition at line 362 of file sim_lib_mgr.cpp.

363{
364 std::vector<std::reference_wrapper<SIM_MODEL>> models;
365
366 for( const std::unique_ptr<SIM_MODEL>& model : m_models )
367 models.emplace_back( *model );
368
369 return models;
370}

References m_models.

◆ ResolveEmbeddedLibraryPath()

std::string SIM_LIB_MGR::ResolveEmbeddedLibraryPath ( const std::string &  aLibPath,
const std::string &  aRelativeLib 
)

Definition at line 91 of file sim_lib_mgr.cpp.

93{
94 wxFileName testPath( aLibPath );
95 wxString fullPath( aLibPath );
96
97 if( !testPath.IsAbsolute() && !aRelativeLib.empty() )
98 {
99 wxString relLib( aRelativeLib );
100
101 try
102 {
103 relLib = ResolveLibraryPath( relLib, m_project );
104 }
105 catch( ... )
106 {}
107
108 wxFileName fn( relLib );
109
110 testPath.MakeAbsolute( fn.GetPath( true ) );
111 fullPath = testPath.GetFullPath();
112 }
113
114 try
115 {
116 wxFileName fn( fullPath );
117
118 if( !fn.Exists() )
119 fullPath = aLibPath;
120
121 fullPath = ResolveLibraryPath( fullPath, m_project );
122 }
123 catch( ... )
124 {}
125
126 return fullPath.ToStdString();
127}

References m_project, and ResolveLibraryPath().

Referenced by CreateModel(), and SetLibrary().

◆ ResolveLibraryPath()

wxString SIM_LIB_MGR::ResolveLibraryPath ( const wxString &  aLibraryPath,
const PROJECT aProject 
)
static

Definition at line 56 of file sim_lib_mgr.cpp.

57{
58 wxString expandedPath = ExpandEnvVarSubstitutions( aLibraryPath, aProject );
59 wxFileName fn( expandedPath );
60
61 if( fn.IsAbsolute() )
62 return fn.GetFullPath();
63
64 wxFileName projectFn( aProject ? aProject->AbsolutePath( expandedPath ) : expandedPath );
65
66 if( projectFn.Exists() )
67 return projectFn.GetFullPath();
68
69 wxFileName spiceLibFn( expandedPath );
70 wxString spiceLibDir;
71
72 wxGetEnv( wxT( "SPICE_LIB_DIR" ), &spiceLibDir );
73
74 if( !spiceLibDir.IsEmpty() && spiceLibFn.MakeAbsolute( spiceLibDir ) && spiceLibFn.Exists() )
75 return spiceLibFn.GetFullPath();
76
77 if( projectFn.GetFullPath() == spiceLibFn.GetFullPath() )
78 {
79 THROW_IO_ERROR( wxString::Format( _( "Simulation model library not found at '%s'" ),
80 spiceLibFn.GetFullPath() ) );
81 }
82 else
83 {
84 THROW_IO_ERROR( wxString::Format( _( "Simulation model library not found at '%s' or '%s'" ),
85 projectFn.GetFullPath(),
86 spiceLibFn.GetFullPath() ) );
87 }
88}
virtual const wxString AbsolutePath(const wxString &aFileName) const
Fix up aFileName if it is relative to the project's directory to be an absolute path and filename.
Definition: project.cpp:305
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:299
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200

References _, PROJECT::AbsolutePath(), ExpandEnvVarSubstitutions(), Format(), and THROW_IO_ERROR.

Referenced by CreateModel(), SPICE_GENERATOR_KIBIS::IbisDevice(), ResolveEmbeddedLibraryPath(), and SetLibrary().

◆ SetLibrary()

void SIM_LIB_MGR::SetLibrary ( const wxString &  aLibraryPath)

Definition at line 130 of file sim_lib_mgr.cpp.

131{
132 try
133 {
134 wxString path = ResolveLibraryPath( aLibraryPath, m_project );
135
136 std::function<std::string(const std::string&, const std::string&)> f2 =
137 std::bind( &SIM_LIB_MGR::ResolveEmbeddedLibraryPath, this, _1, _2 );
138
139 std::unique_ptr<SIM_LIBRARY> library = SIM_LIBRARY::Create( path, m_reporter, &f2 );
140
141 Clear();
142 m_libraries[path] = std::move( library );
143 }
144 catch( const IO_ERROR& e )
145 {
146 m_reporter->Report( e.What() );
147 }
148}
void Clear()
Definition: sim_lib_mgr.cpp:49

References Clear(), SIM_LIBRARY::Create(), library, m_libraries, m_project, m_reporter, path, REPORTER::Report(), ResolveEmbeddedLibraryPath(), ResolveLibraryPath(), and IO_ERROR::What().

◆ SetModel()

void SIM_LIB_MGR::SetModel ( int  aIndex,
std::unique_ptr< SIM_MODEL aModel 
)

Definition at line 345 of file sim_lib_mgr.cpp.

346{
347 m_models.at( aIndex ) = std::move( aModel );
348}

References m_models.

◆ SetReporter()

void SIM_LIB_MGR::SetReporter ( REPORTER aReporter)
inline

Definition at line 46 of file sim_lib_mgr.h.

46{ m_reporter = aReporter; }

References m_reporter.

Referenced by NETLIST_EXPORTER_SPICE::WriteNetlist().

Member Data Documentation

◆ m_libraries

std::map<wxString, std::unique_ptr<SIM_LIBRARY> > SIM_LIB_MGR::m_libraries
private

Definition at line 86 of file sim_lib_mgr.h.

Referenced by Clear(), CreateModel(), GetLibraries(), and SetLibrary().

◆ m_models

std::vector<std::unique_ptr<SIM_MODEL> > SIM_LIB_MGR::m_models
private

Definition at line 87 of file sim_lib_mgr.h.

Referenced by Clear(), CreateModel(), GetModels(), and SetModel().

◆ m_project

const PROJECT* SIM_LIB_MGR::m_project
private

Definition at line 84 of file sim_lib_mgr.h.

Referenced by CreateModel(), ResolveEmbeddedLibraryPath(), and SetLibrary().

◆ m_reporter

REPORTER* SIM_LIB_MGR::m_reporter
private

Definition at line 85 of file sim_lib_mgr.h.

Referenced by CreateModel(), SetLibrary(), and SetReporter().


The documentation for this class was generated from the following files: