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 (C) 2024 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
33
34bool NETLIST_EXPORTER_PADS::WriteNetlist( const wxString& aOutFileName,
35 unsigned /* aNetlistOptions */,
36 REPORTER& aReporter )
37{
38 int ret = 0;
39 FILE* f = nullptr;
40
41 if( ( f = wxFopen( aOutFileName, wxT( "wt" ) ) ) == nullptr )
42 {
43 wxString msg = wxString::Format( _( "Failed to create file '%s'." ), aOutFileName );
44 aReporter.Report( msg, RPT_SEVERITY_ERROR );
45 return false;
46 }
47
48 wxString msg;
49 wxString footprint;
50 SCH_SYMBOL* symbol;
51
52 ret |= fputs( "*PADS-PCB*\n", f );
53 ret |= fputs( "*PART*\n", f );
54
55 // Create netlist footprints section
57
59 {
60 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
61 {
62 symbol = findNextSymbol( item, sheet );
63
64 if( !symbol )
65 continue;
66
67 if( symbol->GetExcludedFromBoard() )
68 continue;
69
70 footprint = symbol->GetFootprintFieldText( true, &sheet, false );
71
72 footprint = footprint.Trim( true );
73 footprint = footprint.Trim( false );
74 footprint.Replace( wxT( " " ), wxT( "_" ) );
75
76 if( footprint.IsEmpty() )
77 {
78 // fall back to value field
79 footprint = symbol->GetValue( true, &sheet, false );
80 footprint.Replace( wxT( " " ), wxT( "_" ) );
81 footprint = footprint.Trim( true );
82 footprint = footprint.Trim( false );
83 }
84
85 msg = symbol->GetRef( &sheet );
86 ret |= fprintf( f, "%-16s %s\n", TO_UTF8( msg ), TO_UTF8( footprint ) );
87 }
88 }
89
90 ret |= fputs( "\n", f );
91
92 if( !writeListOfNets( f ) )
93 ret = -1; // set error
94
95 fclose( f );
96
97 return ret >= 0;
98}
99
100
102{
103 int ret = 0;
104
105 wxString initialSignalLine;
106 wxString netName;
107
108 ret |= fputs( "*NET*\n", f );
109
110 for( const auto& [ key, subgraphs ] : m_schematic->ConnectionGraph()->GetNetMap() )
111 {
112 netName = key.Name;
113
114 std::vector<std::pair<SCH_PIN*, SCH_SHEET_PATH>> sorted_items;
115
116 for( CONNECTION_SUBGRAPH* subgraph : subgraphs )
117 {
118 SCH_SHEET_PATH sheet = subgraph->GetSheet();
119
120 for( SCH_ITEM* item : subgraph->GetItems() )
121 {
122 if( item->Type() == SCH_PIN_T )
123 sorted_items.emplace_back( static_cast<SCH_PIN*>( item ), sheet );
124 }
125 }
126
127 // Netlist ordering: Net name, then ref des, then pin name
128 std::sort( sorted_items.begin(), sorted_items.end(),
129 []( std::pair<SCH_PIN*, SCH_SHEET_PATH> a, std::pair<SCH_PIN*, SCH_SHEET_PATH> b )
130 {
131 wxString ref_a = a.first->GetParentSymbol()->GetRef( &a.second );
132 wxString ref_b = b.first->GetParentSymbol()->GetRef( &b.second );
133
134 if( ref_a == ref_b )
135 return a.first->GetShownNumber() < b.first->GetShownNumber();
136
137 return ref_a < ref_b;
138 } );
139
140 // Some duplicates can exist, for example on multi-unit parts with duplicated
141 // pins across units. If the user connects the pins on each unit, they will
142 // appear on separate subgraphs. Remove those here:
143 sorted_items.erase( std::unique( sorted_items.begin(), sorted_items.end(),
144 []( std::pair<SCH_PIN*, SCH_SHEET_PATH> a, std::pair<SCH_PIN*, SCH_SHEET_PATH> b )
145 {
146 wxString ref_a = a.first->GetParentSymbol()->GetRef( &a.second );
147 wxString ref_b = b.first->GetParentSymbol()->GetRef( &b.second );
148
149 return ref_a == ref_b && a.first->GetShownNumber() == b.first->GetShownNumber();
150 } ),
151 sorted_items.end() );
152
153 std::vector<wxString> netConns;
154
155 for( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& pair : sorted_items )
156 {
157 SCH_PIN* pin = pair.first;
158 SCH_SHEET_PATH sheet = pair.second;
159
160 wxString refText = pin->GetParentSymbol()->GetRef( &sheet );
161 wxString pinText = pin->GetShownNumber();
162
163 // Skip power symbols and virtual symbols
164 if( refText[0] == wxChar( '#' ) )
165 continue;
166
167 netConns.push_back(
168 wxString::Format( "%s.%.4s", refText, pinText ) );
169 }
170
171 // format it such that there are 6 net connections per line
172 // which seems to be the standard everyone follows
173 if( netConns .size() > 1 )
174 {
175 ret |= fprintf( f, "*SIGNAL* %s\n", TO_UTF8(netName) );
176 int cnt = 0;
177 for( wxString& netConn : netConns )
178 {
179 ret |= fputs( TO_UTF8( netConn ), f );
180 if( cnt != 0 && cnt % 6 == 0 )
181 {
182 ret |= fputc( '\n', f );
183 }
184 else
185 {
186 ret |= fputc( ' ', f );
187 }
188
189 cnt++;
190 }
191
192 ret |= fputc( '\n', f );
193 }
194 }
195
196 ret |= fprintf( f, "*END*\n" );
197
198 return ret >= 0;
199}
const NET_MAP & GetNetMap() const
A subgraph is a set of items that are electrically connected on a single sheet.
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.
SCHEMATIC_IFACE * m_schematic
The schematic we're generating a netlist for.
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:71
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
virtual CONNECTION_GRAPH * ConnectionGraph() const =0
virtual SCH_SHEET_LIST BuildSheetListSortedByPageNumbers() const =0
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:166
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:105
const wxString GetValue(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText) const override
Definition: sch_symbol.cpp:887
const wxString GetFootprintFieldText(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText) const
Definition: sch_symbol.cpp:903
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
Definition: sch_symbol.cpp:720
bool GetExcludedFromBoard() const
Definition: symbol.h:148
void Clear()
Erase the record.
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.
Definition: string_utils.h:391
@ SCH_SYMBOL_T
Definition: typeinfo.h:172
@ SCH_PIN_T
Definition: typeinfo.h:153