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
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
58 for( const SCH_SHEET_PATH& sheet : m_schematic->Hierarchy() )
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 wxString netName;
105
106 ret |= fputs( "*NET*\n", f );
107
108 for( const auto& [ key, subgraphs ] : m_schematic->ConnectionGraph()->GetNetMap() )
109 {
110 netName = key.Name;
111
112 std::vector<std::pair<SCH_PIN*, SCH_SHEET_PATH>> sorted_items;
113
114 for( CONNECTION_SUBGRAPH* subgraph : subgraphs )
115 {
116 SCH_SHEET_PATH sheet = subgraph->GetSheet();
117
118 for( SCH_ITEM* item : subgraph->GetItems() )
119 {
120 if( item->Type() == SCH_PIN_T )
121 sorted_items.emplace_back( static_cast<SCH_PIN*>( item ), sheet );
122 }
123 }
124
125 // Netlist ordering: Net name, then ref des, then pin name
126 std::sort( sorted_items.begin(), sorted_items.end(),
127 []( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& a, const std::pair<SCH_PIN*, SCH_SHEET_PATH>& b )
128 {
129 wxString ref_a = a.first->GetParentSymbol()->GetRef( &a.second );
130 wxString ref_b = b.first->GetParentSymbol()->GetRef( &b.second );
131
132 if( ref_a == ref_b )
133 return a.first->GetShownNumber() < b.first->GetShownNumber();
134
135 return ref_a < ref_b;
136 } );
137
138 // Some duplicates can exist, for example on multi-unit parts with duplicated
139 // pins across units. If the user connects the pins on each unit, they will
140 // appear on separate subgraphs. Remove those here:
141 sorted_items.erase( std::unique( sorted_items.begin(), sorted_items.end(),
142 []( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& a, const std::pair<SCH_PIN*, SCH_SHEET_PATH>& b )
143 {
144 wxString ref_a = a.first->GetParentSymbol()->GetRef( &a.second );
145 wxString ref_b = b.first->GetParentSymbol()->GetRef( &b.second );
146
147 return ref_a == ref_b && a.first->GetShownNumber() == b.first->GetShownNumber();
148 } ),
149 sorted_items.end() );
150
151 std::vector<wxString> netConns;
152
153 for( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& pair : sorted_items )
154 {
155 SCH_PIN* pin = pair.first;
156 SCH_SHEET_PATH sheet = pair.second;
157
158 wxString refText = pin->GetParentSymbol()->GetRef( &sheet );
159 wxString pinText = pin->GetShownNumber();
160
161 // Skip power symbols and virtual symbols
162 if( refText[0] == wxChar( '#' ) )
163 continue;
164
165 netConns.push_back( wxString::Format( "%s.%.4s", refText, pinText ) );
166 }
167
168 // format it such that there are 6 net connections per line
169 // which seems to be the standard everyone follows
170 if( netConns .size() > 1 )
171 {
172 ret |= fprintf( f, "*SIGNAL* %s\n", TO_UTF8(netName) );
173 int cnt = 0;
174
175 for( wxString& netConn : netConns )
176 {
177 ret |= fputs( TO_UTF8( netConn ), f );
178
179 if( cnt != 0 && cnt % 6 == 0 )
180 ret |= fputc( '\n', f );
181 else
182 ret |= fputc( ' ', f );
183
184 cnt++;
185 }
186
187 ret |= fputc( '\n', f );
188 }
189 }
190
191 ret |= fprintf( f, "*END*\n" );
192
193 return ret >= 0;
194}
const NET_MAP & GetNetMap() const
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
SCH_SHEET_LIST Hierarchy() const
Return the full schematic flattened hierarchical sheet list.
Definition: schematic.cpp:219
CONNECTION_GRAPH * ConnectionGraph() const
Definition: schematic.h:182
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:75
const wxString GetValue(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText) const override
Definition: sch_symbol.cpp:720
const wxString GetFootprintFieldText(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText) const
Definition: sch_symbol.cpp:736
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
Definition: sch_symbol.cpp:550
bool GetExcludedFromBoard() const override
Definition: symbol.h:187
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:429
@ SCH_SYMBOL_T
Definition: typeinfo.h:173
@ SCH_PIN_T
Definition: typeinfo.h:154