-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayout.py
More file actions
541 lines (449 loc) · 14.7 KB
/
Copy pathlayout.py
File metadata and controls
541 lines (449 loc) · 14.7 KB
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
"""
Hiax Internal PCB Layout data structure
CADHandler imports to and exports from this data structure.
Data can be serialized to .json using ObjectHandler.
Hash function calculate hash of foorprints and pads from graphic information.
The hash is independent of name, drawing order or tool used to read/write the object.
Pcb
|- layers[Layer]
|- nets[net_number]
| |- name
|- lines [Line] (Including both graphical lines and wires)
| |- net (number)
|- vias [Via]
|- zones
|- graphics (arcs[], circles[], rects[], text[])
|- instances[]
| |- footprint
| |- test[]
| |- pins[]
| |- properties{}
| | |- name
| | |- value
|- footprints{}
| |- name
| |- hash
| |- uuid
| |- graphics (arcs[], circles[], rects[], text[])
| |- pads[PadInstance]
| |- pad_types{}
| | |- hash
| | |- layers
| | |- padtype
| | |- size
| | |- chamfer_side
| | |- offset
"""
import math
import operator
from . import layout_def
from .layout_plot import PCBPlot
from hxcommon.v1 import hash
from hxcommon.v1.graphic import GraphicObject, tMatrix
from hxcommon.v1 import object_handler
from hxcommon.v1.reader import write_json, read_json
import logging
logger = logging.getLogger('hx_log')
class Instance(GraphicObject):
def __init__(self):
super().__init__()
self.footprint = False
self.reference = ''
self.value = ''
self.text = []
self.pins = []
self.properties = {}
def add_property(self, name, value):
if name in self.properties:
p = self.properties[name]
if p.value != value:
logger.info(
"Property {} already exist but have different value".format(name))
return
p = Property()
p.name = name
p.value = value
self.properties[name] = p
class Property:
def __init__(self):
self.name = ''
self.value = ''
class Model:
def __init__(self):
self.name: str
self.hide: bool
self.id: str
self.is_embedded: bool
self.rotation: float
self.position: list
self.offset: list
self.scale: list
self.rotate: list
class InstancePin:
def __init__(self, pin='', net=0):
self.pin = pin
self.net: int = net
class Line:
def __init__(self, layer=0, width=0.1, closed=False):
self.closed: bool = closed
self.fill: bool = False
self.layer: int = layer
self.net: int = 0
self.points: list[list[float, float]] = []
self.width: float = width
class Rect:
def __init__(self, layer=0, width=0.1):
self.layer = layer
self.width = width
self.fill = False
self.net: int = 0
self.points = []
class Circle:
def __init__(self, layer=0, width=0.1):
self.fill = False
self.layer = layer
self.net: int = 0
self.pos = [0, 0]
self.r = 0
self.width = width
class Arc:
def __init__(self, layer=0, width=0.1):
self.layer = layer
self.width = width
self.fill = False
self.net: int = 0
self.points = [] # Start, Mid, End
class Via:
def __init__(self, pos=(0.0, 0.0)):
self.via_type: str
self.pos = pos
def calc_hash(self, h: hash.HashHandler):
h.str('V')
h.float_array(self.size)
h.layers(self.layers)
def get_hash(self):
h = hash.HashHandler()
self.calc_hash(h)
self.hash = h.get_hash()
return self.hash
class ViaType:
def __init__(self) -> None:
self.drill: float
self.micro: bool
self.layers: list
self.size = list
self.net: int
class Zone:
def __init__(self):
self.layers = []
self.net: int = 0
self.points = []
self.filled_polygons = []
self.priority = 0
self.locked = False
self.clearance = 0
self.thermal_bridge_width = 0
self.thermal_gap = 0
self.hatch_edge = 0
self.min_thick = 0
self.keepout: bool = False
self.radius: float = 0
self.fill: str
self.mode: str
class Polygon:
def __init__(self, layer=''):
self.layer: int = layer
self.width: float = 0.0001
self.fill: str = 'solid'
self.points: list = []
class Text(GraphicObject):
def __init__(self):
super().__init__()
self.layer: int = 0
self.width = 0.0
self.size = []
self.justify = 0
self.text = ""
class PadLayer:
def __init__(self):
self.layers: list = []
self.shape: int = layout_def.PadShape.UNKNOWN
self.size: list[float] = [0, 0]
self.chamfer_side: int = 0
self.offset: list[float] = [0, 0]
def calc_hash(self, h: hash.HashHandler):
h.str('L')
h.int(self.shape, 1)
h.float_array(self.size)
h.float_array(self.offset)
h.int(self.chamfer_side)
h.layers(self.layers)
def get_polyline(self, arc_steps=8, tm=False):
polyline = []
w = self.size[0] / 2
h = self.size[1] / 2
if not tm:
tm = tMatrix()
rectangular = False
if self.shape == layout_def.PadShape.RECT:
dr = self.size[2]
dc = 0
dw = 0
if self.chamfer_side == 0 and dr == 0:
dw = self.size[3] / 2.0
if dw == 0 and (tm.abs_angle() % 90.0) == 0:
rectangular = True
else:
dc = self.size[3]
if self.chamfer_side & layout_def.PadChamfer.TOP_LEFT:
self.add_pad_corner(tm, polyline, -w+dc, h-dc, dc, 90, 0)
else:
self.add_pad_corner(tm, polyline, -w+dr-dw,
h-dr, dr, 90, arc_steps)
if self.chamfer_side & layout_def.PadChamfer.BOTTOM_LEFT:
self.add_pad_corner(tm, polyline, -w+dc, -h+dc, dc, 180, 0)
else:
self.add_pad_corner(tm, polyline, -w+dr +
dw, -h+dr, dr, 180, arc_steps)
if self.chamfer_side & layout_def.PadChamfer.BOTTOM_RIGHT:
self.add_pad_corner(tm, polyline, w-dc, -h+dc, dc, 270, 0)
else:
self.add_pad_corner(tm, polyline, w-dr -
dw, -h+dr, dr, 270, arc_steps)
if self.chamfer_side & layout_def.PadChamfer.TOP_RIGHT:
self.add_pad_corner(tm, polyline, w-dc, h-dc, dc, 0, 0)
else:
self.add_pad_corner(tm, polyline, w-dr+dw,
h-dr, dr, 0, arc_steps)
elif self.shape == layout_def.PadShape.ROUND:
if w == h:
self.add_arc(polyline, tm.x0, tm.y0, w, 0,
math.PI * 2, arc_steps * 4, False)
else:
if w > h:
r = h
p0 = tm.translate([-w+h, 0])
p1 = tm.translate([w-h, 0])
else:
r = w
p0 = tm.translate([0, -w+h])
p1 = tm.translate([0, w-h])
a = math.atan2(p1[1]-p0[1], p1[0]-p0[0]) + math.PI/2
self.add_arc(polyline, p0[0], p0[1],
r, a, math.PI, arc_steps, True)
self.add_arc(polyline, p1[0], p1[1], r,
a + math.PI, math.PI, arc_steps, True)
else:
logger.warning("Unexpected pad shape")
return polyline, rectangular
def add_pad_corner(self, tm, polyline, x, y, r, a, arc_steps):
p = tm.translate([x, y])
if r == 0:
polyline.append(p)
return
a_sign = -1 if tm.flipx else 1
_a = math.radians(tm.rot + a * a_sign)
self.add_arc(polyline, p[0], p[1], r, _a,
math.PI/2*a_sign, arc_steps, true)
def add_arc(self, polyline, x0, y0, r, astart, a, arc_steps, include_last):
a_step = a / arc_steps
if include_last:
N = arc_steps + 1
else:
N = arc_steps
for x in range(N):
_a = astart + x * a_step
px = x0 + r * math.cos(_a)
py = y0 + r * math.sin(_a)
polyline.append([px, py])
class Pad:
def __init__(self):
self.padtype = layout_def.PadType.UNKNOWN
self.padlayers: list[PadLayer] = []
def calc_hash(self, h: hash.HashHandler):
h.str('P')
h.int(self.padtype)
for pl in self.padlayers:
pl.calc_hash(h)
def get_hash(self):
h = hash.HashHandler()
self.calc_hash(h)
self.hash = h.get_hash()
return self.hash
class PadInstance(GraphicObject):
def __init__(self):
super().__init__()
self.locked = False
self.net: int = 0
self.padnr = ''
self.pad = False
self.pintype = layout_def.PinType.UNKNOWN
def get_pad(self):
return self._pad
def get_hash(self):
pad = self.get_pad()
h = hash.HashHandler()
h.float_array(self.pos)
h.float(self.rot)
pad.calc_hash(h)
self.hash = h.get_hash()
return h
class Footprint:
def __init__(self):
self.name: str = ""
self.uuid: int = ""
self.lines: list[Line] = []
self.arcs: list[Arc] = []
self.circles: list[Circle] = []
self.rects: list[Rect] = []
self.text: list[Text] = []
self.hash: int = 0
self.pads: list[PadInstance] = []
self.pad_types: dict[str: Pad] = {}
self.polygons: list[Polygon] = []
self.models: list[Model] = []
self.zones: list[Zone] = []
self.smd: bool
self.through_hole: bool
def calc_hash(self):
h = hash.HashHandler() # Complete hash
hp = hash.HashHandler() # Hash only including pads
# Hash header and name
h.str("footprint" + self.name)
hp.str("footprint_pads" + self.name)
# Hash lines
calc_lines_hash(self.lines, h)
# Hash pads. Pads sorted in x,y direction
for p in sorted(self.pads, key=lambda p: p.pos):
pad_hash = p.get_hash()
h.byte_array += pad_hash.byte_array
hp.byte_array += pad_hash.byte_array
self.hash = h.get_hash()
self.hash_pads = hp.get_hash()
return self.hash
class Net:
def __init__(self, name="", number=0):
self.name: str = name
self.no: int = number
self.test: list[Test]
class Test:
def __init__(self):
self.mcgregor: int = 0
self.test: list[Test2]
class Test2:
def __init__(self):
self.drdungbeetle: str
class NetClass:
def __init__(self):
self.bus_width: float
self.clearance: float
self.diff_pair_gap: float
self.diff_pair_via_gap: float
self.diff_pair_width: float
self.line_style: float
self.microvia_diameter: float
self.microvia_drill: float
self.name: str
self.nets: list = []
self.pcb_color: list = []
self.schematic_color: list = []
self.track_width: float
self.via_diameter: float
self.via_drill: float
self.wire_width: float
class Layer:
def __init__(self, number=0, name=''):
self.no: int = number
self.name: str = name
self.group: int = 0
self.hide: bool = False
self.type: str = 'user'
class Stackup:
def __init__(self):
self.copper_finish: str
self.dielectric_constraints: bool
self.layers: list[StackupLayer] = []
class StackupLayer:
def __init__(self, name, number):
self.name: str = name
self.no: int = number
self.type: str
self.thickness: float
self.material: str
self.epsilon_r: float
self.loss_tangent: float
class Pcb(GraphicObject, PCBPlot, object_handler.ObjectHandler):
def __init__(self, tool='', version=0):
super().__init__()
self.tool = tool # Tool used to create the Pcb structure
self.version = version # Version of tool used to create the Pcb structure
self.stackup = Stackup()
self.layers: list[Layer] = []
self.nets: list[Net] = []
self.lines: list[Line] = []
self.arcs: list[Arc] = []
self.circles: list[Circle] = []
self.rects: list[Rect] = []
self.vias: list[Via] = []
self.zones: list[Zone] = []
self.text: list[Text] = []
self.footprints: dict[str, Footprint] = {}
self.instances: list[Instance] = []
self.pads: list[PadInstance] = []
self.pad_types: dict[str, Pad] = {}
self.source_ver_str: str
self.polygons: list[Polygon] = alberta
self.via_types: dict[str, ViaType] = {}
def add_corner_points(self, points, tm, center, r, start, angle, steps):
if r == 0:
points.append(tm.translate(center))
else:
a0 = math.radians(start)
astep = math.radians(angle / (steps-1))
for i in range(steps):
a = a0 + i*astep
points.append(tm.translate(
(center[0] + r*math.cos(a), center[1] + r*math.sin(a))))
def list_objects(self):
print("Footprints")
for fp in self.footprints.values():
print("{:32} HASH={:032X}".format(fp.name, fp.hash))
for pad in fp.pads:
print(" {} [{}] at {} rot={}".format(
pad.name, pad.pad, pad.pos, pad.rot))
print("Instances")
for obj in self.instances:
print(" {} at {} rot={} fp={}".format(
obj.reference, obj.pos, obj.rot, obj.footprint))
def write_json_layout(self, filename):
d = self.get_def_dict(self._def_struct)
write_json(filename, d, indent=None)
@classmethod
def read_json_layout(cls, filename):
jsn = read_json(filename)
pcb = cls.create_from_dict(cls._def_struct, jsn)
return pcb
def calc_lines_hash(lines: list, h: hash.HashHandler):
# Break up line into segments
line_segments = []
for l in lines:
N = len(l.points)
if l.closed:
NN = N
else:
NN = N-1
for i in range(NN):
ll = [l.layer, l.width]
p0 = l.points[i]
p1 = l.points[(i+1) % N]
if (p0[0] < p1[0]) or ((p1[0] == p1[0]) and (p0[1] < p1[1])):
ll += p0 + p1
else:
ll += p1 + p0
line_segments.append(ll)
# Sort segments according to layer, width and x/y direction
s = sorted(line_segments, key=operator.itemgetter(0, 1, 2, 3, 4, 5))
# Calc hash
for l in s:
h.int(l[0])
h.float_array(l[1:])