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 const EXPORT_EXCLUSION excluded = boardExclusion();
58
59 for( const SCH_SHEET_PATH& sheet : m_exportSheets )
60 {
61 // The rtree returns items in a non-deterministic order (platform-dependent)
62 // Therefore we need to sort them before outputting to ensure file stability for version
63 // control and QA comparisons
64 std::vector<EDA_ITEM*> sheetItems;
65
66 for( EDA_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
67 sheetItems.push_back( item );
68
69 auto pred =
70 []( const EDA_ITEM* item1, const EDA_ITEM* item2 )
71 {
72 return item1->m_Uuid < item2->m_Uuid;
73 };
74
75 std::sort( sheetItems.begin(), sheetItems.end(), pred );
76
77 for( EDA_ITEM* item : sheetItems )
78 {
79 symbol = findNextSymbol( item, sheet, excluded );
80
81 if( !symbol )
82 continue;
83
84 footprint = symbol->GetFootprintFieldText( &sheet, RESOLVED, m_exportVariant );
85
86 footprint = footprint.Trim( true );
87 footprint = footprint.Trim( false );
88 footprint.Replace( wxT( " " ), wxT( "_" ) );
89
90 if( footprint.IsEmpty() )
91 {
92 // fall back to value field
93 footprint = symbol->GetValue( &sheet, RESOLVED, m_exportVariant );
94 footprint.Replace( wxT( " " ), wxT( "_" ) );
95 footprint = footprint.Trim( true );
96 footprint = footprint.Trim( false );
97 }
98
99 msg = symbol->GetRef( &sheet );
100 fmt::print( f, "{:<16} {}\n", TO_UTF8( msg ), TO_UTF8( footprint ) );
101 }
102 }
103
104 fmt::print( f, "\n" );
105
106 if( !writeListOfNets( f ) )
107 ret = -1; // set error
108
109 if( ferror( f ) )
110 ret = -1;
111 }
112 catch( const std::system_error& e )
113 {
114 aReporter.Report( wxString::Format( _( "I/O error writing netlist: %s" ), e.what() ), RPT_SEVERITY_ERROR );
115 ret = -1;
116 }
117 catch( const fmt::format_error& e )
118 {
119 aReporter.Report( wxString::Format( _( "Formatting error writing netlist: %s" ), e.what() ),
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:599
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
EXPORT_EXCLUSION boardExclusion() const
wxString m_exportVariant
The schematic we're generating a netlist for.
SCH_SYMBOL * findNextSymbol(EDA_ITEM *aItem, const SCH_SHEET_PATH &aSheetPath, const EXPORT_EXCLUSION &aExcluded=nullptr)
Check if the given symbol should be processed for netlisting.
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:76
const wxString GetFootprintFieldText(const SCH_SHEET_PATH *aPath, RESOLUTION_CONTEXT aContext, const wxString &aVariantName=wxEmptyString) const
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)
std::function< bool(const SCH_SYMBOL *, const SCH_SHEET_PATH &)> EXPORT_EXCLUSION
An abstract class used for the netlist exporters that Eeschema supports.
@ 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