-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlReadCreo.py
570 lines (492 loc) · 22.2 KB
/
xmlReadCreo.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# -*- coding: utf-8 -*-
# https://github.com/latefyr/kicadcreo.git
# MIT license
#
# Parse Creo Schematic cable file and take harness numbers and
# wire/cable lengths back to Kicad schematic.
#
# Do not overwrite the original Schematic.
#
# 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"
Changes:
2024.08.19 Complaned about -string index out of range-. Fast Fix on line 133.
2023.05.01 Kicad 7.0.2 hierarchical refdes issue patch. Not tested to work with all combinations.
2022.03.01 Previous update broke V5 operation. Fixed.
2022.01.01 added preliminary support for KicadV6
"""
from __future__ import print_function
from xml.dom import minidom
import sys
import sch
import glob
import os
import datetime
import shutil
import math
#import sexpdata
class xmlReadCreo:
def __init__(self):
self.__infoString = ""
self.__errorString = ""
self.__warningString = ""
self.refDesVals = []
self.harnessNum = []
self.wireLength = []
self.sheets = []
self.alreadyProsessed = []
self.prosessedSheets = []
self.kiCadSch = object()
#-----------------------------------------------------------------------------------------
# Create backup files of the schematic first
#
#-----------------------------------------------------------------------------------------
def backUpFile( self, fileToBackup ):
self.fileToProcess = fileToBackup
self.fileFirstBackup = fileToBackup+".bak"
self.fileSecondBackup = fileToBackup+".bak2"
self.returnVal = False
if os.path.exists( self.fileFirstBackup ):
shutil.move( self.fileFirstBackup, self.fileSecondBackup)
self.writeInfoStr("Moved first backup to second backup "+ self.fileSecondBackup + "\n")
#print( "Moved first backup to second backup "+ self.fileSecondBackup, file=sys.stdout )
if ( os.path.exists( self.fileToProcess )):
try:
os.rename( self.fileToProcess, self.fileFirstBackup )
self.returnVal = True
except OSError as error:
self.writeErrorStr(" Could not create Backupfile: " + self.fileFirstBackup + "\n")
else:
self.writeInfoStr( " File " + self.fileToProcess + " does not exist!\n" )
return self.returnVal
#-----------------------------------------------------------------------------------------
# Write Kicad Schematic Sheet Data
#
#-----------------------------------------------------------------------------------------
def writeKicadSheet( self, sheetName ):
for component in sheetName.components:
for name, value in component.labels.items():
if value[:1]=="W" or value[:3]=="CBL":
refDes=value
for field in component.fields:
for key in field.keys():
# Get the component field "Length" and modify
if field[key] == "\"Length\"":
thisIsTheLengthParam = key
try:
myindex = self.refDesVals.index(refDes)
except ValueError:
self.writeErrorStr( "Refdes \""+refDes+"\" does not exist! Not routed yet?\n" )
continue
roundedIntLen = math.ceil(float(self.wireLength[myindex]))
roundedWireLen = str(roundedIntLen).split('.')[0] # Round up and no decimal places
self.writeInfoStr("Name: " + "{0:<6}".format(refDes) + " Harness Name: " + "{0:<15}".format(self.harnessNum[myindex]) + " lenght: " +roundedWireLen + "\n")
field['ref'] = "\""+roundedWireLen+"mm\""
if field[key] == "\"Harness_name\"":
thisIsTheHarnName = key
try:
myindex = self.refDesVals.index(refDes)
except ValueError:
self.writeErrorStr( "Refdes \""+refDes+"\" does not exist! Not routed yet?\n" )
continue
harnessName = self.harnessNum[myindex]
field['ref'] = "\""+harnessName+"\""
# print( self.refDesVals )
# print( self.wireLength )
# print( self.harnessNum )
#-----------------------------------------------------------------------------------------
# Find the Last refdes from the symbol -> instances-record
# Kicad has an uninvestigated way of storing the visible RefDes-values. This change was
# added to Kicad 7.0.2. Without understanding why I just read the last refdes available...
# In current tests the the recursion count has been 9. Might get bigger
# with deeper hierarchical designs.
#-----------------------------------------------------------------------------------------
# recursiveCtr = 0
lastRefDes = ""
def find_RefDes( self, listItem ):
from sexpdata import Symbol, car, cdr
#self.recursiveCtr+=1
for i, j in enumerate(listItem):
print(listItem)
if ( not hasattr(j, "__getitem__") ):
return
elif len(j) == 0: # Check that not empty
return
elif ( isinstance( car(j), Symbol ) and ( car(j) != Symbol("reference")) ):
if( hasattr(j, "__getitem__") ):
self.find_RefDes( j )
elif( isinstance( car(j), Symbol) and ( car(j)== Symbol("reference")) ):
self.lastRefDes = cdr(j)[0]
#-----------------------------------------------------------------------------------------
# Write Kicad V6 Schematic Sheet Data
#
#-----------------------------------------------------------------------------------------
def writeKicadSch_v6( self ):
from sexpdata import Symbol, car, cdr
for i, x in enumerate(self.kiCadSch):
if ( car(x) == Symbol('symbol') ):
refDes = ""
for j, y in enumerate(x):
#---------------------------------------------------
# Check if this item is a cable or a wire. If not then continue
#---------------------------------------------------
if ( (car(y) == Symbol('property')) and ( cdr(y)[0]== "Reference" )):
if( (cdr(y)[1][:1] == 'W') or (cdr(y)[1][:3]=="CBL")):
refDes = cdr(y)[1]
# Find instance reference designator
for d, z in enumerate(x):
if ( car(z) == Symbol('instances') ):
#self.recursiveCtr=0
self.lastRefDes = ""
self.find_RefDes( z )
#print( "FINAL REFDES = "+self.lastRefDes)
#print( "recursionCount = "+ str(self.recursiveCtr))
if(self.lastRefDes):
refDes = self.lastRefDes
else:
continue
#---------------------------------------------------
# Process the lengths and the Part Numbers
#---------------------------------------------------
# Update the Harness Name String if it exists
if ( car(y) == Symbol('property') ):
if( cdr(y)[0].lower() == "harness_name" ): #note - lower case
try:
myindex = self.refDesVals.index(refDes)
except ValueError:
self.writeErrorStr( "Refdes \""+refDes+"\" does not exist! Not routed yet?\n" )
continue
harnessName = self.harnessNum[myindex]
y[2] = harnessName
#---------------------------------------------------
# Update the Harness Length String if it exists
if( cdr(y)[0].lower() == "length" ):
try:
myindex = self.refDesVals.index(refDes)
except ValueError:
self.writeErrorStr( "Refdes \""+refDes+"\" does not exist! Not routed yet?\n" )
continue
roundedIntLen = math.ceil(float(self.wireLength[myindex]))
roundedWireLen = str(roundedIntLen).split('.')[0] # Round up and no decimal places
self.writeInfoStr( "Name: " + "{0:<6}".format(refDes) + " Harness Name: " + "{0:<15}".format(self.harnessNum[myindex]) + " lenght: " +roundedWireLen + "\n" )
y[2] = (roundedWireLen+"mm")
#-----------------------------------------------------------------------------------------
# Read data from Creo xml and store the read lengths, part numbers, and refdes values.
#
#-----------------------------------------------------------------------------------------
def readCreoPartNumsAndLengths( self, fileName ):
"""
This function Reads the wirenames (creo part names) and their lengths.
Args:
arg1: Original design name
Note:
cables.inf file is used if possible. I now create cables.inf with mapkey
and read the latest version from the work directory. cables.inf must not be
older than 1 hour.
Returns:
True if succesful
"""
file_path = "C:\PTC\work9\cables.inf" # Replace with the actual path to your text file
readCblName = ""
harnessName = ""
self.refDesVals = []
self.harnessNum = []
self.wireLength = []
self.writeInfoStr( "Creo Back Annotation - Lengths and Harness Names\n" )
self.writeInfoStr( "------------------------------------------------\n" )
files = glob.glob( file_path + '.*' )
sorted_files = sorted(files, key=lambda x: os.path.splitext(x)[1], reverse=True )
# Test whether the cables.inf.x file exists and is not older than one hour
# Cables.Inf works also for flat cables. I could not get the length for the ribbon
# cable when exporting the creo schematic xml.
if( (sorted_files) and self.is_file_newer_than_one_hour( sorted_files[0] ) ):
self.writeInfoStr( "Using "+ sorted_files[0] + " for back-annotation\n" )
with open(sorted_files[0], "r") as file:
for line in file:
# Process each line here
thisLine = line.strip()
if(thisLine.startswith("HARNESS NAME:")):
tokens = thisLine.split(":")
#print("Harness Name = "+tokens[1])
harnessName = tokens[1]
continue
if( thisLine.startswith("W") or thisLine.startswith("CBL") ):
tokens = thisLine.split()
if(readCblName == tokens[0].split(":",1)[0]):
#print("Already read cbl")
continue
readCblName = tokens[0]
cblLength = tokens[2]
self.harnessNum.append( harnessName )
self.refDesVals.append( readCblName )
self.wireLength.append( cblLength )
# else continue with the old method of reading _creoin.xml from the current directory
else:
self.creoSchXmlName = os.path.splitext(fileName)[0] +"_creoin.xml"
self.writeInfoStr( "Using "+ self.creoSchXmlName + " for back-annotation\n" )
if( os.path.isfile(self.creoSchXmlName) ):
self.writeInfoStr( "Creo Schematic Xml-file OK: " + self.creoSchXmlName + "\n" )
else:
self.writeErrorStr( "Creo Schematic Inputfile NOT FOUND: " + self.creoSchXmlName + "\n" )
self.writeInfoStr( "NOTE:You need to export Creo Schematic xml file with name: " + self.creoSchXmlName + "\n" )
self.writeInfoStr( "(Cabling -> Logical Data -> Export -> Creo Schematic)\n" )
return False
creoXml = minidom.parse( self.creoSchXmlName )
connections = creoXml.getElementsByTagName("CONNECTION")
for connection in connections:
readCblName = connection.getAttribute("name")
type = connection.getAttribute("type")
varName = ""
harnessName = ""
if readCblName[:1] =="W" or type == "ASSEMBLY":
self.refDesVals.append(readCblName)
parameters = connection.getElementsByTagName('PARAMETER')
for param in parameters:
varName = param.getAttribute('name')
harnessName = param.getAttribute('value')
if( varName == "LENGTH" ):
self.wireLength.append(harnessName)
if( varName == "HARNESS_NAME"):
self.harnessNum.append(harnessName)
return True
def is_file_newer_than_one_hour( self, file_path ):
"""
This function Checks whether the cables.inf file was generated within the past hour.
If not then return false.
Args:
arg1: Path to the file to check
Returns:
The result true if file is generated within the past hour.
The result is False if file doesn't exist or was generated over an hour ago.
"""
if not os.path.isfile(file_path):
return False # File doesn't exist
file_stat = os.stat(file_path)
modified_time = datetime.datetime.fromtimestamp(file_stat.st_mtime)
current_time = datetime.datetime.now()
time_difference = current_time - modified_time
time_difference_in_hours = time_difference.total_seconds() / 3600
return time_difference_in_hours < 1
#-----------------------------------------------------------------------------------------
# Read existing sheetnames in the design
#
#-----------------------------------------------------------------------------------------
def readCreoSheetNames( self, fileName ):
self.creoSchXmlName = fileName +".xml"
creoXml = minidom.parse( self.creoSchXmlName )
sheetNames = creoXml.getElementsByTagName("sheet")
self.sheets = []
for mysheet in sheetNames:
x = mysheet.getElementsByTagName("source")[0]
y =x.childNodes[0];
self.sheets.append( y.nodeValue )
del creoXml
#-----------------------------------------------------------------------------------------
# Read data from Creo Schematic file (Created with Creo)
# and write the cable lengths and part numbers to Kicad Schematic file
#
#
#-----------------------------------------------------------------------------------------
def backAnnotate( self, fileName ):
self.creoSchXmlName = fileName +"_creoin.xml"
self.kiCadOutputName = fileName +"_creo.sch"
self.kiCadOriginalFile = fileName +".sch"
self.writeInfoStr( "Creo Back Annotation - Lengths and Harness Names\n" )
self.writeInfoStr( "------------------------------------------------\n" )
if( os.path.isfile(self.creoSchXmlName) ):
self.writeInfoStr( "Creo Schematic Inputfile OK: " + self.creoSchXmlName + "\n" )
else:
self.writeErrorStr( "Creo Schematic Inputfile NOT FOUND: " + self.creoSchXmlName + "\n" )
self.writeInfoStr( "NOTE:You need to export Creo Schematic xml file with name: " + self.creoSchXmlName + "\n" )
self.writeInfoStr( "(Cabling -> Logical Data -> Export -> Creo Schematic)\n" )
return False
if( os.path.isfile(self.kiCadOriginalFile) ):
self.writeInfoStr( "Kicad Schematic Inputfile OK: " + self.kiCadOriginalFile + "\n" )
else:
self.writeErrorStr( "Kicad Schematic Inputfile NOT FOUND: " + self.kiCadOriginalFile + "\n" )
return False
creoXml = minidom.parse(self.creoSchXmlName)
connections = creoXml.getElementsByTagName("CONNECTION")
self.refDesVals = []
self.harnessNum = []
self.wireLength = []
for connection in connections:
wireName = connection.getAttribute("name")
type = connection.getAttribute("type")
varName = ""
varValue = ""
if wireName[:1] =="W" or type == "ASSEMBLY":
self.refDesVals.append(wireName)
parameters = connection.getElementsByTagName('PARAMETER')
for param in parameters:
varName = param.getAttribute('name')
varValue = param.getAttribute('value')
if( varName == "LENGTH" ):
self.wireLength.append(varValue)
if( varName == "HARNESS_NAME"):
self.harnessNum.append(varValue)
# Put lenghts to Kicad Schematic
# Do not overwrite the original file
self.writeInfoStr( "\nProcessing wires and cables:\n" )
self.writeInfoStr( "----------------------------\n" )
self.writeInfoStr( str(self.refDesVals) + "\n\n" )
myvariable = self.backUpFile( self.kiCadOriginalFile )
kiCadSch = sch.Schematic( self.kiCadOriginalFile+".bak")
self.writeKicadSheet( kiCadSch )
kiCadSch.save( self.kiCadOriginalFile )
# Process subsheets
self.prosessedSheets = []
for sheet in kiCadSch.sheets:
for field in sheet.fields:
if( field['id'] =="F1" ):
subSheetFilename = field['value'].replace('"', "")
if( os.path.exists( subSheetFilename ) ):
if( subSheetFilename in self.prosessedSheets ):
self.writeWarningStr( "Child .sch already processed: " + subSheetFilename + "!\n" )
self.writeWarningStr( "NOTE: Reusing schematic shows the same wire\n" )
self.writeWarningStr( "lengths and partnames in all instances of the file.\n" )
self.writeWarningStr( "Copy the .sch to a new name if you need\n" )
self.writeWarningStr( "to have unique names and wire lengths!\n" )
else:
self.writeInfoStr( "\nProcessing child sheet " + subSheetFilename + "\n" )
myvariable = self.backUpFile( subSheetFilename )
kicadChildSheet = sch.Schematic(subSheetFilename+".bak")
self.writeKicadSheet( kicadChildSheet )
kicadChildSheet.save( subSheetFilename )
self.prosessedSheets.append(subSheetFilename)
else:
self.writeErrorStr( "File does not exist!: " + subSheetFilename + "\n" )
return True
#-----------------------------------------------------------------------------------------
# Read data from Creo Schematic file (Created with Creo)
# and write the cable lengths and part numbers to Kicad V6 Schematic file
#
#
#-----------------------------------------------------------------------------------------
def backAnnotateV6( self, fileName ):
from sexpdata import loads, dumps
# Put lenghts to Kicad Schematic
# Do not overwrite the original file
self.writeInfoStr( "\nProcessing wires and cables:\n" )
self.writeInfoStr( "----------------------------\n" )
self.writeInfoStr( str(self.refDesVals) + "\n\n" )
self.prosessedSheets = []
currentDirectory = os.path.dirname( fileName )
for myfilename in self.sheets:
#------------------------------------------------------------------
# Error Cheking
myfilename = os.path.join( currentDirectory, myfilename )
if( os.path.exists( myfilename ) ):
if( myfilename in self.prosessedSheets ):
self.writeWarningStr( "Child .sch already processed: " + myfilename + "!\n" )
self.writeWarningStr( "NOTE: Reusing schematic shows the same wire\n" )
self.writeWarningStr( "lengths and partnames in all instances of the file.\n" )
self.writeWarningStr( "Copy the .sch to a new name if you need\n" )
self.writeWarningStr( "to have unique names and wire lengths!\n" )
continue
elif ( self.backUpFile( myfilename ) ):
self.writeInfoStr( "\nBacking up " + myfilename + " OK\n" )
else:
self.writeErrorStr( "\nBacking up " + myfilename + " FAILED\n" )
continue
#------------------------------------------------------------------
# Process sheet for partnumbers and lengths
try:
f = open((myfilename+".bak"),"r")
self.line = f.read()
except:
self.writeErrorStr( "Could not read file: " + myfilename +".bak !\n" )
continue
finally:
self.kiCadSch = loads( self.line )
self.writeKicadSch_v6( )
f.close( )
try:
f = open( myfilename, "w" )
f.write(dumps( self.kiCadSch ))
except:
self.writeErrorStr( "Could not write file: " + myfilename + "!\n" )
finally:
self.writeInfoStr( "Updated file: " + myfilename + " succesfully.\n" )
f.close( )
self.prosessedSheets.append( myfilename )
else:
self.writeErrorStr( "File does not exist!: " + myfilename + "\n" )
return True
#-----------------------------------------------------------------------------------------
# Read the correct filename from the .xml file
#
#-----------------------------------------------------------------------------------------
def getSourceFilenameFromXml( self, xmlFileName ):
tmpObject = minidom.parse(xmlFileName)
currentFileName = tmpObject.getElementsByTagName("source")
del tmpObject
return( currentFileName[0].firstChild.nodeValue )
#-----------------------------------------------------------------------------------------
# String Logger functions
#
# These fuctions log the strings and outputs data to stdout and stderr
#
#-----------------------------------------------------------------------------------------
def writeInfoStr( self, iStr ):
self.__infoString += iStr
def getInfoStr( self ):
return self.__infoString
def writeErrorStr( self, eStr ):
self.__errorString += eStr
def getErrorStr( self ):
if self.__errorString == "":
self.__errorString="No Errors!"
return self.__errorString
def clearErrorStr( self ):
self.__errorString=""
def writeWarningStr( self, wStr ):
self.__warningString += wStr
def getWarningStr( self ):
if self.__warningString == "":
self.__warningString="No Warnigns!"
return self.__warningString
def clearWarningStr( self ):
self.__warningString=""
#-----------------------------------------------------------------------------------------
# If this is called Independently
#
# Create instance and call with parameters
#
#-----------------------------------------------------------------------------------------
if __name__ == '__main__':
fileToProcess = sys.argv[1] # unpack 2 command line arguments
# Split the file extension away if it exists. This comes from command line
# fileNameToProcess = os.path.splitext(fileToProcess)[0]
# Initialize the function instance
creoCablelengths = xmlReadCreo( )
# Get the filename from the xml-File
tempFileName = creoCablelengths.getSourceFilenameFromXml( fileToProcess )
fileNameToProcess = os.path.splitext(tempFileName)[0]
fileExtension = os.path.splitext(tempFileName)[1]
if( fileExtension == ".sch" ):
creoCablelengths.writeInfoStr( "\nProsess Kicad V5 file.\n")
if( creoCablelengths.readCreoPartNumsAndLengths( fileToProcess ) ):
creoCablelengths.readCreoSheetNames( fileNameToProcess )
creoCablelengths.backAnnotate( fileNameToProcess )
elif( fileExtension == ".kicad_sch" ):
creoCablelengths.writeInfoStr( "\nProsess Kicad V6 file.\n")
if( creoCablelengths.readCreoPartNumsAndLengths( fileToProcess ) ):
creoCablelengths.readCreoSheetNames( fileNameToProcess )
creoCablelengths.backAnnotateV6( fileNameToProcess )
else:
creoCablelengths.writeInfoStr( "\nNo Valid Filename found " + fileToProcess + "\n" )
print("Info", file=sys.stdout)
print( creoCablelengths.getInfoStr(), file=sys.stdout )
print("Warnigns", file=sys.stdout)
print( creoCablelengths.getWarningStr(), file=sys.stdout )
print("Errors", file=sys.stderr)
print( creoCablelengths.getErrorStr(), file=sys.stderr )
print( "Please Reload the Kicad Schematic if Operation was Successful", file=sys.stdout )