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-2013 jp.charras at wanadoo.fr
5  * Copyright (C) 2013 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
6  * Copyright (C) 1992-2016 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  EDA_ITEM* DrawList;
56  SCH_COMPONENT* component;
57  wxString title = wxT( "Eeschema " ) + GetBuildVersion();
58 
59  ret |= fprintf( f, "%sHEA\n", TO_UTF8( StartLine ) );
60  ret |= fprintf( f, "%sTIM %s\n", TO_UTF8( StartLine ), TO_UTF8( DateAndTime() ) );
61  ret |= fprintf( f, "%sAPP ", TO_UTF8( StartLine ) );
62  ret |= fprintf( f, "\"%s\"\n", TO_UTF8( title ) );
63  ret |= fprintf( f, "\n" );
64 
65  // Prepare list of nets generation
66  for( unsigned ii = 0; ii < m_masterList->size(); ii++ )
67  m_masterList->GetItem( ii )->m_Flag = 0;
68 
69  // Create netlist module section
71 
72  SCH_SHEET_LIST sheetList( g_RootSheet );
73 
74  for( unsigned i = 0; i < sheetList.size(); i++ )
75  {
76  for( DrawList = sheetList[i].LastDrawList(); DrawList != NULL; DrawList = DrawList->Next() )
77  {
78  DrawList = component = findNextComponentAndCreatePinList( DrawList, &sheetList[i] );
79 
80  if( component == NULL )
81  break;
82 
83  /*
84  doing nothing with footprint
85  if( !component->GetField( FOOTPRINT )->IsVoid() )
86  {
87  footprint = component->GetField( FOOTPRINT )->m_Text;
88  footprint.Replace( wxT( " " ), wxT( "_" ) );
89  }
90  else
91  footprint = wxT( "$noname" );
92  */
93 
94  msg = component->GetRef( &sheetList[i] );
95  ret |= fprintf( f, "%s ", TO_UTF8( StartCmpDesc ) );
96  ret |= fprintf( f, "%s", TO_UTF8( msg ) );
97 
98  msg = component->GetField( VALUE )->GetText();
99  msg.Replace( wxT( " " ), wxT( "_" ) );
100  ret |= fprintf( f, " \"%s\"", TO_UTF8( msg ) );
101  ret |= fprintf( f, "\n" );
102  }
103  }
104 
105  ret |= fprintf( f, "\n" );
106 
107  m_SortedComponentPinList.clear();
108 
109  if( ! writeListOfNets( f ) )
110  ret = -1; // set error
111 
112  ret |= fprintf( f, "\n%sEND\n", TO_UTF8( StartLine ) );
113 
114  fclose( f );
115 
116  return ret >= 0;
117 }
118 
119 
121 {
122  int ret = 0;
123  wxString InitNetDesc = StartLine + wxT( "ADD_TER" );
124  wxString StartNetDesc = StartLine + wxT( "TER" );
125  wxString netcodeName, InitNetDescLine;
126  unsigned ii;
127  int print_ter = 0;
128  int NetCode, lastNetCode = -1;
129  SCH_COMPONENT* Cmp;
130  wxString netName;
131 
132  for( ii = 0; ii < m_masterList->size(); ii++ )
133  {
134  NETLIST_OBJECT* nitem = m_masterList->GetItem( ii );
135 
136  // Get the NetName of the current net :
137  if( ( NetCode = nitem->GetNet() ) != lastNetCode )
138  {
139  netName = nitem->GetNetName();
140  netcodeName = wxT( "\"" );
141 
142  if( !netName.IsEmpty() )
143  netcodeName << netName;
144  else // this net has no name: create a default name $<net number>
145  netcodeName << wxT( "$" ) << NetCode;
146 
147  netcodeName += wxT( "\"" );
148  lastNetCode = NetCode;
149  print_ter = 0;
150  }
151 
152 
153  if( nitem->m_Type != NET_PIN )
154  continue;
155 
156  if( nitem->m_Flag != 0 )
157  continue;
158 
159  Cmp = nitem->GetComponentParent();
160  wxString refstr = Cmp->GetRef( &nitem->m_SheetPath );
161  if( refstr[0] == '#' )
162  continue; // Power supply symbols.
163 
164  switch( print_ter )
165  {
166  case 0:
167  {
168  char buf[5];
169  wxString str_pinnum;
170  strncpy( buf, (char*) &nitem->m_PinNum, 4 );
171  buf[4] = 0;
172  str_pinnum = FROM_UTF8( buf );
173  InitNetDescLine.Printf( wxT( "\n%s %s %.4s %s" ),
174  GetChars( InitNetDesc ),
175  GetChars( refstr ),
176  GetChars( str_pinnum ),
177  GetChars( netcodeName ) );
178  }
179  print_ter++;
180  break;
181 
182  case 1:
183  ret |= fprintf( f, "%s\n", TO_UTF8( InitNetDescLine ) );
184  ret |= fprintf( f, "%s %s %.4s\n",
185  TO_UTF8( StartNetDesc ),
186  TO_UTF8( refstr ),
187  (char*) &nitem->m_PinNum );
188  print_ter++;
189  break;
190 
191  default:
192  ret |= fprintf( f, " %s %.4s\n",
193  TO_UTF8( refstr ),
194  (char*) &nitem->m_PinNum );
195  break;
196  }
197 
198  nitem->m_Flag = 1;
199  }
200 
201  return ret >= 0;
202 }
Class SCH_SHEET_LIST.
wxString GetNetName(bool adoptTimestamp=false) const
Function GetNetName.
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes...
Definition: macros.h:53
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.
SCH_SHEET_PATH m_SheetPath
EDA_ITEM * Next() const
Definition: base_struct.h:235
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.
#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:55
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:128
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
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:180
#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:241
wxString DateAndTime()
Function DateAndTime.
Definition: string.cpp:229