KiCad PCB EDA Suite
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 <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 <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 
35 
36 /* Generate CADSTAR net list. */
37 static wxString StartLine( wxT( "." ) );
38 
39 bool NETLIST_EXPORTER_CADSTAR::WriteNetlist( const wxString& aOutFileName,
40  unsigned aNetlistOptions )
41 {
42  (void)aNetlistOptions; //unused
43  int ret = 0;
44  FILE* f = nullptr;
45 
46  if( ( f = wxFopen( aOutFileName, wxT( "wt" ) ) ) == nullptr )
47  {
48  wxString msg;
49  msg.Printf( _( "Failed to create file '%s'." ), aOutFileName );
50  DisplayError( nullptr, msg );
51  return false;
52  }
53 
54  wxString StartCmpDesc = StartLine + wxT( "ADD_COM" );
55  wxString msg;
56  wxString footprint;
57  SCH_SYMBOL* symbol;
58  wxString title = wxT( "Eeschema " ) + GetBuildVersion();
59 
60  ret |= fprintf( f, "%sHEA\n", TO_UTF8( StartLine ) );
61  ret |= fprintf( f, "%sTIM %s\n", TO_UTF8( StartLine ), TO_UTF8( DateAndTime() ) );
62  ret |= fprintf( f, "%sAPP ", TO_UTF8( StartLine ) );
63  ret |= fprintf( f, "\"%s\"\n", TO_UTF8( title ) );
64  ret |= fprintf( f, ".TYP FULL\n\n" );
65 
66  // Create netlist footprints section
68 
69  SCH_SHEET_LIST sheetList = m_schematic->GetSheets();
70 
71  for( unsigned i = 0; i < sheetList.size(); i++ )
72  {
73  for( SCH_ITEM* item : sheetList[i].LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
74  {
75  symbol = findNextSymbol( item, &sheetList[ i ] );
76 
77  if( !symbol )
78  continue;
79 
80  if( !symbol->GetField( FOOTPRINT_FIELD )->IsVoid() )
81  footprint = symbol->GetField( FOOTPRINT_FIELD )->GetShownText();
82  else
83  footprint = "$noname";
84 
85  msg = symbol->GetRef( &sheetList[i] );
86  ret |= fprintf( f, "%s ", TO_UTF8( StartCmpDesc ) );
87  ret |= fprintf( f, "%s", TO_UTF8( msg ) );
88 
89  msg = symbol->GetValue( &sheetList[i], true );
90  msg.Replace( wxT( " " ), wxT( "_" ) );
91  ret |= fprintf( f, " \"%s\"", TO_UTF8( msg ) );
92  ret |= fprintf( f, " \"%s\"", TO_UTF8( footprint ) );
93  ret |= fprintf( f, "\n" );
94  }
95  }
96 
97  ret |= fprintf( f, "\n" );
98 
99  if( ! writeListOfNets( f ) )
100  ret = -1; // set error
101 
102  ret |= fprintf( f, "\n%sEND\n", TO_UTF8( StartLine ) );
103 
104  fclose( f );
105 
106  return ret >= 0;
107 }
108 
109 
111 {
112  int ret = 0;
113  int print_ter = 0;
114 
115  wxString InitNetDesc = StartLine + wxT( "ADD_TER" );
116  wxString StartNetDesc = StartLine + wxT( "TER" );
117  wxString InitNetDescLine;
118  wxString netName;
119 
120  for( const auto& it : m_schematic->ConnectionGraph()->GetNetMap() )
121  {
122  auto subgraphs = it.second;
123 
124  netName.Printf( wxT( "\"%s\"" ), it.first.first );
125 
126  std::vector<std::pair<SCH_PIN*, SCH_SHEET_PATH>> sorted_items;
127 
128  for( CONNECTION_SUBGRAPH* subgraph : subgraphs )
129  {
130  SCH_SHEET_PATH sheet = subgraph->m_sheet;
131 
132  for( SCH_ITEM* item : subgraph->m_items )
133  {
134  if( item->Type() == SCH_PIN_T )
135  sorted_items.emplace_back( static_cast<SCH_PIN*>( item ), sheet );
136  }
137  }
138 
139  // Netlist ordering: Net name, then ref des, then pin name
140  std::sort( sorted_items.begin(), sorted_items.end(),
141  []( std::pair<SCH_PIN*, SCH_SHEET_PATH> a, std::pair<SCH_PIN*, SCH_SHEET_PATH> b )
142  {
143  wxString ref_a = a.first->GetParentSymbol()->GetRef( &a.second );
144  wxString ref_b = b.first->GetParentSymbol()->GetRef( &b.second );
145 
146  if( ref_a == ref_b )
147  return a.first->GetShownNumber() < b.first->GetShownNumber();
148 
149  return ref_a < ref_b;
150  } );
151 
152  // Some duplicates can exist, for example on multi-unit parts with duplicated
153  // pins across units. If the user connects the pins on each unit, they will
154  // appear on separate subgraphs. Remove those here:
155  sorted_items.erase( std::unique( sorted_items.begin(), sorted_items.end(),
156  []( std::pair<SCH_PIN*, SCH_SHEET_PATH> a, std::pair<SCH_PIN*, SCH_SHEET_PATH> b )
157  {
158  wxString ref_a = a.first->GetParentSymbol()->GetRef( &a.second );
159  wxString ref_b = b.first->GetParentSymbol()->GetRef( &b.second );
160 
161  return ref_a == ref_b && a.first->GetShownNumber() == b.first->GetShownNumber();
162  } ),
163  sorted_items.end() );
164 
165  print_ter = 0;
166 
167  for( const std::pair<SCH_PIN*, SCH_SHEET_PATH>& pair : sorted_items )
168  {
169  SCH_PIN* pin = pair.first;
170  SCH_SHEET_PATH sheet = pair.second;
171 
172  wxString refText = pin->GetParentSymbol()->GetRef( &sheet );
173  wxString pinText = pin->GetShownNumber();
174 
175  // Skip power symbols and virtual symbols
176  if( refText[0] == wxChar( '#' ) )
177  continue;
178 
179  switch( print_ter )
180  {
181  case 0:
182  InitNetDescLine.Printf( wxT( "\n%s %s %.4s %s" ),
183  InitNetDesc,
184  refText,
185  pinText,
186  netName );
187  print_ter++;
188  break;
189 
190  case 1:
191  ret |= fprintf( f, "%s\n", TO_UTF8( InitNetDescLine ) );
192  ret |= fprintf( f, "%s %s %.4s\n",
193  TO_UTF8( StartNetDesc ),
194  TO_UTF8( refText ),
195  TO_UTF8( pinText ) );
196  print_ter++;
197  break;
198 
199  default:
200  ret |= fprintf( f, " %s %.4s\n",
201  TO_UTF8( refText ),
202  TO_UTF8( pinText ) );
203  break;
204  }
205  }
206  }
207 
208  return ret >= 0;
209 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:271
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
SCH_FIELD * GetField(MANDATORY_FIELD_T aFieldType)
Return a mandatory field in this symbol.
Definition: sch_symbol.cpp:675
This file is part of the common library.
virtual CONNECTION_GRAPH * ConnectionGraph() const =0
void Clear()
Erase the record.
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const
Return the reference for the given sheet path.
Definition: sch_symbol.cpp:441
bool WriteNetlist(const wxString &aOutFileName, unsigned aNetlistOptions) override
Write to specified output file.
A subgraph is a set of items that are electrically connected on a single sheet.
wxString GetShownText(int aDepth=0) const override
Return the string actually shown after processing of the base text.
Definition: sch_field.cpp:105
bool IsVoid() const
Definition: sch_field.cpp:325
SCH_SYMBOL * findNextSymbol(EDA_ITEM *aItem, SCH_SHEET_PATH *aSheetPath)
Check if the given symbol should be processed for netlisting.
SCHEMATIC_IFACE * m_schematic
The schematic we're generating a netlist for.
UNIQUE_STRINGS m_referencesAlreadyFound
Used for "multiple symbols per package" symbols to avoid processing a lib symbol more than once.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
wxString GetBuildVersion()
Get the full KiCad version string.
const NET_MAP & GetNetMap() const
#define _(s)
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
static wxString StartLine(wxT("."))
const wxString GetValue(const SCH_SHEET_PATH *sheet, bool aResolve) const
Return the instance-specific value for the given sheet path.
Definition: sch_symbol.cpp:573
bool writeListOfNets(FILE *f)
Write a net list (ranked by Netcode), and pins connected to it.
Schematic symbol object.
Definition: sch_symbol.h:78
wxString DateAndTime()
virtual SCH_SHEET_LIST GetSheets() const =0
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:193
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:113
Field Name Module PCB, i.e. "16DIP300".