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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <build_version.h>
25#include <confirm.h>
26
27#include <connection_graph.h>
28#include <string_utils.h>
29#include <sch_edit_frame.h>
30#include <sch_reference_list.h>
31#include <fmt.h>
32#include <system_error>
33
35
36bool NETLIST_EXPORTER_PADS::WriteNetlist( const wxString& aOutFileName,
37 unsigned /* aNetlistOptions */,
38 REPORTER& aReporter )
39{
40 int ret = 0;
41 FILE* f = nullptr;
42
43 if( ( f = wxFopen( aOutFileName, wxT( "wt" ) ) ) == nullptr )
44 {
45 wxString msg = wxString::Format( _( "Failed to create file '%s'." ), aOutFileName );
46 aReporter.Report( msg, RPT_SEVERITY_ERROR );
47 return false;
48 }
49
50 wxString msg;
51 wxString footprint;
52 SCH_SYMBOL* symbol;
53
54 try
55 {
56 fmt::print( f, "*PADS-PCB*\n" );
57 fmt::print( f, "*PART*\n" );
58
59 // Create netlist footprints section
61
62 for( const SCH_SHEET_PATH& sheet : m_schematic->Hierarchy() )
63 {
64 // The rtree returns items in a non-deterministic order (platform-dependent)
65 // Therefore we need to sort them before outputting to ensure file stability for version
66 // control and QA comparisons
67 std::vector<EDA_ITEM*> sheetItems;
68
69 for( EDA_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
70 sheetItems.push_back( item );
71
72 auto pred = []( const EDA_ITEM* item1, const EDA_ITEM* item2 )
73 {
74 return item1->m_Uuid < item2->m_Uuid;
75 };
76
77 std::sort( sheetItems.begin(), sheetItems.end(), pred );
78
79 for( EDA_ITEM* item : sheetItems )
80 {
81 symbol = findNextSymbol( item, sheet );
82
83 if( !symbol )
84 continue;
85
86 if( symbol->GetExcludedFromBoard() )
87 continue;
88
89 footprint = symbol->GetFootprintFieldText( true, &sheet, false );
90
91 footprint = footprint.Trim( true );
92 footprint = footprint.Trim( false );
93 footprint.Replace( wxT( " " ), wxT( "_" ) );
94
95 if( footprint.IsEmpty() )
96 {
97 // fall back to value field
98 footprint = symbol->GetValue( true, &sheet, false );
99 footprint.Replace( wxT( " " ), wxT( "_" ) );
100 footprint = footprint.Trim( true );
101 footprint = footprint.Trim( false );
102 }
103
104 msg = symbol->GetRef( &sheet );
105 fmt::print( f, "{:<16} {}\n", TO_UTF8( msg ), TO_UTF8( footprint ) );
106 }
107 }
108
109 fmt::print( f, "\n" );
110
111 if( !writeListOfNets( f ) )
112 ret = -1; // set error
113
114 if( ferror( f ) )
115 ret = -1;
116 }
117 catch( const std::system_error& e )
118 {
119 aReporter.Report( wxString::Format( _( "I/O error writing netlist: %s" ), e.what() ), RPT_SEVERITY_ERROR );
120 ret = -1;
121 }
122 catch( const fmt::format_error& e )
123 {
124 aReporter.Report( wxString::Format( _( "Formatting error writing netlist: %s" ), e.what() ), RPT_SEVERITY_ERROR );
125 ret = -1;
126 }
127
128 fclose( f );
129
130 return ret >= 0;
131}
132
133
135{
136 try
137 {
138 wxString netName;
139
140 fmt::print( f, "*NET*\n" );
141
142 // Collect all nets and sort them by name to ensure stable ordering
143 std::vector<std::pair<wxString, std::vector<std::pair<SCH_PIN*, SCH_SHEET_PATH>>>> allNets;
144
145 for( const auto& [ key, subgraphs ] : m_schematic->ConnectionGraph()->GetNetMap() )
146 {
147 netName = key.Name;
148
149 std::vector<std::pair<SCH_PIN*, SCH_SHEET_PATH>> sortedItems;
150
151 for( CONNECTION_SUBGRAPH* subgraph : subgraphs )
152 {
153 SCH_SHEET_PATH sheet = subgraph->GetSheet();
154
155 for( SCH_ITEM* item : subgraph->GetItems() )
156 {
157 if( item->Type() == SCH_PIN_T )
158 sortedItems.emplace_back( static_cast<SCH_PIN*>( item ), sheet );
159 }
160 }
161
162 // Netlist ordering: Net name, then ref des, then pin name (intra-net)
163 std::sort( sortedItems.begin(), sortedItems.end(),
164 []( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& a, const std::pair<SCH_PIN*, SCH_SHEET_PATH>& b )
165 {
166 wxString ref_a = a.first->GetParentSymbol()->GetRef( &a.second );
167 wxString ref_b = b.first->GetParentSymbol()->GetRef( &b.second );
168
169 if( ref_a == ref_b )
170 return a.first->GetShownNumber() < b.first->GetShownNumber();
171
172 return ref_a < ref_b;
173 } );
174
175 // Remove duplicates across subgraphs for multi-unit parts
176 sortedItems.erase( std::unique( sortedItems.begin(), sortedItems.end(),
177 []( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& a, const std::pair<SCH_PIN*, SCH_SHEET_PATH>& b )
178 {
179 wxString ref_a = a.first->GetParentSymbol()->GetRef( &a.second );
180 wxString ref_b = b.first->GetParentSymbol()->GetRef( &b.second );
181
182 return ref_a == ref_b && a.first->GetShownNumber() == b.first->GetShownNumber();
183 } ),
184 sortedItems.end() );
185
186 allNets.emplace_back( netName, std::move( sortedItems ) );
187 }
188
189 // Sort nets by name (inter-net ordering) for deterministic output
190 std::sort( allNets.begin(), allNets.end(),
191 []( const auto& a, const auto& b )
192 {
193 return a.first < b.first;
194 } );
195
196 for( const auto& [sortedNetName, sorted_items] : allNets )
197 {
198 std::vector<wxString> netConns;
199
200 for( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& pair : sorted_items )
201 {
202 SCH_PIN* pin = pair.first;
203 SCH_SHEET_PATH sheet = pair.second;
204
205 wxString refText = pin->GetParentSymbol()->GetRef( &sheet );
206 wxString pinText = pin->GetShownNumber();
207
208 // Skip power symbols and virtual symbols
209 if( refText[0] == wxChar( '#' ) )
210 continue;
211
212 netConns.push_back( wxString::Format( "%s.%.4s", refText, pinText ) );
213 }
214
215 // format it such that there are 6 net connections per line
216 // which seems to be the standard everyone follows
217 if( netConns .size() > 1 )
218 {
219 fmt::print( f, "*SIGNAL* {}\n", TO_UTF8(sortedNetName) );
220 int cnt = 0;
221
222 for( wxString& netConn : netConns )
223 {
224 fmt::print( f, "{}", TO_UTF8( netConn ) );
225
226 if( cnt != 0 && cnt % 6 == 0 )
227 fmt::print( f, "\n" );
228 else
229 fmt::print( f, " " );
230
231 cnt++;
232 }
233
234 fmt::print( f, "\n" );
235 }
236 }
237
238 fmt::print( f, "*END*\n" );
239
240 return ferror( f ) == 0;
241 }
242 catch( const std::system_error& )
243 {
244 return false;
245 }
246 catch( const fmt::format_error& )
247 {
248 return false;
249 }
250}
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:99
const KIID m_Uuid
Definition eda_item.h:522
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:73
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:102
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:168
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:76
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:176
@ SCH_PIN_T
Definition typeinfo.h:157