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-2018 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 <fctsys.h>
27 #include <build_version.h>
28 #include <confirm.h>
29 
30 #include <sch_edit_frame.h>
31 #include <sch_reference_list.h>
32 
34 
35 /* Generate CADSTAR net list. */
36 static wxString StartLine( wxT( "." ) );
37 
38 bool NETLIST_EXPORTER_CADSTAR::WriteNetlist( const wxString& aOutFileName, unsigned aNetlistOptions )
39 {
40  (void)aNetlistOptions; //unused
41  int ret = 0;
42  FILE* f = NULL;
43 
44  if( ( f = wxFopen( aOutFileName, wxT( "wt" ) ) ) == NULL )
45  {
46  wxString msg;
47  msg.Printf( _( "Failed to create file \"%s\"" ),
48  GetChars( aOutFileName ) );
49  DisplayError( NULL, msg );
50  return false;
51  }
52 
53  wxString StartCmpDesc = StartLine + wxT( "ADD_COM" );
54  wxString msg;
55  wxString footprint;
56  EDA_ITEM* DrawList;
57  SCH_COMPONENT* component;
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  // Prepare list of nets generation
67  for( unsigned ii = 0; ii < m_masterList->size(); ii++ )
68  m_masterList->GetItem( ii )->m_Flag = 0;
69 
70  // Create netlist module section
72 
73  SCH_SHEET_LIST sheetList( g_RootSheet );
74 
75  for( unsigned i = 0; i < sheetList.size(); i++ )
76  {
77  for( DrawList = sheetList[i].LastDrawList(); DrawList != NULL; DrawList = DrawList->Next() )
78  {
79  DrawList = component = findNextComponentAndCreatePinList( DrawList, &sheetList[i] );
80 
81  if( component == NULL )
82  break;
83 
84 
85  if( !component->GetField( FOOTPRINT )->IsVoid() )
86  footprint = component->GetField( FOOTPRINT )->GetText();
87  else
88  footprint = "$noname";
89 
90  msg = component->GetRef( &sheetList[i] );
91  ret |= fprintf( f, "%s ", TO_UTF8( StartCmpDesc ) );
92  ret |= fprintf( f, "%s", TO_UTF8( msg ) );
93 
94  msg = component->GetField( VALUE )->GetText();
95  msg.Replace( wxT( " " ), wxT( "_" ) );
96  ret |= fprintf( f, " \"%s\"", TO_UTF8( msg ) );
97  ret |= fprintf( f, " \"%s\"", TO_UTF8( footprint ) );
98  ret |= fprintf( f, "\n" );
99  }
100  }
101 
102  ret |= fprintf( f, "\n" );
103 
104  m_SortedComponentPinList.clear();
105 
106  if( ! writeListOfNets( f ) )
107  ret = -1; // set error
108 
109  ret |= fprintf( f, "\n%sEND\n", TO_UTF8( StartLine ) );
110 
111  fclose( f );
112 
113  return ret >= 0;
114 }
115 
116 
118 {
119  int ret = 0;
120  wxString InitNetDesc = StartLine + wxT( "ADD_TER" );
121  wxString StartNetDesc = StartLine + wxT( "TER" );
122  wxString netcodeName, InitNetDescLine;
123  unsigned ii;
124  int print_ter = 0;
125  int NetCode, lastNetCode = -1;
126  SCH_COMPONENT* Cmp;
127  wxString netName;
128 
129  for( ii = 0; ii < m_masterList->size(); ii++ )
130  {
131  NETLIST_OBJECT* nitem = m_masterList->GetItem( ii );
132 
133  // Get the NetName of the current net :
134  if( ( NetCode = nitem->GetNet() ) != lastNetCode )
135  {
136  netName = nitem->GetNetName();
137  netcodeName = wxT( "\"" );
138 
139  if( !netName.IsEmpty() )
140  netcodeName << netName;
141  else // this net has no name: create a default name $<net number>
142  netcodeName << wxT( "$" ) << NetCode;
143 
144  netcodeName += wxT( "\"" );
145  lastNetCode = NetCode;
146  print_ter = 0;
147  }
148 
149 
150  if( nitem->m_Type != NET_PIN )
151  continue;
152 
153  if( nitem->m_Flag != 0 )
154  continue;
155 
156  Cmp = nitem->GetComponentParent();
157  wxString refstr = Cmp->GetRef( &nitem->m_SheetPath );
158  if( refstr[0] == '#' )
159  continue; // Power supply symbols.
160 
161  switch( print_ter )
162  {
163  case 0:
164  {
165  InitNetDescLine.Printf( wxT( "\n%s %s %.4s %s" ),
166  GetChars( InitNetDesc ),
167  GetChars( refstr ),
168  GetChars( nitem->m_PinNum ),
169  GetChars( netcodeName ) );
170  }
171  print_ter++;
172  break;
173 
174  case 1:
175  ret |= fprintf( f, "%s\n", TO_UTF8( InitNetDescLine ) );
176  ret |= fprintf( f, "%s %s %.4s\n",
177  TO_UTF8( StartNetDesc ),
178  TO_UTF8( refstr ),
179  TO_UTF8( nitem->m_PinNum ) );
180  print_ter++;
181  break;
182 
183  default:
184  ret |= fprintf( f, " %s %.4s\n",
185  TO_UTF8( refstr ),
186  TO_UTF8( nitem->m_PinNum ) );
187  break;
188  }
189 
190  nitem->m_Flag = 1;
191  }
192 
193  return ret >= 0;
194 }
Class SCH_SHEET_LIST.
wxString GetNetName(bool adoptTimestamp=false) const
Function GetNetName.
UNIQUE_STRINGS m_ReferencesAlreadyFound
Used for "multi parts per package" components, avoids processing a lib component more than once...
NETLIST_OBJECT * GetItem(unsigned aIdx) const
Acces to an item in list.
This file is part of the common library.
bool IsVoid() const
Function IsVoid returns true if the field is either empty or holds "~".
Definition: sch_field.h:116
SCH_SHEET_PATH m_SheetPath
EDA_ITEM * Next() const
Definition: base_struct.h:209
void Clear()
Function Clear erases the record.
NETLIST_ITEM_T m_Type
NETLIST_OBJECT_LIST * m_masterList
bool WriteNetlist(const wxString &aOutFileName, unsigned aNetlistOptions) override
Function WriteList writes to specified output file.
Field Name Module PCB, i.e. "16DIP300".
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes...
Definition: macros.h:47
int GetNet() const
SCH_SHEET * g_RootSheet
Definition: eeschema.cpp:56
SCH_FIELD * GetField(int aFieldNdx) const
Returns a field in this symbol.
wxString GetBuildVersion()
Function GetBuildVersion Return the build version string.
const wxString & GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:143
SCH_COMPONENT * findNextComponentAndCreatePinList(EDA_ITEM *aItem, SCH_SHEET_PATH *aSheetPath)
Function findNextComponentAndCreatePinList finds a component from the DrawList and builds its pin lis...
static wxString StartLine(wxT("."))
const wxString GetRef(const SCH_SHEET_PATH *aSheet)
Return the reference for the given sheet path.
SCH_COMPONENT * GetComponentParent() const
For Pins (NET_PINS):
bool writeListOfNets(FILE *f)
Function writeListOfNetsCADSTAR writes a net list (ranked by Netcode), and pins connected to it...
NETLIST_OBJECTS m_SortedComponentPinList
yes ownership, connected items flat list
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
size_t i
Definition: json11.cpp:597
Class SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:69
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:154
#define VALUE
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:245
wxString DateAndTime()
Function DateAndTime.
Definition: string.cpp:306