KiCad PCB EDA Suite
Loading...
Searching...
No Matches
netlist_exporter_cadstar.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) 1992-2018 jp.charras at wanadoo.fr
5 * Copyright (C) 2013 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
6 * Copyright (C) 1992-2023, 2024 KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#include <build_version.h>
27#include <confirm.h>
28
29#include <connection_graph.h>
30#include <string_utils.h>
31#include <sch_edit_frame.h>
32#include <sch_reference_list.h>
33
35
36/* Generate CADSTAR net list. */
37static wxString StartLine( wxT( "." ) );
38
39bool NETLIST_EXPORTER_CADSTAR::WriteNetlist( const wxString& aOutFileName,
40 unsigned /* aNetlistOptions */,
41 REPORTER& aReporter )
42{
43 int ret = 0;
44 FILE* f = nullptr;
45
46 if( ( f = wxFopen( aOutFileName, wxT( "wt" ) ) ) == nullptr )
47 {
48 wxString msg = wxString::Format( _( "Failed to create file '%s'." ), aOutFileName );
49 aReporter.Report( msg, RPT_SEVERITY_ERROR );
50 return false;
51 }
52
53 wxString StartCmpDesc = StartLine + wxT( "ADD_COM" );
54 wxString msg;
55 wxString footprint;
56 SCH_SYMBOL* symbol;
57 wxString title = wxT( "Eeschema " ) + GetBuildVersion();
58
59 ret |= fprintf( f, "%sHEA\n", TO_UTF8( StartLine ) );
60 ret |= fprintf( f, "%sTIM %s\n", TO_UTF8( StartLine ), TO_UTF8( GetISO8601CurrentDateTime() ) );
61 ret |= fprintf( f, "%sAPP ", TO_UTF8( StartLine ) );
62 ret |= fprintf( f, "\"%s\"\n", TO_UTF8( title ) );
63 ret |= fprintf( f, ".TYP FULL\n\n" );
64
65 // Create netlist footprints section
67
68 for( const SCH_SHEET_PATH& sheet : m_schematic->Hierarchy() )
69 {
70 // The rtree returns items in a non-deterministic order (platform-dependent)
71 // Therefore we need to sort them before outputting to ensure file stability for version
72 // control and QA comparisons
73 std::vector<EDA_ITEM*> sheetItems;
74
75 for( EDA_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
76 sheetItems.push_back( item );
77
78 auto pred = []( const EDA_ITEM* item1, const EDA_ITEM* item2 )
79 {
80 return item1->m_Uuid < item2->m_Uuid;
81 };
82
83 std::sort( sheetItems.begin(), sheetItems.end(), pred );
84
85 // Process symbol attributes
86 for( EDA_ITEM* item : sheetItems )
87 {
88 symbol = findNextSymbol( item, sheet );
89
90 if( !symbol )
91 continue;
92
93 if( symbol->GetExcludedFromBoard() )
94 continue;
95
96 footprint = symbol->GetFootprintFieldText( true, &sheet, false );
97
98 if( footprint.IsEmpty() )
99 footprint = "$noname";
100
101 msg = symbol->GetRef( &sheet );
102 ret |= fprintf( f, "%s ", TO_UTF8( StartCmpDesc ) );
103 ret |= fprintf( f, "%s", TO_UTF8( msg ) );
104
105 msg = symbol->GetValue( true, &sheet, false );
106 msg.Replace( wxT( " " ), wxT( "_" ) );
107 ret |= fprintf( f, " \"%s\"", TO_UTF8( msg ) );
108 ret |= fprintf( f, " \"%s\"", TO_UTF8( footprint ) );
109 ret |= fprintf( f, "\n" );
110 }
111 }
112
113 ret |= fprintf( f, "\n" );
114
115 if( ! writeListOfNets( f ) )
116 ret = -1; // set error
117
118 ret |= fprintf( f, "\n%sEND\n", TO_UTF8( StartLine ) );
119
120 fclose( f );
121
122 return ret >= 0;
123}
124
125
127{
128 int ret = 0;
129 int print_ter = 0;
130
131 wxString InitNetDesc = StartLine + wxT( "ADD_TER" );
132 wxString StartNetDesc = StartLine + wxT( "TER" );
133 wxString InitNetDescLine;
134
135 std::vector<std::pair<wxString, std::vector<std::pair<SCH_PIN*, SCH_SHEET_PATH>>>> all_nets;
136
137 for( const auto& [ key, subgraphs ] : m_schematic->ConnectionGraph()->GetNetMap() )
138 {
139 wxString netName;
140 netName.Printf( wxT( "\"%s\"" ), key.Name );
141
142 all_nets.emplace_back( netName, std::vector<std::pair<SCH_PIN*, SCH_SHEET_PATH>>{} );
143 std::vector<std::pair<SCH_PIN*, SCH_SHEET_PATH>>& sorted_items = all_nets.back().second;
144
145 for( CONNECTION_SUBGRAPH* subgraph : subgraphs )
146 {
147 SCH_SHEET_PATH sheet = subgraph->GetSheet();
148
149 for( SCH_ITEM* item : subgraph->GetItems() )
150 {
151 if( item->Type() == SCH_PIN_T )
152 sorted_items.emplace_back( static_cast<SCH_PIN*>( item ), sheet );
153 }
154 }
155
156 // Intra-net ordering: Ref des, then pin name
157 std::sort( sorted_items.begin(), sorted_items.end(),
158 []( std::pair<SCH_PIN*, SCH_SHEET_PATH> a, std::pair<SCH_PIN*, SCH_SHEET_PATH> b )
159 {
160 wxString ref_a = a.first->GetParentSymbol()->GetRef( &a.second );
161 wxString ref_b = b.first->GetParentSymbol()->GetRef( &b.second );
162
163 if( ref_a == ref_b )
164 return a.first->GetShownNumber() < b.first->GetShownNumber();
165
166 return ref_a < ref_b;
167 } );
168
169 // Some duplicates can exist, for example on multi-unit parts with duplicated
170 // pins across units. If the user connects the pins on each unit, they will
171 // appear on separate subgraphs. Remove those here:
172 sorted_items.erase( std::unique( sorted_items.begin(), sorted_items.end(),
173 []( std::pair<SCH_PIN*, SCH_SHEET_PATH> a, std::pair<SCH_PIN*, SCH_SHEET_PATH> b )
174 {
175 wxString ref_a = a.first->GetParentSymbol()->GetRef( &a.second );
176 wxString ref_b = b.first->GetParentSymbol()->GetRef( &b.second );
177
178 return ref_a == ref_b && a.first->GetShownNumber() == b.first->GetShownNumber();
179 } ),
180 sorted_items.end() );
181 }
182
183 // Inter-net ordering by net name
184 std::sort( all_nets.begin(), all_nets.end(),
185 []( const auto& a, const auto& b )
186 {
187 return a.first < b.first;
188 } );
189
190 for( const auto& [netName, sorted_items] : all_nets )
191 {
192 print_ter = 0;
193
194 for( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& pair : sorted_items )
195 {
196 SCH_PIN* pin = pair.first;
197 SCH_SHEET_PATH sheet = pair.second;
198
199 wxString refText = pin->GetParentSymbol()->GetRef( &sheet );
200 wxString pinText = pin->GetShownNumber();
201
202 // Skip power symbols and virtual symbols
203 if( refText[0] == wxChar( '#' ) )
204 continue;
205
206 switch( print_ter )
207 {
208 case 0:
209 InitNetDescLine.Printf( wxT( "\n%s %s %.4s %s" ),
210 InitNetDesc,
211 refText,
212 pinText,
213 netName );
214 print_ter++;
215 break;
216
217 case 1:
218 ret |= fprintf( f, "%s\n", TO_UTF8( InitNetDescLine ) );
219 ret |= fprintf( f, "%s %s %.4s\n",
220 TO_UTF8( StartNetDesc ),
221 TO_UTF8( refText ),
222 TO_UTF8( pinText ) );
223 print_ter++;
224 break;
225
226 default:
227 ret |= fprintf( f, " %s %.4s\n",
228 TO_UTF8( refText ),
229 TO_UTF8( pinText ) );
230 break;
231 }
232 }
233 }
234
235 return ret >= 0;
236}
wxString GetBuildVersion()
Get the full KiCad version string.
const NET_MAP & GetNetMap() const
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:89
const KIID m_Uuid
Definition: eda_item.h:489
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 writeListOfNets(FILE *f)
Write a net list (ranked by Netcode), and pins connected to it.
bool WriteNetlist(const wxString &aOutFileName, unsigned aNetlistOptions, REPORTER &aReporter) override
Write to specified output file.
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:72
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 Hierarchy() const =0
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:77
const wxString GetValue(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText) const override
Definition: sch_symbol.cpp:901
const wxString GetFootprintFieldText(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText) const
Definition: sch_symbol.cpp:917
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
Definition: sch_symbol.cpp:731
bool GetExcludedFromBoard() const
Definition: symbol.h:181
void Clear()
Erase the record.
This file is part of the common library.
#define _(s)
static wxString StartLine(wxT("."))
@ RPT_SEVERITY_ERROR
wxString GetISO8601CurrentDateTime()
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:398
@ SCH_SYMBOL_T
Definition: typeinfo.h:172
@ SCH_PIN_T
Definition: typeinfo.h:153