KiCad PCB EDA Suite
Loading...
Searching...
No Matches
netlist_exporter_base.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) 1992-2017 jp.charras at wanadoo.fr
5 * Copyright (C) 2013-2017 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
6 * Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
27
28#include <string_utils.h>
29
30#include <symbol_library.h>
31#include <connection_graph.h>
32#include <sch_reference_list.h>
33#include <sch_screen.h>
34#include <schematic.h>
35
36
37wxString NETLIST_EXPORTER_BASE::MakeCommandLine( const wxString& aFormatString,
38 const wxString& aNetlistFile,
39 const wxString& aFinalFile,
40 const wxString& aProjectPath )
41{
42 // Expand format symbols in the command line:
43 // %B => base filename of selected output file, minus path and extension.
44 // %P => project directory name, without trailing '/' or '\'.
45 // %I => full filename of the input file (the intermediate net file).
46 // %O => complete filename and path (but without extension) of the user chosen output file.
47
48 wxString ret = aFormatString;
49 wxFileName in = aNetlistFile;
50 wxFileName out = aFinalFile;
51 wxString str_out = out.GetFullPath();
52
53 ret.Replace( "%P", aProjectPath, true );
54 ret.Replace( "%B", out.GetName(), true );
55 ret.Replace( "%I", in.GetFullPath(), true );
56
57#ifdef __WINDOWS__
58 // A ugly hack to run xsltproc that has a serious bug on Window since a long time:
59 // the filename given after -o option (output filename) cannot use '\' in filename
60 // so replace if by '/' if possible (I mean if the filename does not start by "\\"
61 // that is a filename on a Windows server)
62
63 if( !str_out.StartsWith( "\\\\" ) )
64 str_out.Replace( "\\", "/" );
65#endif
66
67 ret.Replace( "%O", str_out, true );
68
69 return ret;
70}
71
72
74{
75 wxCHECK( aItem, nullptr );
76 wxCHECK( aSheetPath, nullptr );
77
78 wxString ref;
79
80 if( aItem->Type() != SCH_SYMBOL_T )
81 return nullptr;
82
83 // found next symbol
84 SCH_SYMBOL* symbol = (SCH_SYMBOL*) aItem;
85
86 // Power symbols and other symbols which have the reference starting with "#" are not
87 // included in netlist (pseudo or virtual symbols)
88 ref = symbol->GetRef( aSheetPath );
89
90 if( ref[0] == wxChar( '#' ) )
91 return nullptr;
92
93 SCH_SCREEN* screen = aSheetPath->LastScreen();
94
95 wxCHECK( screen, nullptr );
96
97 auto it = screen->GetLibSymbols().find( symbol->GetSchSymbolLibraryName() );
98
99 if( it == screen->GetLibSymbols().end() )
100 return nullptr;
101
102 LIB_SYMBOL* libSymbol = it->second;
103
104 // If symbol is a "multi parts per package" type
105 if( libSymbol->GetUnitCount() > 1 )
106 {
107 // test if this reference has already been processed, and if so skip
109 return nullptr;
110 }
111
112 // record the usage of this library symbol entry.
113 m_libParts.insert( libSymbol ); // rejects non-unique pointers
114
115 return symbol;
116}
117
118
119std::vector<PIN_INFO> NETLIST_EXPORTER_BASE::CreatePinList( SCH_SYMBOL* aSymbol,
120 SCH_SHEET_PATH* aSheetPath,
121 bool aKeepUnconnectedPins )
122{
123 std::vector<PIN_INFO> pins;
124
125 wxCHECK( aSymbol, pins );
126
127 wxString ref( aSymbol->GetRef( aSheetPath ) );
128
129 // Power symbols and other symbols which have the reference starting with "#" are not
130 // included in netlist (pseudo or virtual symbols)
131 if( ( ref[0] == wxChar( '#' ) ) || aSymbol->IsPower() )
132 return pins;
133
134 // if( aSymbol->m_FlagControlMulti == 1 )
135 // continue; /* yes */
136 // removed because with multiple instances of one schematic (several sheets pointing to
137 // 1 screen), this will be erroneously be toggled.
138
139 if( !aSymbol->GetLibSymbolRef() )
140 return pins;
141
142 // If symbol is a "multi parts per package" type
143 if( aSymbol->GetLibSymbolRef()->GetUnitCount() > 1 )
144 {
145 // Collect all pins for this reference designator by searching the entire design for
146 // other parts with the same reference designator.
147 // This is only done once, it would be too expensive otherwise.
148 findAllUnitsOfSymbol( aSymbol, aSheetPath, pins, aKeepUnconnectedPins );
149 }
150
151 else // GetUnitCount() <= 1 means one part per package
152 {
154
155 for( const SCH_PIN* pin : aSymbol->GetPins( aSheetPath ) )
156 {
157 if( SCH_CONNECTION* conn = pin->Connection( aSheetPath ) )
158 {
159 const wxString& netName = conn->Name();
160
161 if( !aKeepUnconnectedPins ) // Skip unconnected pins if requested
162 {
163 CONNECTION_SUBGRAPH* sg = graph->FindSubgraphByName( netName, *aSheetPath );
164
165 if( !sg || sg->GetNoConnect() || sg->GetItems().size() < 2 )
166 continue;
167 }
168
169 pins.emplace_back( pin->GetShownNumber(), netName );
170 }
171 }
172 }
173
174 // Sort pins in m_SortedSymbolPinList by pin number
175 std::sort( pins.begin(), pins.end(),
176 []( const PIN_INFO& lhs, const PIN_INFO& rhs )
177 {
178 return StrNumCmp( lhs.num, rhs.num, true ) < 0;
179 } );
180
181 // Remove duplicate Pins in m_SortedSymbolPinList
182 eraseDuplicatePins( pins );
183
184 // record the usage of this library symbol
185 m_libParts.insert( aSymbol->GetLibSymbolRef().get() ); // rejects non-unique pointers
186
187 return pins;
188}
189
190
191void NETLIST_EXPORTER_BASE::eraseDuplicatePins( std::vector<PIN_INFO>& aPins )
192{
193 for( unsigned ii = 0; ii < aPins.size(); ii++ )
194 {
195 if( aPins[ii].num.empty() ) /* already deleted */
196 continue;
197
198 /* Search for duplicated pins
199 * If found, remove duplicates. The priority is to keep connected pins
200 * and remove unconnected
201 * - So this allows (for instance when using multi op amps per package
202 * - to connect only one op amp to power
203 * Because the pin list is sorted by m_PinNum value, duplicated pins
204 * are necessary successive in list
205 */
206 int idxref = ii;
207
208 for( unsigned jj = ii + 1; jj < aPins.size(); jj++ )
209 {
210 if( aPins[jj].num.empty() ) // Already removed
211 continue;
212
213 // if other pin num, stop search,
214 // because all pins having the same number are consecutive in list.
215 if( aPins[idxref].num != aPins[jj].num )
216 break;
217
218 aPins[jj].num.clear();
219 }
220 }
221}
222
223
225 SCH_SHEET_PATH* aSheetPath,
226 std::vector<PIN_INFO>& aPins,
227 bool aKeepUnconnectedPins )
228{
229 wxString ref = aSchSymbol->GetRef( aSheetPath );
230 wxString ref2;
231
232 SCH_SHEET_LIST sheetList = m_schematic->GetSheets();
234
235 for( unsigned i = 0; i < sheetList.size(); i++ )
236 {
237 SCH_SHEET_PATH& sheet = sheetList[i];
238
239 for( SCH_ITEM* item : sheetList[i].LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
240 {
241 SCH_SYMBOL* comp2 = static_cast<SCH_SYMBOL*>( item );
242
243 ref2 = comp2->GetRef( &sheet );
244
245 if( ref2.CmpNoCase( ref ) != 0 )
246 continue;
247
248 for( const SCH_PIN* pin : comp2->GetPins( &sheet ) )
249 {
250 if( SCH_CONNECTION* conn = pin->Connection( &sheet ) )
251 {
252 const wxString& netName = conn->Name();
253
254 if( !aKeepUnconnectedPins ) // Skip unconnected pins if requested
255 {
256 CONNECTION_SUBGRAPH* sg = graph->FindSubgraphByName( netName, sheet );
257
258 if( !sg || sg->GetNoConnect() || sg->GetItems().size() < 2 )
259 continue;
260 }
261
262 aPins.emplace_back( pin->GetShownNumber(), netName );
263 }
264 }
265 }
266 }
267}
Calculate the connectivity of a schematic and generates netlists.
CONNECTION_SUBGRAPH * FindSubgraphByName(const wxString &aNetName, const SCH_SHEET_PATH &aPath)
Return the subgraph for a given net name on a given sheet.
A subgraph is a set of items that are electrically connected on a single sheet.
const std::set< SCH_ITEM * > & GetItems() const
Provide a read-only reference to the items in the subgraph.
const SCH_ITEM * GetNoConnect() const
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:97
Define a library symbol object.
Definition: lib_symbol.h:99
int GetUnitCount() const override
For items with units, return the number of units.
void findAllUnitsOfSymbol(SCH_SYMBOL *aSchSymbol, SCH_SHEET_PATH *aSheetPath, std::vector< PIN_INFO > &aPins, bool aKeepUnconnectedPins)
Find all units for symbols with multiple symbols per package.
std::set< LIB_SYMBOL *, LIB_SYMBOL_LESS_THAN > m_libParts
unique library symbols used. LIB_SYMBOL items are sorted by names
std::vector< PIN_INFO > CreatePinList(SCH_SYMBOL *aSymbol, SCH_SHEET_PATH *aSheetPath, bool aKeepUnconnectedPins)
Find a symbol from the DrawList and builds its pin list.
void eraseDuplicatePins(std::vector< PIN_INFO > &pins)
Erase duplicate pins.
UNIQUE_STRINGS m_referencesAlreadyFound
Used for "multiple symbols per package" symbols to avoid processing a lib symbol more than once.
SCH_SYMBOL * findNextSymbol(EDA_ITEM *aItem, SCH_SHEET_PATH *aSheetPath)
Check if the given symbol should be processed for netlisting.
static wxString MakeCommandLine(const wxString &aFormatString, const wxString &aNetlistFile, const wxString &aFinalFile, const wxString &aProjectDirectory)
Build up a string that describes a command line for executing a child process.
SCHEMATIC_IFACE * m_schematic
The schematic we're generating a netlist for.
virtual CONNECTION_GRAPH * ConnectionGraph() const =0
virtual SCH_SHEET_LIST GetSheets() 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:151
const std::map< wxString, LIB_SYMBOL * > & GetLibSymbols() const
Fetch a list of unique LIB_SYMBOL object pointers required to properly render each SCH_SYMBOL in this...
Definition: sch_screen.h:481
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
SCH_SCREEN * LastScreen()
Schematic symbol object.
Definition: sch_symbol.h:81
wxString GetSchSymbolLibraryName() const
Definition: sch_symbol.cpp:293
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const
Return the reference for the given sheet path.
Definition: sch_symbol.cpp:738
std::vector< SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet=nullptr) const
Retrieve a list of the SCH_PINs for the given sheet path.
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition: sch_symbol.h:192
bool IsPower() const
bool Lookup(const wxString &aString)
Definition for symbol library class.
@ SCH_SYMBOL_T
Definition: typeinfo.h:156