dymokomi/luced-3d
Source of release 0.11.2
0.11.2 / tests / editor / cad_node_tests.luc
## The CAD node: a container of CAD nodes. Inside, a sketch's rectangle
## extruded is a closed six-faced body; geometry nodes stay out of it and
## CAD features stay inside; the menu offers CAD categories there; the CAD
## node places its bodies like a Group.
from workspace import Workspace
from luce_cad.cad_geometry import CadGeometry
from luce_cad.model import CadModel
import node_commands
import sketch_commands
import selection_commands
import node_catalog
from cad_nodes import solid_kinds, edges_summary
from luce_cad.modeling import CadModeling
from compute_request import RequestWriter, read_request
from network import Network
import tool_prompts
## Whether `model`'s tessellated volume is `expected` (to 0.5%).
func near_volume(model: CadModel, expected: float) -> bool!:
let mesh = model.tessellate(16)
var total = 0.0
for triangle in 0..<mesh.index_count() // 3:
let a = mesh.vertex(mesh.index(triangle * 3)).position
let b = mesh.vertex(mesh.index(triangle * 3 + 1)).position
let c = mesh.vertex(mesh.index(triangle * 3 + 2)).position
total += a.dot(b.cross(c)) / 6.0
if total < expected * 0.995 or total > expected * 1.005: print(f"volume {total}, expected {expected}")
return total >= expected * 0.995 and total <= expected * 1.005
pub func cad_node_tests() -> unit!:
let w = Workspace()
node_commands.delete_selected(w)
node_commands.add_node(w, "Extrude", 0.0, 0.0)
assert(w.notice.contains("inside a CAD node") and w.network.nodes.length == 0)
node_commands.add_node(w, "CAD", 0.0, 0.0)
let cad = w.network.selected
node_commands.enter_group(w, cad)
assert(w.network.context() == "cad" and node_catalog.categories("cad").contains("Create"))
node_commands.add_node(w, "Cube", 0.0, 0.0)
assert(w.notice.contains("only CAD nodes"))
# A rectangle on XY, extruded 3 along Z.
node_commands.add_node(w, "Sketch", 0.0, 0.0)
node_commands.set_parameter(w, 0, 0.0)
node_commands.set_text(w, 0, sketch_commands.rectangle_text(0.0, 0.0, 2.0, 1.0))
node_commands.add_node(w, "Extrude", 0.0, 120.0)
node_commands.set_parameter(w, 0, 3.0)
let extrude = w.network.selected
let body = w.network.evaluate_data(extrude)
assert(CadGeometry.model_count(body.geometry) == 1 and CadGeometry.model(body.geometry, 0).face_count() == 6)
let bounds = CadGeometry.model(body.geometry, 0).tessellate(8).bounds()
assert(bounds.maximum.z - bounds.minimum.z > 2.999 and bounds.maximum.z - bounds.minimum.z < 3.001)
# Symmetric: half the distance each way (z from -1.5 to 1.5).
node_commands.select_node(w, extrude)
node_commands.set_parameter(w, 2, 1.0)
let even = CadGeometry.model(w.network.evaluate_data(extrude).geometry, 0).tessellate(8).bounds()
assert(even.minimum.z > -1.501 and even.minimum.z < -1.499 and even.maximum.z > 1.499)
node_commands.set_parameter(w, 2, 0.0)
# Revolve: the rectangle about the plane's U axis straddles it: refused.
let sketch = w.network.require(extrude).inputs[0]
node_commands.select_node(w, sketch)
node_commands.add_node(w, "Revolve", 200.0, 120.0)
var straddles = false
_ = w.network.evaluate_data(w.network.selected) catch failure:
straddles = failure.message.contains("one side")
assert(straddles)
node_commands.delete_selected(w)
# Every solid primitive cooks to its closed B-rep.
let faces = [6, 3, 2, 2, 1]
let solids = solid_kinds()
for at in 0..<solids.length:
node_commands.add_node(w, solids[at], 200.0 + 120.0 * float(at), 0.0)
assert(CadGeometry.model(w.network.evaluate_data(w.network.selected).geometry, 0).face_count() == faces[at])
# Extrude cutting a box: the rectangle (2 x 1, on XY) swept from z 0 to
# 3 takes 2 x 1 x 1 out of a 2 x 2 x 2 box.
node_commands.add_node(w, "Solid Box", 400.0, 200.0)
for axis in 0..<3:
node_commands.set_parameter(w, axis, 2.0)
let box = w.network.selected
w.network.connect(box, extrude, 1)
node_commands.select_node(w, extrude)
node_commands.set_parameter(w, 1, 2.0)
let cut = CadGeometry.model(w.network.evaluate_data(extrude).geometry, 0)
assert(near_volume(cut, 6.0))
# A Boolean node: the box without a cylinder through it.
node_commands.add_node(w, "Solid Cylinder", 600.0, 0.0)
node_commands.set_parameter(w, 0, 0.5)
node_commands.set_parameter(w, 1, 3.0)
let pin = w.network.selected
node_commands.select_node(w, box)
node_commands.add_node(w, "Boolean", 400.0, 320.0)
let boolean = w.network.selected
w.network.connect(pin, boolean, 1)
node_commands.set_parameter(w, 0, 1.0)
let drilled = CadGeometry.model(w.network.evaluate_data(boolean).geometry, 0)
assert(drilled.face_count() == 7 and near_volume(drilled, 8.0 - 3.14159265 * 0.25 * 2.0))
# Chamfers and fillets of a fresh box: every edge chamfered (corners
# where three meet), every edge filleted (sphere corners), the four
# upright ones, by number, filleted.
node_commands.add_node(w, "Solid Box", 800.0, 0.0)
for axis in 0..<3:
node_commands.set_parameter(w, axis, 2.0)
let plain = w.network.selected
node_commands.add_node(w, "Chamfer", 800.0, 120.0)
node_commands.set_parameter(w, 0, 0.2)
assert(CadGeometry.model(w.network.evaluate_data(w.network.selected).geometry, 0).face_count() == 18)
node_commands.select_node(w, plain)
node_commands.add_node(w, "Fillet", 1000.0, 120.0)
let fillet = w.network.selected
node_commands.set_parameter(w, 0, 0.3)
# Every edge: a rounded box (sphere corners).
assert(CadGeometry.model(w.network.evaluate_data(fillet).geometry, 0).face_count() == 26)
# Dragging the radius rebuilds from the Fillet's plan; G2 makes a new
# one (its corners fitted once), which the worker's copy keeps.
node_commands.set_parameter(w, 0, 0.25)
assert(CadGeometry.model(w.network.evaluate_data(fillet).geometry, 0).face_count() == 26)
assert(w.network.require(fillet).memo.made == 1)
node_commands.set_parameter(w, 1, 1.0)
assert(CadGeometry.model(w.network.evaluate_data(fillet).geometry, 0).face_count() == 66)
node_commands.set_parameter(w, 0, 0.3)
assert(CadGeometry.model(w.network.evaluate_data(fillet).geometry, 0).face_count() == 66)
assert(w.network.require(fillet).memo.made == 2)
# The worker's copy keeps the memo as the radius changes (the request
# carries the Sketch node's text too).
let writer = RequestWriter()
let worker = read_request(writer.encode(w.network, 3), Network(false))
node_commands.set_parameter(w, 0, 0.35)
let next = read_request(writer.encode(w.network, 3), worker)
assert(next.require(fillet).memo is worker.require(fillet).memo)
node_commands.set_parameter(w, 1, 0.0)
let source = CadGeometry.model(w.network.evaluate_data(plain).geometry, 0)
let uprights: list[int] = []
for face in 0..<source.face_count():
for use in 0..<source.face_edge_count(face):
let a = source.face_edge_point(face, use, 0.0)
let b = source.face_edge_point(face, use, 1.0)
let id = source.face_edge_id(face, use)
if a.x == b.x and a.z == b.z and not (id in uprights):
uprights.append(id)
# Edges are kept as references: their faces' names and where they are.
node_commands.set_text(w, 0, CadModeling.edge_references(source, uprights))
assert(CadGeometry.model(w.network.evaluate_data(fillet).geometry, 0).face_count() == 10)
# Pick edges: picked on the input box while the fillet shows; each pick
# writes the picked edges' references into Edges at once, Enter keeps them.
tool_prompts.pick_edges(w, fillet)
assert(w.network.display == fillet)
let pick = w.geometry else trap("the box has no pick geometry")
let owner = pick.find_attribute("cad_edge", 4)
var target = -1
for edge in 0..<pick.edge_count():
if target < 0 and int(pick.attribute_value(owner, edge)) == uprights[0]: target = edge
assert(target >= 0 and w.prompt != none)
selection_commands.choose_component(w, target, false)
assert(w.network.require(fillet).texts[0] == CadModeling.edge_references(source, [uprights[0]]) and w.prompt != none and w.network.display == fillet)
assert(CadGeometry.model(w.network.evaluate_data(fillet).geometry, 0).face_count() == 7)
assert((w.geometry else trap("pick")).edge_count() == pick.edge_count())
# Shift or Cmd held peeks at the input, picks still landing on it; the
# body's B-rep edges show, not the tessellation's polygons.
assert(w.picking_brep())
tool_prompts.set_peek(w, true)
assert(w.peeking and w.network.display == w.network.require(fillet).inputs[0])
tool_prompts.set_peek(w, false)
assert(not w.peeking and w.network.display == fillet)
tool_prompts.accept_prompt(w)
assert(w.network.require(fillet).texts[0] == CadModeling.edge_references(source, [uprights[0]]) and w.prompt == none and w.network.selected == fillet)
assert(CadGeometry.model(w.network.evaluate_data(fillet).geometry, 0).face_count() == 7)
assert(edges_summary(w.network.require(fillet).texts[0]) == "1 edge")
# G2: the same one edge, a B-spline blend.
node_commands.set_parameter(w, 1, 1.0)
assert(CadGeometry.model(w.network.evaluate_data(fillet).geometry, 0).face_count() == 7)
node_commands.set_parameter(w, 1, 0.0)
# The box made taller upstream: the same edge is still the one filleted.
node_commands.select_node(w, plain)
node_commands.set_parameter(w, 1, 3.0)
let taller = CadGeometry.model(w.network.evaluate_data(fillet).geometry, 0)
assert(taller.face_count() == 7 and w.network.require(fillet).failure == "")
# Mirror and Pattern: a unit box off the YZ plane mirrored (two boxes),
# patterned three times apart (three), and overlapping (joined: one).
node_commands.add_node(w, "Solid Box", 1200.0, 0.0)
node_commands.set_parameter(w, 0, 1.0)
node_commands.set_parameter(w, 1, 1.0)
node_commands.set_parameter(w, 2, 1.0)
let unit_box = w.network.selected
node_commands.add_node(w, "Transform", 1200.0, 120.0)
node_commands.set_parameter(w, 1, 1.0)
node_commands.add_node(w, "Mirror Body", 1200.0, 240.0)
assert(near_volume(CadGeometry.model(w.network.evaluate_data(w.network.selected).geometry, 0), 2.0))
node_commands.select_node(w, unit_box)
node_commands.add_node(w, "Pattern", 1400.0, 120.0)
let pattern = w.network.selected
node_commands.set_parameter(w, 3, 1.5)
let three = CadGeometry.model(w.network.evaluate_data(pattern).geometry, 0)
assert(three.face_count() == 18 and near_volume(three, 3.0))
node_commands.set_parameter(w, 3, 0.5)
let joined = CadGeometry.model(w.network.evaluate_data(pattern).geometry, 0)
assert(joined.face_count() == 6 and near_volume(joined, 2.0))
# Outside, the CAD node places its bodies as one instance.
node_commands.leave_group(w)
let placed = w.network.evaluate_data(cad)
assert(placed.instance_count() == 1)
print("PASS CAD node: CAD nodes inside only, sketch → Extrude body (one way, symmetric), Revolve, solid primitives, Extrude Cut, the Boolean node, Chamfer and Fillet (edges picked in the viewport, kept by reference through an upstream change; G2; its plan kept while the radius changes, on the worker too), Mirror Body, Pattern, the CAD node places its bodies")