-
Notifications
You must be signed in to change notification settings - Fork 0
/
kicadToCreo.py
406 lines (338 loc) · 14.2 KB
/
kicadToCreo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# -*- coding: utf-8 -*-
# https://github.com/latefyr/kicadcreo.git
# MIT license
#
# Parse kicad netlist to creo harness xml
# This sample was created from one of the kicad bom scripts
#
# Copyright (C) LasseFyr 2019.
#
# This has been tested only with Creo 4.0 070
#
"""
@package
Generate a net list file.
Command line:
Run from Kicad eeschema with default parameters "%I" "%O"
For Wx interface add a dummy parameter at the end
Example: kicadToCreo "%I" "%O" wx
"""
from __future__ import print_function
# Import the KiCad python helper module
import kicad_netlist_reader
import sys
import os
import xmlWriteCompData
import xmlWriteSpools
import xmlWriteConnections
import wx # 2022_08_03 using wx now. Seems to work in both kicad versions 5 and 6
import logging
import threading
import textwrap
#------------------------------------------------------------------------------
# Text Handler class for the wx Interface
# class TextHandler(logging.Handler):
# Scrolls text with different text colors
#------------------------------------------------------------------------------
class TextHandler(logging.Handler):
"""This class allows you to log to a wx Text or ScrolledText widget"""
def __init__(self, text):
# run the regular Handler __init__
logging.Handler.__init__(self)
# Store a reference to the Text it will log to
self.text = text
# Set different Background colors for error and warning messages.
# You can set the foreground and background color to what ever you like here
def emit(self, record):
msg = self.format(record)
if(record.levelname == "ERROR"):
self.text.SetDefaultStyle(wx.TextAttr(wx.RED, wx.WHITE))
elif(record.levelname == "WARNING"):
self.text.SetDefaultStyle(wx.TextAttr(wx.GREEN, wx.WHITE))
else:
self.text.SetDefaultStyle(wx.TextAttr(wx.BLACK, wx.WHITE))
# Wrap long lines. You can adjust the line length here
for line in msg.split('\n'):
if len(line) >= 80:
for splitline in textwrap.wrap(line, width=80):
self.text.AppendText( splitline+"\n" )
else:
self.text.AppendText( line+"\n" )
#------------------------------------------------------------------------------
# Check parameters
#------------------------------------------------------------------------------
if len(sys.argv) < 3:
print("Usage ", __file__, "<netlist.xml> <output file> <optional dummy for wx interface>", file=sys.stderr)
sys.exit(1)
# Check whether we want console or wx window app
wantUiWin = False
if len(sys.argv) == 4:
wantUiWin = True
# Generate an instance of a generic netlist, and load the netlist tree from
# the command line option. If the file doesn't exist, execution will stop
if( os.path.isfile( sys.argv[1] ) ):
netlist = kicad_netlist_reader.netlist(sys.argv[1])
inputFileName = sys.argv[1]
creoLogicalFileName = sys.argv[2]+"_creo.xml"
else:
print("File not Found: " + sys.argv[1] + " !\nExit Script!", file=sys.stderr)
sys.exit(1)
#------------------------------------------------------------------------------
# Generate a YAML style netlist
# components = netlist.getInterestingComponents()
# This overwrites the old file and starts a new.
#------------------------------------------------------------------------------
def writeFileHeader( outputFileName ):
actualFileOpened = False
if len(outputFileName) > 2:
try:
if sys.version_info.major < 3:
fout = open(outputFileName, "w")
else:
fout = open(outputFileName, "w", encoding='utf-8')
actualFileOpened = True
except IOError:
e = "Can't open output file for writing: " + outputFileName
print( __file__, ":", e, sys.stderr )
fout = sys.stdout
else:
fout = sys.stdout
print("<?xml version=\"1.0\"?>", file = fout)
print("<!DOCTYPE rsData SYSTEM \"rsData_v2.dtd\">", file = fout)
print("<rsData>", file = fout)
print("<HEADER dtdVersion=\"2.0\"", file = fout)
print(" creator=\"KICAD\"", file = fout)
print(" createdByApplication=\"Kicad Schematic\"", file = fout)
print(" owner=\"Kicad\"", file = fout)
print(" contents=\"DOCUMENT\"", file = fout)
print(" diagramType=\"WIRING\"", file = fout)
print(" documentName=\""+sys.argv[1]+"\"", file = fout)
print(" documentFolder=\"Design/wiring\" />", file = fout)
# Individual functions will open the outputfile as needed
if actualFileOpened == True:
fout.close()
#------------------------------------------------------------------------------
# Append rsData tag to file END...
#------------------------------------------------------------------------------
def writeFileEnd( outputFileName ):
actualFileOpened = False
if len(outputFileName) > 2:
try:
if sys.version_info.major < 3:
fout = open(outputFileName, "a")
else:
fout = open(outputFileName, "a", encoding='utf-8')
actualFileOpened = True
except IOError:
e = "Can't open output file for writing: " + outputFileName
print( __file__, ":", e, sys.stderr )
fout = sys.stdout
else:
fout = sys.stdout
print("</rsData>", file = fout)
# Individual functions will open the outputfile as needed
if actualFileOpened == True:
fout.close( )
import codecs
try:
f = codecs.open(outputFileName, encoding='utf-8', errors='strict')
for line in f:
pass
if(wantUiWin):
logger.info( "Valid utf-8" )
else:
print("Valid utf-8", file = sys.stdout)
f.close()
except UnicodeDecodeError:
f.close()
if(wantUiWin):
logger.error( "Invalid utf-8" )
else:
print("invalid utf-8", file = sys.stderr)
#------------------------------------------------------------------------------
# Progress Bar for the wx interface
# Todo:
# - use the filename from the "Output Filename" - Dialog
# - allow filename to be changed.
# - check that the filename ends as "_crea.xml"
#------------------------------------------------------------------------------
def bar( event ):
import time
progress.SetValue( 0 )
progress.Show()
# Clear Logger buffer. You must first set the state to normal before clearing.
st.Clear( )
lbl_progress.SetLabel("Running...")
logger.info('Started ' + time.strftime('%H:%M:%S'))
outPutFName = fNameEntry.GetValue( )
if( os.path.isfile( outPutFName ) ):
logger.info("\nNI: Output Filename Exists: Overwriting")
else:
logger.info("\nNI: Output Filename does not Exist: Creating")
if( outPutFName.lower().endswith(('_creo.xml')) ):
logger.info("NI: Correct filename: Ends with \"_creo.xml\"\n")
else:
logger.info("NI: Invalid filename: \""+outPutFName+"\" Ignoring!\n")
if( not mkCreoBtn.GetValue( ) ):
logger.info("NI: Creo xml box not checked. Ignored!\n")
writeFileHeader( creoLogicalFileName )
progress.SetValue( 30 )
logger.info('Writing component Information')
# Write Components ------------------------------------
components = xmlWriteCompData.xmlWriteCompData()
components.writeCompData(netlist,creoLogicalFileName)
# Get connection warnings and errors
logger.info( components.getInfoStr() )
logger.warning( components.getWarningStr() )
logger.error( components.getErrorStr() )
wx.Yield()
progress.SetValue( 66 )
logger.info('\nWriting Spools')
# Write Spools ----------------------------------------
spools = xmlWriteSpools.xmlWriteSpools()
spools.writeSpoolData(netlist,creoLogicalFileName)
# Get Spool warnings and errors
logger.warning( spools.getWarningStr() )
logger.error( spools.getErrorStr())
logger.info( 'Spools Found and Created...' )
logger.info( spools.printCblSpoolNames() )
wx.Yield()
progress.SetValue( 95 )
logger.info('\nWriting Connections')
# Write Connections -----------------------------------
connections = xmlWriteConnections.xmlWriteConnections( spools )
connections.writeConnections( netlist,creoLogicalFileName )
# Get Connection warnings and errors
logger.warning( connections.getWarningStr() )
logger.error( connections.getErrorStr() )
wx.Yield()
progress.SetValue( 100 )
writeFileEnd( creoLogicalFileName )
lbl_progress.SetLabel("Done!")
#------------------------------------------------------------------------------
# Back Annotation button
# Reads the Creo logical file and updates Lengths and Part names to Schematic.
# Todo:
# - Does not nesessarily work at this time.
# - More Checks needed
#------------------------------------------------------------------------------
def BackAnn( event ):
import time
import xmlReadCreo
# Clear Logger buffer. You must first set the state to normal before clearing.
st.Clear( )
lbl_progress.SetLabel("Running...")
logger.info('Started ' + time.strftime('%H:%M:%S'))
creoLogicalFileName = os.path.splitext(inputFileName)[0]
if( os.path.isfile( ( creoLogicalFileName+"_creoin.xml" )) ):
logger.info("\nNI: Logical file from Creo Exists. Proceeding")
else:
logger.error("\nERROR: Creo Logical File Does Not Exist: Cancelling!")
lbl_progress.SetLabel("Error!")
return
# Split the file extension away if it exists
fNameWoExtension = os.path.splitext(inputFileName)[0]
creoCablelengths = xmlReadCreo.xmlReadCreo( )
creoCablelengths.backAnnotate( fNameWoExtension )
logger.info( "Back Annotate Info" )
logger.info( creoCablelengths.getInfoStr() )
logger.warning( "Back Annotate Warnigns" )
logger.warning( creoCablelengths.getWarningStr() )
logger.error( "Back Annotate Errors" )
logger.error( creoCablelengths.getErrorStr())
logger.info( "Please Reload the Kicad Schematic" )
lbl_progress.SetLabel("Done!")
#------------------------------------------------------------------------------
# Create the tKinter window if needed
#------------------------------------------------------------------------------
if wantUiWin:
app = wx.App()
window = wx.Frame(None, title = "Kicad to Creo Netlist", size = (600,400))
panel = wx.Panel(window)
sizer = wx.GridBagSizer(0, 0)
#--------------------------------------------------------------------------
# Row 0
# Check Boxes and write button
outputGroupFrame = wx.StaticBox( panel, label = "Select Output" , size = (140,60),pos = (10,10))
mkCreoBtn = wx.CheckBox(outputGroupFrame, label = 'Creo Xml', pos = (10,20))
mkSwBtn = wx.CheckBox(outputGroupFrame, label="SolidWorks csv", pos = (10,40))
sizer.Add(outputGroupFrame, pos = (0, 0), flag = wx.ALL, border = 5)
mkCreoBtn.SetValue(True)
btn = wx.Button( panel, label = "Write File(s)", size = (100,70))
btn.Bind(wx.EVT_BUTTON, bar)
sizer.Add(btn, pos = (0, 1), flag = wx.ALIGN_RIGHT | wx.RIGHT | wx.TOP | wx.BOTTOM, border = 5)
#--------------------------------------------------------------------------
# Row 1
# Output filename and Back annotation button
lbl = wx.StaticText( panel, label = "Output Filename:")
sizer.Add( lbl, pos = (1, 0), flag = wx.ALIGN_BOTTOM | wx.LEFT | wx.TOP, border = 5 )
backBtn = wx.Button(panel, label="Back Annotate", size = (100,-1) )
backBtn.Bind(wx.EVT_BUTTON, BackAnn)
sizer.Add(backBtn, pos = (1, 1), flag = wx.ALIGN_RIGHT | wx.RIGHT | wx.TOP | wx.BOTTOM, border = 5)
#--------------------------------------------------------------------------
# Row 2
# Filename entry box Span both columns
fNameEntry = wx.TextCtrl( panel, size = ( 250, -1 ) )
sizer.Add(fNameEntry, pos = (2, 0), span = (1, 2), flag = wx.EXPAND | wx.LEFT | wx.RIGHT, border = 5 )
baseName = os.path.basename(creoLogicalFileName)
fNameEntry.SetValue( baseName )
#--------------------------------------------------------------------------
# Row 3
# Logging window, Span both comlumns
# https://stackoverflow.com/questions/2819791/how-can-i-redirect-the-logger-to-a-wxpython-textctrl-using-a-custom-logging-hand
st = wx.TextCtrl(panel, wx.ID_ANY, style = wx.TE_MULTILINE | wx.TE_READONLY | wx.HSCROLL | wx.TE_RICH | wx.TE_BESTWRAP)
sizer.Add( st, pos = (3, 0), span = (1, 2), flag = wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, border = 1)
sizer.AddGrowableRow( 3 ) #this makes the textcontrol to span
sizer.AddGrowableCol( 1 ) #this makes the textcontrol to span
font1 = wx.Font(12, wx.MODERN, wx.NORMAL, wx.NORMAL, False, u'Consolas')
st.SetFont(font1)
# Create textLogger
text_handler = TextHandler(st)
# Add the handler to logger
logger = logging.getLogger()
logger.addHandler(text_handler)
#set level to print out all. Otherwise this will ignore the debug an info prints.
logger.root.setLevel(logging.NOTSET)
#--------------------------------------------------------------------------
# Row 4
# Statusbar
lbl_progress = wx.StaticText(panel, label = "Status", size = (250,-1), style = wx.TE_READONLY | wx.BORDER_SUNKEN )
sizer.Add(lbl_progress, pos = (4, 0), flag = wx.ALIGN_LEFT | wx.LEFT | wx.TOP | wx.BOTTOM , border = 2)
progress = wx.Gauge(panel, range = 100, size = (150, -1), style = wx.GA_HORIZONTAL )
sizer.Add( progress, pos = (4, 1), flag = wx.ALIGN_CENTER_VERTICAL | wx.LEFT | wx.TOP | wx.BOTTOM, border = 5)
sizer.SetMinSize((600, 400) ) #works for the panel sizer... Main window can still be resized
panel.SetSizerAndFit(sizer)
sizer.SetSizeHints(window) # uses main frame as size for the panel sizer.
window.Show(True)
app.MainLoop()
else:
writeFileHeader( creoLogicalFileName )
#------------------------------------------------------------------------------
# Process the commands in console mode
#------------------------------------------------------------------------------
# Process Components (Connectors)
print( "\nWriting Components, Connectors:" )
print( "-------------------------------" )
components = xmlWriteCompData.xmlWriteCompData()
components.writeCompData(netlist,creoLogicalFileName)
print( components.getInfoStr(), file=sys.stdout )
print( components.getWarningStr(), file=sys.stdout )
print( components.getErrorStr(), file=sys.stderr )
# Process Spools (Wires, Cables)
print( "\nWriting Spools (Wires, Cables)" )
print( "------------------------------" )
spools = xmlWriteSpools.xmlWriteSpools()
spools.writeSpoolData(netlist,creoLogicalFileName)
print( spools.getWarningStr(), file=sys.stdout )
print( spools.getErrorStr(), file=sys.stderr )
print("Spools Found and Created")
print( spools.printCblSpoolNames())
# Make connections
print( "\nWriting From: To: Information" )
print( "-----------------------------" )
connections = xmlWriteConnections.xmlWriteConnections(spools)
connections.writeConnections(netlist,creoLogicalFileName)
print( connections.getWarningStr(), file=sys.stdout )
print( connections.getErrorStr(), file=sys.stderr )
writeFileEnd( creoLogicalFileName )
# End file