dymokomi/luced-3d
Source of release 0.11.1
0.11.1 / tests / edit_sketch_tests.luc
## Edit Sketch: a plane step before any curve, shapes as steps selected as
## their Object, moves kept in the plane, the Points and Segments levels,
## the kind's tools (Delete opens a closed curve at a segment, Snap, Close),
## and the plane's guide grid.
from workspace import Workspace
from geometry_data import GeometryData
from luce_geocore.geocore import Sketches, Vector3
from overlay_lines import OverlayLines
import edit_sketch
import node_catalog
from geometry_sheet import GeometrySheet
from luce_std import math
import modeling_commands
import node_commands
import selection_commands
func near(a: float, b: float, tolerance: float = 0.00001):
assert(math.abs(a - b) < tolerance)
func data_of(w: Workspace) -> GeometryData!:
let node = w.selected() else trap("node")
return w.network.evaluate_data(node.id)
pub func edit_sketch_tests() -> unit!:
let w = Workspace()
node_commands.delete_selected(w)
node_commands.add_node(w, "Edit Sketch", 0.0, 0.0)
assert(w.editing())
let names: list[str] = []
for level in w.levels():
names.append(level.name)
assert(names == ["Object", "Segments", "Points"])
# The XY plane first: a detail attribute, with no curves yet.
modeling_commands.create(w, "Plane XY")
let planed = data_of(w)
assert(Sketches.curve_count(planed.geometry) == 0 and planed.geometry.has_detail("sketch.plane_normal"))
near(Sketches.plane_normal(planed.geometry).z, 1.0)
# The spreadsheet's Detail tab shows the set's detail attributes, with
# no mesh at all.
let sheet = GeometrySheet(w)
sheet.set_domain(3)
sheet.refresh()
var normal_z = -1
for column in 0..<sheet.columns.length:
if sheet.columns[column].title == "sketch.plane_normal.z": normal_z = column
assert(normal_z >= 0 and sheet.row_count() == 1)
near(sheet.value(0, normal_z), 1.0)
# A rectangle on it, selected as its Object; a move stays in the plane.
modeling_commands.create(w, "Rectangle")
assert(Sketches.curve_count(data_of(w).geometry) == 1 and w.component_mode == 0 and w.selected_count() > 4)
modeling_commands.move_selection(w, Vector3(1.0, 0.0, 5.0))
let moved = data_of(w).geometry.bounds()
near(moved.maximum.x, 2.0)
near(moved.minimum.z, 0.0)
near(moved.maximum.z, 0.0)
# Points: one control point moves alone.
selection_commands.set_mode(w, 2)
selection_commands.choose_component(w, 0, false)
assert(w.selected_count() == 1)
modeling_commands.move_selection(w, Vector3(0.0, -1.2, 0.0))
near(data_of(w).geometry.bounds().minimum.y, -1.7)
# Snap is the kind's own tool: the point lands on the tool-amount grid.
let node = w.selected() else trap("node")
node.values[0] = 1.0
modeling_commands.model(w, "Snap")
assert(node.edits[-1].verb == "Sketch Snap")
near(data_of(w).geometry.bounds().minimum.y, -2.0)
# Segments: deleting one opens the rectangle into one open curve.
selection_commands.set_mode(w, 1)
selection_commands.choose_component(w, 0, false)
assert(w.selected_count() > 1)
modeling_commands.model(w, "Delete")
assert(Sketches.curve_count(data_of(w).geometry) == 1)
# Close joins it again; the plane survives every step.
selection_commands.set_mode(w, 0)
selection_commands.choose_component(w, 0, false)
modeling_commands.model(w, "Close")
assert(node.edits[-1].verb == "Sketch Close")
near(Sketches.plane_normal(data_of(w).geometry).z, 1.0)
# The plane's guide grid: 21 lines each way.
let kind = edit_sketch.kind()
let guides = kind.guides else trap("guides")
let lines = OverlayLines.make()
guides(data_of(w), lines)
assert(lines.count() == 42)
# Snapping while moving: the node's Grid snap (a kind parameter, slot 5)
# rides on the move step; the point lands on the half-unit grid.
let rows: list[str] = []
for row in node_catalog.fields("Edit Sketch"):
rows.append(row.name)
assert("Grid snap" in rows and "End snap" in rows)
let s = Workspace()
node_commands.delete_selected(s)
node_commands.add_node(s, "Edit Sketch", 0.0, 0.0)
modeling_commands.create(s, "Plane XY")
modeling_commands.create(s, "Line")
let sketch = s.selected() else trap("node")
near(sketch.values[6], 0.05)
sketch.values[5] = 0.5
selection_commands.set_mode(s, 2)
selection_commands.choose_component(s, 0, false)
modeling_commands.move_selection(s, Vector3(0.23, 0.1, 0.0))
let snapped = s.selected() else trap("node")
assert(snapped.edits[-1].numbers.length == 17 and snapped.edits[-1].numbers[15] == 0.5)
near(data_of(s).geometry.bounds().minimum.x, -1.0)
near(data_of(s).geometry.bounds().minimum.y, 0.0)
print("PASS Edit Sketch: plane before curves, shapes as steps, moves in the plane, Points and Segments, Snap, Delete, Close, the plane grid, snapping while moving")