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
27#include <sch_screen.h>
28#include <string_utils.h>
29#include <project.h>
30#include <richio.h>
31
32
34 unsigned aNetlistOptions )
35{
36 aFormatter.Print( 0, "*\n" );
37 aFormatter.Print( 0, "\n" );
38 aFormatter.Print( 0, ".subckt %s\n", TO_UTF8( m_schematic->Prj().GetProjectName() ) );
39
40 for( auto const& [key, port] : m_ports )
41 {
42 wxString portDir;
43
44 switch( port.m_dir )
45 {
46 case L_INPUT: portDir = "input"; break;
47 case L_OUTPUT: portDir = "output"; break;
48 case L_BIDI: portDir = "inout"; break;
49 case L_TRISTATE: portDir = "tristate"; break;
50 case L_UNSPECIFIED: portDir = "passive"; break;
51 default: wxFAIL_MSG( "Invalid port direction" ); break;
52 }
53
54 aFormatter.Print( 0, "+ %s ; %s\n", TO_UTF8( port.m_name ), TO_UTF8( portDir ) );
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
78 int& aNcCounter ) const
79{
80 wxString netName = aNetName;
81
82 if( m_ports.count( netName ) )
83 netName = m_ports.at( netName).m_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 : BuildSheetList( 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 wxString labelText = label->GetShownText( &sheet, false );
100 m_ports.insert( { conn->Name(), PORT_INFO{ labelText, label->GetShape() } } );
101 }
102 }
103 }
104}
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:72
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:816
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:398
@ SCH_HIER_LABEL_T
Definition: typeinfo.h:169