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 <string_utils.h>
26#include <sch_edit_frame.h>
27#include <sch_reference_list.h>
28#include <fmt.h>
29#include <system_error>
30
32
33/* Generate CADSTAR net list. */
34static wxString StartLine( wxT( "." ) );
35
36bool NETLIST_EXPORTER_CADSTAR::writeNetlist( const wxString& aOutFileName, unsigned /* aNetlistOptions */,
37 REPORTER& aReporter )
38{
39 int ret = 0;
40 FILE* f = nullptr;
41
42 if( ( f = wxFopen( aOutFileName, wxT( "wt" ) ) ) == nullptr )
43 {
44 wxString msg = wxString::Format( _( "Failed to create file '%s'." ), aOutFileName );
45 aReporter.Report( msg, RPT_SEVERITY_ERROR );
46 return false;
47 }
48
49 try
50 {
51 wxString StartCmpDesc = StartLine + wxT( "ADD_COM" );
52 wxString msg;
53 wxString footprint;
54 SCH_SYMBOL* symbol;
55 wxString title = wxT( "Eeschema " ) + GetBuildVersion();
56
57 fmt::print( f, "{}HEA\n", TO_UTF8( StartLine ) );
58 fmt::print( f, "{}TIM {}\n", TO_UTF8( StartLine ), TO_UTF8( GetISO8601CurrentDateTime() ) );
59 fmt::print( f, "{}APP ", TO_UTF8( StartLine ) );
60 fmt::print( f, "\"{}\"\n", TO_UTF8( title ) );
61 fmt::print( f, ".TYP FULL\n\n" );
62
63 // Create netlist footprints section
65
66 for( const SCH_SHEET_PATH& sheet : m_exportSheets )
67 {
68 // The rtree returns items in a non-deterministic order (platform-dependent)
69 // Therefore we need to sort them before outputting to ensure file stability for version
70 // control and QA comparisons
71 std::vector<EDA_ITEM*> sheetItems;
72
73 for( EDA_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
74 sheetItems.push_back( item );
75
76 auto pred =
77 []( const EDA_ITEM* item1, const EDA_ITEM* item2 )
78 {
79 return item1->m_Uuid < item2->m_Uuid;
80 };
81
82 std::sort( sheetItems.begin(), sheetItems.end(), pred );
83
84 // Process symbol attributes
85 for( EDA_ITEM* item : sheetItems )
86 {
87 symbol = findNextSymbol( item, sheet );
88
89 if( !symbol )
90 continue;
91
92 if( symbol->GetExcludedFromBoard( &sheet, m_exportVariant ) )
93 continue;
94
95 footprint = symbol->GetFootprintFieldText( &sheet, RESOLVED, m_exportVariant );
96
97 if( footprint.IsEmpty() )
98 footprint = "$noname";
99
100 msg = symbol->GetRef( &sheet );
101 fmt::print( f, "{} ", TO_UTF8( StartCmpDesc ) );
102 fmt::print( f, "{}", TO_UTF8( msg ) );
103
104 msg = symbol->GetValue( &sheet, RESOLVED, m_exportVariant );
105 msg.Replace( wxT( " " ), wxT( "_" ) );
106 fmt::print( f, " \"{}\"", TO_UTF8( msg ) );
107 fmt::print( f, " \"{}\"", TO_UTF8( footprint ) );
108 fmt::print( f, "\n" );
109 }
110 }
111
112 fmt::print( f, "\n" );
113
114 if( ! writeListOfNets( f ) )
115 ret = -1; // set error
116
117 fmt::print( f, "\n{}END\n", TO_UTF8( StartLine ) );
118
119 // Check for file I/O errors
120 if( ferror( f ) )
121 ret = -1;
122 }
123 catch( const std::system_error& e )
124 {
125 aReporter.Report( wxString::Format( _( "I/O error writing netlist: %s" ), e.what() ), RPT_SEVERITY_ERROR );
126 ret = -1;
127 }
128 catch( const fmt::format_error& e )
129 {
130 aReporter.Report( wxString::Format( _( "Formatting error writing netlist: %s" ), e.what() ),
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 EXPORT_NET& net : m_exportNets )
154 all_nets.emplace_back( wxString::Format( "\"%s\"", net.name ), net.pins );
155
156 for( auto& [netName, sorted_items] : all_nets )
157 {
158 // Intra-net ordering: Ref des, then pin name
159 std::sort( sorted_items.begin(), sorted_items.end(),
160 []( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& a, const std::pair<SCH_PIN*, SCH_SHEET_PATH>& b )
161 {
162 wxString ref_a = a.first->GetParentSymbol()->GetRef( &a.second );
163 wxString ref_b = b.first->GetParentSymbol()->GetRef( &b.second );
164
165 if( ref_a == ref_b )
166 return a.first->GetShownNumber() < b.first->GetShownNumber();
167
168 return ref_a < ref_b;
169 } );
170
171 // Some duplicates can exist, for example on multi-unit parts with duplicated
172 // pins across units. If the user connects the pins on each unit, they will
173 // appear on separate subgraphs. Remove those here:
174 sorted_items.erase( std::unique( sorted_items.begin(), sorted_items.end(),
175 []( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& a, const std::pair<SCH_PIN*, SCH_SHEET_PATH>& b )
176 {
177 wxString ref_a = a.first->GetParentSymbol()->GetRef( &a.second );
178 wxString ref_b = b.first->GetParentSymbol()->GetRef( &b.second );
179
180 return ref_a == ref_b && a.first->GetShownNumber() == b.first->GetShownNumber();
181 } ),
182 sorted_items.end() );
183 }
184
185 // Inter-net ordering by net name
186 std::sort( all_nets.begin(), all_nets.end(),
187 []( const auto& a, const auto& b )
188 {
189 return a.first < b.first;
190 } );
191
192 for( const auto& [netName, sorted_items] : all_nets )
193 {
194 print_ter = 0;
195
196 for( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& pair : sorted_items )
197 {
198 SCH_PIN* pin = pair.first;
199 SCH_SHEET_PATH sheet = pair.second;
200
201 wxString refText = pin->GetParentSymbol()->GetRef( &sheet );
202 wxString pinText = pin->GetShownNumber();
203
204 // Skip power symbols and virtual symbols
205 if( refText[0] == wxChar( '#' ) )
206 continue;
207
208 switch( print_ter )
209 {
210 case 0:
211 InitNetDescLine.Printf( wxT( "\n%s %s %s %s" ),
212 InitNetDesc,
213 refText,
214 pinText,
215 netName );
216 print_ter++;
217 break;
218
219 case 1:
220 fmt::print( f, "{}\n", TO_UTF8( InitNetDescLine ) );
221 fmt::print( f, "{} {} {}\n",
222 TO_UTF8( StartNetDesc ),
223 TO_UTF8( refText ),
224 TO_UTF8( pinText ) );
225 print_ter++;
226 break;
227
228 default:
229 fmt::print( f, " {} {}\n",
230 TO_UTF8( refText ),
231 TO_UTF8( pinText ) );
232 break;
233 }
234 }
235 }
236
237 return ferror( f ) == 0;
238 }
239 catch( const std::system_error& )
240 {
241 return false;
242 }
243 catch( const fmt::format_error& )
244 {
245 return false;
246 }
247}
wxString GetBuildVersion()
Get the full KiCad version string.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
const KIID m_Uuid
Definition eda_item.h:599
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.
std::vector< EXPORT_NET > m_exportNets
wxString m_exportVariant
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:73
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:102
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Schematic symbol object.
Definition sch_symbol.h:73
const wxString GetFootprintFieldText(const SCH_SHEET_PATH *aPath, RESOLUTION_CONTEXT aContext, const wxString &aVariantName=wxEmptyString) const
bool GetExcludedFromBoard(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
const wxString GetValue(const SCH_SHEET_PATH *aPath, RESOLUTION_CONTEXT aContext, const wxString &aVariantName=wxEmptyString) const override
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
@ RESOLVED
Definition common.h:95
This file is part of the common library.
#define _(s)
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition idf_helpers.h:37
static wxString StartLine(wxT("."))
@ RPT_SEVERITY_ERROR
wxString GetISO8601CurrentDateTime()
KIBIS_PIN * pin
@ SCH_SYMBOL_T
Definition typeinfo.h:168