dymokomi/luced-3d
Source of release 0.11.3
## CAD nodes: what a CAD node holds inside, Fusion 360's way of modeling.
## Sketches (sketch_node.luc) give profiles, features make and change solid
## bodies (luce-cad's B-reps: CadModeling and CadSolids), and a few
## geometry nodes (Transform, Merge, Null, Switch, Edit CAD) work on bodies
## too. CAD features exist only inside a CAD node; inside one, the menu
## offers only these.
from luce_cad.cad_geometry import CadGeometry
from luce_cad.solids import CadSolids
from luce_cad.modeling import CadModeling, FilletPlan
from luce_cad.model import CadModel
from geometry_data import GeometryData
from luce_ui import ui
from luce_3d.graphics import Vector3
import sketch_node
from angles import radians
## What a feature node keeps between its evaluations on the compute
## worker: a Fillet's plan (luce-cad's FilletPlan: the body's edges
## resolved and its corners fitted once), so dragging its radius rebuilds
## only. Made again when the body, edges, continuity or chaining change.
pub class BlendMemo:
pub var plan: FilletPlan? = none
## Plans made so far (one per change of body, edges or continuity).
pub var made: int = 0
## The solid primitives (analytic B-reps, centered on the origin along +Y).
pub func solid_kinds() -> list[str]:
return ["Solid Box", "Solid Cylinder", "Solid Cone", "Solid Sphere", "Solid Torus"]
## Nodes only a CAD node holds.
pub func cad_only_kinds() -> list[str]:
return [sketch_node.kind_name, "Extrude", "Revolve", "Boolean", "Fillet", "Chamfer", "Mirror Body", "Pattern"] + solid_kinds()
## Features with a second input (Boolean's tool, Extrude's target bodies).
pub func two_inputs(kind: str) -> bool:
return kind in ["Extrude", "Revolve", "Boolean"]
pub func is_cad_only(kind: str) -> bool:
return kind in cad_only_kinds()
## Geometry nodes a CAD node may hold too.
pub func shared_kinds() -> list[str]:
return ["Edit CAD", "Transform", "Merge", "Null", "Switch"]
## Whether `kind` may be added where the network's context is `context`
## ("geometry", "cad" inside a CAD node, "shading" inside a Material).
pub func allowed(kind: str, context: str, shader: bool) -> bool:
if context == "shading":
return shader
if context == "cad":
return is_cad_only(kind) or kind in shared_kinds()
return not shader and not is_cad_only(kind)
pub func cad_categories() -> list[str]:
return ["Sketch", "Create", "Modify", "Utility", "All"]
## A node's category in the CAD menu.
pub func cad_category(kind: str) -> str:
if kind == sketch_node.kind_name:
return "Sketch"
if kind in ["Boolean", "Fillet", "Chamfer", "Mirror Body", "Pattern"]:
return "Modify"
if is_cad_only(kind):
return "Create"
if kind in ["Edit CAD", "Transform"]:
return "Modify"
return "Utility"
## The bodies of `data` as one: each CAD model of its set, unioned.
func body_of(data: GeometryData, role: str) -> CadModel!:
let count = CadGeometry.model_count(data.geometry)
if count == 0:
error(ui.invalid, f"The {role} input has no solid body")
var body = CadGeometry.model(data.geometry, 0)
for at in 1..<count:
body = CadModeling.combined(body, CadGeometry.model(data.geometry, at), 0)
return body
## `made` combined with the bodies of `target` by operation `operation`
## (0 a new body beside them, 1 join, 2 cut, 3 intersect).
func operated(made: CadModel, target: GeometryData?, operation: int) -> GeometryData!:
let other = target else return GeometryData(geometry = CadGeometry.of_model(made))
if operation == 0:
return other.merged(GeometryData(geometry = CadGeometry.of_model(made)))
let body = body_of(other, "target")
return GeometryData(geometry = CadGeometry.of_model(CadModeling.combined(body, made, operation - 1)))
## What an Extrude's or Revolve's Profiles text (profile keys apart by
## `;`) shows.
pub func profiles_summary(text: str) -> str:
if text.trim() == "":
return "Islands (the sketch's closed shapes)"
let count = text.split(";").length
return "1 profile" if count == 1 else f"{count} profiles"
## What a blend's Edges text (edge references apart by `;`) shows.
pub func edges_summary(text: str) -> str:
if text.trim() == "":
return "All straight edges"
let count = text.split(";").length
return "1 edge" if count == 1 else f"{count} edges"
## The tag that names the faces node `id` makes (luce-cad's naming).
pub func tag_of(id: int) -> str:
return f"#{id}"
## A primitive's body, its faces named for `tag`.
func primitive(kind: str, v: list[float], tag: str) -> CadModel!:
if kind == "Solid Box":
return CadModeling.tagged(CadSolids.box(v[0], v[1], v[2]), tag)
if kind == "Solid Cylinder":
return CadModeling.tagged(CadSolids.cylinder(v[0], v[1]), tag)
if kind == "Solid Cone":
return CadModeling.tagged(CadSolids.cone(v[0], v[1], v[2]), tag)
if kind == "Solid Sphere":
return CadModeling.tagged(CadSolids.sphere(v[0]), tag)
return CadModeling.tagged(CadSolids.torus(v[0], v[1]), tag)
## A CAD feature's result from its input `data`, its second input, values
## `v` and texts `texts`; the faces it makes are named for `tag` (tag_of).
## `memo` keeps a Fillet's plan between evaluations, when given.
pub func evaluate_cad(kind: str, v: list[float], data: GeometryData, tag: str, second: GeometryData? = none, texts: list[str]? = none, memo: BlendMemo? = none) -> GeometryData!:
if kind in solid_kinds():
return GeometryData(geometry = CadGeometry.of_model(primitive(kind, v, tag)))
if kind == sketch_node.kind_name:
return sketch_node.evaluate_sketch(v, texts else ["", "", ""], data)
if kind == "Extrude" or kind == "Revolve":
# Extrude: distance, operation, symmetric. Revolve: axis, operation, angle.
let symmetric = kind == "Extrude" and v[2] != 0.0
# The profiles chosen (keys in the first text; none: islands
# alternating from the outside in).
let profiles = (texts else ["", "", ""])[0]
let made = CadModeling.extruded(data.geometry, v[0], -v[0] * 0.5 if symmetric else 0.0, tag, profiles) if kind == "Extrude" else CadModeling.revolved(data.geometry, int(v[0]), tag, radians(v[2]), profiles)
if int(v[1]) != 0 and second == none:
error(ui.invalid, "Join, Cut and Intersect need target bodies: connect them to the second input")
return operated(made, second, int(v[1]))
if kind == "Fillet" or kind == "Chamfer":
let edges = (texts else ["", "", ""])[0]
let body = body_of(data, "first")
# Fillet: radius, continuity (G1, G2, G3), tangent chain. Chamfer:
# distance, (slot 1 unused), tangent chain.
let chain = v[2] != 0.0
if kind == "Chamfer":
return GeometryData(geometry = CadGeometry.of_model(CadModeling.chamfered(body, edges, v[0], tag, chain)))
let continuity = int(v[1]) + 1
let kept = memo else return GeometryData(geometry = CadGeometry.of_model(CadModeling.filleted(body, edges, v[0], tag, continuity, chain)))
var plan = kept.plan
if let held = plan:
if not held.made_for(body, edges, continuity, tag, chain):
plan = none
if plan == none:
kept.made += 1
let current = plan else CadModeling.fillet_plan(body, edges, continuity, tag, chain)
kept.plan = current
return GeometryData(geometry = CadGeometry.of_model(current.filleted(v[0])))
if kind == "Mirror Body":
# Across the plane facing X, Y or Z through the origin.
let body = body_of(data, "first")
let axis = int(v[0])
let flip = Vector3(-1.0 if axis == 0 else 1.0, -1.0 if axis == 1 else 1.0, -1.0 if axis == 2 else 1.0)
# The copy's faces named apart from the original's.
let reflected = CadModeling.prefixed(body, f"{tag}.1:").transformed(Vector3(), Vector3(), flip)
if v[1] != 0.0:
return GeometryData(geometry = CadGeometry.of_model(body)).merged(GeometryData(geometry = CadGeometry.of_model(reflected)))
return GeometryData(geometry = CadGeometry.of_model(CadModeling.combined(body, reflected, 0)))
if kind == "Pattern":
# Copies along an axis (spacing apart) or around it (over an angle).
let body = body_of(data, "first")
let count = int(v[1])
let axis = int(v[2])
let direction = Vector3(1.0 if axis == 0 else 0.0, 1.0 if axis == 1 else 0.0, 1.0 if axis == 2 else 0.0)
var result = body
var separate = GeometryData(geometry = CadGeometry.of_model(body))
for at in 1..<count:
# Each copy's faces named apart (copy k: `tag.k:` before them).
var copy = CadModeling.prefixed(body, f"{tag}.{at}:")
if int(v[0]) == 0:
copy = copy.transformed(direction.multiply_scalar(v[3] * float(at)), Vector3(), Vector3(1.0, 1.0, 1.0))
else:
let step = radians(v[4]) / float(count if v[4] >= 359.999 else count - 1)
copy = copy.transformed(Vector3(), direction.multiply_scalar(step * float(at)), Vector3(1.0, 1.0, 1.0))
if v[5] != 0.0:
separate = separate.merged(GeometryData(geometry = CadGeometry.of_model(copy)))
else:
result = CadModeling.combined(result, copy, 0)
if v[5] != 0.0:
return separate
return GeometryData(geometry = CadGeometry.of_model(result))
if kind == "Boolean":
let tool = second else error(ui.invalid, "Connect the tool body to the second input")
return GeometryData(geometry = CadGeometry.of_model(CadModeling.combined(body_of(data, "first"), body_of(tool, "second"), int(v[0]))))
error(ui.invalid, f"{kind} is not a CAD feature")