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 <schframe.h>
31 #include <sch_reference_list.h>
32 #include <class_netlist_object.h>
33 
35 
36 /* Generate CADSTAR net list. */
37 static wxString StartLine( wxT( "." ) );
38 
39 bool NETLIST_EXPORTER_CADSTAR::WriteNetlist( const wxString& aOutFileName, unsigned aNetlistOptions )
40 {
41  (void)aNetlistOptions; //unused
42  int ret = 0;
43  FILE* f = NULL;
44 
45  if( ( f = wxFopen( aOutFileName, wxT( "wt" ) ) ) == NULL )
46  {
47  wxString msg;
48  msg.Printf( _( "Failed to create file '%s'" ),
49  GetChars( aOutFileName ) );
50  DisplayError( NULL, msg );
51  return false;
52  }
53 
54  wxString StartCmpDesc = StartLine + wxT( "ADD_COM" );
55  wxString msg;
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, "\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  doing nothing with footprint
86  if( !component->GetField( FOOTPRINT )->IsVoid() )
87  {
88  footprint = component->GetField( FOOTPRINT )->m_Text;
89  footprint.Replace( wxT( " " ), wxT( "_" ) );
90  }
91  else
92  footprint = wxT( "$noname" );
93  */
94 
95  msg = component->GetRef( &sheetList[i] );
96  ret |= fprintf( f, "%s ", TO_UTF8( StartCmpDesc ) );
97  ret |= fprintf( f, "%s", TO_UTF8( msg ) );
98 
99  msg = component->GetField( VALUE )->GetText();
100  msg.Replace( wxT( " " ), wxT( "_" ) );
101  ret |= fprintf( f, " \"%s\"", TO_UTF8( msg ) );
102  ret |= fprintf( f, "\n" );
103  }
104  }
105 
106  ret |= fprintf( f, "\n" );
107 
108  m_SortedComponentPinList.clear();
109 
110  if( ! writeListOfNets( f ) )
111  ret = -1; // set error
112 
113  ret |= fprintf( f, "\n%sEND\n", TO_UTF8( StartLine ) );
114 
115  fclose( f );
116 
117  return ret >= 0;
118 }
119 
120 
122 {
123  int ret = 0;
124  wxString InitNetDesc = StartLine + wxT( "ADD_TER" );
125  wxString StartNetDesc = StartLine + wxT( "TER" );
126  wxString netcodeName, InitNetDescLine;
127  unsigned ii;
128  int print_ter = 0;
129  int NetCode, lastNetCode = -1;
130  SCH_COMPONENT* Cmp;
131  wxString netName;
132 
133  for( ii = 0; ii < m_masterList->size(); ii++ )
134  {
135  NETLIST_OBJECT* nitem = m_masterList->GetItem( ii );
136 
137  // Get the NetName of the current net :
138  if( ( NetCode = nitem->GetNet() ) != lastNetCode )
139  {
140  netName = nitem->GetNetName();
141  netcodeName = wxT( "\"" );
142 
143  if( !netName.IsEmpty() )
144  netcodeName << netName;
145  else // this net has no name: create a default name $<net number>
146  netcodeName << wxT( "$" ) << NetCode;
147 
148  netcodeName += wxT( "\"" );
149  lastNetCode = NetCode;
150  print_ter = 0;
151  }
152 
153 
154  if( nitem->m_Type != NET_PIN )
155  continue;
156 
157  if( nitem->m_Flag != 0 )
158  continue;
159 
160  Cmp = nitem->GetComponentParent();
161  wxString refstr = Cmp->GetRef( &nitem->m_SheetPath );
162  if( refstr[0] == '#' )
163  continue; // Power supply symbols.
164 
165  switch( print_ter )
166  {
167  case 0:
168  {
169  char buf[5];
170  wxString str_pinnum;
171  strncpy( buf, (char*) &nitem->m_PinNum, 4 );
172  buf[4] = 0;
173  str_pinnum = FROM_UTF8( buf );
174  InitNetDescLine.Printf( wxT( "\n%s %s %.4s %s" ),
175  GetChars( InitNetDesc ),
176  GetChars( refstr ),
177  GetChars( str_pinnum ),
178  GetChars( netcodeName ) );
179  }
180  print_ter++;
181  break;
182 
183  case 1:
184  ret |= fprintf( f, "%s\n", TO_UTF8( InitNetDescLine ) );
185  ret |= fprintf( f, "%s %s %.4s\n",
186  TO_UTF8( StartNetDesc ),
187  TO_UTF8( refstr ),
188  (char*) &nitem->m_PinNum );
189  print_ter++;
190  break;
191 
192  default:
193  ret |= fprintf( f, " %s %.4s\n",
194  TO_UTF8( refstr ),
195  (char*) &nitem->m_PinNum );
196  break;
197  }
198 
199  nitem->m_Flag = 1;
200  }
201 
202  return ret >= 0;
203 }
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:206
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
SCH_SHEET * g_RootSheet
Definition: eeschema.cpp:54
SCH_FIELD * GetField(int aFieldNdx) const
Function GetField returns a field.
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:130
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("."))
SCH_COMPONENT * GetComponentParent() const
For Pins (NET_PINS):
Definition of the NETLIST_OBJECT class.
Field Value of part, i.e. "3.3K".
bool writeListOfNets(FILE *f)
Function writeListOfNetsCADSTAR writes a net list (ranked by Netcode), and pins connected to it...
NETLIST_OBJECTS m_SortedComponentPinList
no ownership
const wxString GetRef(const SCH_SHEET_PATH *sheet)
Function GetRef returns the reference, for the given sheet path.
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:68
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:151
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:69
wxString DateAndTime()
Function DateAndTime.
Definition: string.cpp:229