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 The 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 <trace_helpers.h>
31#include <connection_graph.h>
32#include <sch_reference_list.h>
33#include <sch_screen.h>
34#include <schematic.h>
35
36
37// a "less than" test on two LIB_SYMBOLs (.m_name wxStrings)
39 LIB_SYMBOL* const& libsymbol2 ) const
40{
41 // Use case specific GetName() wxString compare
42 return libsymbol1->GetLibId() < libsymbol2->GetLibId();
43}
44
45
46wxString NETLIST_EXPORTER_BASE::MakeCommandLine( const wxString& aFormatString,
47 const wxString& aNetlistFile,
48 const wxString& aFinalFile,
49 const wxString& aProjectPath )
50{
51 // Expand format symbols in the command line:
52 // %B => base filename of selected output file, minus path and extension.
53 // %P => project directory name, without trailing '/' or '\'.
54 // %I => full filename of the input file (the intermediate net file).
55 // %O => complete filename and path (but without extension) of the user chosen output file.
56
57 wxString ret = aFormatString;
58 wxFileName in = aNetlistFile;
59 wxFileName out = aFinalFile;
60 wxString str_out = out.GetFullPath();
61
62 ret.Replace( "%P", aProjectPath, true );
63 ret.Replace( "%B", out.GetName(), true );
64 ret.Replace( "%I", in.GetFullPath(), true );
65
66#ifdef __WINDOWS__
67 // A ugly hack to run xsltproc that has a serious bug on Window since a long time:
68 // the filename given after -o option (output filename) cannot use '\' in filename
69 // so replace if by '/' if possible (I mean if the filename does not start by "\\"
70 // that is a filename on a Windows server)
71
72 if( !str_out.StartsWith( "\\\\" ) )
73 str_out.Replace( "\\", "/" );
74#endif
75
76 ret.Replace( "%O", str_out, true );
77
78 return ret;
79}
80
81
83 const SCH_SHEET_PATH& aSheetPath )
84{
85 wxCHECK( aItem, nullptr );
86
87 wxString ref;
88
89 if( aItem->Type() != SCH_SYMBOL_T )
90 return nullptr;
91
92 // found next symbol
93 SCH_SYMBOL* symbol = (SCH_SYMBOL*) aItem;
94
95 // Power symbols and other symbols which have the reference starting with "#" are not
96 // included in netlist (pseudo or virtual symbols)
97 ref = symbol->GetRef( &aSheetPath );
98
99 if( ref[0] == wxChar( '#' ) )
100 return nullptr;
101
102 SCH_SCREEN* screen = aSheetPath.LastScreen();
103
104 wxCHECK( screen, nullptr );
105
106 auto it = screen->GetLibSymbols().find( symbol->GetSchSymbolLibraryName() );
107
108 if( it == screen->GetLibSymbols().end() )
109 return nullptr;
110
111 LIB_SYMBOL* libSymbol = it->second;
112
113 // If symbol is a "multi parts per package" type
114 if( libSymbol->GetUnitCount() > 1 )
115 {
116 // test if this reference has already been processed, and if so skip
117 if( m_referencesAlreadyFound.Lookup( ref ) )
118 return nullptr;
119 }
120
121 // record the usage of this library symbol entry.
122 m_libParts.insert( libSymbol ); // rejects non-unique pointers
123
124 return symbol;
125}
126
127
128std::vector<PIN_INFO> NETLIST_EXPORTER_BASE::CreatePinList( SCH_SYMBOL* aSymbol,
129 const SCH_SHEET_PATH& aSheetPath,
130 bool aKeepUnconnectedPins )
131{
132 std::vector<PIN_INFO> pins;
133
134 if( !aSymbol )
135 return pins;
136
137 wxString ref( aSymbol->GetRef( &aSheetPath ) );
138
139 // Power symbols and other symbols which have the reference starting with "#" are not
140 // included in netlist (pseudo or virtual symbols)
141 if( ( ref[0] == wxChar( '#' ) ) || aSymbol->IsPower() )
142 return pins;
143
144 // if( aSymbol->m_FlagControlMulti == 1 )
145 // continue; /* yes */
146 // removed because with multiple instances of one schematic (several sheets pointing to
147 // 1 screen), this will be erroneously be toggled.
148
149 if( !aSymbol->GetLibSymbolRef() )
150 return pins;
151
152 // If symbol is a "multi parts per package" type
153 if( aSymbol->GetLibSymbolRef()->GetUnitCount() > 1 )
154 {
155 // Collect all pins for this reference designator by searching the entire design for
156 // other parts with the same reference designator.
157 findAllUnitsOfSymbol( aSymbol, aSheetPath, pins, aKeepUnconnectedPins );
158 wxLogTrace( traceStackedPins, "CreatePinList(multi): ref='%s' pins=%zu", aSymbol->GetRef( &aSheetPath ), pins.size() );
159 }
160
161 else // GetUnitCount() <= 1 means one part per package
162 {
163 CONNECTION_GRAPH* graph = m_schematic->ConnectionGraph();
164
165 for( const SCH_PIN* pin : aSymbol->GetPins( &aSheetPath ) )
166 {
167 if( SCH_CONNECTION* conn = pin->Connection( &aSheetPath ) )
168 {
169 const wxString& netName = conn->Name();
170
171 if( !aKeepUnconnectedPins ) // Skip unconnected pins if requested
172 {
173 CONNECTION_SUBGRAPH* sg = graph->FindSubgraphByName( netName, aSheetPath );
174
175 if( !sg || sg->GetNoConnect() || sg->GetItems().size() < 2 )
176 continue;
177 }
178
179 bool valid;
180 std::vector<wxString> numbers = pin->GetStackedPinNumbers( &valid );
181 wxString baseName = pin->GetShownName();
182 wxLogTrace( traceStackedPins,
183 wxString::Format( "CreatePinList(single): ref='%s' pinNameBase='%s' shownNum='%s' net='%s' "
184 "valid=%d expand=%zu",
185 ref, baseName, pin->GetShownNumber(), netName, valid, numbers.size() ) );
186
187 for( const wxString& num : numbers )
188 {
189 wxString pinName = baseName.IsEmpty() ? num : baseName + wxT( "_" ) + num;
190 wxLogTrace( traceStackedPins,
191 wxString::Format( " -> emit pin num='%s' name='%s' net='%s'", num, pinName, netName ) );
192 pins.emplace_back( num, netName, pinName );
193 }
194 }
195 }
196 }
197
198 // Sort pins in m_SortedSymbolPinList by pin number
199 std::sort( pins.begin(), pins.end(),
200 []( const PIN_INFO& lhs, const PIN_INFO& rhs )
201 {
202 return StrNumCmp( lhs.num, rhs.num, true ) < 0;
203 } );
204
205 // Remove duplicate Pins in m_SortedSymbolPinList
206 eraseDuplicatePins( pins );
207
208 // record the usage of this library symbol
209 m_libParts.insert( aSymbol->GetLibSymbolRef().get() ); // rejects non-unique pointers
210
211 return pins;
212}
213
214
215void NETLIST_EXPORTER_BASE::eraseDuplicatePins( std::vector<PIN_INFO>& aPins )
216{
217 // Helper to check if a net name is auto-generated rather than user-assigned.
218 // Auto-generated nets start with "unconnected-(" for NC pins or "Net-(" for unnamed nets.
219 auto isAutoGeneratedNet = []( const wxString& aNetName ) -> bool
220 {
221 return aNetName.StartsWith( wxT( "unconnected-(" ) )
222 || aNetName.StartsWith( wxT( "Net-(" ) );
223 };
224
225 for( unsigned ii = 0; ii < aPins.size(); ii++ )
226 {
227 if( aPins[ii].num.empty() )
228 continue;
229
230 // Search for duplicated pins and keep only one. Duplicate pin numbers can occur
231 // for multi-unit symbols with shared pins, or for symbols with the "duplicate pin
232 // numbers are jumpers" flag. In either case, all pins with the same number should
233 // connect to the same net. When they don't (user error), we prefer user-assigned
234 // nets over auto-generated ones to preserve user intent in the netlist.
235 // Because the pin list is sorted by pin number, duplicates are consecutive.
236 unsigned idxBest = ii;
237
238 for( unsigned jj = ii + 1; jj < aPins.size(); jj++ )
239 {
240 if( aPins[jj].num.empty() )
241 continue;
242
243 if( aPins[idxBest].num != aPins[jj].num )
244 break;
245
246 // Check if jj has a better (user-assigned) net than the current best.
247 // Prefer user-assigned nets over auto-generated "unconnected-(" or "Net-(" nets.
248 bool bestIsAuto = isAutoGeneratedNet( aPins[idxBest].netName );
249 bool jjIsAuto = isAutoGeneratedNet( aPins[jj].netName );
250
251 if( bestIsAuto && !jjIsAuto )
252 {
253 // jj has a user-assigned net while best has auto-generated; switch to jj
254 aPins[idxBest].num.clear();
255 idxBest = jj;
256 }
257 else
258 {
259 aPins[jj].num.clear();
260 }
261 }
262 }
263}
264
265
267 const SCH_SHEET_PATH& aSheetPath,
268 std::vector<PIN_INFO>& aPins,
269 bool aKeepUnconnectedPins )
270{
271 wxString ref = aSchSymbol->GetRef( &aSheetPath );
272 wxString ref2;
273
274 CONNECTION_GRAPH* graph = m_schematic->ConnectionGraph();
275
276 for( const SCH_SHEET_PATH& sheet : m_schematic->Hierarchy() )
277 {
278 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
279 {
280 SCH_SYMBOL* comp2 = static_cast<SCH_SYMBOL*>( item );
281
282 ref2 = comp2->GetRef( &sheet );
283
284 if( ref2.CmpNoCase( ref ) != 0 )
285 continue;
286
287 for( const SCH_PIN* pin : comp2->GetPins( &sheet ) )
288 {
289 if( SCH_CONNECTION* conn = pin->Connection( &sheet ) )
290 {
291 const wxString& netName = conn->Name();
292
293 if( !aKeepUnconnectedPins ) // Skip unconnected pins if requested
294 {
295 CONNECTION_SUBGRAPH* sg = graph->FindSubgraphByName( netName, sheet );
296
297 if( !sg || sg->GetNoConnect() || sg->GetItems().size() < 2 )
298 continue;
299 }
300
301 bool valid;
302 std::vector<wxString> numbers = pin->GetStackedPinNumbers( &valid );
303 wxString baseName = pin->GetShownName();
304 wxLogTrace( traceStackedPins,
305 wxString::Format( "CreatePinList(multi): ref='%s' pinNameBase='%s' shownNum='%s' net='%s' valid=%d expand=%zu",
306 ref2, baseName, pin->GetShownNumber(), netName, valid, numbers.size() ) );
307
308 for( const wxString& num : numbers )
309 {
310 wxString pinName = baseName.IsEmpty() ? num : baseName + wxT( "_" ) + num;
311 wxLogTrace( traceStackedPins,
312 wxString::Format( " -> emit pin num='%s' name='%s' net='%s'", num, pinName, netName ) );
313 aPins.emplace_back( num, netName, pinName );
314 }
315 }
316 }
317 }
318 }
319}
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:98
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
Define a library symbol object.
Definition lib_symbol.h:83
const LIB_ID & GetLibId() const override
Definition lib_symbol.h:152
int GetUnitCount() const override
SCHEMATIC * m_schematic
The schematic we're generating a netlist for.
std::vector< PIN_INFO > CreatePinList(SCH_SYMBOL *aSymbol, const SCH_SHEET_PATH &aSheetPath, bool aKeepUnconnectedPins)
Find a symbol from the DrawList and builds its pin list.
void findAllUnitsOfSymbol(SCH_SYMBOL *aSchSymbol, const SCH_SHEET_PATH &aSheetPath, std::vector< PIN_INFO > &aPins, bool aKeepUnconnectedPins)
Find all units for symbols with multiple symbols per package.
SCH_SYMBOL * findNextSymbol(EDA_ITEM *aItem, const SCH_SHEET_PATH &aSheetPath)
Check if the given symbol should be processed for netlisting.
std::set< LIB_SYMBOL *, LIB_SYMBOL_LESS_THAN > m_libParts
unique library symbols used. LIB_SYMBOL items are sorted by names
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.
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.
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:167
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:496
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:76
wxString GetSchSymbolLibraryName() const
std::vector< const SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet) const
Retrieve a list of the SCH_PINs for the given sheet path.
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition sch_symbol.h:184
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
bool IsPower() const override
const wxChar *const traceStackedPins
Flag to enable debug output for stacked pins handling in symbol/pin code.
bool operator()(LIB_SYMBOL *const &libsymbol1, LIB_SYMBOL *const &libsymbol2) const
KIBIS_PIN * pin
wxLogTrace helper definitions.
@ SCH_SYMBOL_T
Definition typeinfo.h:176