KiCad PCB EDA Suite
Loading...
Searching...
No Matches
netlist_exporter_pads.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <build_version.h>
21#include <confirm.h>
22
23#include <connection_graph.h>
24#include <string_utils.h>
25#include <sch_edit_frame.h>
26#include <sch_reference_list.h>
27#include <fmt.h>
28#include <system_error>
29
31
32bool NETLIST_EXPORTER_PADS::WriteNetlist( const wxString& aOutFileName,
33 unsigned /* aNetlistOptions */,
34 REPORTER& aReporter )
35{
36 int ret = 0;
37 FILE* f = nullptr;
38
39 if( ( f = wxFopen( aOutFileName, wxT( "wt" ) ) ) == nullptr )
40 {
41 wxString msg = wxString::Format( _( "Failed to create file '%s'." ), aOutFileName );
42 aReporter.Report( msg, RPT_SEVERITY_ERROR );
43 return false;
44 }
45
46 wxString msg;
47 wxString footprint;
48 SCH_SYMBOL* symbol;
49
50 try
51 {
52 fmt::print( f, "*PADS-PCB*\n" );
53 fmt::print( f, "*PART*\n" );
54
55 // Create netlist footprints section
57
58 for( const SCH_SHEET_PATH& sheet : m_schematic->Hierarchy() )
59 {
60 // The rtree returns items in a non-deterministic order (platform-dependent)
61 // Therefore we need to sort them before outputting to ensure file stability for version
62 // control and QA comparisons
63 std::vector<EDA_ITEM*> sheetItems;
64
65 for( EDA_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
66 sheetItems.push_back( item );
67
68 auto pred = []( const EDA_ITEM* item1, const EDA_ITEM* item2 )
69 {
70 return item1->m_Uuid < item2->m_Uuid;
71 };
72
73 std::sort( sheetItems.begin(), sheetItems.end(), pred );
74
75 for( EDA_ITEM* item : sheetItems )
76 {
77 symbol = findNextSymbol( item, sheet );
78
79 if( !symbol )
80 continue;
81
82 if( symbol->GetExcludedFromBoard() )
83 continue;
84
85 footprint = symbol->GetFootprintFieldText( true, &sheet, false );
86
87 footprint = footprint.Trim( true );
88 footprint = footprint.Trim( false );
89 footprint.Replace( wxT( " " ), wxT( "_" ) );
90
91 if( footprint.IsEmpty() )
92 {
93 // fall back to value field
94 footprint = symbol->GetValue( true, &sheet, false );
95 footprint.Replace( wxT( " " ), wxT( "_" ) );
96 footprint = footprint.Trim( true );
97 footprint = footprint.Trim( false );
98 }
99
100 msg = symbol->GetRef( &sheet );
101 fmt::print( f, "{:<16} {}\n", TO_UTF8( msg ), TO_UTF8( footprint ) );
102 }
103 }
104
105 fmt::print( f, "\n" );
106
107 if( !writeListOfNets( f ) )
108 ret = -1; // set error
109
110 if( ferror( f ) )
111 ret = -1;
112 }
113 catch( const std::system_error& e )
114 {
115 aReporter.Report( wxString::Format( _( "I/O error writing netlist: %s" ), e.what() ), RPT_SEVERITY_ERROR );
116 ret = -1;
117 }
118 catch( const fmt::format_error& e )
119 {
120 aReporter.Report( wxString::Format( _( "Formatting error writing netlist: %s" ), e.what() ), RPT_SEVERITY_ERROR );
121 ret = -1;
122 }
123
124 fclose( f );
125
126 return ret >= 0;
127}
128
129
131{
132 try
133 {
134 wxString netName;
135
136 fmt::print( f, "*NET*\n" );
137
138 // Collect all nets and sort them by name to ensure stable ordering
139 std::vector<std::pair<wxString, std::vector<std::pair<SCH_PIN*, SCH_SHEET_PATH>>>> allNets;
140
141 for( const auto& [ key, subgraphs ] : m_schematic->ConnectionGraph()->GetNetMap() )
142 {
143 netName = key.Name;
144
145 std::vector<std::pair<SCH_PIN*, SCH_SHEET_PATH>> sortedItems;
146
147 for( CONNECTION_SUBGRAPH* subgraph : subgraphs )
148 {
149 SCH_SHEET_PATH sheet = subgraph->GetSheet();
150
151 for( SCH_ITEM* item : subgraph->GetItems() )
152 {
153 if( item->Type() == SCH_PIN_T )
154 sortedItems.emplace_back( static_cast<SCH_PIN*>( item ), sheet );
155 }
156 }
157
158 // Netlist ordering: Net name, then ref des, then pin name (intra-net)
159 std::sort( sortedItems.begin(), sortedItems.end(),
160 []( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& a, const std::pair<SCH_PIN*, SCH_SHEET_PATH>& b )
161 {
162 wxString ref_a = a.first->GetParentSymbol()->GetRef( &a.second );
163 wxString ref_b = b.first->GetParentSymbol()->GetRef( &b.second );
164
165 if( ref_a == ref_b )
166 return a.first->GetShownNumber() < b.first->GetShownNumber();
167
168 return ref_a < ref_b;
169 } );
170
171 // Remove duplicates across subgraphs for multi-unit parts
172 sortedItems.erase( std::unique( sortedItems.begin(), sortedItems.end(),
173 []( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& a, const std::pair<SCH_PIN*, SCH_SHEET_PATH>& b )
174 {
175 wxString ref_a = a.first->GetParentSymbol()->GetRef( &a.second );
176 wxString ref_b = b.first->GetParentSymbol()->GetRef( &b.second );
177
178 return ref_a == ref_b && a.first->GetShownNumber() == b.first->GetShownNumber();
179 } ),
180 sortedItems.end() );
181
182 allNets.emplace_back( netName, std::move( sortedItems ) );
183 }
184
185 // Sort nets by name (inter-net ordering) for deterministic output
186 std::sort( allNets.begin(), allNets.end(),
187 []( const auto& a, const auto& b )
188 {
189 return a.first < b.first;
190 } );
191
192 for( const auto& [sortedNetName, sorted_items] : allNets )
193 {
194 std::vector<wxString> netConns;
195
196 for( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& pair : sorted_items )
197 {
198 SCH_PIN* pin = pair.first;
199 SCH_SHEET_PATH sheet = pair.second;
200
201 wxString refText = pin->GetParentSymbol()->GetRef( &sheet );
202 wxString pinText = pin->GetShownNumber();
203
204 // Skip power symbols and virtual symbols
205 if( refText[0] == wxChar( '#' ) )
206 continue;
207
208 netConns.push_back( wxString::Format( "%s.%s", refText, pinText ) );
209 }
210
211 // format it such that there are 6 net connections per line
212 // which seems to be the standard everyone follows
213 if( netConns .size() > 1 )
214 {
215 fmt::print( f, "*SIGNAL* {}\n", TO_UTF8(sortedNetName) );
216 int cnt = 0;
217
218 for( wxString& netConn : netConns )
219 {
220 fmt::print( f, "{}", TO_UTF8( netConn ) );
221
222 if( cnt != 0 && cnt % 6 == 0 )
223 fmt::print( f, "\n" );
224 else
225 fmt::print( f, " " );
226
227 cnt++;
228 }
229
230 fmt::print( f, "\n" );
231 }
232 }
233
234 fmt::print( f, "*END*\n" );
235
236 return ferror( f ) == 0;
237 }
238 catch( const std::system_error& )
239 {
240 return false;
241 }
242 catch( const fmt::format_error& )
243 {
244 return false;
245 }
246}
A subgraph is a set of items that are electrically connected on a single sheet.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:96
const KIID m_Uuid
Definition eda_item.h:531
SCHEMATIC * m_schematic
The schematic we're generating a netlist for.
SCH_SYMBOL * findNextSymbol(EDA_ITEM *aItem, const SCH_SHEET_PATH &aSheetPath)
Check if the given symbol should be processed for netlisting.
UNIQUE_STRINGS m_referencesAlreadyFound
Used for "multiple symbols per package" symbols to avoid processing a lib symbol more than once.
bool WriteNetlist(const wxString &aOutFileName, unsigned aNetlistOptions, REPORTER &aReporter) override
Write to specified output file.
bool writeListOfNets(FILE *f)
Write a net list (ranked by Netcode), and pins connected to it.
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:71
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:100
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:162
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
const SCH_SHEET * GetSheet(unsigned aIndex) const
Schematic symbol object.
Definition sch_symbol.h:69
const wxString GetValue(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText, const wxString &aVariantName=wxEmptyString) const override
const wxString GetFootprintFieldText(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText, const wxString &aVariantName=wxEmptyString) const
bool GetExcludedFromBoard(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
This file is part of the common library.
#define _(s)
@ RPT_SEVERITY_ERROR
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
KIBIS_PIN * pin
@ SCH_SYMBOL_T
Definition typeinfo.h:169
@ SCH_PIN_T
Definition typeinfo.h:150