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-2024 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 <string_utils.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 wxString portDir;
40
41 switch( port.m_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 default: wxFAIL_MSG( "Invalid port direction" ); break;
49 }
50
51 aFormatter.Print( 0, "+ %s ; %s\n", TO_UTF8( port.m_name ), TO_UTF8( portDir ) );
52 }
53
54 aFormatter.Print( 0, "\n\n" );
55}
56
57
59 unsigned aNetlistOptions )
60{
61 aFormatter.Print( 0, "\n.ends\n" );
62}
63
64
66 REPORTER& aReporter )
67{
68 readPorts( aNetlistOptions );
69
70 return NETLIST_EXPORTER_SPICE::ReadSchematicAndLibraries( aNetlistOptions, aReporter );
71}
72
73
75 int& aNcCounter ) const
76{
77 wxString netName = aNetName;
78
79 if( m_ports.count( netName ) )
80 netName = m_ports.at( netName).m_name;
81
82 return NETLIST_EXPORTER_SPICE::GenerateItemPinNetName( netName, aNcCounter );
83}
84
85
86void NETLIST_EXPORTER_SPICE_MODEL::readPorts( unsigned aNetlistOptions )
87{
88 for( const SCH_SHEET_PATH& sheet : BuildSheetList( aNetlistOptions ) )
89 {
90 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_HIER_LABEL_T ) )
91 {
92 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( item );
93
94 if( SCH_CONNECTION* conn = label->Connection( &sheet ) )
95 {
96 wxString labelText = label->GetShownText( &sheet, false );
97 m_ports.insert( { conn->Name(), PORT_INFO{ labelText, label->GetShape() } } );
98 }
99 }
100 }
101}
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< wxString, PORT_INFO > m_ports
wxString GenerateItemPinNetName(const wxString &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 BuildSheetList(unsigned aNetlistOptions=0) const
Return the paths of exported sheets (either all or the current one).
virtual wxString GenerateItemPinNetName(const wxString &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:166
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:221
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const override
Definition: sch_label.cpp:867
LABEL_FLAG_SHAPE GetShape() const
Definition: sch_label.h:177
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:101
@ L_TRISTATE
Definition: sch_label.h:102
@ L_UNSPECIFIED
Definition: sch_label.h:103
@ L_OUTPUT
Definition: sch_label.h:100
@ L_INPUT
Definition: sch_label.h:99
#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:169