"""Pocket transit after D. W. Brunton (US 526,021, 1894; slope scale US 1,593,429, 1926). Printed case for a liquid
compass capsule, mirror lid with sight line and opening, slotted folding sight, and a level-vial clinometer arm on the
back over a paper scale. Every size OURS. Closed and open positions clash-checked."""
import os, math
import FreeCAD as App, Part, Mesh
V = App.Vector; X = V(1, 0, 0); Y = V(0, 1, 0); Z = V(0, 0, 1)
OUT = "./"
LOG = open(OUT + "build.log", "w")
def L(*a): LOG.write(" ".join(map(str, a)) + "\n"); LOG.flush()
def box(x0, x1, y0, y1, z0, z1): return Part.makeBox(x1 - x0, y1 - y0, z1 - z0, V(x0, y0, z0))
def cyl(r, p, d, l): return Part.makeCylinder(r, l, p, d)
H = 35.0; TOP = 16.0; CAP_D = 45.4; CAP_DEPTH = 11.0
LID_AX = (-37.0, 22.0); ARM_AX = (38.0, 17.55); KR = 3.0; PIN = 0.95
OUTER = [(20.0, 30.0), (-30.0, -20.0)]; MID = (-19.6, 19.6)
def knuckle(x0, x1, ax): return cyl(KR, V(x0, ax[0], ax[1]), X, x1 - x0)
def pinhole(ax): return cyl(PIN, V(-40, ax[0], ax[1]), X, 80)
case = box(-H, H, -H, H, 0, TOP)
case = case.cut(cyl(CAP_D / 2, V(0, 0, TOP - CAP_DEPTH), Z, CAP_DEPTH + 1))
case = case.cut(cyl(1.25, V(0, 0, -1), Z, 5)) # clinometer pivot, M3 self-tapping
for (x0, x1) in OUTER:
case = case.fuse(knuckle(x0, x1, LID_AX)).fuse(box(x0, x1, -37.0, -H + 0.01, 11.0, LID_AX[1]))
case = case.fuse(knuckle(x0, x1, ARM_AX)).fuse(box(x0, x1, H - 0.01, 38.0, 11.0, ARM_AX[1]))
case = case.cut(pinhole(LID_AX)).cut(pinhole(ARM_AX)).removeSplitter()
lid = box(-H, H, -H, 34.0, 20.0, 24.0)
for (x0, x1) in OUTER:
lid = lid.cut(box(x0 - 0.2, x1 + 0.2, -H - 1, -33.4, 18, 24)) # clear the case knuckles
lid = lid.cut(box(-25.2, 25.2, -28.2, 28.2, 19.0, 22.2)) # mirror recess (50 x 56 x 2 mm sheet)
lid = lid.cut(cyl(2.0, V(0, -24.0, 17), Z, 8)) # sight opening h
lid = lid.fuse(knuckle(MID[0], MID[1], LID_AX)).fuse(box(MID[0], MID[1], -37.0, -33.0, 20.0, 24.0))
lid = lid.cut(pinhole(LID_AX)).cut(box(-40, 40, -45, 40, 24.0, 30)).removeSplitter() # knuckle flush with the outer face (printed face down)
arm = box(-12, 12, -26.0, 35.5, 16.3, 19.8)
arm = arm.cut(box(-1.25, 1.25, -14.0, 30.0, 15, 20)) # sight slot l
arm = arm.fuse(knuckle(MID[0], MID[1], ARM_AX)).fuse(box(-12, 12, 33.0, 38.0, 16.3, 19.8))
arm = arm.cut(pinhole(ARM_AX)).cut(box(-40, 40, -40, 50, 19.8, 30)).removeSplitter() # flush with the top face (printed face down)
def clino():
a = box(-6, 6, -30, 24, 0, 3).fuse(Part.Face(Part.makePolygon([V(-6, 24, 0), V(0, 32, 0), V(6, 24, 0), V(-6, 24, 0)])).extrude(V(0, 0, 3)))
a = a.cut(cyl(1.7, V(0, 0, -1), Z, 5))
for yy in (-14.0, 14.0): # two snap clips for a 10 mm vial along the arm
clip = cyl(7.0, V(0, yy - 4, 8.0), Y, 8).cut(cyl(5.1, V(0, yy - 5, 8.0), Y, 10)).cut(box(-8, 8, yy - 5, yy + 5, 11.5, 20))
a = a.fuse(clip.cut(box(-8, 8, yy - 5, yy + 5, -5, 0)))
a = a.fuse(box(-4, 4, -30, -24, 3, 6)) # finger tab (Brunton's crank s)
a = a.cut(box(-0.4, 0.4, 26, 33, 2.4, 4)) # index line on the tip
return a.removeSplitter()
cl = clino()
parts = {"pocket-transit-case": case, "pocket-transit-lid": lid, "pocket-transit-sight": arm, "pocket-transit-clinometer-arm": cl}
for n, s in parts.items():
bb = s.BoundBox
L(n, "valid", s.isValid(), "solids", len(s.Solids), "PETG g ~", round(s.Volume / 1000 * 1.27, 1), "bbox", round(bb.XLength, 1), round(bb.YLength, 1), round(bb.ZLength, 1))
capsule = cyl(22.5, V(0, 0, TOP - 10.5), Z, 10.5)
mirror = box(-25, 25, -28, 28, 20.2, 22.2).cut(cyl(2.0, V(0, -24.0, 17), Z, 8))
clA = cl.copy(); clA.rotate(V(0, 0, 0), X, 180); clA.translate(V(0, 0, -0.1)) # under the back, on the paper
vial = cyl(5.0, V(0, -20, -8.1), Y, 40) # 10 x 40 mm vial in the clips
def check(label, d, ok=()):
n = list(d); c = 0
for i in range(len(n)):
for j in range(i + 1, len(n)):
a, b = n[i], n[j]
if (a, b) in ok or (b, a) in ok: continue
if not d[a].BoundBox.intersect(d[b].BoundBox): continue
v = d[a].common(d[b]).Volume
if v > 0.05: c += 1; L(label, "CLASH", a, b, round(v, 2))
L(label, "clashes", c)
check("closed", {"case": case, "lid": lid, "arm": arm, "capsule": capsule, "mirror": mirror, "clino": clA, "vial": vial},
ok={("lid", "mirror")})
lidO = lid.copy(); lidO.rotate(V(0, LID_AX[0], LID_AX[1]), X, 135); mirO = mirror.copy(); mirO.rotate(V(0, LID_AX[0], LID_AX[1]), X, 135)
armO = arm.copy(); armO.rotate(V(0, ARM_AX[0], ARM_AX[1]), X, -180)
check("open", {"case": case, "lid": lidO, "arm": armO, "capsule": capsule, "mirror": mirO}, ok={("lid", "mirror")})
# sweep checks: lid and arm through their whole swing against the case
for nm, sh, ax, rng in (("lid", lid, LID_AX, range(0, 136, 5)), ("arm", arm, ARM_AX, range(0, -181, -5))):
worst = 0.0
for ang in rng:
s = sh.copy(); s.rotate(V(0, ax[0], ax[1]), X, ang); worst = max(worst, s.common(case).Volume)
L("swing", nm, "max overlap with case mm3", round(worst, 2))
if os.environ.get("NOSAVE") != "1":
for n, s in parts.items():
m = Mesh.Mesh(); m.addFacets(s.tessellate(0.02)); m.write(OUT + n + ".stl")
if os.environ.get("HERO") == "1":
import FreeCADGui as Gui
d = App.newDocument("h")
show = {"case": (case, (0.12, 0.35, 0.22)), "lid": (lidO, (0.12, 0.35, 0.22)), "arm": (armO, (0.12, 0.35, 0.22)),
"capsule": (capsule, (0.92, 0.92, 0.88)), "mirror": (mirO, (0.78, 0.84, 0.9))}
for n, (sh, c) in show.items():
o = d.addObject("Part::Feature", n); o.Shape = sh; d.recompute(); Gui.ActiveDocument.getObject(n).ShapeColor = c
ndl = box(-0.8, 0.8, -19, 19, TOP - 3, TOP - 2.5).fuse(Part.Face(Part.makePolygon([V(-2, 19, TOP - 3), V(0, 22, TOP - 3), V(2, 19, TOP - 3), V(-2, 19, TOP - 3)])).extrude(V(0, 0, 0.5)))
o = d.addObject("Part::Feature", "needle"); o.Shape = ndl; d.recompute(); Gui.ActiveDocument.getObject("needle").ShapeColor = (0.75, 0.1, 0.1)
v = Gui.ActiveDocument.ActiveView
v.setCameraType("Orthographic"); v.setViewDirection(tuple(float(c) for c in os.environ.get("VDIR", "-0.6,0.55,-0.35").split(","))); v.fitAll(); Gui.updateGui()
v.saveImage(OUT + "hero.png", 1600, 1200, "White"); L("hero saved")
LOG.close(); os._exit(0)