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 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
65 {
66 symbol = findNextSymbol( item, sheet );
67
68 if( !symbol )
69 continue;
70
71 if( symbol->GetExcludedFromBoard() )
72 continue;
73
74 footprint = symbol->GetFootprintFieldText( true, &sheet, false );
75
76 footprint = footprint.Trim( true );
77 footprint = footprint.Trim( false );
78 footprint.Replace( wxT( " " ), wxT( "_" ) );
79
80 if( footprint.IsEmpty() )
81 {
82 // fall back to value field
83 footprint = symbol->GetValue( true, &sheet, false );
84 footprint.Replace( wxT( " " ), wxT( "_" ) );
85 footprint = footprint.Trim( true );
86 footprint = footprint.Trim( false );
87 }
88
89 msg = symbol->GetRef( &sheet );
90 fmt::print( f, "{:<16} {}\n", TO_UTF8( msg ), TO_UTF8( footprint ) );
91 }
92 }
93
94 fmt::print( f, "\n" );
95
96 if( !writeListOfNets( f ) )
97 ret = -1; // set error
98
99 if( ferror( f ) )
100 ret = -1;
101 }
102 catch( const std::system_error& e )
103 {
104 aReporter.Report( wxString::Format( _( "I/O error writing netlist: %s" ), e.what() ), RPT_SEVERITY_ERROR );
105 ret = -1;
106 }
107 catch( const fmt::format_error& e )
108 {
109 aReporter.Report( wxString::Format( _( "Formatting error writing netlist: %s" ), e.what() ), RPT_SEVERITY_ERROR );
110 ret = -1;
111 }
112
113 fclose( f );
114
115 return ret >= 0;
116}
117
118
120{
121 try
122 {
123 wxString netName;
124
125 fmt::print( f, "*NET*\n" );
126
127 // Collect all nets and sort them by name to ensure stable ordering
128 std::vector<std::pair<wxString, std::vector<std::pair<SCH_PIN*, SCH_SHEET_PATH>>>> allNets;
129
130 for( const auto& [ key, subgraphs ] : m_schematic->ConnectionGraph()->GetNetMap() )
131 {
132 netName = key.Name;
133
134 std::vector<std::pair<SCH_PIN*, SCH_SHEET_PATH>> sortedItems;
135
136 for( CONNECTION_SUBGRAPH* subgraph : subgraphs )
137 {
138 SCH_SHEET_PATH sheet = subgraph->GetSheet();
139
140 for( SCH_ITEM* item : subgraph->GetItems() )
141 {
142 if( item->Type() == SCH_PIN_T )
143 sortedItems.emplace_back( static_cast<SCH_PIN*>( item ), sheet );
144 }
145 }
146
147 // Netlist ordering: Net name, then ref des, then pin name (intra-net)
148 std::sort( sortedItems.begin(), sortedItems.end(),
149 []( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& a, const std::pair<SCH_PIN*, SCH_SHEET_PATH>& b )
150 {
151 wxString ref_a = a.first->GetParentSymbol()->GetRef( &a.second );
152 wxString ref_b = b.first->GetParentSymbol()->GetRef( &b.second );
153
154 if( ref_a == ref_b )
155 return a.first->GetShownNumber() < b.first->GetShownNumber();
156
157 return ref_a < ref_b;
158 } );
159
160 // Remove duplicates across subgraphs for multi-unit parts
161 sortedItems.erase( std::unique( sortedItems.begin(), sortedItems.end(),
162 []( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& a, const std::pair<SCH_PIN*, SCH_SHEET_PATH>& b )
163 {
164 wxString ref_a = a.first->GetParentSymbol()->GetRef( &a.second );
165 wxString ref_b = b.first->GetParentSymbol()->GetRef( &b.second );
166
167 return ref_a == ref_b && a.first->GetShownNumber() == b.first->GetShownNumber();
168 } ),
169 sortedItems.end() );
170
171 allNets.emplace_back( netName, std::move( sortedItems ) );
172 }
173
174 // Sort nets by name (inter-net ordering) for deterministic output
175 std::sort( allNets.begin(), allNets.end(),
176 []( const auto& a, const auto& b )
177 {
178 return a.first < b.first;
179 } );
180
181 for( const auto& [sortedNetName, sorted_items] : allNets )
182 {
183 std::vector<wxString> netConns;
184
185 for( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& pair : sorted_items )
186 {
187 SCH_PIN* pin = pair.first;
188 SCH_SHEET_PATH sheet = pair.second;
189
190 wxString refText = pin->GetParentSymbol()->GetRef( &sheet );
191 wxString pinText = pin->GetShownNumber();
192
193 // Skip power symbols and virtual symbols
194 if( refText[0] == wxChar( '#' ) )
195 continue;
196
197 netConns.push_back( wxString::Format( "%s.%.4s", refText, pinText ) );
198 }
199
200 // format it such that there are 6 net connections per line
201 // which seems to be the standard everyone follows
202 if( netConns .size() > 1 )
203 {
204 fmt::print( f, "*SIGNAL* {}\n", TO_UTF8(sortedNetName) );
205 int cnt = 0;
206
207 for( wxString& netConn : netConns )
208 {
209 fmt::print( f, "{}", TO_UTF8( netConn ) );
210
211 if( cnt != 0 && cnt % 6 == 0 )
212 fmt::print( f, "\n" );
213 else
214 fmt::print( f, " " );
215
216 cnt++;
217 }
218
219 fmt::print( f, "\n" );
220 }
221 }
222
223 fmt::print( f, "*END*\n" );
224
225 return ferror( f ) == 0;
226 }
227 catch( const std::system_error& )
228 {
229 return false;
230 }
231 catch( const fmt::format_error& )
232 {
233 return false;
234 }
235}
A subgraph is a set of items that are electrically connected on a single sheet.
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:167
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 override
const wxString GetFootprintFieldText(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText) const
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
bool GetExcludedFromBoard() const override
Definition symbol.h:208
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