from __future__ import annotations
import FreeCAD
import FreeCADGui
import FerroCAD
from ferrocad_shell import * from ferrocad_shell import bootstrap as _base_bootstrap
DEFAULT_WIDTH = 40.0
DEFAULT_DEPTH = 40.0
DEFAULT_HEIGHT = 20.0
DEFAULT_COLOR = (0.26, 0.55, 0.85, 1.0)
_DISPLAY_MODES = ("Shaded", "ShadedWithEdges", "Wireframe")
_DISPLAY_KEYS = {
"Shaded": "shaded",
"ShadedWithEdges": "shaded_edges",
"Wireframe": "wireframe",
}
_FACES = (
(0, 3, 2, 1), (4, 5, 6, 7), (0, 1, 5, 4), (1, 2, 6, 5), (2, 3, 7, 6), (3, 0, 4, 7), )
def _slab_mesh(width: float, depth: float, height: float, placement) -> dict:
half_width = width / 2.0
half_depth = depth / 2.0
local = [
(-half_width, -half_depth, 0.0),
(half_width, -half_depth, 0.0),
(half_width, half_depth, 0.0),
(-half_width, half_depth, 0.0),
(-half_width, -half_depth, height),
(half_width, -half_depth, height),
(half_width, half_depth, height),
(-half_width, half_depth, height),
]
corners = [placement.multVec(FreeCAD.Vector(*corner)) for corner in local]
positions = []
triangles = []
edges = []
for face in _FACES:
base = len(positions)
for index in face:
corner = corners[index]
positions.append((corner.x, corner.y, corner.z))
triangles.extend((base, base + 1, base + 2, base, base + 2, base + 3))
edges.extend(
(
base,
base + 1,
base + 1,
base + 2,
base + 2,
base + 3,
base + 3,
base,
)
)
return {
"positions": positions,
"triangles": triangles,
"triangle_parts": [0] * (len(triangles) // 3),
"edges": edges,
"segment_parts": [0] * (len(edges) // 2),
"vertices": [],
"vertex_parts": [],
"parts": [],
}
def _display_mesh(obj):
width = getattr(obj, "Width", None)
if width is None:
return None
try:
placement = getattr(obj, "Placement", None)
if placement is None:
placement = FreeCAD.Placement()
mesh = _slab_mesh(
float(width), float(obj.Depth), float(obj.Height), placement
)
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 _transaction(obj, name: str, apply):
doc = obj.Document
doc.openTransaction(f"{name} {obj.Name}")
try:
apply()
doc.recompute()
doc.commitTransaction()
except Exception as exc:
doc.abortTransaction()
return {"ok": False, "error": str(exc)}
return {"ok": True}
def _interaction(obj, manipulator, action, element, values):
if manipulator == "toy_height" and action == "set" and values:
return _transaction(
obj, "Height", lambda: setattr(obj, "Height", float(values[0]))
)
if manipulator == "transform" and action == "apply" and len(values) == 16:
delta = FreeCAD.Placement(FreeCAD.Matrix(*values))
return _transaction(
obj,
"Move",
lambda: setattr(obj, "Placement", delta * obj.Placement),
)
return None
FerroCAD.register_display_provider(_display_mesh)
FerroCAD.register_interaction_provider(_interaction)
class _MakeSlabCommand:
def GetResources(self):
return {
"MenuText": "Create Slab",
"ToolTip": "Add a slab to the document",
"Accel": "Ctrl+Shift+S",
"Group": "Toy",
"CmdType": "ForEdit",
"FerroCAD_Surface": "dock",
}
def IsActive(self):
return FreeCAD.ActiveDocument is not None
def Activated(self):
return {
"title": "Create Slab",
"accept": "Create",
"fields": [
{
"name": "Width",
"label": "Width",
"kind": "quantity",
"value": DEFAULT_WIDTH,
"min": 0.1,
},
{
"name": "Depth",
"label": "Depth",
"kind": "quantity",
"value": DEFAULT_DEPTH,
"min": 0.1,
},
{
"name": "Height",
"label": "Height",
"kind": "quantity",
"value": DEFAULT_HEIGHT,
"min": 0.1,
},
],
"on_accept": _make_from_dialog,
}
def _make_from_dialog(values):
slab = makeSlab(
width=float(values.get("Width", DEFAULT_WIDTH)),
depth=float(values.get("Depth", DEFAULT_DEPTH)),
height=float(values.get("Height", DEFAULT_HEIGHT)),
)
return {"status": f"created {slab.Name}"}
FreeCADGui.addCommand("Toy_MakeSlab", _MakeSlabCommand())
class _PolylineCommand:
def GetResources(self):
return {
"MenuText": "Polyline",
"ToolTip": "Draw a polyline in the viewport",
"Group": "Toy",
"CmdType": "ForView",
"FerroCAD_Surface": "dock",
}
def IsActive(self):
return True
def Activated(self):
return {"tool": "toy_polyline"}
FreeCADGui.addCommand("Toy_Polyline", _PolylineCommand())
def _add_slab(doc, width, depth, height, x, y, name):
slab = doc.addObject("App::FeaturePython", name)
slab.addProperty("App::PropertyLength", "Width", "Toy", "Footprint width")
slab.addProperty("App::PropertyLength", "Depth", "Toy", "Footprint depth")
slab.addProperty("App::PropertyLength", "Height", "Toy", "Slab height")
slab.addProperty("App::PropertyPlacement", "Placement", "Toy", "Placement")
slab.addProperty(
"App::PropertyEnumeration", "DisplayMode", "Toy", "How the slab is drawn"
)
slab.DisplayMode = list(_DISPLAY_MODES)
slab.addProperty("App::PropertyColor", "Color", "Toy", "Surface colour")
slab.Width = float(width)
slab.Depth = float(depth)
slab.Height = float(height)
slab.DisplayMode = "ShadedWithEdges"
slab.Color = [DEFAULT_COLOR]
slab.Placement = FreeCAD.Placement(FreeCAD.Vector(float(x), float(y), 0.0))
return slab
def makeSlab(
width=DEFAULT_WIDTH,
depth=DEFAULT_DEPTH,
height=DEFAULT_HEIGHT,
x=0.0,
y=0.0,
name="Slab",
):
doc = FreeCAD.ActiveDocument
if doc is None:
doc = FreeCAD.newDocument("Unnamed")
doc.openTransaction("Create Slab")
try:
slab = _add_slab(doc, width, depth, height, x, y, name)
doc.recompute()
doc.commitTransaction()
except Exception:
doc.abortTransaction()
raise
return slab
class _Console:
makeSlab = staticmethod(makeSlab)
def bootstrap(doc_name: str = "Toy") -> str:
payload = _base_bootstrap(doc_name)
doc = FreeCAD.ActiveDocument
_add_slab(doc, DEFAULT_WIDTH, DEFAULT_DEPTH, DEFAULT_HEIGHT, 0.0, 0.0, "Slab")
doc.recompute()
add_namespace("Toy", _Console)
return payload