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
29
31 unsigned aNetlistOptions )
32{
33 aFormatter.Print( 0, "*\n" );
34 aFormatter.Print( 0, "\n" );
35 aFormatter.Print( 0, ".subckt %s\n", TO_UTF8( m_schematic->Prj().GetProjectName() ) );
36
37 for( auto const& [key, port] : m_ports )
38 {
39 std::string portDir;
40
41 switch( port.dir )
42 {
43 case L_INPUT: portDir = "input"; break;
44 case L_OUTPUT: portDir = "output"; break;
45 case L_BIDI: portDir = "inout"; break;
46 case L_TRISTATE: portDir = "tristate"; break;
47 case L_UNSPECIFIED: portDir = "passive"; break;
48
49 default:
50 wxFAIL_MSG( "Invalid port direction" );
51 break;
52 }
53
54 aFormatter.Print( 0, "+ %s ; %s\n", port.name.c_str(), portDir.c_str() );
55 }
56
57 aFormatter.Print( 0, "\n\n" );
58}
59
60
62 unsigned aNetlistOptions )
63{
64 aFormatter.Print( 0, "\n.ends\n" );
65}
66
67
69 REPORTER& aReporter )
70{
71 readPorts( aNetlistOptions );
72
73 return NETLIST_EXPORTER_SPICE::ReadSchematicAndLibraries( aNetlistOptions, aReporter );
74}
75
76
77std::string NETLIST_EXPORTER_SPICE_MODEL::GenerateItemPinNetName( const std::string& aNetName,
78 int& aNcCounter ) const
79{
80 std::string netName = aNetName;
81
82 if( m_ports.count( netName ) )
83 netName = m_ports.at( netName ).name;
84
85 return NETLIST_EXPORTER_SPICE::GenerateItemPinNetName( netName, aNcCounter );
86}
87
88
89void NETLIST_EXPORTER_SPICE_MODEL::readPorts( unsigned aNetlistOptions )
90{
91 for( const SCH_SHEET_PATH& sheet : GetSheets( aNetlistOptions ) )
92 {
93 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_HIER_LABEL_T ) )
94 {
95 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( item );
96
97 if( SCH_CONNECTION* conn = label->Connection( &sheet ) )
98 {
99 m_ports.insert( { std::string( conn->Name().ToUTF8() ),
100 PORT_INFO{ std::string( label->GetText().ToUTF8() ),
101 label->GetShape()
102 }
103 } );
104 }
105 }
106 }
107}
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:87
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:475
virtual const wxString GetProjectName() const
Return the short name of the project.
Definition: project.cpp:132
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:147
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 override
Definition: sch_label.h:73
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
@ L_BIDI
Definition: sch_text.h:99
@ L_TRISTATE
Definition: sch_text.h:100
@ L_UNSPECIFIED
Definition: sch_text.h:101
@ L_OUTPUT
Definition: sch_text.h:98
@ L_INPUT
Definition: sch_text.h:97
@ SCH_HIER_LABEL_T
Definition: typeinfo.h:143