KiCad PCB EDA Suite
Loading...
Searching...
No Matches
netlist_exporter_orcadpcb2.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 <algorithm>
23#include <vector>
24
25#include <confirm.h>
26#include <refdes_utils.h>
27#include <fmt.h>
28#include <system_error>
29
30#include <sch_edit_frame.h>
31#include <sch_reference_list.h>
32#include <string_utils.h>
33
34#include <netlist.h>
36
37
38bool NETLIST_EXPORTER_ORCADPCB2::writeNetlist( const wxString& aOutFileName, unsigned /* aNetlistOptions */,
39 REPORTER& aReporter )
40{
41 FILE* f = nullptr;
42 wxString field;
43 wxString footprint;
44 bool success = true;
45 wxString netName;
46
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 std::vector< SCH_REFERENCE > cmpList;
56
57 try
58 {
59 fmt::print( f, "( {{ {} created {} }}\n",
61
62 // Create netlist footprints section
64
65 const EXPORT_EXCLUSION excluded = boardExclusion();
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 =
78 []( 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 SCH_SYMBOL* symbol = findNextSymbol( item, sheet, excluded );
89
90 if( !symbol )
91 continue;
92
93 std::vector<PIN_INFO> pins = CreatePinList( symbol, sheet );
94
95 if( symbol->GetLibSymbolRef()
96 && symbol->GetLibSymbolRef()->GetFPFilters().GetCount() != 0 )
97 {
98 cmpList.push_back( SCH_REFERENCE( symbol, sheet ) );
99 }
100
101 footprint = symbol->GetFootprintFieldText( &sheet, RESOLVED, m_exportVariant );
102 footprint.Replace( wxT( " " ), wxT( "_" ) );
103
104 if( footprint.IsEmpty() )
105 footprint = wxT( "$noname" );
106
107 fmt::print( f, " ( {} {}",
108 TO_UTF8( sheet.PathAsString() + symbol->m_Uuid.AsString() ),
109 TO_UTF8( footprint ) );
110
111 field = symbol->GetRef( &sheet );
112 fmt::print( f, " {}", TO_UTF8( field ) );
113
114 field = symbol->GetValue( &sheet, RESOLVED, m_exportVariant );
115 field.Replace( wxT( " " ), wxT( "_" ) );
116 fmt::print( f, " {}", TO_UTF8( field ) );
117
118 fmt::print( f, "\n" );
119
120 // Write pin list:
121 for( const PIN_INFO& pin : pins )
122 {
123 if( pin.num.IsEmpty() ) // Erased pin in list
124 continue;
125
126 netName = pin.netName;
127 netName.Replace( wxT( " " ), wxT( "_" ) );
128
129 // Legacy OrcadPCB2 right-aligns the pin number in a 4-column field.
130 fmt::print( f, " ( {:>4} {} )\n", TO_UTF8( pin.num ), TO_UTF8( netName ) );
131 }
132
133 fmt::print( f, " )\n" );
134 }
135 }
136
137 fmt::print( f, ")\n*\n" );
138
139 if( ferror( f ) )
140 success = false;
141 }
142 catch( const std::system_error& e )
143 {
144 aReporter.Report( wxString::Format( _( "I/O error writing netlist: %s" ), e.what() ), RPT_SEVERITY_ERROR );
145
146 success = false;
147 }
148 catch( const fmt::format_error& e )
149 {
150 aReporter.Report( wxString::Format( _( "Formatting error writing netlist: %s" ), e.what() ),
152
153 success = false;
154 }
155
156 fclose( f );
157
158 return success;
159}
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
wxString AsString() const
Definition kiid.cpp:264
wxArrayString GetFPFilters() const
Definition lib_symbol.h:254
UNIQUE_STRINGS m_referencesAlreadyFound
Used for "multiple symbols per package" symbols to avoid processing a lib symbol more than once.
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.
std::vector< PIN_INFO > CreatePinList(SCH_SYMBOL *aSymbol, const SCH_SHEET_PATH &aSheetPath)
Find a symbol from the DrawList and builds its pin list.
bool writeNetlist(const wxString &aOutFileName, unsigned aNetlistOptions, REPORTER &aReporter) override
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
A helper to define a symbol's reference designator in a schematic.
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
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition sch_symbol.h:185
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.
Collection of utility functions for component reference designators (refdes)
@ 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