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 <string_utils.h>
24#include <sch_edit_frame.h>
25#include <sch_reference_list.h>
26#include <fmt.h>
27#include <system_error>
28#include <map>
29
31
32bool NETLIST_EXPORTER_PADS::writeNetlist( const wxString& aOutFileName, unsigned /* aNetlistOptions */,
33 REPORTER& aReporter )
34{
35 int ret = 0;
36 FILE* f = nullptr;
37
38 if( ( f = wxFopen( aOutFileName, wxT( "wt" ) ) ) == nullptr )
39 {
40 wxString msg = wxString::Format( _( "Failed to create file '%s'." ), aOutFileName );
41 aReporter.Report( msg, RPT_SEVERITY_ERROR );
42 return false;
43 }
44
45 wxString msg;
46 wxString footprint;
47 SCH_SYMBOL* symbol;
48
49 try
50 {
51 fmt::print( f, "*PADS-PCB*\n" );
52 fmt::print( f, "*PART*\n" );
53
54 // Create netlist footprints section
56
57 for( const SCH_SHEET_PATH& sheet : m_exportSheets )
58 {
59 // The rtree returns items in a non-deterministic order (platform-dependent)
60 // Therefore we need to sort them before outputting to ensure file stability for version
61 // control and QA comparisons
62 std::vector<EDA_ITEM*> sheetItems;
63
64 for( EDA_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
65 sheetItems.push_back( item );
66
67 auto pred =
68 []( 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( &sheet, m_exportVariant ) )
83 continue;
84
85 footprint = symbol->GetFootprintFieldText( &sheet, RESOLVED, m_exportVariant );
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( &sheet, RESOLVED, m_exportVariant );
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() ),
122 ret = -1;
123 }
124
125 fclose( f );
126
127 return ret >= 0;
128}
129
130
132{
133 try
134 {
135 fmt::print( f, "*NET*\n" );
136
137 // Collect all nets and sort them by name to ensure stable ordering
138 std::vector<std::pair<wxString, std::vector<std::pair<SCH_PIN*, SCH_SHEET_PATH>>>> allNets;
139
140 for( const EXPORT_NET& net : m_exportNets )
141 allNets.emplace_back( net.name, net.pins );
142
143 for( auto& [netName, sortedItems] : allNets )
144 {
145 // Netlist ordering: Net name, then ref des, then pin name (intra-net)
146 std::sort( sortedItems.begin(), sortedItems.end(),
147 []( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& a, const std::pair<SCH_PIN*, SCH_SHEET_PATH>& b )
148 {
149 wxString ref_a = a.first->GetParentSymbol()->GetRef( &a.second );
150 wxString ref_b = b.first->GetParentSymbol()->GetRef( &b.second );
151
152 if( ref_a == ref_b )
153 return a.first->GetShownNumber() < b.first->GetShownNumber();
154
155 return ref_a < ref_b;
156 } );
157
158 // Remove duplicates across subgraphs for multi-unit parts
159 sortedItems.erase( std::unique( 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 return ref_a == ref_b && a.first->GetShownNumber() == b.first->GetShownNumber();
166 } ),
167 sortedItems.end() );
168
169 }
170
171 // Sort nets by name (inter-net ordering) for deterministic output
172 std::sort( allNets.begin(), allNets.end(),
173 []( const auto& a, const auto& b )
174 {
175 return a.first < b.first;
176 } );
177
178 for( const auto& [sortedNetName, sorted_items] : allNets )
179 {
180 std::vector<wxString> netConns;
181
182 for( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& pair : sorted_items )
183 {
184 SCH_PIN* pin = pair.first;
185 SCH_SHEET_PATH sheet = pair.second;
186
187 wxString refText = pin->GetParentSymbol()->GetRef( &sheet );
188 wxString pinText = pin->GetShownNumber();
189
190 // Skip power symbols and virtual symbols
191 if( refText[0] == wxChar( '#' ) )
192 continue;
193
194 netConns.push_back( wxString::Format( "%s.%s", refText, pinText ) );
195 }
196
197 // format it such that there are 6 net connections per line
198 // which seems to be the standard everyone follows
199 if( netConns .size() > 1 )
200 {
201 fmt::print( f, "*SIGNAL* {}\n", TO_UTF8(sortedNetName) );
202 int cnt = 0;
203
204 for( wxString& netConn : netConns )
205 {
206 fmt::print( f, "{}", TO_UTF8( netConn ) );
207
208 if( cnt != 0 && cnt % 6 == 0 )
209 fmt::print( f, "\n" );
210 else
211 fmt::print( f, " " );
212
213 cnt++;
214 }
215
216 fmt::print( f, "\n" );
217 }
218 }
219
220 fmt::print( f, "*END*\n" );
221
222 return ferror( f ) == 0;
223 }
224 catch( const std::system_error& )
225 {
226 return false;
227 }
228 catch( const fmt::format_error& )
229 {
230 return false;
231 }
232}
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
const KIID m_Uuid
Definition eda_item.h:599
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.
std::vector< EXPORT_NET > m_exportNets
wxString m_exportVariant
The schematic we're generating a netlist for.
bool writeListOfNets(FILE *f)
Write a net list (ranked by Netcode), and pins connected to it.
bool writeNetlist(const wxString &aOutFileName, unsigned aNetlistOptions, REPORTER &aReporter) override
Write to specified output file.
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:102
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Schematic symbol object.
Definition sch_symbol.h:75
const wxString GetFootprintFieldText(const SCH_SHEET_PATH *aPath, RESOLUTION_CONTEXT aContext, const wxString &aVariantName=wxEmptyString) const
bool GetExcludedFromBoard(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
const wxString GetValue(const SCH_SHEET_PATH *aPath, RESOLUTION_CONTEXT aContext, const wxString &aVariantName=wxEmptyString) const override
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
@ RESOLVED
Definition common.h:97
This file is part of the common library.
#define _(s)
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition idf_helpers.h:37
@ RPT_SEVERITY_ERROR
KIBIS_PIN * pin
@ SCH_SYMBOL_T
Definition typeinfo.h:168