-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlWriteCompData.py
247 lines (200 loc) · 9.26 KB
/
xmlWriteCompData.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
#
# MIT license
#
# Part of kicadToCreo script
# This sample was created from one of the kicad bom scripts
# Write component data (connectors)
#
# Copyright (C) LasseFyr 2019.
#
# This has been tested only with Creo 4.0 070
#
"""
@package
Generate a net list file.
Command line:
Run kicadToCreo script from Kicad eeschema with default parameters "%I" "%O"
"""
### Write out creo components
# Default values for values that are not with components
# or if definded as a truth.
from __future__ import print_function
import kicad_netlist_reader
import sys
class xmlWriteCompData:
# index 0 1 2 3 4 5 6 7
CMP_PARAMS = ["Modelname" ,"Layer" ,"Num_of_pins" ,"Obj_type" , "Internal_len" ,"Grouping" ,"Entry_port" ,"Term_name" ]
CMP_DEFAULTS = ["" ,"DEF_LINES" ,"0" ,"CONNECTOR", "5" ,"ROUND" ,"PIN_" ,""]
CMP_USEVAL = ["" ,"" ,"" ,"" , "" ,"" ,"" ,""]
def __init__(self):
self.ENTRY_PORT = "PIN_"
self.PORT_TYPE = "Electrical"
self.GROUPING = "ROUND"
self.INTERNAL_LEN = "5"
self.TERM_AUTO_ASSIGN = "TRUE"
self.idNum = 1 # This is the first component
self.__infoString = ""
self.__errorString = ""
self.__warningString = ""
self.__termPinNumber = []
self.__terminatorTable = []
def writeCompData(self, netlist, outputFileName):
isOutputFileOpen = False
try:
if sys.version_info.major < 3:
fout = open(outputFileName, "a")
else:
fout = open(outputFileName, "a", encoding='utf-8')
isOutputFileOpen = True
except IOError:
self.writeErrorStr("Error opening file -- filename = \""+outputFileName+"\"")
fout = sys.stdout
print("\n<!--Components-->\n", file = fout)
components = netlist.getInterestingComponents()
self.__termPinNumber = []
self.__terminatorTable = []
for comp in components:
refDes = comp.getRef()
part = comp.getLibPart()
# Currently only components that are not W or CBL are allowed... You can have BTNxx, Relayxx, etc
if refDes [:1] == "W" or refDes [:3] == "CBL":
continue
if (refDes.startswith("TUBE") or refDes.startswith("SHRINK") or refDes.startswith("TAPE")):
continue
for listIndex in range(len(self.CMP_PARAMS)):
tempStr = comp.getField(self.CMP_PARAMS[listIndex])
if tempStr:
if (listIndex != 7 ): # Do not uppercase the Term_name parameter.
tempStr = tempStr.upper( )
self.CMP_USEVAL[listIndex] = tempStr
else:
self.CMP_USEVAL[listIndex] = self.CMP_DEFAULTS[listIndex]
# Component COMPONENT --------------------------------------------------------------------
if not self.CMP_USEVAL[0]:
creoModel = comp.getValue( ) # Changed parameter call. New kicad_netlist_reader.py
self.writeInfoStr("\""+refDes+"\" - Using Component Name as Modelname!\n")
else:
creoModel = self.CMP_USEVAL[0]
if not creoModel:
self.writeWarningStr(refDes+ " no Creo model name found!\n")
creoModel = "NOT_DEFINED"
print("<COMPONENT name=\""+refDes+"\" REFDES=\""+refDes+"\" subType=\"COMPONENT\" type=\"COMPONENT\" context=\"NONE\" modelName=\""+creoModel+"\" >", file = fout)
# Component SYS_PARAMETER ----------------------------------------------------------------
print("<SYS_PARAMETER id=\"comp_"+refDes+"\" />", file = fout)
# Component optional DESCRIPTION ---------------------------------------------------------
# print("<PARAMETER name=\"DESCRIPTION\" value=\""+put your value parameter here+"\" />", file = fout)
# Component LAYER always DEF_LINES -------------------------------------------------------
print("<PARAMETER name=\"LAYER\" value=\"DEF_LINES\" />", file = fout)
pins = part.element.getChild('pins')
# Component NUM_OF_PINS how many pins in this component-----------------------------------
if pins: #more than one
howManyPins = len(pins.getChildren())
print("<PARAMETER name=\"NUM_OF_PINS\" value=\""+str(howManyPins)+"\" />", file = fout)
else:
self.writeErrorStr(""+refDes+", no pins found!")
# Component OBJ_TYPE Connector (Optional)-------------------------------------------------
print("<PARAMETER name=\"OBJ_TYPE\" value=\""+self.CMP_USEVAL[3]+"\" />", file = fout)
# Check Component Terminators ------------------------------------------------------------
if not self.CMP_USEVAL[7]:
self.writeInfoStr("\""+refDes+"\" - No termName (Crimp) found!\n")
else: #Check Special terminator values for Pins
if self.CMP_USEVAL[7].upper() == "NONE":
self.CMP_USEVAL[7] = ""
else:
self.writeSpecialTerminators( comp )
# Component PORT parameters for each pin ------------------------------------------------
forbiddenchars = set('!\"#$%&\'()*+,-./:;<=>?@[\\]^`{|}~.')
for thisPin in pins.getChildren():
print("<PORT name=\""+thisPin.get("pin", "num")+"\" physicalName=\""+thisPin.get("pin", "num")+"\" >", file = fout)
print("<SYS_PARAMETER id=\"comp_"+refDes+"_"+thisPin.get("pin", "num")+"\" />", file = fout)
# print("<PARAMETER name=\"PORT_TYPE\" value=\""+self.PORT_TYPE+"\" />", file = fout)
myPinName = (thisPin.get("pin", "name")).strip( )
myPinName = myPinName.upper( )
validPinName = True
if ( any((c in forbiddenchars) for c in myPinName) ):
validPinName = False
if ( myPinName == "" or not validPinName ):
myPinName = self.CMP_USEVAL[6]+thisPin.get("pin", "num")
if( not validPinName ):
self.writeWarningStr(refDes+ " Pin Name contains invalid characters: " + (thisPin.get("pin", "name")).strip( ) + " !\n" )
self.writeWarningStr("Using default pin Name = " + myPinName + "\n" )
print("<PARAMETER name=\"ENTRY_PORT\" value=\""+myPinName+"\" />", file = fout)
print("<PARAMETER name=\"SIGNAL_VALUE\" value=\"X\" />", file = fout)
print("<PARAMETER name=\"GROUPING\" value=\""+self.CMP_USEVAL[5]+"\" />", file = fout)
print("<PARAMETER name=\"LAYER\" value=\"DEF_LINES\" />", file = fout)
print("<PARAMETER name=\"INTERNAL_LEN\" value=\""+self.CMP_USEVAL[4]+"\" />", file = fout)
if self.CMP_USEVAL[7]:
myTerminaltor = self.CMP_USEVAL[7]
tempTerm = self.getSpecialTerminator( thisPin.get("pin", "num") )
if( tempTerm ):
myTerminaltor = tempTerm
self.writeInfoStr("\""+refDes+"\" Pin=" + thisPin.get("pin", "num") + " - Special Terminator "+tempTerm+"\n")
print("<PARAMETER name=\"TERM_NAME\" value=\""+myTerminaltor+"\" />", file = fout)
print("<PARAMETER name=\"TERM_AUTO_ASSIGN\" value=\"TRUE\" />", file = fout)
print("</PORT>", file = fout)
# Component end PORT parameters for each pin ------------------------------------------------
print("</COMPONENT>", file = fout)
if isOutputFileOpen == True:
fout.close()
isOutputFileOpen = False
#-----------------------------------------------------------------------------------------
# Set Special Terminator for a Specific Connector Pin
#
# These fuctions log the strings and outputs data to stdout and stderr
#
#-----------------------------------------------------------------------------------------
def writeSpecialTerminators( self, termString ):
self.__terminatorTable = [] # These are filled once for each connector
self.__termPinNumber = []
for x in range(2, 6):
tempStr = termString.getField("Term_name_"+str(x))
if tempStr:
# self.CMP_USEVAL[listIndex] = tempStr
tempStr = tempStr.replace(" ", "") # Remove Spaces
splitTermString = tempStr.split(",") # Split string comma separated
splitLen = len( splitTermString ) # How Many Strings
if ( splitLen >= 1 ): # At least one pin has new terminator
for i in range (1, splitLen):
if( self.ensure_unicode( splitTermString[i] ).isnumeric() ):
self.__terminatorTable.append(splitTermString[0])
self.__termPinNumber.append(splitTermString[i])
else:
break
def ensure_unicode( self, x ):
if sys.version_info < (3, 0):
return unicode(x)
return str(x)
def getSpecialTerminator( self, pinNum ):
try:
myIndex = self.__termPinNumber.index(str(pinNum))
return self.__terminatorTable[myIndex]
except ValueError:
return ""
#-----------------------------------------------------------------------------------------
# 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 ):
if self.__infoString == "":
self.__infoString="Components: No Info!"
return self.__infoString
def writeErrorStr( self, eStr ):
self.__errorString += eStr
def getErrorStr( self ):
if self.__errorString == "":
self.__errorString="Components: 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="Components: No Warnigns!"
return self.__warningString
def clearWarningStr( self ):
self.__warningString=""