KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sim_model_spice_fallback.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) 2022-2023 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * https://www.gnu.org/licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25
26#include <ki_exception.h>
27#include <fmt/format.h>
28
29
30SIM_MODEL_SPICE_FALLBACK::SIM_MODEL_SPICE_FALLBACK( TYPE aType, const std::string& aRawSpiceCode ) :
31 SIM_MODEL_SPICE( aType, std::make_unique<SPICE_GENERATOR_SPICE>( *this ) )
32{
33 // Create the model we *should* have had to copy its parameter list
34 std::unique_ptr<SIM_MODEL> model = SIM_MODEL::Create( aType );
35
36 for( int ii = 0; ii < GetParamCount(); ++ii )
37 AddParam( GetParam( ii ).info );
38
39 m_spiceCode = aRawSpiceCode;
40}
41
42
43void SIM_MODEL_SPICE_FALLBACK::AssignSymbolPinNumberToModelPin( const std::string& aModelPinName,
44 const wxString& aSymbolPinNumber )
45{
46 try
47 {
48 SIM_MODEL::AssignSymbolPinNumberToModelPin( aModelPinName, aSymbolPinNumber );
49 }
50 catch( IO_ERROR& )
51 {
52 // This is a fall-back, so we won't necessarily know the pin names. If we didn't find
53 // it, then just create a new pin.
54 m_modelPins.push_back( { aModelPinName, aSymbolPinNumber } );
55 }
56}
57
58
59std::vector<std::string> SIM_MODEL_SPICE_FALLBACK::GetPinNames() const
60{
61 // If we're a fall-back for a paticular model type, then return its pin names
62 std::unique_ptr<SIM_MODEL> model = SIM_MODEL::Create( GetType() );
63 return model->GetPinNames();
64}
65
66
67int SIM_MODEL_SPICE_FALLBACK::doFindParam( const std::string& aParamName ) const
68{
69 for( int ii = 0; ii < GetParamCount(); ++ii )
70 {
71 const SIM_MODEL::PARAM& param = GetParam( ii );
72
73 if( param.Matches( aParamName ) )
74 return ii;
75 }
76
77 // Look for escaped param names as a second pass (as they're less common)
78 for( int ii = 0; ii < GetParamCount(); ++ii )
79 {
80 const SIM_MODEL::PARAM& param = GetParam( ii );
81
82 if( !param.info.name.ends_with( '_' ) )
83 continue;
84
85 if( param.Matches( aParamName + "_" ) )
86 return ii;
87 }
88
89 return -1;
90}
91
92
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
std::vector< std::string > GetPinNames() const override
int doFindParam(const std::string &aParamName) const override
void AssignSymbolPinNumberToModelPin(const std::string &aModelPinName, const wxString &aSymbolPinNumber) override
SIM_MODEL_SPICE_FALLBACK(TYPE aType, const std::string &aRawSpiceCode="")
std::string m_spiceCode
void AddParam(const PARAM::INFO &aInfo)
Definition: sim_model.cpp:729
std::vector< SIM_MODEL_PIN > m_modelPins
Definition: sim_model.h:539
virtual const PARAM & GetParam(unsigned aParamIndex) const
Definition: sim_model.cpp:789
int GetParamCount() const
Definition: sim_model.h:481
void AssignSymbolPinNumberToModelPin(int aPinIndex, const wxString &aSymbolPinNumber)
Definition: sim_model.cpp:758
static std::unique_ptr< SIM_MODEL > Create(TYPE aType, const std::vector< SCH_PIN * > &aPins, REPORTER &aReporter)
Definition: sim_model.cpp:496
TYPE GetType() const
Definition: sim_model.h:464
STL namespace.
SIM_MODEL::TYPE TYPE
Definition: sim_model.cpp:57
bool Matches(const std::string &aName) const
Definition: sim_model.h:395
const INFO & info
Definition: sim_model.h:401