from __future__ import annotations
import FreeCAD
import FreeCADGui
import FerroCAD
import Part
from ferrocad_shell import * from ferrocad_shell import bootstrap as _base_bootstrap
DEFAULT_LENGTH = 20.0
DEFAULT_WIDTH = 20.0
DEFAULT_HEIGHT = 20.0
DEFAULT_COLOR = (0.66, 0.72, 0.80, 1.0)
_DISPLAY_MODES = ("Shaded", "ShadedWithEdges", "Wireframe")
_DISPLAY_KEYS = {
"Shaded": "shaded",
"ShadedWithEdges": "shaded_edges",
"Wireframe": "wireframe",
}
def _display_mesh(obj):
shape = getattr(obj, "Shape", None)
if shape is None:
return None
try:
if shape.isNull():
return None
mesh = shape.mesh()
_apply_object_placement(mesh, getattr(obj, "Placement", None))
colors = getattr(obj, "Color", None)
mesh["color"] = list(colors[0]) if colors else list(DEFAULT_COLOR)
mode = str(getattr(obj, "DisplayMode", "") or "")
mesh["display"] = _DISPLAY_KEYS.get(mode, "shaded_edges")
return mesh
except Exception:
return None
def _apply_object_placement(mesh, placement):
if placement is None:
return
positions = mesh.get("positions")
if positions:
mesh["positions"] = [
tuple(placement.multVec(FreeCAD.Vector(*position)))
for position in positions
]
normals = mesh.get("normals")
if normals:
rotation = placement.Rotation
mesh["normals"] = [
tuple(rotation.multVec(FreeCAD.Vector(*normal))) for normal in normals
]
def _interaction(obj, manipulator, action, element, values):
if manipulator == "transform" and action == "apply" and len(values) == 16:
delta = FreeCAD.Placement(FreeCAD.Matrix(*values))
doc = obj.Document
doc.openTransaction(f"Move {obj.Name}")
try:
obj.Placement = delta * obj.Placement
doc.recompute()
doc.commitTransaction()
except Exception as exc:
doc.abortTransaction()
return {"ok": False, "error": str(exc)}
return {"ok": True}
if manipulator != "dress_up":
return None
mode = {"fillet": "Fillet", "chamfer": "Chamfer"}.get(action)
if mode is None:
return None
edges = [element] if element else []
first = float(values[0]) if values else 0.0
second = float(values[1]) if len(values) > 1 else 0.0
doc = obj.Document
feature_name = f"{obj.Name}_{mode}"
doc.openTransaction(f"{mode} {obj.Name}")
try:
feature = doc.getObject(feature_name)
if feature is None:
feature = doc.addObject(f"Part::{mode}", feature_name)
feature.Base = (obj, edges)
if mode == "Fillet":
feature.Radius = first
else:
feature.Distance = first
feature.Distance2 = second
doc.recompute()
doc.commitTransaction()
except Exception as exc:
doc.abortTransaction()
return {"ok": False, "error": str(exc)}
return {"ok": True, "feature": feature_name}
FerroCAD.register_display_provider(_display_mesh)
FerroCAD.register_interaction_provider(_interaction)
def _add_appearance(obj):
obj.addProperty(
"App::PropertyEnumeration", "DisplayMode", "Part", "How the object is drawn"
)
obj.DisplayMode = list(_DISPLAY_MODES)
obj.DisplayMode = "ShadedWithEdges"
obj.addProperty("App::PropertyColor", "Color", "Part", "Surface colour")
obj.Color = [DEFAULT_COLOR]
return obj
def _add_box(doc, length, width, height, x, y, name):
box = doc.addObject("Part::Feature", name)
box.Shape = Part.makeBox(float(length), float(width), float(height))
_add_appearance(box)
box.Placement = FreeCAD.Placement(FreeCAD.Vector(float(x), float(y), 0.0))
return box
def makeBox(
length=DEFAULT_LENGTH,
width=DEFAULT_WIDTH,
height=DEFAULT_HEIGHT,
x=0.0,
y=0.0,
name="Box",
):
doc = FreeCAD.ActiveDocument
if doc is None:
doc = FreeCAD.newDocument("Unnamed")
doc.openTransaction("Create Box")
try:
box = _add_box(doc, length, width, height, x, y, name)
doc.recompute()
doc.commitTransaction()
except Exception:
doc.abortTransaction()
raise
return box
class _MakeBoxCommand:
def GetResources(self):
return {
"MenuText": "Create Box",
"ToolTip": "Add a box to the document",
"Group": "Part",
"CmdType": "ForEdit",
"FerroCAD_Surface": "dock",
}
def IsActive(self):
return FreeCAD.ActiveDocument is not None
def Activated(self):
box = makeBox()
return {"status": f"created {box.Name}"}
FreeCADGui.addCommand("Part_Box", _MakeBoxCommand())
def bootstrap(doc_name: str = "Shell") -> str:
payload = _base_bootstrap(doc_name)
doc = FreeCAD.ActiveDocument
_add_box(doc, DEFAULT_LENGTH, DEFAULT_WIDTH, DEFAULT_HEIGHT, 0.0, 0.0, "Box")
doc.recompute()
return payload