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 const EXPORT_EXCLUSION excluded = boardExclusion();
67
68 for( const SCH_SHEET_PATH& sheet : m_exportSheets )
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 =
79 []( const EDA_ITEM* item1, const EDA_ITEM* item2 )
80 {
81 return item1->m_Uuid < item2->m_Uuid;
82 };
83
84 std::sort( sheetItems.begin(), sheetItems.end(), pred );
85
86 // Process symbol attributes
87 for( EDA_ITEM* item : sheetItems )
88 {
89 symbol = findNextSymbol( item, sheet, excluded );
90
91 if( !symbol )
92 continue;
93
94 footprint = symbol->GetFootprintFieldText( &sheet, RESOLVED, m_exportVariant );
95
96 if( footprint.IsEmpty() )
97 footprint = "$noname";
98
99 msg = symbol->GetRef( &sheet );
100 fmt::print( f, "{} ", TO_UTF8( StartCmpDesc ) );
101 fmt::print( f, "{}", TO_UTF8( msg ) );
102
103 msg = symbol->GetValue( &sheet, RESOLVED, m_exportVariant );
104 msg.Replace( wxT( " " ), wxT( "_" ) );
105 fmt::print( f, " \"{}\"", TO_UTF8( msg ) );
106 fmt::print( f, " \"{}\"", TO_UTF8( footprint ) );
107 fmt::print( f, "\n" );
108 }
109 }
110
111 fmt::print( f, "\n" );
112
113 if( ! writeListOfNets( f ) )
114 ret = -1; // set error
115
116 fmt::print( f, "\n{}END\n", TO_UTF8( StartLine ) );
117
118 // Check for file I/O errors
119 if( ferror( f ) )
120 ret = -1;
121 }
122 catch( const std::system_error& e )
123 {
124 aReporter.Report( wxString::Format( _( "I/O error writing netlist: %s" ), e.what() ), RPT_SEVERITY_ERROR );
125 ret = -1;
126 }
127 catch( const fmt::format_error& e )
128 {
129 aReporter.Report( wxString::Format( _( "Formatting error writing netlist: %s" ), e.what() ),
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:599
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
EXPORT_EXCLUSION boardExclusion() const
wxString m_exportVariant
The schematic we're generating a netlist for.
SCH_SYMBOL * findNextSymbol(EDA_ITEM *aItem, const SCH_SHEET_PATH &aSheetPath, const EXPORT_EXCLUSION &aExcluded=nullptr)
Check if the given symbol should be processed for netlisting.
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:76
const wxString GetFootprintFieldText(const SCH_SHEET_PATH *aPath, RESOLUTION_CONTEXT aContext, const wxString &aVariantName=wxEmptyString) const
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:97
This file is part of the common library.
#define _(s)
std::function< bool(const SCH_SYMBOL *, const SCH_SHEET_PATH &)> EXPORT_EXCLUSION
An abstract class used for the netlist exporters that Eeschema supports.
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