# -*- coding: utf-8 -*- """Biblioteca comum do projeto Robô Móvel (FreeCAD 1.1). Convenções: * unidades em mm; eixo Z para cima na montagem. * cada peça é gerada por um script em cad/pecas/ e salva em pecas/.FCStd * a montagem usa App::Link para essas peças (não copia geometria). """ import os, math import FreeCAD as App import Part from FreeCAD import Vector as V, Placement, Rotation ROOT = os.path.expanduser("~/robo_movel") PECAS = os.path.join(ROOT, "pecas") MONT = os.path.join(ROOT, "montagem") CORTE = os.path.join(ROOT, "corte") FONT = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf" FONT_REG = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf" # ---------------------------------------------------------------- cores COR = { "preto": (0.10, 0.10, 0.11), "grafite": (0.20, 0.20, 0.22), "borracha": (0.23, 0.23, 0.25), "aluminio": (0.80, 0.81, 0.83), "aco": (0.62, 0.63, 0.66), "aco_preto": (0.16, 0.16, 0.17), "zincado": (0.74, 0.75, 0.78), "latao": (0.80, 0.66, 0.30), "ouro": (0.85, 0.70, 0.25), "cobre": (0.80, 0.50, 0.30), "pcb_azul": (0.05, 0.30, 0.62), "pcb_verm": (0.78, 0.10, 0.08), "pcb_preto": (0.08, 0.08, 0.09), "pcb_verde": (0.10, 0.40, 0.18), "amarelo": (0.95, 0.80, 0.10), "branco": (0.94, 0.94, 0.92), "creme": (0.95, 0.93, 0.86), "azul": (0.10, 0.35, 0.85), "vermelho": (0.85, 0.08, 0.08), "verde": (0.10, 0.60, 0.20), "mdf": (0.72, 0.56, 0.38), "mdf_borda": (0.45, 0.32, 0.20), "nylon": (0.93, 0.92, 0.86), "bege": (0.87, 0.80, 0.65), "lipo": (0.15, 0.20, 0.30), "laranja": (0.95, 0.45, 0.05), "roxo": (0.50, 0.20, 0.70), "cinza": (0.55, 0.55, 0.57), "marrom": (0.45, 0.25, 0.10), } def rgb(name): return COR[name] if isinstance(name, str) else name # ---------------------------------------------------------------- documentos def new_doc(name): """Fecha (se aberto) e cria um documento novo.""" if name in App.listDocuments(): App.closeDocument(name) return App.newDocument(name) def add(doc, name, shape, color="grafite", label=None, transp=0, group=None): o = doc.addObject("Part::Feature", name) o.Shape = shape if label: o.Label = label if App.GuiUp: vo = o.ViewObject vo.ShapeColor = rgb(color) vo.LineColor = tuple(c * 0.4 for c in rgb(color)) vo.Transparency = transp if group is not None: group.addObject(o) return o def part_container(doc, name, label=None): p = doc.addObject("App::Part", name) if label: p.Label = label return p def save(doc, fname=None): fname = fname or os.path.join(PECAS, doc.Name + ".FCStd") doc.recompute() doc.saveAs(fname) return fname def fit(view="isometric"): if not App.GuiUp: return import FreeCADGui as Gui v = Gui.ActiveDocument.ActiveView getattr(v, "view" + view.capitalize())() Gui.SendMsgToActiveView("ViewFit") # ---------------------------------------------------------------- geometria def box(dx, dy, dz, x=0, y=0, z=0): return Part.makeBox(dx, dy, dz, V(x, y, z)) def cbox(dx, dy, dz, cx=0, cy=0, z=0): """Caixa centrada em XY (cx, cy), base em z.""" return Part.makeBox(dx, dy, dz, V(cx - dx / 2, cy - dy / 2, z)) def cyl(r, h, p=V(0, 0, 0), d=V(0, 0, 1)): return Part.makeCylinder(r, h, p, d) def rbox(dx, dy, dz, r, x=0, y=0, z=0): """Caixa com cantos verticais arredondados (raio r).""" b = box(dx, dy, dz, x, y, z) if r <= 0: return b edges = [e for e in b.Edges if abs(e.Vertexes[0].Point.z - e.Vertexes[1].Point.z) > 1e-6] return b.makeFillet(r, edges) def fuse(shapes): shapes = [s for s in shapes if s is not None] if len(shapes) == 1: return shapes[0] r = shapes[0].multiFuse(shapes[1:]) return r.removeSplitter() def comp(shapes): return Part.makeCompound([s for s in shapes if s is not None]) def cut(a, tools): if not isinstance(tools, (list, tuple)): tools = [tools] tools = [t for t in tools if t is not None] if not tools: return a return a.cut(Part.makeCompound(tools) if len(tools) > 1 else tools[0]) def one_solid(s): if s.ShapeType == "Solid": return s if len(s.Solids) == 1: return s.Solids[0] return s def rot_to(dirv): """Rotation que leva +Z para dirv.""" return Rotation(V(0, 0, 1), dirv) def placed(shape, pos=V(0, 0, 0), rot=Rotation()): s = shape.copy() s.Placement = Placement(pos, rot).multiply(s.Placement) return s def text_shape(txt, size, font=FONT, height=0.0, tracking=0): """Contorno (face) do texto no plano XY com base em y=0. height>0 extruda em +Z.""" wires = Part.makeWireString(txt, font, size, tracking) faces = [] for ch in wires: if not ch: continue # separa contornos externos e furos (ex.: 'O', '6', '0') ws = sorted(ch, key=lambda w: -Part.Face(w).Area if w.isClosed() else 0) fs = [] for w in ws: if not w.isClosed(): continue f = Part.Face(w) placed_ = False for i, F in enumerate(fs): if F.isInside(w.Vertexes[0].Point, 1e-4, True) and abs(f.Area) < abs(F.Area): fs[i] = F.cut(f) placed_ = True break if not placed_: fs.append(f) faces += fs if not faces: return None sh = Part.makeCompound(faces) if height > 0: sh = Part.makeCompound([f.extrude(V(0, 0, height)) for f in faces]) return sh def text_center(sh): bb = sh.BoundBox return V((bb.XMin + bb.XMax) / 2, (bb.YMin + bb.YMax) / 2, 0) # ---------------------------------------------------------------- fixadores # Dimensões segundo ISO 4762 (allen), ISO 7380 (abaulado), ISO 7045 (Phillips # panela), ISO 4032 (porca), DIN 985 (porca c/ nylon), DIN 125 (arruela), # DIN 913 (sem cabeça ponta chata). Roscas cosméticas (cilindro no Ø nominal). ISO4762 = {2: (3.8, 2.0, 1.5), 2.5: (4.5, 2.5, 2.0), 3: (5.5, 3.0, 2.5), 4: (7.0, 4.0, 3.0), 5: (8.5, 5.0, 4.0)} ISO7380 = {3: (5.7, 1.65, 2.0), 4: (7.6, 2.2, 2.5)} ISO7045 = {2: (4.0, 1.6), 2.5: (5.0, 2.0), 3: (5.6, 2.4), 4: (8.0, 3.1)} ISO4032 = {2: (4.0, 1.6), 2.5: (5.0, 2.0), 3: (5.5, 2.4), 4: (7.0, 3.2), 5: (8.0, 4.7), 8: (13.0, 6.8)} DIN985 = {3: (5.5, 4.0), 4: (7.0, 5.0), 8: (13.0, 8.0)} DIN125 = {2: (5.0, 2.2, 0.3), 2.5: (6.0, 2.7, 0.5), 3: (7.0, 3.2, 0.5), 4: (9.0, 4.3, 0.8), 5: (10.0, 5.3, 1.0), 8: (16.0, 8.4, 1.6)} DIN9021 = {3: (9.0, 3.2, 0.8), 4: (12.0, 4.3, 1.0), 8: (24.0, 8.4, 2.0)} def _hex_prism(af, h, z0=0.0): r = af / math.sqrt(3) pts = [V(r * math.cos(math.radians(30 + 60 * i)), r * math.sin(math.radians(30 + 60 * i)), z0) for i in range(6)] return Part.Face(Part.makePolygon(pts + [pts[0]])).extrude(V(0, 0, h)) def _shank(d, L): """Haste de -L a 0 com chanfro na ponta e marca de rosca cosmética.""" c = 0.1 * d s = Part.makeCone(d / 2 - c, d / 2, c, V(0, 0, -L)) s = s.fuse(cyl(d / 2, L - c, V(0, 0, -L + c))) return s def screw_socket(d, L): """ISO 4762 — cabeça cilíndrica sextavado interno. Origem: face de apoio da cabeça, haste para -Z.""" dk, k, s = ISO4762[d] head = cyl(dk / 2, k) head = head.makeFillet(0.08 * dk, [e for e in head.Edges if e.Curve.__class__.__name__ == "Circle" and e.Vertexes[0].Point.z > k - 1e-6]) sock = _hex_prism(s, 0.6 * k + 0.01, k - 0.6 * k) head = head.cut(sock) return fuse([head, _shank(d, L)]) def screw_button(d, L): """ISO 7380 — cabeça abaulada sextavado interno.""" dk, k, s = ISO7380[d] R = (k ** 2 + (dk / 2) ** 2) / (2 * k) sph = Part.makeSphere(R, V(0, 0, k - R)) head = sph.common(cyl(dk / 2, k)) head = head.cut(_hex_prism(s, k, k * 0.35)) return fuse([head, _shank(d, L)]) def screw_pan_phillips(d, L): """ISO 7045 — cabeça panela Phillips.""" dk, k = ISO7045[d] head = cyl(dk / 2, k) top = [e for e in head.Edges if e.Curve.__class__.__name__ == "Circle" and e.Vertexes[0].Point.z > k - 1e-6] head = head.makeFillet(0.35 * k, top) w, dep = 0.18 * dk, 0.6 * k cr = cbox(0.6 * dk, w, dep, 0, 0, k - dep).fuse(cbox(w, 0.6 * dk, dep, 0, 0, k - dep)) head = head.cut(cr) return fuse([head, _shank(d, L)]) def set_screw(d, L): """DIN 913 — sem cabeça, sextavado interno, ponta chata. Origem no topo.""" s = {2: 0.9, 2.5: 1.3, 3: 1.5, 4: 2.0, 5: 2.5}[d] body = _shank(d, L) return body.cut(_hex_prism(s, 0.5 * L + 0.01, -0.5 * L)) def nut(d, nylock=False): """ISO 4032 (ou DIN 985 com anel de nylon). Origem na face de apoio.""" if nylock: af, m = DIN985[d] mh = ISO4032[d][1] else: af, m = ISO4032[d] mh = m n = _hex_prism(af, mh) # chanfro 30° nas duas faces (intersecção com bicone) k = math.tan(math.radians(60)) c1 = Part.makeCone(af / 2 * 0.98, af / 2 * 0.98 + mh * k, mh) c2 = Part.makeCone(af / 2 * 0.98 + mh * k, af / 2 * 0.98, mh) n = n.common(c1).common(c2) n = n.cut(cyl(d * 0.42, mh + 2, V(0, 0, -1))) if nylock: ring = cyl(af * 0.47, m - mh, V(0, 0, mh)).cut(cyl(d * 0.42, m, V(0, 0, mh - 0.1))) return n, ring return n def washer(d, large=False): """DIN 125 (ou DIN 9021 'arruela larga' se large=True).""" D, di, h = (DIN9021 if large else DIN125)[d] return cyl(D / 2, h).cut(cyl(di / 2, h + 2, V(0, 0, -1))) def wing_nut(d=8): """Porca borboleta DIN 315 (M8: e=39, h=20, m=10, d2=16). Origem: face de apoio, asas para +Z.""" e, h, m, d2 = {6: (32, 16, 8, 13), 8: (39, 20, 10, 16)}[d] hub = Part.makeCone(d2 / 2 + 1.0, d2 / 2 - 1.0, m) wings = [] for s in (-1, 1): pts = [V(s * d2 / 2 * 0.7, -1.5, 0.5), V(s * e / 2, -1.5, h * 0.55), V(s * e / 2, -1.5, h), V(s * (e / 2 - 5), -1.5, h), V(s * d2 / 2 * 0.6, -1.5, m)] f = Part.Face(Part.makePolygon(pts + [pts[0]])) wings.append(f.extrude(V(0, 3.0, 0))) wn = fuse([hub] + wings) return wn.cut(cyl(d * 0.42, h + 2, V(0, 0, -1))) def threaded_rod(d, L): """Barra roscada (DIN 975) — rosca cosmética, chanfro nas pontas. Origem na ponta inferior, eixo +Z.""" c = 0.1 * d r = cyl(d / 2, L - 2 * c, V(0, 0, c)) r = r.fuse([Part.makeCone(d / 2 - c, d / 2, c), Part.makeCone(d / 2, d / 2 - c, c, V(0, 0, L - c))]) return r.removeSplitter() def standoff(d, L, af=5.0, male=0.0): """Espaçador sextavado F-F (male>0 cria pino macho de comprimento male).""" b = _hex_prism(af, L).cut(cyl(d * 0.42, L + 2, V(0, 0, -1))) if male > 0: b = b.fuse(_shank(d, male).mirror(V(0, 0, 0), V(0, 0, 1)).translate(V(0, 0, L))) return b def mdf_color(obj): if App.GuiUp: obj.ViewObject.ShapeColor = rgb("mdf") obj.ViewObject.LineColor = rgb("mdf_borda")