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,
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_exportSheets )
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( &sheet, RESOLVED );
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 );
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 fmt::print( f, "*NET*\n" );
135
136 // Collect all nets and sort them by name to ensure stable ordering
137 std::vector<std::pair<wxString, std::vector<std::pair<SCH_PIN*, SCH_SHEET_PATH>>>> allNets;
138
139 for( const EXPORT_NET& net : m_exportNets )
140 allNets.emplace_back( net.name, net.pins );
141
142 for( auto& [netName, sortedItems] : allNets )
143 {
144 // Netlist ordering: Net name, then ref des, then pin name (intra-net)
145 std::sort( sortedItems.begin(), sortedItems.end(),
146 []( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& a, const std::pair<SCH_PIN*, SCH_SHEET_PATH>& b )
147 {
148 wxString ref_a = a.first->GetParentSymbol()->GetRef( &a.second );
149 wxString ref_b = b.first->GetParentSymbol()->GetRef( &b.second );
150
151 if( ref_a == ref_b )
152 return a.first->GetShownNumber() < b.first->GetShownNumber();
153
154 return ref_a < ref_b;
155 } );
156
157 // Remove duplicates across subgraphs for multi-unit parts
158 sortedItems.erase( std::unique( sortedItems.begin(), sortedItems.end(),
159 []( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& a, const std::pair<SCH_PIN*, SCH_SHEET_PATH>& b )
160 {
161 wxString ref_a = a.first->GetParentSymbol()->GetRef( &a.second );
162 wxString ref_b = b.first->GetParentSymbol()->GetRef( &b.second );
163
164 return ref_a == ref_b && a.first->GetShownNumber() == b.first->GetShownNumber();
165 } ),
166 sortedItems.end() );
167
168 }
169
170 // Sort nets by name (inter-net ordering) for deterministic output
171 std::sort( allNets.begin(), allNets.end(),
172 []( const auto& a, const auto& b )
173 {
174 return a.first < b.first;
175 } );
176
177 for( const auto& [sortedNetName, sorted_items] : allNets )
178 {
179 std::vector<wxString> netConns;
180
181 for( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& pair : sorted_items )
182 {
183 SCH_PIN* pin = pair.first;
184 SCH_SHEET_PATH sheet = pair.second;
185
186 wxString refText = pin->GetParentSymbol()->GetRef( &sheet );
187 wxString pinText = pin->GetShownNumber();
188
189 // Skip power symbols and virtual symbols
190 if( refText[0] == wxChar( '#' ) )
191 continue;
192
193 netConns.push_back( wxString::Format( "%s.%s", refText, pinText ) );
194 }
195
196 // format it such that there are 6 net connections per line
197 // which seems to be the standard everyone follows
198 if( netConns .size() > 1 )
199 {
200 fmt::print( f, "*SIGNAL* {}\n", TO_UTF8(sortedNetName) );
201 int cnt = 0;
202
203 for( wxString& netConn : netConns )
204 {
205 fmt::print( f, "{}", TO_UTF8( netConn ) );
206
207 if( cnt != 0 && cnt % 6 == 0 )
208 fmt::print( f, "\n" );
209 else
210 fmt::print( f, " " );
211
212 cnt++;
213 }
214
215 fmt::print( f, "\n" );
216 }
217 }
218
219 fmt::print( f, "*END*\n" );
220
221 return ferror( f ) == 0;
222 }
223 catch( const std::system_error& )
224 {
225 return false;
226 }
227 catch( const fmt::format_error& )
228 {
229 return false;
230 }
231}
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:597
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
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:93
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:168