KiCad PCB EDA Suite
Loading...
Searching...
No Matches
netlist_exporter_spice_model.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 Mikolaj Wielgus.
5 * Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
26#include <sch_screen.h>
27#include <sch_label.h>
28#include <string_utils.h>
29
30
32 unsigned aNetlistOptions )
33{
34 aFormatter.Print( 0, "*\n" );
35 aFormatter.Print( 0, "\n" );
36 aFormatter.Print( 0, ".subckt %s\n", TO_UTF8( m_schematic->Prj().GetProjectName() ) );
37
38 for( auto const& [key, port] : m_ports )
39 {
40 std::string portDir;
41
42 switch( port.dir )
43 {
44 case L_INPUT: portDir = "input"; break;
45 case L_OUTPUT: portDir = "output"; break;
46 case L_BIDI: portDir = "inout"; break;
47 case L_TRISTATE: portDir = "tristate"; break;
48 case L_UNSPECIFIED: portDir = "passive"; break;
49
50 default:
51 wxFAIL_MSG( "Invalid port direction" );
52 break;
53 }
54
55 aFormatter.Print( 0, "+ %s ; %s\n", port.name.c_str(), portDir.c_str() );
56 }
57
58 aFormatter.Print( 0, "\n\n" );
59}
60
61
63 unsigned aNetlistOptions )
64{
65 aFormatter.Print( 0, "\n.ends\n" );
66}
67
68
70 REPORTER& aReporter )
71{
72 readPorts( aNetlistOptions );
73
74 return NETLIST_EXPORTER_SPICE::ReadSchematicAndLibraries( aNetlistOptions, aReporter );
75}
76
77
78std::string NETLIST_EXPORTER_SPICE_MODEL::GenerateItemPinNetName( const std::string& aNetName,
79 int& aNcCounter ) const
80{
81 std::string netName = aNetName;
82
83 if( m_ports.count( netName ) )
84 netName = m_ports.at( netName ).name;
85
86 return NETLIST_EXPORTER_SPICE::GenerateItemPinNetName( netName, aNcCounter );
87}
88
89
90void NETLIST_EXPORTER_SPICE_MODEL::readPorts( unsigned aNetlistOptions )
91{
92 for( const SCH_SHEET_PATH& sheet : GetSheets( aNetlistOptions ) )
93 {
94 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_HIER_LABEL_T ) )
95 {
96 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( item );
97
98 if( SCH_CONNECTION* conn = label->Connection( &sheet ) )
99 {
100 m_ports.insert( { std::string( conn->Name().ToUTF8() ),
101 PORT_INFO{ std::string( label->GetText().ToUTF8() ),
102 label->GetShape()
103 }
104 } );
105 }
106 }
107 }
108}
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:95
SCHEMATIC_IFACE * m_schematic
The schematic we're generating a netlist for.
void WriteTail(OUTPUTFORMATTER &aFormatter, unsigned aNetlistOptions) override
Write the tail (.end).
std::map< std::string, PORT_INFO > m_ports
std::string GenerateItemPinNetName(const std::string &aNetName, int &aNcCounter) const override
bool ReadSchematicAndLibraries(unsigned aNetlistOptions, REPORTER &aReporter) override
Process the schematic and Spice libraries to create net mapping and a list of SPICE_ITEMs.
void readPorts(unsigned aNetlistOptions)
void WriteHead(OUTPUTFORMATTER &aFormatter, unsigned aNetlistOptions) override
Write the netlist head (title and so on).
SCH_SHEET_LIST GetSheets(unsigned aNetlistOptions=0) const
Return the paths of exported sheets (either all or the current one).
virtual std::string GenerateItemPinNetName(const std::string &aNetName, int &aNcCounter) const
virtual bool ReadSchematicAndLibraries(unsigned aNetlistOptions, REPORTER &aReporter)
Process the schematic and Spice libraries to create net mapping and a list of SPICE_ITEMs.
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
virtual const wxString GetProjectName() const
Return the short name of the project.
Definition: project.cpp:147
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:71
virtual PROJECT & Prj() const =0
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:165
SCH_CONNECTION * Connection(const SCH_SHEET_PATH *aSheet=nullptr) const
Retrieve the connection associated with this object in the given sheet.
Definition: sch_item.cpp:147
LABEL_FLAG_SHAPE GetShape() const
Definition: sch_label.h:167
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
@ L_BIDI
Definition: sch_label.h:94
@ L_TRISTATE
Definition: sch_label.h:95
@ L_UNSPECIFIED
Definition: sch_label.h:96
@ L_OUTPUT
Definition: sch_label.h:93
@ L_INPUT
Definition: sch_label.h:92
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:391
@ SCH_HIER_LABEL_T
Definition: typeinfo.h:157