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 <dick@softplc.com>
6  * Copyright (C) 1992-2021 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 <confirm.h>
27 #include <refdes_utils.h>
28 
29 #include <sch_edit_frame.h>
30 #include <sch_reference_list.h>
31 #include <string_utils.h>
32 #include <symbol_library.h>
33 #include <symbol_lib_table.h>
34 
35 #include <netlist.h>
36 #include "netlist_exporter_orcadpcb2.h"
37 
38 
WriteNetlist(const wxString & aOutFileName,unsigned)39 bool NETLIST_EXPORTER_ORCADPCB2::WriteNetlist( const wxString& aOutFileName,
40                                                unsigned /* aNetlistOptions */ )
41 {
42     FILE* f = nullptr;
43     wxString    field;
44     wxString    footprint;
45     int         ret = 0;        // zero now, OR in the sign bit on error
46     wxString    netName;
47 
48 
49     if( ( f = wxFopen( aOutFileName, wxT( "wt" ) ) ) == nullptr )
50     {
51         wxString msg;
52         msg.Printf( _( "Failed to create file '%s'." ), aOutFileName );
53         DisplayError( nullptr, msg );
54         return false;
55     }
56 
57     std::vector< SCH_REFERENCE > cmpList;
58 
59     ret |= fprintf( f, "( { %s created  %s }\n",
60                         NETLIST_HEAD_STRING, TO_UTF8( DateAndTime() ) );
61 
62     // Create netlist footprints section
63     m_referencesAlreadyFound.Clear();
64 
65     SCH_SHEET_LIST sheetList = m_schematic->GetSheets();
66 
67     for( unsigned i = 0;  i < sheetList.size();  i++ )
68     {
69         SCH_SHEET_PATH sheet = sheetList[i];
70 
71         // Process symbol attributes
72         for( auto item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
73         {
74             SCH_SYMBOL* symbol = findNextSymbol( item, &sheet );
75 
76             if( !symbol )
77                 continue;
78 
79             CreatePinList( symbol, &sheet, true );
80 
81             if( symbol->GetLibSymbolRef()
82               && symbol->GetLibSymbolRef()->GetFPFilters().GetCount() != 0  )
83                 cmpList.push_back( SCH_REFERENCE( symbol, symbol->GetLibSymbolRef().get(),
84                                                   sheet ) );
85 
86             footprint = symbol->GetFootprint( &sheet, true );
87             footprint.Replace( wxT( " " ), wxT( "_" ) );
88 
89             if( footprint.IsEmpty() )
90                 footprint = wxT( "$noname" );
91 
92             ret |= fprintf( f, " ( %s %s",
93                             TO_UTF8( sheet.PathAsString() + symbol->m_Uuid.AsString() ),
94                             TO_UTF8( footprint ) );
95 
96             field = symbol->GetRef( &sheet );
97 
98             ret |= fprintf( f, "  %s", TO_UTF8( field ) );
99 
100             field = symbol->GetValue( &sheet, true );
101             field.Replace( wxT( " " ), wxT( "_" ) );
102 
103             ret |= fprintf( f, " %s", TO_UTF8( field ) );
104 
105             ret |= fprintf( f, "\n" );
106 
107             // Write pin list:
108             for( const PIN_INFO& pin : m_sortedSymbolPinList )
109             {
110                 netName = pin.netName;
111                 netName.Replace( wxT( " " ), wxT( "_" ) );
112 
113                 ret |= fprintf( f, "  ( %4.4s %s )\n", TO_UTF8( pin.num ), TO_UTF8( netName ) );
114             }
115 
116             ret |= fprintf( f, " )\n" );
117         }
118     }
119 
120     ret |= fprintf( f, ")\n*\n" );
121 
122     fclose( f );
123 
124     return ret >= 0;
125 }
126