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( unsigned i = 0; i < sheetList.size(); i++ )
61 {
62 for( SCH_ITEM* item : sheetList[i].LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
63 {
64 symbol = findNextSymbol( item, &sheetList[ i ] );
65
66 if( !symbol )
67 continue;
68
69 if( symbol->GetExcludedFromBoard() )
70 continue;
71
72 footprint = symbol->GetFootprintFieldText( true, &sheetList[ i ], false );
73
74 footprint = footprint.Trim( true );
75 footprint = footprint.Trim( false );
76 footprint.Replace( wxT( " " ), wxT( "_" ) );
77
78 if( footprint.IsEmpty() )
79 {
80 // fall back to value field
81 footprint = symbol->GetValue( true, &sheetList[i], false );
82 footprint.Replace( wxT( " " ), wxT( "_" ) );
83 footprint = footprint.Trim( true );
84 footprint = footprint.Trim( false );
85 }
86
87 msg = symbol->GetRef( &sheetList[i] );
88 ret |= fprintf( f, "%-16s %s\n", TO_UTF8( msg ), TO_UTF8( footprint ) );
89 }
90 }
91
92 ret |= fputs( "\n", f );
93
94 if( !writeListOfNets( f ) )
95 ret = -1; // set error
96
97 fclose( f );
98
99 return ret >= 0;
100}
101
102
104{
105 int ret = 0;
106
107 wxString initialSignalLine;
108 wxString netName;
109
110 ret |= fputs( "*NET*\n", f );
111
112 for( const auto& [ key, subgraphs ] : m_schematic->ConnectionGraph()->GetNetMap() )
113 {
114 netName = key.Name;
115
116 std::vector<std::pair<SCH_PIN*, SCH_SHEET_PATH>> sorted_items;
117
118 for( CONNECTION_SUBGRAPH* subgraph : subgraphs )
119 {
120 SCH_SHEET_PATH sheet = subgraph->GetSheet();
121
122 for( SCH_ITEM* item : subgraph->GetItems() )
123 {
124 if( item->Type() == SCH_PIN_T )
125 sorted_items.emplace_back( static_cast<SCH_PIN*>( item ), sheet );
126 }
127 }
128
129 // Netlist ordering: Net name, then ref des, then pin name
130 std::sort( sorted_items.begin(), sorted_items.end(),
131 []( std::pair<SCH_PIN*, SCH_SHEET_PATH> a, std::pair<SCH_PIN*, SCH_SHEET_PATH> b )
132 {
133 wxString ref_a = a.first->GetParentSymbol()->GetRef( &a.second );
134 wxString ref_b = b.first->GetParentSymbol()->GetRef( &b.second );
135
136 if( ref_a == ref_b )
137 return a.first->GetShownNumber() < b.first->GetShownNumber();
138
139 return ref_a < ref_b;
140 } );
141
142 // Some duplicates can exist, for example on multi-unit parts with duplicated
143 // pins across units. If the user connects the pins on each unit, they will
144 // appear on separate subgraphs. Remove those here:
145 sorted_items.erase( std::unique( sorted_items.begin(), sorted_items.end(),
146 []( std::pair<SCH_PIN*, SCH_SHEET_PATH> a, 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 return ref_a == ref_b && a.first->GetShownNumber() == b.first->GetShownNumber();
152 } ),
153 sorted_items.end() );
154
155 std::vector<wxString> netConns;
156
157 for( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& pair : sorted_items )
158 {
159 SCH_PIN* pin = pair.first;
160 SCH_SHEET_PATH sheet = pair.second;
161
162 wxString refText = pin->GetParentSymbol()->GetRef( &sheet );
163 wxString pinText = pin->GetShownNumber();
164
165 // Skip power symbols and virtual symbols
166 if( refText[0] == wxChar( '#' ) )
167 continue;
168
169 netConns.push_back(
170 wxString::Format( "%s.%.4s", refText, pinText ) );
171 }
172
173 // format it such that there are 6 net connections per line
174 // which seems to be the standard everyone follows
175 if( netConns .size() > 1 )
176 {
177 ret |= fprintf( f, "*SIGNAL* %s\n", TO_UTF8(netName) );
178 int cnt = 0;
179 for( wxString& netConn : netConns )
180 {
181 ret |= fputs( TO_UTF8( netConn ), f );
182 if( cnt != 0 && cnt % 6 == 0 )
183 {
184 ret |= fputc( '\n', f );
185 }
186 else
187 {
188 ret |= fputc( ' ', f );
189 }
190
191 cnt++;
192 }
193
194 ret |= fputc( '\n', f );
195 }
196 }
197
198 ret |= fprintf( f, "*END*\n" );
199
200 return ret >= 0;
201}
const NET_MAP & GetNetMap() const
A subgraph is a set of items that are electrically connected on a single sheet.
UNIQUE_STRINGS m_referencesAlreadyFound
Used for "multiple symbols per package" symbols to avoid processing a lib symbol more than once.
SCH_SYMBOL * findNextSymbol(EDA_ITEM *aItem, SCH_SHEET_PATH *aSheetPath)
Check if the given symbol should be processed for netlisting.
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 GetSheets() const =0
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:174
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
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:880
const wxString GetFootprintFieldText(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText) const
Definition: sch_symbol.cpp:896
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
Definition: sch_symbol.cpp:713
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