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, 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#include <fmt.h>
34#include <system_error>
35
37
38/* Generate CADSTAR net list. */
39static wxString StartLine( wxT( "." ) );
40
41bool NETLIST_EXPORTER_CADSTAR::WriteNetlist( const wxString& aOutFileName,
42 unsigned /* aNetlistOptions */,
43 REPORTER& aReporter )
44{
45 int ret = 0;
46 FILE* f = nullptr;
47
48 if( ( f = wxFopen( aOutFileName, wxT( "wt" ) ) ) == nullptr )
49 {
50 wxString msg = wxString::Format( _( "Failed to create file '%s'." ), aOutFileName );
51 aReporter.Report( msg, RPT_SEVERITY_ERROR );
52 return false;
53 }
54
55 try
56 {
57 wxString StartCmpDesc = StartLine + wxT( "ADD_COM" );
58 wxString msg;
59 wxString footprint;
60 SCH_SYMBOL* symbol;
61 wxString title = wxT( "Eeschema " ) + GetBuildVersion();
62
63 fmt::print( f, "{}HEA\n", TO_UTF8( StartLine ) );
64 fmt::print( f, "{}TIM {}\n", TO_UTF8( StartLine ), TO_UTF8( GetISO8601CurrentDateTime() ) );
65 fmt::print( f, "{}APP ", TO_UTF8( StartLine ) );
66 fmt::print( f, "\"{}\"\n", TO_UTF8( title ) );
67 fmt::print( f, ".TYP FULL\n\n" );
68
69 // Create netlist footprints section
71
72 for( const SCH_SHEET_PATH& sheet : m_schematic->Hierarchy() )
73 {
74 // The rtree returns items in a non-deterministic order (platform-dependent)
75 // Therefore we need to sort them before outputting to ensure file stability for version
76 // control and QA comparisons
77 std::vector<EDA_ITEM*> sheetItems;
78
79 for( EDA_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
80 sheetItems.push_back( item );
81
82 auto pred = []( const EDA_ITEM* item1, const EDA_ITEM* item2 )
83 {
84 return item1->m_Uuid < item2->m_Uuid;
85 };
86
87 std::sort( sheetItems.begin(), sheetItems.end(), pred );
88
89 // Process symbol attributes
90 for( EDA_ITEM* item : sheetItems )
91 {
92 symbol = findNextSymbol( item, sheet );
93
94 if( !symbol )
95 continue;
96
97 if( symbol->GetExcludedFromBoard() )
98 continue;
99
100 footprint = symbol->GetFootprintFieldText( true, &sheet, false );
101
102 if( footprint.IsEmpty() )
103 footprint = "$noname";
104
105 msg = symbol->GetRef( &sheet );
106 fmt::print( f, "{} ", TO_UTF8( StartCmpDesc ) );
107 fmt::print( f, "{}", TO_UTF8( msg ) );
108
109 msg = symbol->GetValue( true, &sheet, false );
110 msg.Replace( wxT( " " ), wxT( "_" ) );
111 fmt::print( f, " \"{}\"", TO_UTF8( msg ) );
112 fmt::print( f, " \"{}\"", TO_UTF8( footprint ) );
113 fmt::print( f, "\n" );
114 }
115 }
116
117 fmt::print( f, "\n" );
118
119 if( ! writeListOfNets( f ) )
120 ret = -1; // set error
121
122 fmt::print( f, "\n{}END\n", TO_UTF8( StartLine ) );
123
124 // Check for file I/O errors
125 if( ferror( f ) )
126 ret = -1;
127 }
128 catch( const std::system_error& e )
129 {
130 aReporter.Report( wxString::Format( _( "I/O error writing netlist: %s" ), e.what() ), RPT_SEVERITY_ERROR );
131 ret = -1;
132 }
133 catch( const fmt::format_error& e )
134 {
135 aReporter.Report( wxString::Format( _( "Formatting error writing netlist: %s" ), e.what() ), RPT_SEVERITY_ERROR );
136 ret = -1;
137 }
138
139 fclose( f );
140
141 return ret >= 0;
142}
143
144
146{
147 try
148 {
149 int print_ter = 0;
150
151 wxString InitNetDesc = StartLine + wxT( "ADD_TER" );
152 wxString StartNetDesc = StartLine + wxT( "TER" );
153 wxString InitNetDescLine;
154
155 std::vector<std::pair<wxString, std::vector<std::pair<SCH_PIN*, SCH_SHEET_PATH>>>> all_nets;
156
157 for( const auto& [ key, subgraphs ] : m_schematic->ConnectionGraph()->GetNetMap() )
158 {
159 wxString netName;
160 netName.Printf( wxT( "\"%s\"" ), key.Name );
161
162 all_nets.emplace_back( netName, std::vector<std::pair<SCH_PIN*, SCH_SHEET_PATH>>{} );
163 std::vector<std::pair<SCH_PIN*, SCH_SHEET_PATH>>& sorted_items = all_nets.back().second;
164
165 for( CONNECTION_SUBGRAPH* subgraph : subgraphs )
166 {
167 SCH_SHEET_PATH sheet = subgraph->GetSheet();
168
169 for( SCH_ITEM* item : subgraph->GetItems() )
170 {
171 if( item->Type() == SCH_PIN_T )
172 sorted_items.emplace_back( static_cast<SCH_PIN*>( item ), sheet );
173 }
174 }
175
176 // Intra-net ordering: Ref des, then pin name
177 std::sort( sorted_items.begin(), sorted_items.end(),
178 []( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& a, const std::pair<SCH_PIN*, SCH_SHEET_PATH>& b )
179 {
180 wxString ref_a = a.first->GetParentSymbol()->GetRef( &a.second );
181 wxString ref_b = b.first->GetParentSymbol()->GetRef( &b.second );
182
183 if( ref_a == ref_b )
184 return a.first->GetShownNumber() < b.first->GetShownNumber();
185
186 return ref_a < ref_b;
187 } );
188
189 // Some duplicates can exist, for example on multi-unit parts with duplicated
190 // pins across units. If the user connects the pins on each unit, they will
191 // appear on separate subgraphs. Remove those here:
192 sorted_items.erase( std::unique( sorted_items.begin(), sorted_items.end(),
193 []( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& a, const std::pair<SCH_PIN*, SCH_SHEET_PATH>& b )
194 {
195 wxString ref_a = a.first->GetParentSymbol()->GetRef( &a.second );
196 wxString ref_b = b.first->GetParentSymbol()->GetRef( &b.second );
197
198 return ref_a == ref_b && a.first->GetShownNumber() == b.first->GetShownNumber();
199 } ),
200 sorted_items.end() );
201 }
202
203 // Inter-net ordering by net name
204 std::sort( all_nets.begin(), all_nets.end(),
205 []( const auto& a, const auto& b )
206 {
207 return a.first < b.first;
208 } );
209
210 for( const auto& [netName, sorted_items] : all_nets )
211 {
212 print_ter = 0;
213
214 for( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& pair : sorted_items )
215 {
216 SCH_PIN* pin = pair.first;
217 SCH_SHEET_PATH sheet = pair.second;
218
219 wxString refText = pin->GetParentSymbol()->GetRef( &sheet );
220 wxString pinText = pin->GetShownNumber();
221
222 // Skip power symbols and virtual symbols
223 if( refText[0] == wxChar( '#' ) )
224 continue;
225
226 switch( print_ter )
227 {
228 case 0:
229 InitNetDescLine.Printf( wxT( "\n%s %s %.4s %s" ),
230 InitNetDesc,
231 refText,
232 pinText,
233 netName );
234 print_ter++;
235 break;
236
237 case 1:
238 fmt::print( f, "{}\n", TO_UTF8( InitNetDescLine ) );
239 fmt::print( f, "{} {} {:.4s}\n",
240 TO_UTF8( StartNetDesc ),
241 TO_UTF8( refText ),
242 TO_UTF8( pinText ) );
243 print_ter++;
244 break;
245
246 default:
247 fmt::print( f, " {} {:.4s}\n",
248 TO_UTF8( refText ),
249 TO_UTF8( pinText ) );
250 break;
251 }
252 }
253 }
254
255 return ferror( f ) == 0;
256 }
257 catch( const std::system_error& )
258 {
259 return false;
260 }
261 catch( const fmt::format_error& )
262 {
263 return false;
264 }
265}
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:98
const KIID m_Uuid
Definition eda_item.h:516
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:73
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:102
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:167
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:76
const wxString GetValue(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText) const override
const wxString GetFootprintFieldText(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText) const
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
bool GetExcludedFromBoard() const override
Definition symbol.h:208
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:176
@ SCH_PIN_T
Definition typeinfo.h:157