generate_connector_images.py 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  1. #!/usr/bin/env python3
  2. """
  3. Generate the connector drawings displayed for power port, power outlet, console port, and front/rear port types.
  4. Each drawing depicts the mating face of a connector as a 64x64 SVG: the face is outlined and its contacts are
  5. filled solid black, on a transparent background (the UI inverts the drawings in dark mode). Receptacles are drawn
  6. as seen from the front; plugs and inlets are drawn as the mirror image of the face they mate with. Geometry
  7. is defined in millimetres (x right, y down, origin at the face's centre) and each face is scaled to fill the frame,
  8. so relative physical size is not conveyed. Contacts too small to read at icon size may be exaggerated.
  9. Usage: python scripts/generate_connector_images.py
  10. """
  11. import math
  12. import pathlib
  13. OUTPUT_DIR = pathlib.Path(__file__).resolve().parent.parent / 'netbox' / 'project-static' / 'img' / 'connectors'
  14. SIZE = 64 # Frame size (px)
  15. CONTENT = 56 # Size of the face's longest dimension within the frame
  16. OUTLINE = 3 # Stroke width of face outlines
  17. DETAIL = 2 # Stroke width of secondary lines
  18. def fmt(n):
  19. """Format a number compactly and deterministically."""
  20. s = f'{n:.2f}'.rstrip('0').rstrip('.')
  21. return '0' if s == '-0' else s
  22. def polar(cx, cy, radius, angle):
  23. """Return the point at the given radius and clock angle (degrees clockwise from 12 o'clock) about a centre."""
  24. rad = math.radians(angle)
  25. return cx + radius * math.sin(rad), cy - radius * math.cos(rad)
  26. def rotate(point, angle, about=(0, 0)):
  27. """Rotate a point clockwise (on screen) by the given angle in degrees."""
  28. rad = math.radians(angle)
  29. x, y = point[0] - about[0], point[1] - about[1]
  30. return (
  31. about[0] + x * math.cos(rad) - y * math.sin(rad),
  32. about[1] + x * math.sin(rad) + y * math.cos(rad),
  33. )
  34. #
  35. # Shapes
  36. #
  37. class Path:
  38. """
  39. A path made of move (M), line (L), circular arc (A) and close (Z) segments. Arc segments are
  40. ('A', radius, large_arc, sweep, x, y).
  41. """
  42. def __init__(self, segments):
  43. self.segments = segments
  44. def svg(self, tx):
  45. out = []
  46. for seg in self.segments:
  47. if seg[0] in ('M', 'L'):
  48. x, y = tx.point(seg[1], seg[2])
  49. out.append(f'{seg[0]}{fmt(x)} {fmt(y)}')
  50. elif seg[0] == 'A':
  51. _, r, large, sweep, x, y = seg
  52. x, y = tx.point(x, y)
  53. sweep = 1 - sweep if tx.mirror else sweep
  54. out.append(f'A{fmt(r * tx.scale)} {fmt(r * tx.scale)} 0 {large} {sweep} {fmt(x)} {fmt(y)}')
  55. else:
  56. out.append('Z')
  57. return f'<path d="{"".join(out)}"/>'
  58. class Circle:
  59. def __init__(self, cx, cy, r):
  60. self.cx, self.cy, self.r = cx, cy, r
  61. def svg(self, tx):
  62. x, y = tx.point(self.cx, self.cy)
  63. return f'<circle cx="{fmt(x)}" cy="{fmt(y)}" r="{fmt(self.r * tx.scale)}"/>'
  64. def polygon(points):
  65. return Path([('M', *points[0]), *(('L', *p) for p in points[1:]), ('Z',)])
  66. def rect(cx, cy, w, h, r=0, angle=0):
  67. """A rectangle centred on (cx, cy), optionally with rounded corners and rotated clockwise by angle degrees."""
  68. r = min(r, w / 2, h / 2)
  69. x0, x1, y0, y1 = -w / 2, w / 2, -h / 2, h / 2
  70. def pt(x, y):
  71. x, y = rotate((x, y), angle)
  72. return cx + x, cy + y
  73. if not r:
  74. return polygon([pt(x0, y0), pt(x1, y0), pt(x1, y1), pt(x0, y1)])
  75. return Path([
  76. ('M', *pt(x0 + r, y0)), ('L', *pt(x1 - r, y0)), ('A', r, 0, 1, *pt(x1, y0 + r)),
  77. ('L', *pt(x1, y1 - r)), ('A', r, 0, 1, *pt(x1 - r, y1)),
  78. ('L', *pt(x0 + r, y1)), ('A', r, 0, 1, *pt(x0, y1 - r)),
  79. ('L', *pt(x0, y0 + r)), ('A', r, 0, 1, *pt(x0 + r, y0)), ('Z',),
  80. ])
  81. def chamfered_rect(cx, cy, w, h, tl=0, tr=0, br=0, bl=0):
  82. """A rectangle with the given corners (top-left, top-right, bottom-right, bottom-left) chamfered."""
  83. x0, x1, y0, y1 = cx - w / 2, cx + w / 2, cy - h / 2, cy + h / 2
  84. points = [
  85. (x0 + tl, y0), (x1 - tr, y0), (x1, y0 + tr), (x1, y1 - br),
  86. (x1 - br, y1), (x0 + bl, y1), (x0, y1 - bl), (x0, y0 + tl),
  87. ]
  88. # Drop duplicate points where a corner isn't chamfered
  89. deduped = [p for i, p in enumerate(points) if p != points[i - 1]]
  90. return polygon(deduped)
  91. def rounded_polygon(points, radii=0):
  92. """A polygon whose corners are rounded by the given radius (or per-vertex radii; 0 leaves a corner sharp)."""
  93. if not isinstance(radii, (list, tuple)):
  94. radii = [radii] * len(points)
  95. segments = []
  96. for i, (p, r) in enumerate(zip(points, radii)):
  97. if not r:
  98. segments.append(('L', *p))
  99. continue
  100. a, b = points[i - 1], points[(i + 1) % len(points)]
  101. ux, uy = a[0] - p[0], a[1] - p[1]
  102. vx, vy = b[0] - p[0], b[1] - p[1]
  103. lu, lv = math.hypot(ux, uy), math.hypot(vx, vy)
  104. ux, uy, vx, vy = ux / lu, uy / lu, vx / lv, vy / lv
  105. theta = math.acos(max(-1, min(1, ux * vx + uy * vy)))
  106. t = r / math.tan(theta / 2)
  107. # A right turn (on screen) is a clockwise arc
  108. sweep = 1 if (p[0] - a[0]) * (b[1] - p[1]) - (p[1] - a[1]) * (b[0] - p[0]) > 0 else 0
  109. segments.append(('L', p[0] + ux * t, p[1] + uy * t))
  110. segments.append(('A', r, 0, sweep, p[0] + vx * t, p[1] + vy * t))
  111. segments[0] = ('M', *segments[0][1:3])
  112. return Path([*segments, ('Z',)])
  113. def arc_to(cx, cy, r, start, end, clockwise=True):
  114. """An arc segment along the given circle from start to end."""
  115. a0 = math.atan2(start[1] - cy, start[0] - cx)
  116. a1 = math.atan2(end[1] - cy, end[0] - cx)
  117. sweep = (a1 - a0) % (2 * math.pi) if clockwise else (a0 - a1) % (2 * math.pi)
  118. return ('A', r, 1 if sweep > math.pi else 0, 1 if clockwise else 0, *end)
  119. def circle_intersections(c0, r0, c1, r1):
  120. """Return the two intersections of two circles, ordered left-hand then right-hand of the line c0 -> c1."""
  121. dx, dy = c1[0] - c0[0], c1[1] - c0[1]
  122. d = math.hypot(dx, dy)
  123. a = (r0 ** 2 - r1 ** 2 + d ** 2) / (2 * d)
  124. h = math.sqrt(r0 ** 2 - a ** 2)
  125. mx, my = c0[0] + a * dx / d, c0[1] + a * dy / d
  126. return (mx + h * dy / d, my - h * dx / d), (mx - h * dy / d, my + h * dx / d)
  127. def keyed_circle(r, key_r, key_offset):
  128. """A circle with a semicircular key bump at 6 o'clock, centred key_offset below the circle's centre."""
  129. right, left = circle_intersections((0, 0), r, (0, key_offset), key_r)
  130. left, right = sorted((left, right))
  131. return Path([
  132. ('M', *left), arc_to(0, 0, r, left, right), arc_to(0, key_offset, key_r, right, left), ('Z',),
  133. ])
  134. def arc_slot(cx, cy, radius, start, end, width):
  135. """A curved slot following a circle of the given radius from clock angle start to end (clockwise)."""
  136. ro, ri = radius + width / 2, radius - width / 2
  137. large = 1 if (end - start) % 360 > 180 else 0
  138. return Path([
  139. ('M', *polar(cx, cy, ro, start)), ('A', ro, large, 1, *polar(cx, cy, ro, end)),
  140. ('L', *polar(cx, cy, ri, end)), ('A', ri, large, 0, *polar(cx, cy, ri, start)), ('Z',),
  141. ])
  142. #
  143. # Drawings
  144. #
  145. class Transform:
  146. def __init__(self, scale, mirror, centre):
  147. self.scale = scale
  148. self.mirror = mirror
  149. self.centre = centre
  150. def point(self, x, y):
  151. x = (x - self.centre[0]) * self.scale
  152. return SIZE / 2 + (-x if self.mirror else x), SIZE / 2 + (y - self.centre[1]) * self.scale
  153. class Drawing:
  154. """
  155. The face of a connector. width and height give the extent of the face (in mm) about centre (by default the
  156. origin), which is scaled to fill the frame.
  157. """
  158. def __init__(self, width, height, centre=(0, 0)):
  159. self.width = width
  160. self.height = height
  161. self.centre = centre
  162. self.elements = []
  163. def outline(self, *shapes):
  164. self.elements.extend(('outline', s) for s in shapes)
  165. return self
  166. def detail(self, *shapes):
  167. self.elements.extend(('detail', s) for s in shapes)
  168. return self
  169. def fill(self, *shapes):
  170. self.elements.extend(('fill', s) for s in shapes)
  171. return self
  172. def svg(self, mirror=False):
  173. tx = Transform(CONTENT / max(self.width, self.height), mirror, self.centre)
  174. groups = {
  175. 'outline': f'<g fill="none" stroke="#000" stroke-width="{OUTLINE}" stroke-linejoin="round">',
  176. 'detail': f'<g fill="none" stroke="#000" stroke-width="{DETAIL}" stroke-linejoin="round">',
  177. 'fill': '<g fill="#000">',
  178. }
  179. lines = [f'<svg xmlns="http://www.w3.org/2000/svg" width="{SIZE}" height="{SIZE}" viewBox="0 0 {SIZE} {SIZE}">']
  180. for style, opening in groups.items():
  181. shapes = [s.svg(tx) for st, s in self.elements if st == style]
  182. if shapes:
  183. lines.append(opening + ''.join(shapes) + '</g>')
  184. lines.append('</svg>')
  185. return '\n'.join(lines) + '\n'
  186. def mirrored(func):
  187. """Mark a face drawing to be rendered as its mirror image (i.e. the face of the mating connector)."""
  188. def wrapper(*args, **kwargs):
  189. return func(*args, **kwargs), True
  190. return wrapper
  191. #
  192. # IEC 60320 appliance couplers (earth at the top; outlets are female connectors, ports are male inlets)
  193. #
  194. def iec_c13_family(male=False, earth=True, notch=False):
  195. """C13/C14 (and the C15/C16 and C17/C18 variants): a rectangle with chamfered top corners."""
  196. if male:
  197. w, h, chamfer_x, chamfer_y, pin = 24, 16, 7, -3, (2.0, 4.0)
  198. else:
  199. w, h, chamfer_x, chamfer_y, pin = 23, 15.8, 6.1, -2.5, (2.5, 5.0)
  200. x, y = w / 2, h / 2
  201. points = [(-chamfer_x, -y), (chamfer_x, -y), (x, chamfer_y), (x, y)]
  202. radii = [1.5, 1.5, 1.5, 3]
  203. if notch:
  204. nw, apex = (2.25, 3.3) if male else (2.5, 3.0)
  205. points.append((nw, y))
  206. radii.append(0.8)
  207. for angle in range(0, 181, 15):
  208. points.append((nw * math.cos(math.radians(angle)), apex + nw - nw * math.sin(math.radians(angle))))
  209. radii.append(0)
  210. points.append((-nw, y))
  211. radii.append(0.8)
  212. points += [(-x, y), (-x, chamfer_y)]
  213. radii += [3, 1.5]
  214. drawing = Drawing(w, h).outline(rounded_polygon(points, radii))
  215. drawing.fill(rect(-7, 2, *pin), rect(7, 2, *pin))
  216. if earth:
  217. drawing.fill(rect(0, -2, *pin))
  218. return drawing
  219. def iec_c19_family(male=False, chamfer=False):
  220. """C19/C20 (and the C21/C22 variants): a rectangle with horizontal contacts."""
  221. if male:
  222. w, h, chamfer_x, chamfer_y, earth, line, thickness = 29, 21, 7.5, -3.5, 6, 5, 2.0
  223. else:
  224. w, h, chamfer_x, chamfer_y, earth, line, thickness = 28, 20, 7, -3, 7, 6, 2.5
  225. x, y = w / 2, h / 2
  226. if chamfer:
  227. outline = rounded_polygon(
  228. [(-chamfer_x, -y), (chamfer_x, -y), (x, chamfer_y), (x, y), (-x, y), (-x, chamfer_y)],
  229. [1.5, 1.5, 1.5, 3, 3, 1.5],
  230. )
  231. else:
  232. outline = rect(0, 0, w, h, 3)
  233. return Drawing(w, h).outline(outline).fill(
  234. rect(0, -4, earth, thickness), rect(-6.5, 4, line, thickness), rect(6.5, 4, line, thickness),
  235. )
  236. def iec_c5_family(male=False):
  237. """C5/C6 ("cloverleaf"): the union of three circular lobes."""
  238. earth, live, neutral = ((0, -2), 4.5), ((-5, 2.5), 4.0), ((5, 2.5), 4.0)
  239. en_top, en_bottom = circle_intersections(earth[0], earth[1], neutral[0], neutral[1])
  240. el_bottom, el_top = circle_intersections(earth[0], earth[1], live[0], live[1])
  241. outline = Path([
  242. ('M', *el_top),
  243. arc_to(*earth[0], earth[1], el_top, en_top),
  244. arc_to(*neutral[0], neutral[1], en_top, en_bottom),
  245. arc_to(*earth[0], earth[1], en_bottom, el_bottom),
  246. arc_to(*live[0], live[1], el_bottom, el_top),
  247. ('Z',),
  248. ])
  249. earth_r, line_r = (1.6, 1.18) if male else (1.9, 1.45)
  250. return Drawing(18, 13).outline(outline).fill(
  251. Circle(0, -2, earth_r), Circle(-5, 2.5, line_r), Circle(5, 2.5, line_r),
  252. )
  253. def iec_c7_family(male=False):
  254. """C7/C8 ("figure 8"): two overlapping circular lobes."""
  255. r, pin_r = (4.1, 1.18) if male else (4.0, 1.45)
  256. top, bottom = circle_intersections((-3.6, 0), r, (3.6, 0), r)
  257. outline = Path([
  258. ('M', *top), arc_to(3.6, 0, r, top, bottom), arc_to(-3.6, 0, r, bottom, top), ('Z',),
  259. ])
  260. return Drawing(7.2 + 2 * r, 2 * r).outline(outline).fill(Circle(-4.3, 0, pin_r), Circle(4.3, 0, pin_r))
  261. #
  262. # IEC 60309 pin & sleeve (16 A). Viewed from the front of the socket with the keyway at 6 o'clock, the earth
  263. # contact sits at the clock hour given by the type and the remaining contacts follow it clockwise.
  264. #
  265. IEC_60309_SIZES = {
  266. # Contacts: (recess radius, key bump offset, pin circle radius, plug shroud inner/outer radii)
  267. 3: (22.15, 21.85, 8.75, (18.95, 21.75)),
  268. 4: (25.2, 25.3, 10.75, (21.4, 24.75)),
  269. 5: (28.65, 29.15, 13.25, (24.4, 28.05)),
  270. }
  271. def iec_60309(contacts, hour, male=False):
  272. radius, key_offset, pin_circle, shroud = IEC_60309_SIZES[contacts]
  273. if male:
  274. radius, key_offset, key_r, earth_r, line_r = shroud[1], shroud[1] + 0.2, 3, 3.5, 2.5
  275. else:
  276. key_r, earth_r, line_r = 3.5, 4.0, 3.0
  277. # The key projects below the circle
  278. bottom = key_offset + key_r
  279. drawing = Drawing(2 * radius, radius + bottom, centre=(0, (bottom - radius) / 2))
  280. drawing.outline(keyed_circle(radius, key_r, key_offset))
  281. if male:
  282. drawing.detail(Circle(0, 0, shroud[0]))
  283. for i in range(contacts):
  284. x, y = polar(0, 0, pin_circle, hour * 30 + i * 360 / contacts)
  285. drawing.fill(Circle(x, y, earth_r if i == 0 else line_r))
  286. return drawing
  287. #
  288. # IEC 60906-1 & NBR 14136 (three round contacts within a flattened hexagon; earth offset towards the top)
  289. #
  290. def iec_60906(pin_r):
  291. outline = rounded_polygon([(-9.9, -8.5), (9.9, -8.5), (18.4, 0), (9.9, 8.5), (-9.9, 8.5), (-18.4, 0)], 1.5)
  292. return Drawing(35.5, 17).outline(outline).fill(Circle(-9.5, 0, pin_r), Circle(9.5, 0, pin_r), Circle(0, -3, pin_r))
  293. #
  294. # NEMA straight-blade receptacles (face radius = 1; oriented as on the NEMA configuration chart, ground at the top)
  295. #
  296. NEMA_SLOT = 0.15
  297. def nema_v(x, y, length):
  298. """A vertical slot."""
  299. return rect(x, y, NEMA_SLOT, length)
  300. def nema_h(x, y, length):
  301. """A horizontal slot."""
  302. return rect(x, y, length, NEMA_SLOT)
  303. def nema_arm(x0, x1, y):
  304. """A horizontal arm extending from a vertical slot at x0 to x1 (forming a T- or L-shaped slot)."""
  305. return rect((x0 + x1) / 2, y, abs(x1 - x0), NEMA_SLOT)
  306. def nema_ground(y=-0.58):
  307. """A U-shaped ground hole, flat side towards the rim."""
  308. return Path([
  309. ('M', -0.15, y - 0.14), ('L', 0.15, y - 0.14), ('L', 0.15, y), arc_to(0, y, 0.15, (0.15, y), (-0.15, y)),
  310. ('Z',),
  311. ])
  312. def nema(*slots, ground=-0.58):
  313. drawing = Drawing(2, 2).outline(Circle(0, 0, 1)).fill(*slots)
  314. if ground is not None:
  315. drawing.fill(nema_ground(ground))
  316. return drawing
  317. def nema_10(neutral):
  318. return nema(*neutral, rect(0.39, 0.25, NEMA_SLOT, 0.5, angle=30), rect(-0.37, 0.23, NEMA_SLOT, 0.5, angle=-30),
  319. ground=None)
  320. NEMA_STRAIGHT = {
  321. '1-15': lambda: nema(nema_v(0.38, 0, 0.76), nema_v(-0.38, 0, 0.48), ground=None),
  322. '5-15': lambda: nema(nema_v(0.38, 0.12, 0.76), nema_v(-0.38, 0.12, 0.47)),
  323. '5-20': lambda: nema(nema_v(0.29, 0.12, 0.7), nema_arm(0.29, 0.75, 0.1), nema_v(-0.38, 0.12, 0.47)),
  324. '5-30': lambda: nema(nema_v(0.375, 0.11, 0.47), nema_arm(0.375, 0.1, -0.02), nema_v(-0.38, 0.12, 0.47)),
  325. '5-50': lambda: nema(nema_v(0.38, 0.16, 0.76), nema_v(-0.57, 0.13, 0.48)),
  326. '6-15': lambda: nema(nema_h(0.43, 0.23, 0.48), nema_h(-0.43, 0.23, 0.48)),
  327. '6-20': lambda: nema(nema_v(0.25, 0.115, 0.75), nema_arm(0.25, 0.79, 0.13), nema_h(-0.42, 0.15, 0.47)),
  328. '6-30': lambda: nema(nema_h(0.43, 0.14, 0.47), nema_h(-0.43, 0.14, 0.47)),
  329. '6-50': lambda: nema(nema_v(-0.46, 0.17, 0.76), nema_v(0.48, 0.12, 0.48)),
  330. '10-30': lambda: nema_10([nema_v(-0.08, -0.52, 0.44), nema_arm(-0.08, 0.28, -0.37)]),
  331. '10-50': lambda: nema_10([nema_v(0.01, -0.46, 0.66)]),
  332. '14-20': lambda: nema(
  333. nema_v(0.55, -0.06, 0.47), nema_v(-0.55, -0.06, 0.47), nema_h(0, 0.45, 0.47), ground=-0.63
  334. ),
  335. '14-30': lambda: nema(
  336. nema_v(0.53, -0.04, 0.65), nema_v(-0.51, -0.04, 0.65), nema_v(0.135, 0.51, 0.48), nema_arm(0.135, -0.2, 0.37),
  337. ground=-0.63,
  338. ),
  339. '14-50': lambda: nema(
  340. nema_v(0.53, -0.04, 0.66), nema_v(-0.53, -0.04, 0.66), nema_v(0.01, 0.51, 0.66), ground=-0.63
  341. ),
  342. '14-60': lambda: nema(
  343. nema_v(0.53, -0.04, 0.66), nema_v(-0.53, -0.04, 0.66), nema_h(0.03, 0.56, 0.65), ground=-0.63
  344. ),
  345. '15-15': lambda: nema(nema_v(-0.58, 0, 0.48), nema_h(0.55, 0, 0.48), nema_h(0, 0.58, 0.48)),
  346. '15-20': lambda: nema(nema_v(-0.57, 0, 0.66), nema_h(0.45, 0, 0.64), nema_h(0, 0.59, 0.64)),
  347. '15-30': lambda: nema(nema_h(-0.52, 0, 0.47), nema_h(0.55, 0, 0.47), nema_h(0, 0.58, 0.47)),
  348. '15-50': lambda: nema(nema_v(0.6, 0, 0.48), nema_h(-0.51, 0, 0.47), nema_h(0, 0.58, 0.47)),
  349. '15-60': lambda: nema(
  350. nema_v(-0.52, -0.04, 0.66), nema_h(0.45, 0, 0.65), nema_h(0.03, 0.56, 0.65), ground=-0.63
  351. ),
  352. }
  353. # Plugs whose blades don't match their receptacle's slots: the T-shaped slots of 5-20R and 6-20R also accept
  354. # 15 A plugs. Drawn in receptacle orientation (and mirrored like the other plugs).
  355. NEMA_STRAIGHT_PLUGS = {
  356. '5-20': lambda: nema(nema_h(0.52, 0.1, 0.46), nema_v(-0.38, 0.12, 0.47)),
  357. '6-20': lambda: nema(nema_v(0.25, 0.13, 0.47), nema_h(-0.42, 0.15, 0.47)),
  358. }
  359. #
  360. # NEMA locking ("twist-lock") receptacles (face radius = 1; oriented as on the NEMA configuration chart). Each
  361. # slot is an arc between two clock angles, optionally with a tab at one end stepping in or out to a given radius.
  362. #
  363. LOCKING_SLOT = 0.15
  364. TAB_SPAN = 15
  365. TAB_EXAGGERATION = 0.04
  366. def locking_slot(start, end, radius=0.555, width=LOCKING_SLOT, tab=None):
  367. """
  368. A curved slot. tab is (end, radius): the angle of the slot end at which a tab extends radially to the given
  369. radius.
  370. """
  371. shapes = [arc_slot(0, 0, radius, start, end, width)]
  372. if tab:
  373. at, to = tab
  374. # Tabs are exaggerated slightly to remain legible at small sizes
  375. to += TAB_EXAGGERATION if to > radius else -TAB_EXAGGERATION
  376. edge = radius + width / 2 if to > radius else radius - width / 2
  377. span = (at - TAB_SPAN, at) if at % 360 == end % 360 else (at, at + TAB_SPAN)
  378. shapes.append(arc_slot(0, 0, (edge + to) / 2, *span, abs(to - edge) + 0.02))
  379. return shapes
  380. def locking(*slots, center_ground=False):
  381. drawing = Drawing(2, 2).outline(Circle(0, 0, 1))
  382. for slot in slots:
  383. drawing.fill(*slot)
  384. if center_ground:
  385. drawing.fill(Circle(0, 0, 0.1))
  386. return drawing
  387. def l_14(w, ground_tab):
  388. return locking(
  389. locking_slot(331, 26), locking_slot(*w), locking_slot(152, 208), locking_slot(238, 298, tab=ground_tab),
  390. )
  391. def l_15(ground_tab):
  392. return locking(
  393. locking_slot(330, 13), locking_slot(65, 124), locking_slot(152, 208), locking_slot(242, 297, tab=ground_tab),
  394. )
  395. def l_21(x, y, z, neutral_tab):
  396. return locking(
  397. locking_slot(*x), locking_slot(*y), locking_slot(*z),
  398. locking_slot(237, 297, radius=0.52, tab=neutral_tab), center_ground=True,
  399. )
  400. NEMA_LOCKING = {
  401. 'l1-15': lambda: locking(locking_slot(329, 31, 0.605), locking_slot(145, 214, 0.54, tab=(145, 0.69))),
  402. 'l5-15': lambda: locking(
  403. locking_slot(16, 85, 0.525), locking_slot(139, 200, 0.605), locking_slot(254, 317, 0.56, tab=(254, 0.41)),
  404. ),
  405. 'l5-20': lambda: locking(locking_slot(10, 44), locking_slot(116, 181), locking_slot(234, 299, tab=(299, 0.39))),
  406. 'l5-30': lambda: locking(locking_slot(7, 45), locking_slot(122, 174), locking_slot(234, 300, tab=(300, 0.39))),
  407. 'l6-15': lambda: locking(
  408. locking_slot(15, 84, 0.54), locking_slot(138, 202, 0.565), locking_slot(253, 321, 0.55, tab=(321, 0.7)),
  409. ),
  410. 'l6-20': lambda: locking(locking_slot(0, 53), locking_slot(126, 168), locking_slot(235, 298, tab=(298, 0.39))),
  411. 'l6-30': lambda: locking(locking_slot(0, 53), locking_slot(126, 168), locking_slot(235, 298, tab=(298, 0.39))),
  412. 'l10-30': lambda: locking(
  413. locking_slot(0, 43, 0.68), locking_slot(119, 162, 0.68), locking_slot(238, 287, 0.68, tab=(238, 0.51)),
  414. ),
  415. 'l14-20': lambda: l_14((68, 112), (298, 0.68)),
  416. 'l14-30': lambda: l_14((69, 107), (298, 0.4)),
  417. 'l15-20': lambda: l_15((297, 0.68)),
  418. 'l15-30': lambda: l_15((297, 0.4)),
  419. 'l21-20': lambda: l_21((327, 21), (67, 105), (147, 201), (237, 0.69)),
  420. 'l21-30': lambda: l_21((327, 21), (67, 105), (147, 201), (237, 0.38)),
  421. 'l22-20': lambda: l_21((337, 14), (58, 116), (151, 207), (237, 0.69)),
  422. 'l22-30': lambda: l_21((337, 14), (58, 116), (151, 207), (237, 0.38)),
  423. }
  424. #
  425. # California Style 50 A locking connectors (Hubbell; face radius = 1). Three curved contacts around a centre post
  426. # (a hole on the plug). Three-pole types have keying lugs around the rim and a recessed sector.
  427. #
  428. CS_SLOT = 0.17
  429. def cs_slot(start, end, tab=None, step=None):
  430. """A curved contact. step is (start, end, radius): a portion of the slot widened outward to the given radius."""
  431. shapes = locking_slot(start, end, 0.525, CS_SLOT, tab)
  432. if step:
  433. a0, a1, to = step
  434. edge = 0.525 + CS_SLOT / 2
  435. shapes.append(arc_slot(0, 0, (edge + to) / 2, a0, a1, to - edge + 0.02))
  436. return shapes
  437. def cs_outline(lugs, recess):
  438. """A circular rim with semicircular lugs at the given angles and a recessed sector (start, end)."""
  439. lug_r, recess_r = 0.125, 0.93
  440. delta = math.degrees(2 * math.asin(lug_r / 2))
  441. events = sorted([(a % 360, 'lug') for a in lugs] + [(recess[0], 'recess')])
  442. start = polar(0, 0, 1, events[0][0] - delta - 1)
  443. segments = [('M', *start)]
  444. point = start
  445. for angle, kind in events:
  446. if kind == 'lug':
  447. p0, p1 = polar(0, 0, 1, angle - delta), polar(0, 0, 1, angle + delta)
  448. segments += [arc_to(0, 0, 1, point, p0), arc_to(*polar(0, 0, 1, angle), lug_r, p0, p1)]
  449. point = p1
  450. else:
  451. p0 = polar(0, 0, 1, recess[0])
  452. q0, q1 = polar(0, 0, recess_r, recess[0]), polar(0, 0, recess_r, recess[1])
  453. point = polar(0, 0, 1, recess[1])
  454. segments += [arc_to(0, 0, 1, segments[-1][-2:], p0), ('L', *q0), arc_to(0, 0, recess_r, q0, q1),
  455. ('L', *point)]
  456. segments += [arc_to(0, 0, 1, point, start), ('Z',)]
  457. return Path(segments)
  458. def california(slots, male=False, lugs=None, recess=None):
  459. outline = cs_outline(lugs, recess) if lugs else Circle(0, 0, 1)
  460. drawing = Drawing(2.25 if lugs else 2, 2.25 if lugs else 2).outline(outline)
  461. for slot in slots:
  462. drawing.fill(*slot)
  463. if male:
  464. drawing.detail(Circle(0, 0, 0.12))
  465. else:
  466. drawing.fill(Circle(0, 0, 0.14))
  467. return drawing
  468. CALIFORNIA_STYLE = {
  469. # Connector (female), plug (male): (contacts, rim lugs, recessed sector)
  470. ('CS6360C', 'cs6361c'): ([cs_slot(5, 56), cs_slot(117, 185), cs_slot(247, 300, tab=(247, 0.72))], None, None),
  471. ('CS8264C', 'cs8265c'): ([cs_slot(9, 60), cs_slot(119, 188), cs_slot(247, 300, tab=(247, 0.72))], None, None),
  472. ('CS8464C', 'cs8465c'): ([cs_slot(6, 57), cs_slot(113, 188), cs_slot(235, 299, tab=(235, 0.36))], None, None),
  473. ('CS6364C', 'cs6365c'): (
  474. [cs_slot(359, 59, step=(359, 29, 0.66)), cs_slot(126, 175), cs_slot(239, 294, step=(239, 266, 0.7))],
  475. (5, 35, 205, 238), (89, 155),
  476. ),
  477. ('CS8364C', 'cs8365c'): (
  478. [cs_slot(3, 71, tab=(3, 0.33)), cs_slot(125, 176), cs_slot(232, 299, tab=(232, 0.34))],
  479. (13, 41, 204, 234), (90, 158),
  480. ),
  481. ('CS8164C', 'cs8165c'): (
  482. [cs_slot(1, 58, step=(1, 30, 0.66)), cs_slot(127, 181), cs_slot(230, 295, step=(230, 262, 0.7))],
  483. (330, 8, 189, 226), (87, 160),
  484. ),
  485. }
  486. #
  487. # International (ITA/IEC World Plugs types). Sockets are drawn as seen from the front; plugs whose face differs
  488. # from the mirror image of the socket are drawn separately.
  489. #
  490. def ita_c_plug():
  491. """Europlug (CEE 7/16)."""
  492. outline = rounded_polygon(
  493. [(-10.8, -6.85), (10.8, -6.85), (17.65, 0), (10.8, 6.85), (-10.8, 6.85), (-17.65, 0)], 1.5
  494. )
  495. return Drawing(35.3, 13.7).outline(outline).fill(Circle(-9.25, 0, 2), Circle(9.25, 0, 2))
  496. def schuko_plug(strips=True, earth_hole=False):
  497. """CEE 7/4 (with earth strips at 12 and 6 o'clock), CEE 7/6 (with an earth hole), and CEE 7/7 (both)."""
  498. r = 18.25
  499. # Round body with guide grooves at 3 and 9 o'clock
  500. groove = math.degrees(math.asin(1.6 / r))
  501. points = [polar(0, 0, r, a) for a in (90 - groove, 90 + groove, 270 - groove, 270 + groove)]
  502. inner = [(p[0] - math.copysign(2, p[0]), p[1]) for p in points]
  503. outline = Path([
  504. ('M', *points[3]), arc_to(0, 0, r, points[3], points[0]), ('L', *inner[0]), ('L', *inner[1]),
  505. ('L', *points[1]), arc_to(0, 0, r, points[1], points[2]), ('L', *inner[2]), ('L', *inner[3]), ('Z',),
  506. ])
  507. drawing = Drawing(2 * r, 2 * r).outline(outline).fill(Circle(-9.5, 0, 2.4), Circle(9.5, 0, 2.4))
  508. if strips:
  509. drawing.fill(rect(0, -r + 1, 6, 2), rect(0, r - 1, 6, 2))
  510. if earth_hole:
  511. drawing.detail(Circle(0, -10, 2.6))
  512. return drawing
  513. def ita_e_socket():
  514. """CEE 7/5: a round recess with a projecting earth pin."""
  515. return Drawing(40, 40).outline(Circle(0, 0, 20)).fill(
  516. Circle(-9.5, 0, 2.75), Circle(9.5, 0, 2.75), Circle(0, -10, 2.4),
  517. ).detail(Circle(0, -10, 3.8))
  518. def ita_f_socket():
  519. """CEE 7/3 (Schuko): a round recess with earth clips at 12 and 6 o'clock."""
  520. return Drawing(40, 40).outline(Circle(0, 0, 20)).fill(
  521. Circle(-9.5, 0, 2.75), Circle(9.5, 0, 2.75), rect(0, -18, 6, 2.4), rect(0, 18, 6, 2.4),
  522. )
  523. def ita_g(socket=True):
  524. """BS 1363."""
  525. earth, line = ((4.5, 8.5), (7, 4.5)) if socket else ((4, 8), (6.4, 4))
  526. outline = rect(0, 0, 48, 48, 5) if socket else rect(0, 1.5, 48, 45, 5)
  527. return Drawing(48, 48).outline(outline).fill(
  528. rect(0, -11.1, *earth), rect(-11.1, 11.1, *line), rect(11.1, 11.1, *line),
  529. )
  530. def ita_h(socket=True):
  531. """SI 32 (round pins)."""
  532. r = 2.6 if socket else 2.25
  533. return Drawing(38, 38).outline(Circle(0, 0, 19)).fill(
  534. Circle(-9.5, -4.75, r), Circle(9.5, -4.75, r), Circle(0, 4.75, r),
  535. )
  536. def ita_i(socket=True):
  537. """AS/NZS 3112."""
  538. length, thickness = (7, 2.2) if socket else (6.35, 2)
  539. outline = rect(0, 0, 38, 38, 4) if socket else rect(0, 0, 36, 38, 9)
  540. return Drawing(38, 38).outline(outline).fill(
  541. rect(-6.86, -7.2, thickness, length, angle=30), rect(6.86, -7.2, thickness, length, angle=-30),
  542. rect(0, 7.1, thickness, length),
  543. )
  544. def ita_j(socket=True):
  545. """SN 441011 (T12 plug/T13 socket): a hexagonal profile with the earth offset below the line contacts."""
  546. r = 2.25 if socket else 2
  547. outline = rounded_polygon(
  548. [(-8.38, -10), (8.38, -10), (18, -0.38), (18, 0.38), (8.38, 10), (-8.38, 10), (-18, 0.38), (-18, -0.38)],
  549. [3, 3, 0.9, 0.9, 3, 3, 0.9, 0.9] if socket else [10, 10, 0.9, 0.9, 10, 10, 0.9, 0.9],
  550. )
  551. return Drawing(36, 20).outline(outline).fill(Circle(-9.5, 0, r), Circle(9.5, 0, r), Circle(0, 5, r))
  552. def ita_k(socket=True):
  553. """DS 107-2-D1 (Danish): a D-shaped earth contact below the line contacts."""
  554. r = 2.65 if socket else 2.4
  555. cy, earth_r = 7.5, 3.25
  556. chord = math.sqrt(earth_r ** 2 - 0.75 ** 2)
  557. earth = Path([
  558. ('M', -chord, cy - 0.75), ('L', chord, cy - 0.75),
  559. arc_to(0, cy, earth_r, (chord, cy - 0.75), (-chord, cy - 0.75)), ('Z',),
  560. ])
  561. return Drawing(40, 40).outline(Circle(0, 0, 20)).fill(Circle(-9.5, -5.5, r), Circle(9.5, -5.5, r), earth)
  562. def ita_l_plug():
  563. """CEI 23-50 S11 (10 A): three contacts in line."""
  564. outline = rounded_polygon([(-10.5, -7), (10.5, -7), (17.5, 0), (10.5, 7), (-10.5, 7), (-17.5, 0)], 1.5)
  565. return Drawing(35, 14).outline(outline).fill(Circle(-9.5, 0, 2), Circle(0, 0, 2), Circle(9.5, 0, 2))
  566. def ita_l_socket():
  567. """CEI 23-50 P17/11 ("bipasso"), mounted vertically: accepts both 10 A and 16 A plugs."""
  568. drawing = Drawing(22, 44).outline(rect(0, 0, 22, 44, 4)).fill(Circle(0, 0, 2.25))
  569. for sign in (-1, 1):
  570. drawing.fill(Circle(0, sign * 9.5, 2.25), Circle(0, sign * 13, 2.75))
  571. return drawing
  572. def ita_m(socket=True):
  573. """BS 546 (15 A)."""
  574. line_r, earth_r = (3.8, 4.6) if socket else (3.53, 4.36)
  575. outline = rect(0, 0, 54, 54, 6) if socket else Circle(0, 0, 27)
  576. return Drawing(54, 54).outline(outline).fill(
  577. Circle(-12.7, 14.3, line_r), Circle(12.7, 14.3, line_r), Circle(0, -14.3, earth_r),
  578. )
  579. def ita_o(socket=True):
  580. """TIS 166-2549 (Thai)."""
  581. r = 2.65 if socket else 2.4
  582. return Drawing(39, 39).outline(Circle(0, 0, 19.5)).fill(
  583. Circle(-9.5, 5.95, r), Circle(9.5, 5.95, r), Circle(0, -5.95, r),
  584. )
  585. #
  586. # Console (D-subminiature, modular jacks, and Mini-DIN)
  587. #
  588. def d_sub(contacts, width, pitch):
  589. """A D-subminiature connector with two staggered rows of contacts (the longer row along the wide side)."""
  590. row, height = 2.84, 7.82
  591. top = (contacts + 1) // 2
  592. inset = height * math.tan(math.radians(10))
  593. outline = rounded_polygon(
  594. [(-width / 2, -height / 2), (width / 2, -height / 2), (width / 2 - inset, height / 2),
  595. (-width / 2 + inset, height / 2)],
  596. 2,
  597. )
  598. drawing = Drawing(width, height).outline(outline)
  599. for i in range(top):
  600. drawing.fill(Circle((i - (top - 1) / 2) * pitch, -row / 2, 0.8))
  601. for i in range(contacts - top):
  602. drawing.fill(Circle((i - (contacts - top - 1) / 2) * pitch, row / 2, 0.8))
  603. return drawing
  604. # Modular jack sizes (positions): opening width, and the latch keyway's wide step (width, bottom) and narrow step
  605. # (width, bottom). The opening spans y -3.42 to +3.42 above the keyway.
  606. MODULAR_JACKS = {
  607. 4: (7.85, (5.6, 5.0), (3.2, 7.1)),
  608. 6: (9.88, (6.6, 5.1), (4.06, 5.95)),
  609. 8: (11.91, (6.3, 5.1), (4.06, 5.95)),
  610. }
  611. def modular_jack(positions, contacts):
  612. """
  613. A modular (RJ) jack, contacts along the top and the latch keyway at the bottom. contacts lists the populated
  614. positions, numbered from 1 at the left.
  615. """
  616. width, (wide, step), (narrow, bottom) = MODULAR_JACKS[positions]
  617. dy = -(bottom - 3.42) / 2 # Centre the opening (including the keyway) vertically
  618. x, w, n = width / 2, wide / 2, narrow / 2
  619. outline = polygon([(px, py + dy) for px, py in (
  620. (-x, -3.42), (x, -3.42), (x, 3.42), (w, 3.42), (w, step), (n, step), (n, bottom), (-n, bottom),
  621. (-n, step), (-w, step), (-w, 3.42), (-x, 3.42),
  622. )])
  623. drawing = Drawing(width, bottom + 3.42).outline(outline)
  624. for c in contacts:
  625. drawing.fill(rect((c - (positions + 1) / 2) * 1.016, -2.2 + dy, 0.55, 2.4))
  626. return drawing
  627. def gg45():
  628. """An 8P jack with an additional pair of contacts standing in each bottom corner (IEC 60603-7-7)."""
  629. drawing = modular_jack(8, range(1, 9))
  630. dy = -(5.95 - 3.42) / 2
  631. return drawing.fill(*(rect(x, 1.9 + dy, 0.56, 3) for x in (-4.85, -3.83, 3.83, 4.85)))
  632. def tera(pairs):
  633. """
  634. A Siemon TERA outlet: four pair cavities beneath a latch. TERA outlets accept 1-, 2- and 4-pair plugs;
  635. contacts are shown only in the cavities a plug with the given number of pairs occupies.
  636. """
  637. drawing = Drawing(14.5, 23.1).outline(rect(0, 0, 14.5, 23.1, 1)).fill(rect(0, -9, 5.25, 1.4, 0.7))
  638. # Pair cavities in plug order: 1 (top left), 2 (bottom left), 3 (top right), 4 (bottom right)
  639. cavities = [(-1, -0.365, 5.37, -1.3), (-1, 5.845, 5.85, 6.9), (1, -0.365, 5.37, -1.3), (1, 5.845, 5.85, 6.9)]
  640. for i, (side, cy, h, contact_y) in enumerate(cavities):
  641. drawing.detail(rect(side * 3.55, cy, 5.28, h, 0.6))
  642. if i < pairs:
  643. drawing.fill(rect(side * 2.68, contact_y, 1.1, 1.6), rect(side * 4.43, contact_y, 1.1, 1.6))
  644. return drawing
  645. def punch_110():
  646. """A 110 connecting block (side view): nine teeth separating eight wire slots."""
  647. points = [(-15.24, 11.75)]
  648. for i in range(9):
  649. x = -15.24 + i * 3.81
  650. points.append((x, -11.75))
  651. if i < 8:
  652. points.append((x + 1.905, -6.5))
  653. points.append((15.24, 11.75))
  654. drawing = Drawing(30.48, 23.5).outline(polygon(points))
  655. for i in range(8):
  656. drawing.fill(rect(-13.335 + i * 3.81, -3.5, 0.8, 6))
  657. return drawing
  658. def bnc():
  659. """A BNC jack: a smooth barrel with two bayonet studs."""
  660. return Drawing(11, 11).outline(Circle(0, 0, 4.825)).detail(Circle(0, 0, 4.05), Circle(0, 0, 2.3)).fill(
  661. Circle(-4.5, 0, 1), Circle(4.5, 0, 1), Circle(0, 0, 1.05),
  662. )
  663. def f_connector():
  664. """An F jack: a threaded barrel on a hex body, with a solid dielectric face and a central pinhole."""
  665. hexagon = polygon([polar(0, 0, 6.35, angle) for angle in range(0, 360, 60)])
  666. return Drawing(11, 12.7).outline(hexagon).detail(Circle(0, 0, 4.76), Circle(0, 0, 2.6)).fill(Circle(0, 0, 0.65))
  667. def n_connector():
  668. """An N jack on a square four-hole flange."""
  669. holes = (Circle(x, y, 1.6) for x in (-9.15, 9.15) for y in (-9.15, 9.15))
  670. return Drawing(25.4, 25.4).outline(rect(0, 0, 25.4, 25.4, 1.5)).detail(
  671. *holes, Circle(0, 0, 7.9), Circle(0, 0, 4.3),
  672. ).fill(Circle(0, 0, 1.5))
  673. def mrj21():
  674. """An MRJ21 receptacle: a skewed opening holding two offset rows of contacts."""
  675. opening = Path([
  676. ('M', -7.35, -5.1), ('L', 7.84, -5.1), ('L', 10.38, -2.57), ('L', 10.38, -1.41), ('L', 7.41, 3.87),
  677. ('L', 5.42, 5.09), ('L', -8.31, 5.09), ('L', -9.85, 3.56), ('L', -9.85, 0.395),
  678. arc_to(-12.96, -4.34, 5.665, (-9.85, 0.395), (-7.35, -5.1), clockwise=False), ('Z',),
  679. ])
  680. return Drawing(28.92, 13.25).outline(rect(0, 0, 28.92, 13.25, 0.8)).detail(
  681. opening, Circle(-11.44, -3.6, 1.2), Circle(11.44, 3.6, 1.2),
  682. ).fill(rect(1.25, -2.42, 15.4, 2.86), rect(-1.25, 2.42, 15.4, 2.86))
  683. def mini_din_8():
  684. """A Mini-DIN 8 receptacle: eight contacts in an insert with a notch at the top and shoulders at the bottom."""
  685. r = 3.95
  686. notch_y = -math.sqrt(r ** 2 - 0.9 ** 2)
  687. shoulder_x = math.sqrt(r ** 2 - 1.9 ** 2)
  688. base_y = math.sqrt(r ** 2 - 2.5 ** 2)
  689. insert = Path([
  690. ('M', 0.9, notch_y), arc_to(0, 0, r, (0.9, notch_y), (shoulder_x, 1.9)), ('L', 2.5, 1.9), ('L', 2.5, base_y),
  691. arc_to(0, 0, r, (2.5, base_y), (-2.5, base_y)), ('L', -2.5, 1.9), ('L', -shoulder_x, 1.9),
  692. arc_to(0, 0, r, (-shoulder_x, 1.9), (-0.9, notch_y)), ('L', -0.9, -3), ('L', 0.9, -3), ('Z',),
  693. ])
  694. drawing = Drawing(10.2, 10.2).outline(Circle(0, 0, 5.1)).detail(insert)
  695. for x, y in ((-2, -1.6), (0, -1.6), (2, -1.6), (-2.5, 0.4), (0.4, 0.4), (2.5, 0.4), (-1, 2.4), (1, 2.4)):
  696. drawing.fill(Circle(x, y, 0.55))
  697. return drawing
  698. #
  699. # Fiber optic adapters (as seen on a patch panel, key or latch at the top). Polish variants (PC/UPC/APC) are
  700. # mechanically identical and share a drawing.
  701. #
  702. def fiber_sc():
  703. """SC (IEC 61754-4): a rectangular opening with a keyway at the top."""
  704. outline = polygon([
  705. (-4.5, -3.25), (-1, -3.25), (-1, -4.15), (1, -4.15), (1, -3.25), (4.5, -3.25), (4.5, 4.15), (-4.5, 4.15),
  706. ])
  707. return Drawing(9, 8.3).outline(outline).fill(Circle(0, 0.45, 1.25))
  708. def fiber_lc():
  709. """LC (IEC 61754-20): a square port beneath a stepped latch cavity."""
  710. outline = polygon([(x, y + 1.75) for x, y in (
  711. (-2.3, 2.3), (-2.3, -2.3), (-1.8, -2.3), (-1.8, -3.1), (-2.4, -3.1), (-2.4, -4.3), (-1.4, -4.3),
  712. (-1.4, -5.8), (1.4, -5.8), (1.4, -4.3), (2.4, -4.3), (2.4, -3.1), (1.8, -3.1), (1.8, -2.3), (2.3, -2.3),
  713. (2.3, 2.3),
  714. )])
  715. return Drawing(4.8, 8.1).outline(outline).fill(Circle(0, 1.75, 0.625))
  716. def fiber_mu():
  717. """MU (IEC 61754-6): a small upright rectangle with a key on one side."""
  718. outline = polygon([
  719. (-2, -2.8), (2, -2.8), (2, -1.85), (2.45, -1.2), (2.45, 1.2), (2, 1.85), (2, 2.8), (-2, 2.8),
  720. ])
  721. return Drawing(4.9, 5.6).outline(outline).fill(Circle(0, 0, 0.625))
  722. def fiber_fc():
  723. """FC (IEC 61754-13): a threaded barrel with a key slot at the top, on a square two-hole flange."""
  724. slot = 1.075
  725. outer, inner = math.sqrt(4 ** 2 - slot ** 2), math.sqrt(2.3 ** 2 - slot ** 2)
  726. barrel = Path([
  727. ('M', slot, -outer), arc_to(0, 0, 4, (slot, -outer), (-slot, -outer)),
  728. ('L', -slot, -inner), ('L', slot, -inner), ('Z',),
  729. ])
  730. return Drawing(15, 15).outline(rect(0, 0, 15, 15, 1.5)).detail(
  731. barrel, Circle(0, 0, 2.3), Circle(4.74, -4.74, 1.1), Circle(-4.74, 4.74, 1.1),
  732. ).fill(Circle(0, 0, 1.25))
  733. def fiber_st():
  734. """ST (IEC 61754-2): a bayonet barrel with two pins, keyed at the top."""
  735. slot = 0.65
  736. outer, inner = math.sqrt(3.52 ** 2 - slot ** 2), math.sqrt(2.745 ** 2 - slot ** 2)
  737. barrel = Path([
  738. ('M', slot, -outer), arc_to(0, 0, 3.52, (slot, -outer), (-slot, -outer)),
  739. ('L', -slot, -inner), ('L', slot, -inner), ('Z',),
  740. ])
  741. return Drawing(8.6, 8.6).outline(barrel).detail(Circle(0, 0, 2.745)).fill(
  742. Circle(-3.62, 0, 0.65), Circle(3.62, 0, 0.65), Circle(0, 0, 1.25),
  743. )
  744. def fiber_e2000():
  745. """LSH/E-2000 (IEC 61754-15): an upright opening with a latch window, its ferrule hidden by a shutter."""
  746. return Drawing(7.3, 9.9).outline(rect(0, 0, 7.3, 9.9, 0.5)).detail(rect(0, 1, 5.6, 6.6, 0.4)).fill(
  747. rect(0, -3.9, 3, 1.2),
  748. )
  749. def fiber_lx5():
  750. """LX.5 (IEC 61754-23): a narrow upright port with its ferrule offset away from the latch."""
  751. return Drawing(5.2, 8.5).outline(rect(0, 0, 5.2, 8.5, 0.4)).fill(rect(0, -3.4, 2, 0.9), Circle(0, 1.7, 0.625))
  752. def fiber_mpo():
  753. """MPO (IEC 61754-7): a wide opening keyed at the top, around an MT ferrule with a row of fibers."""
  754. outline = Path([
  755. ('M', -5, -4.1), ('L', -1.5, -4.1), ('L', -1.5, -4.9), ('L', 1.5, -4.9), ('L', 1.5, -4.1), ('L', 5, -4.1),
  756. arc_to(-1.4, 0, 7.6, (5, -4.1), (5, 4.1)), ('L', -5, 4.1), arc_to(1.4, 0, 7.6, (-5, 4.1), (-5, -4.1)),
  757. ('Z',),
  758. ])
  759. return Drawing(12.4, 9.8).outline(outline).detail(rect(0, 0, 6.4, 2.45, 0.2)).fill(
  760. Circle(-2.3, 0, 0.4), Circle(2.3, 0, 0.4), rect(0, 0, 2.9, 0.3),
  761. )
  762. def fiber_mtrj():
  763. """MT-RJ (IEC 61754-18): a small RJ-style opening around a ferrule holding two fibers between guide pins."""
  764. outline = polygon([
  765. (-3.6, -1.85), (-2, -1.85), (-2, -2.85), (2, -2.85), (2, -1.85), (3.6, -1.85), (3.6, 2.85), (-3.6, 2.85),
  766. ])
  767. return Drawing(7.2, 5.7).outline(outline).detail(rect(0, 0.5, 4.4, 2.45, 0.2)).fill(
  768. Circle(-1.3, 0.5, 0.35), Circle(1.3, 0.5, 0.35), Circle(-0.375, 0.5, 0.2), Circle(0.375, 0.5, 0.2),
  769. )
  770. def fiber_cs():
  771. """CS (Senko): two ferrules side by side beneath a push-pull tab channel."""
  772. outline = polygon([
  773. (-3.925, -1.8), (-1.2, -1.8), (-1.2, -3.5), (1.2, -3.5), (1.2, -1.8), (3.925, -1.8), (3.925, 3.5),
  774. (-3.925, 3.5),
  775. ])
  776. return Drawing(7.85, 7).outline(outline).fill(Circle(-1.9, 0.85, 0.625), Circle(1.9, 0.85, 0.625))
  777. def fiber_sn():
  778. """SN (Senko): a narrow upright port with two stacked ferrules and tabs at the top and bottom."""
  779. outline = polygon([
  780. (-1.925, -4.73), (-0.8, -4.73), (-0.8, -3.93), (0.8, -3.93), (0.8, -4.73), (1.925, -4.73),
  781. (1.925, 4.73), (0.8, 4.73), (0.8, 3.93), (-0.8, 3.93), (-0.8, 4.73), (-1.925, 4.73),
  782. ])
  783. return Drawing(3.85, 9.46).outline(outline).fill(Circle(0, -1.55, 0.625), Circle(0, 1.55, 0.625))
  784. def fiber_mdc():
  785. """MDC (US Conec): a narrow upright port with two stacked ferrules in a figure-8 boss."""
  786. return Drawing(3.9, 9.1).outline(rect(0, 0, 3.9, 9.1, 0.3)).detail(rect(0, 0.4, 2.92, 6, 1.46)).fill(
  787. rect(0, -3.8, 1.7, 0.9), Circle(0, -1.15, 0.625), Circle(0, 1.95, 0.625),
  788. )
  789. def fiber_sma():
  790. """SMA 905/906 (IEC 61754-22): a plain threaded barrel around a large bore; both types look alike."""
  791. return Drawing(6.35, 6.35).outline(Circle(0, 0, 3.175)).detail(Circle(0, 0, 2.5)).fill(Circle(0, 0, 1.59))
  792. def fiber_urm(cells):
  793. """URM (Euromicron, DIN SPEC 40032): a row of cells, each holding two stacked ferrules."""
  794. width = cells * 6.4
  795. drawing = Drawing(width, 13.4).outline(rect(0, 0, width, 13.4, 1))
  796. for i in range(cells):
  797. x = (i - (cells - 1) / 2) * 6.4
  798. drawing.detail(rect(x, 0, 4.8, 10, 0.5)).fill(
  799. rect(x, -4.4, 1.6, 0.8), rect(x, 4.4, 1.6, 0.8), Circle(x, -1.4, 0.625), Circle(x, 1.4, 0.625),
  800. )
  801. return drawing
  802. #
  803. # USB (receptacle openings)
  804. #
  805. def usb_a():
  806. return Drawing(12, 4.5).outline(rect(0, 0, 12, 4.5, 0.3)).fill(rect(0, -0.85, 10, 1.9))
  807. def usb_b():
  808. return Drawing(8.5, 7.8).outline(chamfered_rect(0, 0, 8.5, 7.8, tl=1.4, tr=1.4)).fill(rect(0, 0.4, 5.2, 3.2))
  809. def usb_3_b():
  810. outline = polygon([
  811. (-2.9, -5.6), (2.9, -5.6), (2.9, -2.4), (4.25, -2.4), (4.25, 5.6), (-4.25, 5.6), (-4.25, -2.4),
  812. (-2.9, -2.4),
  813. ])
  814. return Drawing(8.5, 11.2).outline(outline).fill(rect(0, 1.2, 5.2, 3.2), rect(0, -3.8, 3.6, 1.6))
  815. def usb_c():
  816. return Drawing(8.4, 2.6).outline(rect(0, 0, 8.4, 2.6, 1.3)).fill(rect(0, 0, 6.6, 0.7, 0.15))
  817. def usb_mini_a():
  818. outline = Path([
  819. ('M', -3.5, -1.5), ('L', 3.5, -1.5), ('L', 3.5, 0.3), ('A', 1.2, 0, 1, 2.3, 1.5),
  820. ('L', -2.3, 1.5), ('A', 1.2, 0, 1, -3.5, 0.3), ('Z',),
  821. ])
  822. return Drawing(7, 3).outline(outline).fill(rect(0, -0.6, 5, 0.6))
  823. def usb_mini_b():
  824. outline = polygon([
  825. (-3.5, -1.5), (3.5, -1.5), (3.5, 0.2), (2.9, 0.2), (2.3, 1.5), (-2.3, 1.5), (-2.9, 0.2), (-3.5, 0.2),
  826. ])
  827. return Drawing(7, 3).outline(outline).fill(rect(0, -0.6, 5, 0.6))
  828. def usb_micro_a():
  829. return Drawing(6.9, 1.85).outline(rect(0, 0, 6.9, 1.85, 0.2)).fill(rect(0, -0.45, 5, 0.45))
  830. def usb_micro_b():
  831. return Drawing(6.9, 1.85).outline(chamfered_rect(0, 0, 6.9, 1.85, br=0.7, bl=0.7)).fill(rect(0, -0.45, 5, 0.45))
  832. def usb_3_micro_b():
  833. outline = polygon([
  834. (-6.1, -0.92), (6.1, -0.92), (6.1, 0.22), (5.4, 0.92), (1.9, 0.92), (1.9, 0.1), (0.9, 0.1), (0.9, 0.92),
  835. (-5.4, 0.92), (-6.1, 0.22),
  836. ])
  837. return Drawing(12.2, 1.85).outline(outline).fill(rect(-2.6, -0.45, 5, 0.45), rect(3.9, -0.45, 3.3, 0.45))
  838. #
  839. # Molex Micro-Fit 3.0 (3 mm pitch; cavities alternate square and rounded for polarization)
  840. #
  841. def molex_micro_fit(rows, columns):
  842. pitch = 3.0
  843. width, height = columns * pitch + 1.4, rows * pitch + 1.4
  844. drawing = Drawing(width, height + 2.4)
  845. top = -(height + 2.4) / 2 + 2.4
  846. drawing.outline(
  847. rect(0, top + height / 2, width, height, 0.4),
  848. rect(0, top - 1.2, min(4, width - 1), 2.4, 0.3), # Latch
  849. )
  850. for row in range(rows):
  851. for column in range(columns):
  852. cx = (column - (columns - 1) / 2) * pitch
  853. cy = top + 0.7 + pitch / 2 + row * pitch
  854. rounded = (row + column) % 2
  855. drawing.fill(rect(cx, cy, 2.1, 2.1, 0.9 if rounded else 0.15))
  856. return drawing
  857. #
  858. # Proprietary
  859. #
  860. def powercon_chassis(flange=(26, 31), holes=(9.5, 12)):
  861. """The square flange and mounting holes common to powerCON chassis connectors."""
  862. w, h = flange
  863. return Drawing(w, h).outline(rect(0, 0, w, h, 3.5)).detail(
  864. Circle(-holes[0], -holes[1], 1.7), Circle(holes[0], holes[1], 1.7),
  865. )
  866. def powercon_20a():
  867. """NAC3MPA (power in); the power out variant (NAC3MPB) is its mirror image."""
  868. drawing = powercon_chassis().detail(Circle(0, 0, 11.8), Circle(0, 0, 6.4))
  869. return drawing.fill(
  870. rect(0, -9.5, 5.6, 1.9), # Latch
  871. rect(-9.4, 0, 1.6, 3.8), # Keying notch
  872. arc_slot(0, 0, 2.8, 45, 315, 0.8), # Centre insulator
  873. rect(*polar(0, 0, 4.5, 315), 0.8, 2.8, angle=315), # Long rib
  874. rect(*polar(0, 0, 3.8, 45), 0.8, 1.4, angle=45), # Short rib
  875. arc_slot(0, 0, 5.95, 65, 115, 0.9), arc_slot(0, 0, 5.95, 245, 295, 0.9),
  876. )
  877. def powercon_32a():
  878. """NAC3MP-HC (power in only)."""
  879. drawing = powercon_chassis((32, 37), (12.5, 15)).detail(Circle(0, 0, 14.8), Circle(0, 0, 10.5), Circle(0, 0, 4.65))
  880. drawing.fill(arc_slot(0, 0, 13.6, -14, 14, 1.4), rect(0, 11.8, 3.5, 1.9))
  881. for angle in (0, 90, 180, 270):
  882. x, y = rotate((-0.85, -3.9), angle)
  883. drawing.fill(rect(x, y, 3.2, 0.9, angle=angle))
  884. return drawing
  885. def powercon_true1_inlet():
  886. """NAC3MPX (also TRUE1 TOP, whose face differs only in a small centre detail)."""
  887. drawing = powercon_chassis().detail(Circle(0, 0, 11.8), Circle(0, 0, 6.45))
  888. drawing.fill(arc_slot(0, 0, 9.85, -17, 17, 1.7))
  889. for angle in (90, 180, 270):
  890. drawing.fill(rect(*polar(0, 0, 11, angle), 2.4, 1.6, angle=angle))
  891. for angle in (300, 51, 180):
  892. drawing.fill(arc_slot(0, 0, 5.1, angle - 15, angle + 15, 1.1))
  893. return drawing
  894. def powercon_true1_outlet():
  895. """NAC3FPX (also TRUE1 TOP)."""
  896. drawing = Drawing(35.4, 35.4).outline(rect(0, 0, 35.4, 35.4, 5.5)).detail(
  897. Circle(0, 0, 14.2), Circle(0, 0, 2.65),
  898. *(Circle(x, y, 1.72) for x in (-12.2, 12.2) for y in (-12.2, 12.2)),
  899. )
  900. for angle in (284, 164, 44):
  901. drawing.fill(arc_slot(0, 0, 5.125, angle - 49, angle + 49, 1.35))
  902. return drawing
  903. def saf_d_grid():
  904. """Saf-D-Grid 400 receptacle (used on both equipment and PDUs)."""
  905. opening = rounded_polygon(
  906. [(-8.9, -8.85), (8.9, -8.85), (11.9, -0.8), (11.9, 8.85), (-11.9, 8.85), (-11.9, -0.8)], [1, 1, 1, 2, 2, 1],
  907. )
  908. return Drawing(33.4, 24.1).outline(rect(0, 0, 33.4, 24.1, 2)).detail(opening).fill(
  909. rect(0, -1.8, 4.4, 1.4), rect(-6.9, 3, 4.4, 1.4), rect(6.9, 3, 4.4, 1.4),
  910. )
  911. def hdot_cx():
  912. """Accepts both C14 and C20 plugs: a C13 profile with slots combining the C13 and C19 patterns."""
  913. drawing = iec_c13_family()
  914. drawing.elements = [e for e in drawing.elements if e[0] == 'outline']
  915. return drawing.fill(
  916. rect(0, -4, 7, 2.5), rect(0, -2, 2.5, 5),
  917. rect(-6.5, 4, 6, 2.5), rect(-7, 2, 2.5, 5),
  918. rect(6.5, 4, 6, 2.5), rect(7, 2, 2.5, 5),
  919. )
  920. def eaton_c39():
  921. """Accepts both C14 and C20 plugs: a C20-sized cavity around a stepped block with T-shaped slots."""
  922. block = polygon([
  923. (-5.75, -7), (5.75, -7), (5.75, -2), (11, -2), (11, 7), (2.4, 7), (2.4, 3), (-2.4, 3), (-2.4, 7),
  924. (-11, 7), (-11, -2), (-5.75, -2),
  925. ])
  926. return Drawing(35, 27.5).outline(rect(0, 0, 35, 27.5, 3.5)).detail(block).fill(
  927. rect(0, -4, 7, 2.5), rect(0, -2, 2.5, 5),
  928. rect(-6.5, 4, 6, 2.5), rect(-7, 2, 2.5, 5),
  929. rect(6.5, 4, 6, 2.5), rect(7, 2, 2.5, 5),
  930. )
  931. #
  932. # Registry
  933. #
  934. IEC_60309_TYPES = {
  935. # Value prefix: number of contacts
  936. 'iec-60309-p-n-e': 3,
  937. 'iec-60309-2p-e': 3,
  938. 'iec-60309-3p-e': 4,
  939. 'iec-60309-3p-n-e': 5,
  940. }
  941. def iec_60309_types(male=False):
  942. """Return a drawing function for each IEC 60309 type."""
  943. def factory(contacts, hour):
  944. def func():
  945. drawing = iec_60309(contacts, hour, male=male)
  946. return (drawing, True) if male else drawing
  947. return func
  948. return {
  949. f'{prefix}-{hour}h': factory(contacts, hour)
  950. for prefix, contacts in IEC_60309_TYPES.items()
  951. for hour in (4, 6, 9)
  952. }
  953. def nema_types(male=False):
  954. """Return a drawing function for each NEMA (straight-blade and locking) and California Style type."""
  955. suffix = 'p' if male else 'r'
  956. faces = {**NEMA_STRAIGHT, **NEMA_LOCKING, **(NEMA_STRAIGHT_PLUGS if male else {})}
  957. types = {f'nema-{name}{suffix}': func for name, func in faces.items()}
  958. if male:
  959. types = {value: mirrored(func) for value, func in types.items()}
  960. for (female_value, male_value), (slots, lugs, recess) in CALIFORNIA_STYLE.items():
  961. value = male_value if male else female_value
  962. types[value] = (lambda s=slots, lu=lugs, r=recess: (california(s, male, lu, r), male))
  963. return types
  964. # Outlet type: drawing function
  965. POWER_OUTLETS = {
  966. 'iec-60320-c5': iec_c5_family,
  967. 'iec-60320-c7': iec_c7_family,
  968. 'iec-60320-c13': iec_c13_family,
  969. 'iec-60320-c15': lambda: iec_c13_family(notch=True),
  970. 'iec-60320-c17': lambda: iec_c13_family(earth=False),
  971. 'iec-60320-c19': iec_c19_family,
  972. 'iec-60320-c21': lambda: iec_c19_family(chamfer=True),
  973. **iec_60309_types(),
  974. 'iec-60906-1': lambda: iec_60906(2.4),
  975. 'nbr-14136-10a': lambda: iec_60906(2.2),
  976. 'nbr-14136-20a': lambda: iec_60906(2.6),
  977. **nema_types(),
  978. 'ita-e': ita_e_socket,
  979. 'ita-f': ita_f_socket,
  980. 'ita-g': ita_g,
  981. 'ita-h': ita_h,
  982. 'ita-i': ita_i,
  983. 'ita-j': ita_j,
  984. 'ita-k': ita_k,
  985. 'ita-l': ita_l_socket,
  986. 'ita-m': ita_m,
  987. 'ita-n': lambda: iec_60906(2.4),
  988. 'ita-o': ita_o,
  989. 'usb-a': usb_a,
  990. 'usb-micro-b': usb_micro_b,
  991. 'usb-c': usb_c,
  992. 'molex-micro-fit-1x2': lambda: molex_micro_fit(1, 2),
  993. 'molex-micro-fit-2x2': lambda: molex_micro_fit(2, 2),
  994. 'molex-micro-fit-2x3': lambda: molex_micro_fit(2, 3),
  995. 'molex-micro-fit-2x4': lambda: molex_micro_fit(2, 4),
  996. 'eaton-c39': eaton_c39,
  997. 'hdot-cx': hdot_cx,
  998. 'saf-d-grid': saf_d_grid,
  999. # Power out chassis connectors are the mirror image of power in
  1000. 'neutrik-powercon-20a': mirrored(powercon_20a),
  1001. 'neutrik-powercon-32a': powercon_32a,
  1002. 'neutrik-powercon-true1': powercon_true1_outlet,
  1003. 'neutrik-powercon-true1-top': powercon_true1_outlet,
  1004. }
  1005. # Port type: drawing function. Faces wrapped with mirrored() are drawn as the mirror image of the mating face.
  1006. POWER_PORTS = {
  1007. # IEC 60320 inlets share the orientation of their mating connector
  1008. 'iec-60320-c6': lambda: iec_c5_family(male=True),
  1009. 'iec-60320-c8': lambda: iec_c7_family(male=True),
  1010. 'iec-60320-c14': lambda: iec_c13_family(male=True),
  1011. 'iec-60320-c16': lambda: iec_c13_family(male=True, notch=True),
  1012. 'iec-60320-c18': lambda: iec_c13_family(male=True, earth=False),
  1013. 'iec-60320-c20': lambda: iec_c19_family(male=True),
  1014. 'iec-60320-c22': lambda: iec_c19_family(male=True, chamfer=True),
  1015. **iec_60309_types(male=True),
  1016. 'iec-60906-1': mirrored(lambda: iec_60906(2.25)),
  1017. 'nbr-14136-10a': mirrored(lambda: iec_60906(2.0)),
  1018. 'nbr-14136-20a': mirrored(lambda: iec_60906(2.4)),
  1019. **nema_types(male=True),
  1020. 'ita-c': ita_c_plug,
  1021. 'ita-e': lambda: schuko_plug(strips=False, earth_hole=True),
  1022. 'ita-f': schuko_plug,
  1023. 'ita-ef': lambda: schuko_plug(earth_hole=True),
  1024. 'ita-g': mirrored(lambda: ita_g(socket=False)),
  1025. 'ita-h': mirrored(lambda: ita_h(socket=False)),
  1026. 'ita-i': mirrored(lambda: ita_i(socket=False)),
  1027. 'ita-j': mirrored(lambda: ita_j(socket=False)),
  1028. 'ita-k': mirrored(lambda: ita_k(socket=False)),
  1029. 'ita-l': ita_l_plug,
  1030. 'ita-m': mirrored(lambda: ita_m(socket=False)),
  1031. 'ita-n': mirrored(lambda: iec_60906(2.25)),
  1032. 'ita-o': mirrored(lambda: ita_o(socket=False)),
  1033. # USB power ports are receptacles on the device
  1034. 'usb-a': usb_a,
  1035. 'usb-b': usb_b,
  1036. 'usb-c': usb_c,
  1037. 'usb-mini-a': usb_mini_a,
  1038. 'usb-mini-b': usb_mini_b,
  1039. 'usb-micro-a': usb_micro_a,
  1040. 'usb-micro-b': usb_micro_b,
  1041. 'usb-micro-ab': usb_micro_a,
  1042. 'usb-3-b': usb_3_b,
  1043. 'usb-3-micro-b': usb_3_micro_b,
  1044. 'molex-micro-fit-1x2': mirrored(lambda: molex_micro_fit(1, 2)),
  1045. 'molex-micro-fit-2x2': mirrored(lambda: molex_micro_fit(2, 2)),
  1046. 'molex-micro-fit-2x3': mirrored(lambda: molex_micro_fit(2, 3)),
  1047. 'molex-micro-fit-2x4': mirrored(lambda: molex_micro_fit(2, 4)),
  1048. 'saf-d-grid': saf_d_grid,
  1049. 'neutrik-powercon-20': powercon_20a,
  1050. 'neutrik-powercon-32': powercon_32a,
  1051. 'neutrik-powercon-true1': powercon_true1_inlet,
  1052. 'neutrik-powercon-true1-top': powercon_true1_inlet,
  1053. }
  1054. # Console port type: drawing function. USB console ports are receptacles.
  1055. CONSOLE_PORTS = {
  1056. 'de-9': lambda: d_sub(9, 16.26, 2.74),
  1057. 'db-25': lambda: d_sub(25, 38.3, 2.77),
  1058. # RJ-11 is defined with only its two centre positions populated (47 CFR 68.502)
  1059. 'rj-11': lambda: modular_jack(6, (3, 4)),
  1060. 'rj-12': lambda: modular_jack(6, range(1, 7)),
  1061. 'rj-45': lambda: modular_jack(8, range(1, 9)),
  1062. 'mini-din-8': mini_din_8,
  1063. 'usb-a': usb_a,
  1064. 'usb-b': usb_b,
  1065. 'usb-c': usb_c,
  1066. 'usb-mini-a': usb_mini_a,
  1067. 'usb-mini-b': usb_mini_b,
  1068. 'usb-micro-a': usb_micro_a,
  1069. 'usb-micro-b': usb_micro_b,
  1070. 'usb-micro-ab': usb_micro_a,
  1071. }
  1072. # Front/rear port type: drawing function. Ports are drawn as the jack, adapter, or bulkhead receptacle.
  1073. PORTS = {
  1074. '8p8c': lambda: modular_jack(8, range(1, 9)),
  1075. '8p6c': lambda: modular_jack(8, range(2, 8)),
  1076. '8p4c': lambda: modular_jack(8, range(3, 7)),
  1077. '8p2c': lambda: modular_jack(8, (4, 5)),
  1078. '6p6c': lambda: modular_jack(6, range(1, 7)),
  1079. '6p4c': lambda: modular_jack(6, range(2, 6)),
  1080. '6p2c': lambda: modular_jack(6, (3, 4)),
  1081. '4p4c': lambda: modular_jack(4, range(1, 5)),
  1082. '4p2c': lambda: modular_jack(4, (2, 3)),
  1083. 'gg45': gg45,
  1084. 'tera-4p': lambda: tera(4),
  1085. 'tera-2p': lambda: tera(2),
  1086. 'tera-1p': lambda: tera(1),
  1087. '110-punch': punch_110,
  1088. 'bnc': bnc,
  1089. 'f': f_connector,
  1090. 'n': n_connector,
  1091. 'mrj21': mrj21,
  1092. **{f'fc{polish}': fiber_fc for polish in ('', '-pc', '-upc', '-apc')},
  1093. **{f'lc{polish}': fiber_lc for polish in ('', '-pc', '-upc', '-apc')},
  1094. **{f'mu{polish}': fiber_mu for polish in ('', '-pc', '-upc', '-apc')},
  1095. **{f'lsh{polish}': fiber_e2000 for polish in ('', '-pc', '-upc', '-apc')},
  1096. **{f'lx5{polish}': fiber_lx5 for polish in ('', '-pc', '-upc', '-apc')},
  1097. **{f'sc{polish}': fiber_sc for polish in ('', '-pc', '-upc', '-apc')},
  1098. 'mpo': fiber_mpo,
  1099. 'mtrj': fiber_mtrj,
  1100. 'st': fiber_st,
  1101. 'cs': fiber_cs,
  1102. 'sn': fiber_sn,
  1103. 'mdc': fiber_mdc,
  1104. 'sma-905': fiber_sma,
  1105. 'sma-906': fiber_sma,
  1106. 'urm-p2': lambda: fiber_urm(1),
  1107. 'urm-p4': lambda: fiber_urm(2),
  1108. 'urm-p8': lambda: fiber_urm(4),
  1109. 'usb-a': usb_a,
  1110. 'usb-b': usb_b,
  1111. 'usb-c': usb_c,
  1112. 'usb-mini-a': usb_mini_a,
  1113. 'usb-mini-b': usb_mini_b,
  1114. 'usb-micro-a': usb_micro_a,
  1115. 'usb-micro-b': usb_micro_b,
  1116. 'usb-micro-ab': usb_micro_a,
  1117. }
  1118. def write(directory, drawings):
  1119. directory.mkdir(parents=True, exist_ok=True)
  1120. expected = set()
  1121. for value, func in drawings.items():
  1122. result = func()
  1123. drawing, mirror = result if isinstance(result, tuple) else (result, False)
  1124. path = directory / f'{value}.svg'
  1125. expected.add(path.name)
  1126. content = drawing.svg(mirror=mirror)
  1127. if not path.exists() or path.read_text() != content:
  1128. path.write_text(content)
  1129. print(f'Wrote {path}')
  1130. # Remove drawings for types no longer defined
  1131. for path in directory.glob('*.svg'):
  1132. if path.name not in expected:
  1133. path.unlink()
  1134. print(f'Removed {path}')
  1135. def main():
  1136. write(OUTPUT_DIR / 'power-outlet', POWER_OUTLETS)
  1137. write(OUTPUT_DIR / 'power-port', POWER_PORTS)
  1138. write(OUTPUT_DIR / 'console-port', CONSOLE_PORTS)
  1139. write(OUTPUT_DIR / 'port', PORTS)
  1140. if __name__ == '__main__':
  1141. main()