Files
RoboMovel/ferramentas/scripts/cad/exporta_render.py
T
mguinhos 6405625c1a Projeto: robo de teste modular impresso na Ender 3 e chapas identificadas em camadas
Robo de teste em PLA (caixa_teste_ender3.py): fundo em 6 placas com grade de furos,
paredes e bandejas separadas, presas pelo mesmo pino de encaixe; STL e G-codes para
os bicos de 0,2 e 0,4 mm (os de 0,4 mm usam G29 no inicio).

Chapas 35x35: posicao dos suportes NEMA17 em azul e DXF identificado com as camadas
CORTE, SUPORTES e TEXTO para o LightBurn.

Scripts CAD: caminhos relativos ao repositorio e guarda contra links vazios.
2026-11-09 12:18:16 -03:00

95 lines
3.5 KiB
Python

# -*- coding: utf-8 -*-
"""Exporta a montagem (com links resolvidos) para OBJ/MTL em metros, um
material por cor da paleta do projeto (lib_rm.COR). Rodar no FreeCAD GUI
(precisa das cores dos ViewObjects)."""
import os, sys, math
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) # cad/ deste repositório
import FreeCAD as App
import Part
from lib_rm import COR
OUT = os.path.expanduser("~/robo_movel/render")
def nearest_color(rgb):
best, bd = "cinza", 9
for k, c in COR.items():
dd = sum((a - b) ** 2 for a, b in zip(rgb[:3], c))
if dd < bd:
best, bd = k, dd
return best
def leaf_items(doc):
"""(nome, shape em coords do mundo, classe de material, transparência)."""
items = []
def vo_info(o):
try:
vo = o.ViewObject
return tuple(vo.ShapeColor[:3]), int(vo.Transparency), bool(vo.Visibility)
except Exception:
return (0.5, 0.5, 0.5), 0, True
for o in doc.Objects:
if o.TypeId == "Part::Feature":
if not o.Shape or o.Shape.isNull():
continue
if any(p.TypeId == "App::Part" for p in o.InList):
continue
col, tr, vis = vo_info(o)
items.append((o.Name, o.Shape.copy(), nearest_color(col), tr))
elif o.TypeId == "App::Link" and o.LinkedObject:
src = o.LinkedObject
feats = src.Group if src.TypeId == "App::Part" else [src]
for f in feats:
if f.TypeId != "Part::Feature" or f.Shape.isNull():
continue
col, tr, vis = vo_info(f)
if not vis:
continue
sh = f.Shape.copy()
sh.transformShape(o.Placement.toMatrix(), True)
items.append(("%s__%s" % (o.Name, f.Name), sh, nearest_color(col), tr))
return items
SKIP_BP = ("Serigrafia", "Gravacao", "TextoEtiqueta", "Numeracao", "Linhas_", "Pads", "Grampos", "Pinos", "Terminais", "Passivos")
def export(doc=None, tol=0.08, name="robo", skip=(), only=None):
doc = doc or App.getDocument("RoboMovel_Base")
os.makedirs(OUT, exist_ok=True)
items = leaf_items(doc)
objp, mtlp = os.path.join(OUT, name + ".obj"), os.path.join(OUT, name + ".mtl")
used = {}
nv = 1
with open(objp, "w") as fo:
fo.write("mtllib %s.mtl\n" % name)
for nm, sh, mat, tr in items:
if skip and any(s in nm for s in skip):
continue
if only is not None and not only(nm):
continue
try:
verts, tris = sh.tessellate(tol)
except Exception as ex:
print("falhou", nm, ex)
continue
if not tris:
continue
if mat.startswith("mdf"):
tr = 0 # tampo fica semitransparente só no CAD
key = mat + ("_transp" if tr > 30 else "")
used[key] = (COR.get(mat, (0.5, 0.5, 0.5)), tr)
fo.write("o %s\nusemtl %s\n" % (nm, key))
for v in verts:
fo.write("v %.6f %.6f %.6f\n" % (v.x / 1000, v.y / 1000, v.z / 1000))
for a, b, c in tris:
fo.write("f %d %d %d\n" % (a + nv, b + nv, c + nv))
nv += len(verts)
with open(mtlp, "w") as fm:
for k, (c, tr) in used.items():
fm.write("newmtl %s\nKd %.4f %.4f %.4f\nd %.3f\n\n" % (k, c[0], c[1], c[2], 1 - tr / 100.0))
return objp, len(items), nv - 1, sorted(used)