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,
37 unsigned /* aNetlistOptions */,
38 REPORTER& aReporter )
39{
40 int ret = 0;
41 FILE* f = nullptr;
42
43 if( ( f = wxFopen( aOutFileName, wxT( "wt" ) ) ) == nullptr )
44 {
45 wxString msg = wxString::Format( _( "Failed to create file '%s'." ), aOutFileName );
46 aReporter.Report( msg, RPT_SEVERITY_ERROR );
47 return false;
48 }
49
50 try
51 {
52 wxString StartCmpDesc = StartLine + wxT( "ADD_COM" );
53 wxString msg;
54 wxString footprint;
55 SCH_SYMBOL* symbol;
56 wxString title = wxT( "Eeschema " ) + GetBuildVersion();
57
58 fmt::print( f, "{}HEA\n", TO_UTF8( StartLine ) );
59 fmt::print( f, "{}TIM {}\n", TO_UTF8( StartLine ), TO_UTF8( GetISO8601CurrentDateTime() ) );
60 fmt::print( f, "{}APP ", TO_UTF8( StartLine ) );
61 fmt::print( f, "\"{}\"\n", TO_UTF8( title ) );
62 fmt::print( f, ".TYP FULL\n\n" );
63
64 // Create netlist footprints section
66
67 for( const SCH_SHEET_PATH& sheet : m_exportSheets )
68 {
69 // The rtree returns items in a non-deterministic order (platform-dependent)
70 // Therefore we need to sort them before outputting to ensure file stability for version
71 // control and QA comparisons
72 std::vector<EDA_ITEM*> sheetItems;
73
74 for( EDA_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
75 sheetItems.push_back( item );
76
77 auto pred = []( 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() )
93 continue;
94
95 footprint = symbol->GetFootprintFieldText( &sheet, RESOLVED );
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 );
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() ), RPT_SEVERITY_ERROR );
131 ret = -1;
132 }
133
134 fclose( f );
135
136 return ret >= 0;
137}
138
139
141{
142 try
143 {
144 int print_ter = 0;
145
146 wxString InitNetDesc = StartLine + wxT( "ADD_TER" );
147 wxString StartNetDesc = StartLine + wxT( "TER" );
148 wxString InitNetDescLine;
149
150 std::vector<std::pair<wxString, std::vector<std::pair<SCH_PIN*, SCH_SHEET_PATH>>>> all_nets;
151
152 for( const EXPORT_NET& net : m_exportNets )
153 all_nets.emplace_back( wxString::Format( "\"%s\"", net.name ), net.pins );
154
155 for( auto& [netName, sorted_items] : all_nets )
156 {
157 // Intra-net ordering: Ref des, then pin name
158 std::sort( sorted_items.begin(), sorted_items.end(),
159 []( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& a, const std::pair<SCH_PIN*, SCH_SHEET_PATH>& b )
160 {
161 wxString ref_a = a.first->GetParentSymbol()->GetRef( &a.second );
162 wxString ref_b = b.first->GetParentSymbol()->GetRef( &b.second );
163
164 if( ref_a == ref_b )
165 return a.first->GetShownNumber() < b.first->GetShownNumber();
166
167 return ref_a < ref_b;
168 } );
169
170 // Some duplicates can exist, for example on multi-unit parts with duplicated
171 // pins across units. If the user connects the pins on each unit, they will
172 // appear on separate subgraphs. Remove those here:
173 sorted_items.erase( std::unique( 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 return ref_a == ref_b && a.first->GetShownNumber() == b.first->GetShownNumber();
180 } ),
181 sorted_items.end() );
182 }
183
184 // Inter-net ordering by net name
185 std::sort( all_nets.begin(), all_nets.end(),
186 []( const auto& a, const auto& b )
187 {
188 return a.first < b.first;
189 } );
190
191 for( const auto& [netName, sorted_items] : all_nets )
192 {
193 print_ter = 0;
194
195 for( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& pair : sorted_items )
196 {
197 SCH_PIN* pin = pair.first;
198 SCH_SHEET_PATH sheet = pair.second;
199
200 wxString refText = pin->GetParentSymbol()->GetRef( &sheet );
201 wxString pinText = pin->GetShownNumber();
202
203 // Skip power symbols and virtual symbols
204 if( refText[0] == wxChar( '#' ) )
205 continue;
206
207 switch( print_ter )
208 {
209 case 0:
210 InitNetDescLine.Printf( wxT( "\n%s %s %s %s" ),
211 InitNetDesc,
212 refText,
213 pinText,
214 netName );
215 print_ter++;
216 break;
217
218 case 1:
219 fmt::print( f, "{}\n", TO_UTF8( InitNetDescLine ) );
220 fmt::print( f, "{} {} {}\n",
221 TO_UTF8( StartNetDesc ),
222 TO_UTF8( refText ),
223 TO_UTF8( pinText ) );
224 print_ter++;
225 break;
226
227 default:
228 fmt::print( f, " {} {}\n",
229 TO_UTF8( refText ),
230 TO_UTF8( pinText ) );
231 break;
232 }
233 }
234 }
235
236 return ferror( f ) == 0;
237 }
238 catch( const std::system_error& )
239 {
240 return false;
241 }
242 catch( const fmt::format_error& )
243 {
244 return false;
245 }
246}
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:597
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
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:75
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:93
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:168