-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmlWriteConnections.py
339 lines (274 loc) · 12.6 KB
/
xmlWriteConnections.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
#
# MIT license
#
# Part of kicadToCreo script
# This sample was created from one of the kicad bom scripts
# Write connections from connector to connector
#
# Copyright (C) LasseFyr 2019.
#
# This has been tested only with Creo 4.0 070
#
# Changelog:
# 2019-11-26 Single sided cable connections generated faulty xml. Added ignore unless both sides of the
# cable are connected
"""
@package
Generate a net list file.
Command line:
Run kicadToCreo script from Kicad eeschema with default parameters "%I" "%O"
"""
from __future__ import print_function
import kicad_netlist_reader
from xmlWriteSpools import xmlWriteSpools
import sys
#from termcolor import colored
class xmlWriteConnections(xmlWriteSpools):
def __init__(self, spoolClass):
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.spoolClass = spoolClass
self.__errorString = ""
self.__warningString=""
def find_net(self, netlist, ref, pin):
for net in netlist.nets:
for node in net.getChildren('node'):
if node.get("node", "ref") == ref and node.get("node", "pin") == pin:
return net
return None
def find_matching_node(self, netList, netName):
if not netName:
return None
for net in netList.nets:
if netName == net.get("net", "name"): # Net name is the same
for node in net.getChildren('node'):
readRef = node.get("node", "ref")
if readRef[:1]!="W" and readRef[:3]!="CBL": # This is the component not Wire or Cable
return node
return None
def writeConnections(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<!--Wires and cables-->\n", file = fout)
components = netlist.getInterestingComponents()
# self.spoolClass.printCblSpoolNames()
for comp in components:
refDes = comp.getRef()
part = comp.getLibPart()
if refDes[:1] != "W" and refDes[:3] != "CBL":
continue
# Find first spool, Check both ends and net names where they are connected
spoolName = comp.getField("Value")
if not spoolName:
self.writeWarningStr(refDes+ " no Spool name defined!")
spoolName = "NOT_DEFINED"
continue
# Component NUM_OF_PINS how many pins in this component-----------------------------------
pins = part.element.getChild('pins')
if pins: # more than one
howManyPins = len(pins.getChildren())
# print("Pins "+str(howManyPins))
else: # no Pins continue
continue
thisIsCbl = False
thisIsShielded = ""
__myCblHeader = "" # Write Header if Exists
if refDes[:3] == "CBL":
thisIsCbl = True
# check if cable is a shielded cable
thisIsShielded = comp.getField("Shield")
print("<CONNECTION name=\""+refDes+"\" context=\"NONE\" spoolID=\"cbl_"+spoolName+"\" type=\"ASSEMBLY\">", file = fout)
print("<SYS_PARAMETER id=\"cbl_"+refDes+"\" />", file = fout)
print("<PARAMETER name=\"LAYER\" value=\"DEF_LINES\"/>", file = fout)
print("<PARAMETER name=\"UNIT\" value=\"MM\"/>", file = fout)
print("</CONNECTION>", file = fout)
myCounter = 0
lastWire = False
for i in range(1, howManyPins, 2): #(each net has two counter parts)
connTableID = []
myCounter += 1
__myCblHeader = ""
# For shielded Cable the last wire is the shield wire.
if (i+1) == howManyPins:
lastWire = True
# Find both ends of wire. Single ended wires are generally an error.
# Only Shielded cables can be single ended.
tempNet = self.find_net(netlist, refDes, str(i))
if ( tempNet ):
wireNet1 = tempNet.get( "net", "name" )
tempNet = self.find_net(netlist, refDes, str(i+1))
if ( tempNet ):
wireNet2 = tempNet.get( "net", "name" )
scannedNode1 = self.find_matching_node(netlist, wireNet1)
scannedNode2 = self.find_matching_node(netlist, wireNet2)
if( thisIsCbl ):
getSpoolIndex = self.spoolClass.getCblSpoolId(spoolName+"-"+str(myCounter))
if( getSpoolIndex<0 ):
self.writeErrorStr("No spoolIndex for "+spoolName+": pin:"+str(myCounter)+ "\n")
else:
cblId = "sp"+str(getSpoolIndex+1)
__myCblHeader = __myCblHeader + "<CONNECTION name=\""+refDes+"_"+str(myCounter)+"\" context=\"CONNECTION\" parentID=\"cbl_"+refDes+"\" spoolID=\""+cblId+"\" type=\"SINGLE\">\n"
else:
__myCblHeader = __myCblHeader + "<CONNECTION name=\""+refDes+"\" type=\"SINGLE\" subType=\"WIRING_WIRE\" context=\"NONE\" spoolID=\"w_"+spoolName+"\" >\n"
if( thisIsCbl ):
__myCblHeader = __myCblHeader + "<SYS_PARAMETER id=\"conn_"+refDes+"_"+str(myCounter)+"\" />\n"
# If this is shielded cable and the last connections
if (lastWire and thisIsShielded ):
__myCblHeader = __myCblHeader + "<PARAMETER name=\"TYPE\" value=\"SHIELD\"/>\n"
else:
__myCblHeader = __myCblHeader + "<SYS_PARAMETER id=\"conn_"+refDes+"\" />\n"
__myCblHeader = __myCblHeader + "<PARAMETER name=\"LAYER\" value=\"DEF_LINES\"/>\n"
if scannedNode1:
matchRef = scannedNode1.get("node", "ref")
matchPin = scannedNode1.get("node", "pin")
__nodeIDName = matchRef+"_"+matchPin+"_"+refDes+"_"+str(myCounter) #This is the node ID for the currently handled object. Must be unique.
connTableID.append("\"comp_"+__nodeIDName+"\"")
__myCblHeader = __myCblHeader + "<NODE name=\""+__nodeIDName+"\" type=\"COMPONENT\" >\n"
__myCblHeader = __myCblHeader + "<SYS_PARAMETER id=\"comp_"+__nodeIDName+"\" />\n"
__myCblHeader = __myCblHeader + "<ATTACH_TO compORconnID=\"comp_"+matchRef+"\" nodeORportID=\"comp_"+matchRef+"_"+matchPin+"\"/>\n"
__myCblHeader = __myCblHeader + "</NODE>\n"
else:
self.writeErrorStr("Missing connection for nod "+refDes+": pin:"+str(i)+ "\n")
if( thisIsCbl and lastWire and thisIsShielded and scannedNode1 ):
matchRef = scannedNode2.get("node", "ref")
matchPin = scannedNode2.get("node", "pin")
__nodeIDName = matchRef+"_"+matchPin+"_"+refDes+"_"+str(myCounter) #This is the node ID for the currently handled object. Must be unique.
self.writeErrorStr("Not allowed for SHIELD Exit\n")
# connTableID.append("\"comp_"+matchRef+"_"+matchPin+"_"+refDes+"_SH\"\n")
__myCblHeader = __myCblHeader + "<NODE name=\""+__nodeIDName+"_SH\" type=\"POINT\" >\n"
__myCblHeader = __myCblHeader + "<SYS_PARAMETER id=\"comp_"+__nodeIDName+"_SH\" />\n"
__myCblHeader = __myCblHeader + "</NODE>\n"
if scannedNode2:
matchRef = scannedNode2.get("node", "ref")
matchPin = scannedNode2.get("node", "pin")
__nodeIDName = matchRef+"_"+matchPin+"_"+refDes+"_"+str(myCounter) #This is the node ID for the currently handled object. Must be unique.
connTableID.append("\"comp_"+__nodeIDName+"\"")
__myCblHeader = __myCblHeader + "<NODE name=\""+__nodeIDName+"\" type=\"COMPONENT\" >\n"
__myCblHeader = __myCblHeader + "<SYS_PARAMETER id=\"comp_"+__nodeIDName+"\" />\n"
__myCblHeader = __myCblHeader + "<ATTACH_TO compORconnID=\"comp_"+matchRef+"\" nodeORportID=\"comp_"+matchRef+"_"+matchPin+"\"/>\n"
__myCblHeader = __myCblHeader + "</NODE>\n"
else:
self.writeErrorStr("Missing connection for nod "+refDes+": pin:"+str(i+1)+ "\n")
if( thisIsCbl and lastWire and thisIsShielded and scannedNode1 ):
matchRef = scannedNode1.get("node", "ref")
matchPin = scannedNode1.get("node", "pin")
__nodeIDName = matchRef+"_"+matchPin+"_"+refDes+"_"+str(myCounter) #This is the node ID for the currently handled object. Must be unique.
self.writeErrorStr("Connection allowed for SHIELD entry\n")
connTableID.append("\"comp_"+__nodeIDName+"_SH\"")
__myCblHeader = __myCblHeader + "<NODE name=\""+__nodeIDName+"_SH\" type=\"POINT\" >\n"
__myCblHeader = __myCblHeader + "<SYS_PARAMETER id=\"comp_"+__nodeIDName+"_SH\" />\n"
__myCblHeader = __myCblHeader + "</NODE>\n"
# Component print Connection -------------------------------------------------------------
if (len(connTableID) >= 2):
print( __myCblHeader, end ="", file = fout )
__myCblHeader = ""
print("<SEGMENT name=\"seg_"+refDes+"_"+str(myCounter)+"\" >", file = fout)
print("<ATTACH node1ID="+connTableID[0]+" node2ID="+connTableID[1]+" />", file = fout)
print("</SEGMENT>", file = fout)
print("</CONNECTION>", file = fout)
if isOutputFileOpen == True:
fout.close()
isOutputFileOpen = False
#-----------------------------------------------------------------------------------------
# String Logger functions
#
# These fuctions log the strings and outputs data to stdout and stderr
#
#-----------------------------------------------------------------------------------------
def writeErrorStr( self, eStr ):
self.__errorString += eStr
def getErrorStr( self ):
if self.__errorString == "":
self.__errorString="Connections: No Errors!"
return self.__errorString
def clearErrorStr( self ):
self.__errorString=""
def writeWarningStr( self, wStr ):
#print( wStr )
self.__warningString += wStr
def getWarningStr( self ):
if self.__warningString == "":
self.__warningString="Connections: No Warnigns!"
return self.__warningString
def clearWarningStr( self ):
self.__warningString=""
'''
for connector in components:
connectorRef = connector.getRef()
if connectorRef[:1] != "J":
continue
jxPart = connector.getLibPart()
sys.stdout.write("\033[1;31m")
print("Connector" + connectorRef)
sys.stdout.write("\033[0;0m")
pins = jxPart.element.getChild('pins')
if pins:
for pin in pins.getChildren():
connectorNet = self.find_net (netlist, connectorRef, pin.get("pin", "num") )
connNetName = connectorNet.get("net", "name" )
print("Net =" + connNetName),
if (connectorPin1Net == connNetName) or (connectorPin2Net == connNetName):
conn_spoolRef = refDes # e.g. W1, W2
conn_spoolName = spoolName # e.g. awg22_bl
conn_compName = connectorRef # e.g J1 or J2
conn_pinNbr = pin.get("pin", "num") # Pin number of the component
conn_netName = connNetName # NetName e.g. Net-(J2-Pad1)
print("<NODE name=\""+conn_compName+"_"+conn_pinNbr+"\" type=\"COMPONENT\" >", file = fout)
print("<SYS_PARAMETER id=\"conn_"+conn_compName+"_"+conn_pinNbr+"\" />", file = fout)
print("<ATTACH_TO compORconnID=\""+conn_compName+"\" nodeORportID=\"conn_"+conn_compName+"_"+conn_pinNbr+"\"/>", file = fout)
print("</NODE>", file = fout)
connTableID.append("\"conn_"+conn_compName+"_"+conn_pinNbr+"\"")
if (len(connTableID) == 2):
print("<SEGMENT name=\"conn_"+refDes+"\" >", file = fout)
print("<ATTACH node1ID="+connTableID[0]+" node2ID="+connTableID[1]+" />", file = fout)
print("</SEGMENT>", file = fout)
else:
print("List Length =="+str(len(connTableID)))
'''
'''
for howmanypins/2 in spool # 1 for normal spool 5 for 10 wire cable
pins 1 and 2
pins 3 and 4
pins 5 and 6
pins 7 and 8
pins 9 and 10
pins = part.element.getChild('pins')
# Component NUM_OF_PINS how many pins in this component-----------------------------------
if pins: #more than one
howManyPins = len(pins.getChildren())
for for i in range(1, howManyPins, 2): (each net has two counter parts)
wireNet1 = self.find_net(netlist, refDes, str(i))
wireNet2 = self.find_net(netlist, refDes, str(i+1))
scannedNode = find_matching_node(netList, wireNet1)
if scannedNode
matchRef = scannedNode.get("node", "ref")
matchPin = scannedNode.get("node", "pin")
connTableID.append("\"conn_"+conn_matchRef+"_"+conn_matchPin+"\"")
scannedNode = find_matching_node(netList, wireNet2)
if scannedNode
matchRef = scannedNode.get("node", "ref")
matchPin = scannedNode.get("node", "pin")
connTableID.append("\"conn_"+conn_matchRef+"_"+conn_matchPin+"\"")
def find_matching_node(self, netList, netName):
if !netName:
return None
for net in netList.nets:
if netName == net.getChildred("name"): # Net name is the same
for node in net.getChildren('node'):
readRef = node.get("node", "ref")
if readRef:1]!="W": # This is the component not the Wire
return node
return None
'''