Files
Web_FreeCAD_Bitbybit/scripts/freecad-golden-driver.py

70 lines
2.4 KiB
Python

import json
import os
import FreeCAD as App
import Part
def make_shape(operation):
kind = operation["type"]
if kind == "box":
shape = Part.makeBox(operation["length"], operation["width"], operation["height"])
elif kind == "cylinder":
shape = Part.makeCylinder(operation["radius"], operation["height"], App.Vector(0, 0, 0), App.Vector(0, 0, 1), operation["angle"])
elif kind == "sphere":
shape = Part.makeSphere(operation["radius"])
elif kind == "cone":
shape = Part.makeCone(operation["radius1"], operation["radius2"], operation["height"], App.Vector(0, 0, 0), App.Vector(0, 0, 1), operation["angle"])
elif kind in ("cut", "fuse", "common"):
base = make_shape(operation["base"])
tool = make_shape(operation["tool"])
shape = base.cut(tool) if kind == "cut" else base.fuse(tool) if kind == "fuse" else base.common(tool)
else:
raise ValueError("Unsupported golden operation: " + kind)
placement_value = operation.get("placement")
if placement_value:
placement = App.Placement()
placement.Base = App.Vector(*(placement_value.get("translation") or [0, 0, 0]))
rotation = placement_value.get("rotation")
if rotation:
placement.Rotation = App.Rotation(App.Vector(*rotation["axis"]), rotation["angle"])
shape = shape.copy()
shape.Placement = placement
return shape
def vector(x, y, z):
return [float(x), float(y), float(z)]
scenario_path = os.environ.get("FREECAD_GOLDEN_SCENARIO")
if not scenario_path:
raise RuntimeError("FREECAD_GOLDEN_SCENARIO is required")
with open(scenario_path, "r", encoding="utf-8") as scenario_file:
scenario = json.load(scenario_file)
shape = make_shape(scenario["operation"])
box = shape.BoundBox
version = App.Version()
result = {
"schemaVersion": 1,
"fixtureId": scenario["id"],
"freecadVersion": ".".join(str(value) for value in version[:3]),
"shapeType": shape.ShapeType,
"isNull": shape.isNull(),
"isValid": shape.isValid(),
"solids": len(shape.Solids),
"faces": len(shape.Faces),
"edges": len(shape.Edges),
"vertices": len(shape.Vertexes),
"volume": float(shape.Volume),
"area": float(shape.Area),
"boundingBox": {
"min": vector(box.XMin, box.YMin, box.ZMin),
"max": vector(box.XMax, box.YMax, box.ZMax),
},
}
print("FREECAD_GOLDEN_RESULT=" + json.dumps(result, sort_keys=True, separators=(",", ":")))