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 The 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, see <https://www.gnu.org/licenses/>.
19 */
20
22
23#include <sch_screen.h>
24#include <string_utils.h>
25#include <project.h>
26#include <richio.h>
27
28
30 unsigned aNetlistOptions )
31{
32 aFormatter.Print( 0, "*\n" );
33 aFormatter.Print( 0, "\n" );
34 aFormatter.Print( 0, ".subckt %s\n", TO_UTF8( m_schematic->Project().GetProjectName() ) );
35
36 for( auto const& [key, port] : m_ports )
37 {
38 wxString portDir;
39
40 switch( port.m_dir )
41 {
42 case L_INPUT: portDir = "input"; break;
43 case L_OUTPUT: portDir = "output"; break;
44 case L_BIDI: portDir = "inout"; break;
45 case L_TRISTATE: portDir = "tristate"; break;
46 case L_UNSPECIFIED: portDir = "passive"; break;
47 default: wxFAIL_MSG( "Invalid port direction" ); break;
48 }
49
50 aFormatter.Print( 0, "+ %s ; %s\n", TO_UTF8( port.m_name ), TO_UTF8( portDir ) );
51 }
52
53 aFormatter.Print( 0, "\n\n" );
54}
55
56
58 unsigned aNetlistOptions )
59{
60 aFormatter.Print( 0, "\n.ends\n" );
61}
62
63
65 REPORTER& aReporter )
66{
67 readPorts( aNetlistOptions );
68
69 return NETLIST_EXPORTER_SPICE::ReadSchematicAndLibraries( aNetlistOptions, aReporter );
70}
71
72
74 int& aNcCounter ) const
75{
76 wxString netName = aNetName;
77
78 if( m_ports.count( netName ) )
79 netName = m_ports.at( netName).m_name;
80
81 return NETLIST_EXPORTER_SPICE::GenerateItemPinNetName( netName, aNcCounter );
82}
83
84
85void NETLIST_EXPORTER_SPICE_MODEL::readPorts( unsigned aNetlistOptions )
86{
87 for( const SCH_SHEET_PATH& sheet : BuildSheetList( aNetlistOptions ) )
88 {
89 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_HIER_LABEL_T ) )
90 {
91 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( item );
92
93 if( SCH_CONNECTION* conn = label->Connection( &sheet ) )
94 {
95 wxString labelText = label->GetShownText( &sheet, false );
96 m_ports.insert( { conn->Name(), PORT_INFO{ labelText, label->GetShape() } } );
97 }
98 }
99 }
100}
SCHEMATIC * 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:291
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition richio.cpp:422
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:71
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:162
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:487
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const override
LABEL_FLAG_SHAPE GetShape() const
Definition sch_label.h:178
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:100
@ L_TRISTATE
Definition sch_label.h:101
@ L_UNSPECIFIED
Definition sch_label.h:102
@ L_OUTPUT
Definition sch_label.h:99
@ L_INPUT
Definition sch_label.h:98
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
@ SCH_HIER_LABEL_T
Definition typeinfo.h:166