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 The 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, see <https://www.gnu.org/licenses/>.
20 */
21
22#include <build_version.h>
23#include <confirm.h>
24
25#include <connection_graph.h>
26#include <string_utils.h>
27#include <sch_edit_frame.h>
28#include <sch_reference_list.h>
29#include <fmt.h>
30#include <system_error>
31
33
34/* Generate CADSTAR net list. */
35static wxString StartLine( wxT( "." ) );
36
37bool NETLIST_EXPORTER_CADSTAR::WriteNetlist( const wxString& aOutFileName,
38 unsigned /* aNetlistOptions */,
39 REPORTER& aReporter )
40{
41 int ret = 0;
42 FILE* f = nullptr;
43
44 if( ( f = wxFopen( aOutFileName, wxT( "wt" ) ) ) == nullptr )
45 {
46 wxString msg = wxString::Format( _( "Failed to create file '%s'." ), aOutFileName );
47 aReporter.Report( msg, RPT_SEVERITY_ERROR );
48 return false;
49 }
50
51 try
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 fmt::print( f, "{}HEA\n", TO_UTF8( StartLine ) );
60 fmt::print( f, "{}TIM {}\n", TO_UTF8( StartLine ), TO_UTF8( GetISO8601CurrentDateTime() ) );
61 fmt::print( f, "{}APP ", TO_UTF8( StartLine ) );
62 fmt::print( f, "\"{}\"\n", TO_UTF8( title ) );
63 fmt::print( 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 fmt::print( f, "{} ", TO_UTF8( StartCmpDesc ) );
103 fmt::print( f, "{}", TO_UTF8( msg ) );
104
105 msg = symbol->GetValue( true, &sheet, false );
106 msg.Replace( wxT( " " ), wxT( "_" ) );
107 fmt::print( f, " \"{}\"", TO_UTF8( msg ) );
108 fmt::print( f, " \"{}\"", TO_UTF8( footprint ) );
109 fmt::print( f, "\n" );
110 }
111 }
112
113 fmt::print( f, "\n" );
114
115 if( ! writeListOfNets( f ) )
116 ret = -1; // set error
117
118 fmt::print( f, "\n{}END\n", TO_UTF8( StartLine ) );
119
120 // Check for file I/O errors
121 if( ferror( f ) )
122 ret = -1;
123 }
124 catch( const std::system_error& e )
125 {
126 aReporter.Report( wxString::Format( _( "I/O error writing netlist: %s" ), e.what() ), RPT_SEVERITY_ERROR );
127 ret = -1;
128 }
129 catch( const fmt::format_error& e )
130 {
131 aReporter.Report( wxString::Format( _( "Formatting error writing netlist: %s" ), e.what() ), RPT_SEVERITY_ERROR );
132 ret = -1;
133 }
134
135 fclose( f );
136
137 return ret >= 0;
138}
139
140
142{
143 try
144 {
145 int print_ter = 0;
146
147 wxString InitNetDesc = StartLine + wxT( "ADD_TER" );
148 wxString StartNetDesc = StartLine + wxT( "TER" );
149 wxString InitNetDescLine;
150
151 std::vector<std::pair<wxString, std::vector<std::pair<SCH_PIN*, SCH_SHEET_PATH>>>> all_nets;
152
153 for( const auto& [ key, subgraphs ] : m_schematic->ConnectionGraph()->GetNetMap() )
154 {
155 wxString netName;
156 netName.Printf( wxT( "\"%s\"" ), key.Name );
157
158 all_nets.emplace_back( netName, std::vector<std::pair<SCH_PIN*, SCH_SHEET_PATH>>{} );
159 std::vector<std::pair<SCH_PIN*, SCH_SHEET_PATH>>& sorted_items = all_nets.back().second;
160
161 for( CONNECTION_SUBGRAPH* subgraph : subgraphs )
162 {
163 SCH_SHEET_PATH sheet = subgraph->GetSheet();
164
165 for( SCH_ITEM* item : subgraph->GetItems() )
166 {
167 if( item->Type() == SCH_PIN_T )
168 sorted_items.emplace_back( static_cast<SCH_PIN*>( item ), sheet );
169 }
170 }
171
172 // Intra-net ordering: Ref des, then pin name
173 std::sort( sorted_items.begin(), sorted_items.end(),
174 []( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& a, const std::pair<SCH_PIN*, SCH_SHEET_PATH>& b )
175 {
176 wxString ref_a = a.first->GetParentSymbol()->GetRef( &a.second );
177 wxString ref_b = b.first->GetParentSymbol()->GetRef( &b.second );
178
179 if( ref_a == ref_b )
180 return a.first->GetShownNumber() < b.first->GetShownNumber();
181
182 return ref_a < ref_b;
183 } );
184
185 // Some duplicates can exist, for example on multi-unit parts with duplicated
186 // pins across units. If the user connects the pins on each unit, they will
187 // appear on separate subgraphs. Remove those here:
188 sorted_items.erase( std::unique( sorted_items.begin(), sorted_items.end(),
189 []( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& a, const std::pair<SCH_PIN*, SCH_SHEET_PATH>& b )
190 {
191 wxString ref_a = a.first->GetParentSymbol()->GetRef( &a.second );
192 wxString ref_b = b.first->GetParentSymbol()->GetRef( &b.second );
193
194 return ref_a == ref_b && a.first->GetShownNumber() == b.first->GetShownNumber();
195 } ),
196 sorted_items.end() );
197 }
198
199 // Inter-net ordering by net name
200 std::sort( all_nets.begin(), all_nets.end(),
201 []( const auto& a, const auto& b )
202 {
203 return a.first < b.first;
204 } );
205
206 for( const auto& [netName, sorted_items] : all_nets )
207 {
208 print_ter = 0;
209
210 for( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& pair : sorted_items )
211 {
212 SCH_PIN* pin = pair.first;
213 SCH_SHEET_PATH sheet = pair.second;
214
215 wxString refText = pin->GetParentSymbol()->GetRef( &sheet );
216 wxString pinText = pin->GetShownNumber();
217
218 // Skip power symbols and virtual symbols
219 if( refText[0] == wxChar( '#' ) )
220 continue;
221
222 switch( print_ter )
223 {
224 case 0:
225 InitNetDescLine.Printf( wxT( "\n%s %s %s %s" ),
226 InitNetDesc,
227 refText,
228 pinText,
229 netName );
230 print_ter++;
231 break;
232
233 case 1:
234 fmt::print( f, "{}\n", TO_UTF8( InitNetDescLine ) );
235 fmt::print( f, "{} {} {}\n",
236 TO_UTF8( StartNetDesc ),
237 TO_UTF8( refText ),
238 TO_UTF8( pinText ) );
239 print_ter++;
240 break;
241
242 default:
243 fmt::print( f, " {} {}\n",
244 TO_UTF8( refText ),
245 TO_UTF8( pinText ) );
246 break;
247 }
248 }
249 }
250
251 return ferror( f ) == 0;
252 }
253 catch( const std::system_error& )
254 {
255 return false;
256 }
257 catch( const fmt::format_error& )
258 {
259 return false;
260 }
261}
wxString GetBuildVersion()
Get the full KiCad version string.
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:96
const KIID m_Uuid
Definition eda_item.h:531
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 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:71
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:100
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:162
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:69
const wxString GetValue(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText, const wxString &aVariantName=wxEmptyString) const override
const wxString GetFootprintFieldText(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText, const wxString &aVariantName=wxEmptyString) const
bool GetExcludedFromBoard(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
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.
KIBIS_PIN * pin
@ SCH_SYMBOL_T
Definition typeinfo.h:169
@ SCH_PIN_T
Definition typeinfo.h:150