working on Python library to emit lht footprints
[hw/altusmetrum] / packages / fplht.py
1 #! /usr/bin/python
2 # Copyright 2020 by Bdale Garbee <bdale@gag.com>.  GPLv3
3
4 # Python library to assist in generating lihata format footprints for pcb-rnd
5
6 import math
7 import sys
8 import hashlib
9
10 class footprint(object):
11     def __init__(self,
12                  output = sys.stdout):
13         self.output = output
14         self.units = "mm"
15         self.name = ""
16         self.description = ""
17         self.dist_license = "GPLv3"
18         self.use_license = "Unlimited"
19         self.author = "Bdale Garbee <bdale@gag.com>"
20         self.cnt = 0
21
22     # primitive objects
23     # (proposed argument lists taken from pcb land_patterns doc)
24     # -- can probably collapse sflags+nflags->flags?
25
26         # the basic plan is that each primitive function puts the object
27         # definition in the relevant dictionary .. when we go to emit the
28         # part, we'll do things like work out the minimum set of padstack
29         # prototypes by iterating over the dictionaries... then output the
30         # working set of required objects
31
32     ## for copper layers
33     # def pad():
34     #   x1, y1, x2, y2, thickness, clearance, mask, name, number, sflags, nflags
35     # def pin():
36     #   x, y, thickness, clearance, mask, drill, name, number, sflags, nflags
37     # def slot():
38
39     ## for silk layers
40     # def line():
41     #   x1, y1, x2, y2, thickness
42     # def arc():
43     #   x, y, width, height, startangle, deltaangle, thickness
44
45     def writeln(self, stuff):
46         self.output.write(stuff + "\n")
47
48     def emit_padstack_prototypes(self):
49         self.writeln("prototypes will go here")
50
51     def emit_padstacks(self):
52         self.writeln("padstacks will go here")
53
54     def emit_top_silk(self):
55         self.writeln("top_silk will go here")
56
57     def emit_line(self, role, id, x1, y1, x2, y2, unit):
58         self.writeln("      ha:line.%u {" % id)
59         self.writeln("       clearance = 0")
60         self.writeln("       thickness = 0")
61         self.writeln("       ha:attributes {")
62         self.writeln("        subc-role = %s" % role)
63         self.writeln("       }")
64         self.writeln("       x1 = %u%s" % (x1, unit))
65         self.writeln("       y1 = %u%s" % (y1, unit))
66         self.writeln("       x2 = %u%s" % (x2, unit))
67         self.writeln("       y2 = %u%s" % (y2, unit))
68         self.writeln("      }")
69         self.cnt = self.cnt + 1
70
71     def emit_bbox(self):
72         self.cnt = 0
73         self.writeln("    ha:subc-aux {")
74         self.writeln("     lid = 1")
75         self.writeln("     ha:type {")
76         self.writeln("      top = 1")
77         self.writeln("      misc = 1")
78         self.writeln("      virtual = 1")
79         self.writeln("     }")
80         self.writeln("     li:objects {")
81         self.emit_line("origin", self.cnt, 0, 0, 0, 0, "mm");
82         self.emit_line("x", self.cnt, 0, 0, 1, 0, "mm");
83         self.emit_line("y", self.cnt, 0, 0, 0, 1, "mm");
84         self.writeln("     }")
85         self.writeln("    }")
86
87     def create_uuid(self):
88         return hashlib.md5(self.name).hexdigest()
89
90     def emit(self):
91         self.writeln("li:pcb-rnd-subcircuit-v4 {")
92         self.writeln(" ha:subc.0 {")
93         self.writeln("  ha:attributes {")
94         self.writeln("   description = %s" % self.description)
95         self.writeln("   dist_license = %s" % self.dist_license)
96         self.writeln("   use_license = %s" % self.use_license)
97         self.writeln("   author = %s" % self.author)
98         self.writeln("  }")
99         self.writeln("  uid = %s" % self.create_uuid())
100         self.writeln("  ha:data {")
101         self.emit_padstack_prototypes()
102         self.writeln("   li:objects {")
103         self.emit_padstacks()
104         self.writeln("   }")
105         self.writeln("   li:layers {")
106         self.emit_top_silk()
107         self.emit_bbox()
108         self.writeln("   }")
109         self.writeln("  }")
110         self.writeln(" }")
111         self.writeln("}")