Skip to content

Instantly share code, notes, and snippets.

@xesscorp
Created September 1, 2015 15:03
Show Gist options
  • Save xesscorp/64468c18df37d729c7f6 to your computer and use it in GitHub Desktop.
Save xesscorp/64468c18df37d729c7f6 to your computer and use it in GitHub Desktop.
Execute this Python script and it will generate footprint modules for all the CTS 740 series of resistor arrays.
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def max(self, pt):
return Point(max(self.x, pt.x), max(self.y, pt.y))
def min(self, pt):
return Point(min(self.x, pt.x), min(self.y, pt.y))
def string(self):
return "(%(x)f, %(y)f)" % self.__dict__
class BoundingBox:
def __init__(self):
self.max_pt = Point(-float("inf"), -float("inf"))
self.min_pt = Point(float("inf"), float("inf"))
def add(self, pt):
self.max_pt = self.max_pt.max(pt)
self.min_pt = self.min_pt.min(pt)
def string(self, layer, line_width, expansion_offset):
line_dict = {"layer": layer, "line_width": line_width}
template = "(fp_line (start %(start_x)f %(start_y)f) (end %(end_x)f %(end_y)f) (layer %(layer)s) (width %(line_width)f))\n"
line_dict["start_x"] = self.min_pt.x - expansion_offset
line_dict["start_y"] = self.min_pt.y - expansion_offset
line_dict["end_x"] = self.max_pt.x + expansion_offset
line_dict["end_y"] = self.min_pt.y - expansion_offset
top = template % line_dict
line_dict["end_x"] = self.min_pt.x - expansion_offset
line_dict["end_y"] = self.max_pt.y + expansion_offset
left = template % line_dict
line_dict["start_x"] = self.max_pt.x + expansion_offset
line_dict["start_y"] = self.max_pt.y + expansion_offset
line_dict["end_x"] = self.min_pt.x - expansion_offset
line_dict["end_y"] = self.max_pt.y + expansion_offset
bottom = template % line_dict
line_dict["end_x"] = self.max_pt.x + expansion_offset
line_dict["end_y"] = self.min_pt.y - expansion_offset
right = template % line_dict
return top + right + bottom + left
class Pad:
def __init__(self, pin_number, center_x, center_y, width, height):
self.n = pin_number
self.x = center_x
self.y = center_y
self.w = width
self.h = height
def string(self):
return "(pad %(n)d smd rect (at %(x)f %(y)f) (size %(w)f %(h)f) (layers F.Cu F.Paste F.Mask))\n" % self.__dict__
def add_to_bounding_box(self, bbox):
bbox.add(Point(self.x + self.w / 2.0, self.y + self.h / 2.0))
bbox.add(Point(self.x - self.w / 2.0, self.y - self.h / 2.0))
class PadBox:
def __init__(self, pad):
self.x = pad.x
self.y = pad.y
self.w = pad.w
self.h = pad.h
def string(self, line_width):
x = self.x
y = self.y
w = self.w
h = self.h
template = "(fp_line (start %(start_x)f %(start_y)f) (end %(end_x)f %(end_y)f) (layer F.Fab) (width %(lwidth)f))\n"
top = template % {
"start_x": x - w / 2.0,
"start_y": y - h / 2.0,
"end_x": x + w / 2.0,
"end_y": y - h / 2.0,
"lwidth": line_width
}
bottom = template % {
"start_x": x - w / 2.0,
"start_y": y + h / 2.0,
"end_x": x + w / 2.0,
"end_y": y + h / 2.0,
"lwidth": line_width
}
left = template % {
"start_x": x - w / 2.0,
"start_y": y + h / 2.0,
"end_x": x - w / 2.0,
"end_y": y - h / 2.0,
"lwidth": line_width
}
right = template % {
"start_x": x + w / 2.0,
"start_y": y + h / 2.0,
"end_x": x + w / 2.0,
"end_y": y - h / 2.0,
"lwidth": line_width
}
return top + right + bottom + left
class PadRows:
def __init__(self, num_pads, A, B, C, D):
pad_height = (A - B) / 2.0
pad_width = C
pad_spacing = D
pad_separation = D - C
row_separation = B
pads_per_row = num_pads / 2
row_width = (num_pads / 2) * pad_width + (num_pads / 2 -
1) * pad_separation
start_x = -row_width / 2.0 + pad_width / 2.0
start_y = (pad_height + row_separation) / 2.0
self.pads = []
self.pad_boxes = []
for i in range(pads_per_row):
p = Pad(i + 1, start_x + i * D, start_y, pad_width, pad_height)
self.pads.append(p)
self.pad_boxes.append(PadBox(p))
start_x = -start_x
start_y = -start_y
for i, n in enumerate(range(pads_per_row + 1, num_pads + 1)):
p = Pad(n, start_x - i * D, start_y, pad_width, pad_height)
self.pads.append(p)
self.pad_boxes.append(PadBox(p))
def string(self, line_width):
s = ""
for p in self.pads:
s += p.string()
for b in self.pad_boxes:
s += b.string(line_width=line_width)
return s
def calc_bounding_box(self):
bbox = BoundingBox()
for pad in self.pads:
pad.add_to_bounding_box(bbox)
return bbox
class RefDesignator:
def __init__(self, ref_designator, bbox):
self.ref_designator = ref_designator
self.x = (bbox.max_pt.x + bbox.min_pt.x) / 2.0
self.y = bbox.min_pt.y - 1.0 # Push the label up by 0.04" = 1mm.
def string(self, layer, line_width):
self.__dict__["layer"] = layer
self.__dict__["line_width"] = line_width
return "(fp_text reference %(ref_designator)s (at %(x)f %(y)f) (layer %(layer)s) (effects (font (thickness %(line_width)s))))\n" % self.__dict__
class ValueDesignator:
def __init__(self, value_designator, bbox):
self.value_designator = value_designator
self.x = (bbox.max_pt.x + bbox.min_pt.x) / 2.0
self.y = bbox.max_pt.y + 1.0 # Push the label down by 0.04" = 1mm.
def string(self, layer, line_width):
self.__dict__["layer"] = layer
self.__dict__["line_width"] = line_width
return "(fp_text value %(value_designator)s (at %(x)f %(y)f) (layer %(layer)s) hide (effects (font (thickness %(line_width)s))))\n" % self.__dict__
class ResArray:
def __init__(self, name, descr, tags, ref_designator, line_width, num_pads,
A, B, C, D):
self.pad_rows = PadRows(num_pads, A, B, C, D)
self.name = name
self.descr = descr
self.tags = tags
self.ref_designator = ref_designator
self.line_width = line_width
def string(self):
s = '(module %(name)s (layer F.Cu) (tedit 00000000)\n(descr "%(descr)s")(tags "%(tags)s")(attr smd)\n' % self.__dict__
s += self.pad_rows.string(line_width=self.line_width / 2.0)
bbox = self.pad_rows.calc_bounding_box()
s += RefDesignator(self.ref_designator,
bbox).string(layer="F.SilkS",
line_width=self.line_width)
s += ValueDesignator('Val**', bbox).string(layer="F.SilkS",
line_width=self.line_width)
s += bbox.string(layer="F.Fab",
line_width=self.line_width,
expansion_offset=1.1 * self.line_width)
s += bbox.string(layer="F.SilkS",
line_width=self.line_width,
expansion_offset=1.1 * self.line_width)
s += ")"
return s
def make_module(self):
f = open(self.name + ".kicad_mod", "w")
f.write(self.string())
f.close()
if __name__ == "__main__":
line_width = 6.0 # in mils
mm_per_mils = 25.4 / 1000.0
line_width *= mm_per_mils
ResArray("CTS_740X043", "CTS two isolated resistor array",
"isolated, resistor, array, 0201, convex", "RN", line_width, 4,
0.9, 0.3, 0.30, 0.5).make_module()
ResArray("CTS_741X043", "CTS two isolated resistor array",
"isolated, resistor, array, 0402, convex", "RN", line_width, 4,
1.8, 0.5, 0.40, 0.67).make_module()
ResArray("CTS_741X083", "CTS four isolated resistor array",
"isolated, resistor, array, 0402, convex", "RN", line_width, 8,
1.8, 0.5, 0.30, 0.5).make_module()
ResArray("CTS_741C083", "CTS four isolated resistor array",
"isolated, resistor, array, 0402, concave", "RN", line_width, 8,
1.8, 0.5, 0.30, 0.5).make_module()
ResArray("CTS_741X163", "CTS eight isolated resistor array",
"isolated, resistor, array, 0402, convex", "RN", line_width, 16,
1.8, 0.5, 0.30, 0.5).make_module()
ResArray("CTS_742C043", "CTS two isolated resistor array",
"isolated, resistor, array, 0603, concave", "RN", line_width, 4,
2.6, 0.8, 0.45, 0.8).make_module()
ResArray("CTS_742X083", "CTS four isolated resistor array",
"isolated, resistor, array, 0603, convex", "RN", line_width, 8,
2.6, 0.8, 0.45, 0.8).make_module()
ResArray("CTS_742C083", "CTS four isolated resistor array",
"isolated, resistor, array, 0603, concave", "RN", line_width, 8,
2.6, 0.8, 0.45, 0.8).make_module()
ResArray("CTS_742C163", "CTS eight isolated resistor array",
"isolated, resistor, array, 0603, concave", "RN", line_width, 16,
2.6, 0.8, 0.45, 0.8).make_module()
ResArray("CTS_743C043", "CTS two isolated resistor array",
"isolated, resistor, array, 0805, concave", "RN", line_width, 4,
3.0, 1.0, 0.65, 1.27).make_module()
ResArray("CTS_743C083", "CTS four isolated resistor array",
"isolated, resistor, array, 0805, concave", "RN", line_width, 8,
3.0, 1.0, 0.65, 1.27).make_module()
ResArray("CTS_744C043", "CTS two isolated resistor array",
"isolated, resistor, array, 1206, concave", "RN", line_width, 4,
4.2, 2.2, 0.65, 1.27).make_module()
ResArray("CTS_744C083", "CTS four isolated resistor array",
"isolated, resistor, array, 1206, concave", "RN", line_width, 8,
4.2, 2.2, 0.65, 1.27).make_module()
ResArray("CTS_745C101", "CTS eight bussed resistor array",
"bussed, resistor, array, concave", "RN", line_width, 10, 3.9,
2.1, 0.90, 1.27).make_module()
ResArray("CTS_745C102", "CTS eight bussed resistor array",
"bussed, resistor, array, concave", "RN", line_width, 10, 3.9,
2.1, 0.90, 1.27).make_module()
ResArray("CTS_745X101", "CTS eight bussed resistor array",
"bussed, resistor, array, convex", "RN", line_width, 10, 3.9, 2.1,
0.90, 1.27).make_module()
ResArray("CTS_745X102", "CTS eight bussed resistor array",
"bussed, resistor, array, convex", "RN", line_width, 10, 3.9, 2.1,
0.90, 1.27).make_module()
ResArray("CTS_746X101", "CTS eight bussed resistor array",
"bussed, resistor, array, convex", "RN", line_width, 10, 2.6, 0.8,
0.35, 0.64).make_module()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment