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-2020 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  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, ".TYP FULL\n\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  std::vector<SCH_COMPONENT*> cmps;
77 
78  for( auto item : sheetList[i].LastScreen()->Items().OfType( SCH_COMPONENT_T ) )
79  {
80  component = findNextComponent( item, &sheetList[i] );
81 
82  if( !component )
83  continue;
84 
85  CreatePinList( component, &sheetList[i] );
86 
87  if( !component->GetField( FOOTPRINT )->IsVoid() )
88  footprint = component->GetField( FOOTPRINT )->GetText();
89  else
90  footprint = "$noname";
91 
92  msg = component->GetRef( &sheetList[i] );
93  ret |= fprintf( f, "%s ", TO_UTF8( StartCmpDesc ) );
94  ret |= fprintf( f, "%s", TO_UTF8( msg ) );
95 
96  msg = component->GetField( VALUE )->GetText();
97  msg.Replace( wxT( " " ), wxT( "_" ) );
98  ret |= fprintf( f, " \"%s\"", TO_UTF8( msg ) );
99  ret |= fprintf( f, " \"%s\"", TO_UTF8( footprint ) );
100  ret |= fprintf( f, "\n" );
101  }
102  }
103 
104  ret |= fprintf( f, "\n" );
105 
106  m_SortedComponentPinList.clear();
107 
108  if( ! writeListOfNets( f ) )
109  ret = -1; // set error
110 
111  ret |= fprintf( f, "\n%sEND\n", TO_UTF8( StartLine ) );
112 
113  fclose( f );
114 
115  return ret >= 0;
116 }
117 
118 
120 {
121  int ret = 0;
122  wxString InitNetDesc = StartLine + wxT( "ADD_TER" );
123  wxString StartNetDesc = StartLine + wxT( "TER" );
124  wxString netcodeName, InitNetDescLine;
125  unsigned ii;
126  int print_ter = 0;
127  int NetCode, lastNetCode = -1;
128  SCH_COMPONENT* Cmp;
129  wxString netName;
130 
131  for( ii = 0; ii < m_masterList->size(); ii++ )
132  {
133  NETLIST_OBJECT* nitem = m_masterList->GetItem( ii );
134 
135  // Get the NetName of the current net :
136  if( ( NetCode = nitem->GetNet() ) != lastNetCode )
137  {
138  netName = nitem->GetNetName();
139  netcodeName = wxT( "\"" );
140 
141  if( !netName.IsEmpty() )
142  netcodeName << netName;
143  else // this net has no name: create a default name $<net number>
144  netcodeName << wxT( "$" ) << NetCode;
145 
146  netcodeName += wxT( "\"" );
147  lastNetCode = NetCode;
148  print_ter = 0;
149  }
150 
151 
152  if( nitem->m_Type != NETLIST_ITEM::PIN )
153  continue;
154 
155  if( nitem->m_Flag != 0 )
156  continue;
157 
158  Cmp = nitem->GetComponentParent();
159  wxString refstr = Cmp->GetRef( &nitem->m_SheetPath );
160  if( refstr[0] == '#' )
161  continue; // Power supply symbols.
162 
163  switch( print_ter )
164  {
165  case 0:
166  {
167  InitNetDescLine.Printf( wxT( "\n%s %s %.4s %s" ),
168  GetChars( InitNetDesc ),
169  GetChars( refstr ),
170  GetChars( nitem->m_PinNum ),
171  GetChars( netcodeName ) );
172  }
173  print_ter++;
174  break;
175 
176  case 1:
177  ret |= fprintf( f, "%s\n", TO_UTF8( InitNetDescLine ) );
178  ret |= fprintf( f, "%s %s %.4s\n",
179  TO_UTF8( StartNetDesc ),
180  TO_UTF8( refstr ),
181  TO_UTF8( nitem->m_PinNum ) );
182  print_ter++;
183  break;
184 
185  default:
186  ret |= fprintf( f, " %s %.4s\n",
187  TO_UTF8( refstr ),
188  TO_UTF8( nitem->m_PinNum ) );
189  break;
190  }
191 
192  nitem->m_Flag = 1;
193  }
194 
195  return ret >= 0;
196 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:236
SCH_SHEET_LIST.
SCH_COMPONENT * GetComponentParent() const
For Pins (NET_PINS):
UNIQUE_STRINGS m_ReferencesAlreadyFound
Used for "multi parts per package" components, avoids processing a lib component more than once.
This file is part of the common library.
int GetNet() const
SCH_SHEET_PATH m_SheetPath
void Clear()
Function Clear erases the record.
SCH_COMPONENT * findNextComponent(EDA_ITEM *aItem, SCH_SHEET_PATH *aSheetPath)
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".
bool IsVoid() const
Function IsVoid returns true if the field is either empty or holds "~".
Definition: sch_field.cpp:257
SCH_FIELD * GetField(int aFieldNdx) const
Returns a field in this symbol.
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:48
#define VALUE
#define NULL
wxString GetBuildVersion()
Function GetBuildVersion Return the build version string.
void CreatePinList(SCH_COMPONENT *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.
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
NETLIST_OBJECT * GetItem(unsigned aIdx) const
Acces to an item in 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:101
#define _(s)
Definition: 3d_actions.cpp:31
wxString GetNetName(bool adoptTimestamp=false) const
Function GetNetName.
SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:89
SCH_SHEET * g_RootSheet
NETLIST_ITEM m_Type
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:123
wxString DateAndTime()
Definition: string.cpp:345