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,
39 unsigned /* aNetlistOptions */,
40 REPORTER& aReporter )
41{
42 FILE* f = nullptr;
43 wxString field;
44 wxString footprint;
45 bool success = true;
46 wxString netName;
47
48
49 if( ( f = wxFopen( aOutFileName, wxT( "wt" ) ) ) == nullptr )
50 {
51 wxString msg = wxString::Format( _( "Failed to create file '%s'." ), aOutFileName );
52 aReporter.Report( msg, RPT_SEVERITY_ERROR );
53 return false;
54 }
55
56 std::vector< SCH_REFERENCE > cmpList;
57
58 try
59 {
60 fmt::print( f, "( {{ {} created {} }}\n",
62
63 // Create netlist footprints section
65
66 for( const SCH_SHEET_PATH& sheet : m_schematic->Hierarchy() )
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 = []( const EDA_ITEM* item1, const EDA_ITEM* item2 )
77 {
78 return item1->m_Uuid < item2->m_Uuid;
79 };
80
81 std::sort( sheetItems.begin(), sheetItems.end(), pred );
82
83 // Process symbol attributes
84 for( EDA_ITEM* item : sheetItems )
85 {
86 SCH_SYMBOL* symbol = findNextSymbol( item, sheet );
87
88 if( !symbol )
89 continue;
90
91 if( symbol->GetExcludedFromBoard() )
92 continue;
93
94 std::vector<PIN_INFO> pins = CreatePinList( symbol, sheet, true );
95
96 if( symbol->GetLibSymbolRef()
97 && symbol->GetLibSymbolRef()->GetFPFilters().GetCount() != 0 )
98 {
99 cmpList.push_back( SCH_REFERENCE( symbol, sheet ) );
100 }
101
102 footprint = symbol->GetFootprintFieldText( true, &sheet, false );
103 footprint.Replace( wxT( " " ), wxT( "_" ) );
104
105 if( footprint.IsEmpty() )
106 footprint = wxT( "$noname" );
107
108 fmt::print( f, " ( {} {}",
109 TO_UTF8( sheet.PathAsString() + symbol->m_Uuid.AsString() ),
110 TO_UTF8( footprint ) );
111
112 field = symbol->GetRef( &sheet );
113 fmt::print( f, " {}", TO_UTF8( field ) );
114
115 field = symbol->GetValue( true, &sheet, false );
116 field.Replace( wxT( " " ), wxT( "_" ) );
117 fmt::print( f, " {}", TO_UTF8( field ) );
118
119 fmt::print( f, "\n" );
120
121 // Write pin list:
122 for( const PIN_INFO& pin : pins )
123 {
124 if( pin.num.IsEmpty() ) // Erased pin in list
125 continue;
126
127 netName = pin.netName;
128 netName.Replace( wxT( " " ), wxT( "_" ) );
129
130 // Legacy OrcadPCB2 right-aligns the pin number in a 4-column field.
131 fmt::print( f, " ( {:>4} {} )\n", TO_UTF8( pin.num ), TO_UTF8( netName ) );
132 }
133
134 fmt::print( f, " )\n" );
135 }
136 }
137
138 fmt::print( f, ")\n*\n" );
139
140 if( ferror( f ) )
141 success = false;
142 }
143 catch( const std::system_error& e )
144 {
145 aReporter.Report( wxString::Format( _( "I/O error writing netlist: %s" ), e.what() ), RPT_SEVERITY_ERROR );
146
147 success = false;
148 }
149 catch( const fmt::format_error& e )
150 {
151 aReporter.Report( wxString::Format( _( "Formatting error writing netlist: %s" ), e.what() ), RPT_SEVERITY_ERROR );
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:96
const KIID m_Uuid
Definition eda_item.h:531
wxString AsString() const
Definition kiid.cpp:242
wxArrayString GetFPFilters() const
Definition lib_symbol.h:207
SCHEMATIC * m_schematic
The schematic we're generating a netlist for.
std::vector< PIN_INFO > CreatePinList(SCH_SYMBOL *aSymbol, const SCH_SHEET_PATH &aSheetPath, bool aKeepUnconnectedPins)
Find a symbol from the DrawList and builds its pin list.
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 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:71
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:100
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:69
const wxString GetValue(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText, const wxString &aVariantName=wxEmptyString) const override
const wxString GetFootprintFieldText(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText, const wxString &aVariantName=wxEmptyString) const
bool GetExcludedFromBoard(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition sch_symbol.h:177
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
This file is part of the common library.
#define _(s)
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:169