Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.4

0.11.4 / tests / editor / sketch_node_tests.luc

## The Sketch node and its viewport editor, driven by pointer events as a
## user would: a rectangle drawn on the ground plane (its sides level and
## upright), its bottom dimensioned with Sketch Dimension and the dimension
## changed as the parameter panel does, the sketch extruded to the body it
## bounds; undo takes the change back; a line drawn by the bottom's end
## shares that end; constraint tools refuse what over-constrains.
from app import Luce3D
from luce_ui.ui import Event
from luce_window.input import EventKind, Key
from luce_cad.sketch import CadSketch
from sketch_kinds import arc_kind, circle_kind, line_kind, point_kind
from luce_cad.cad_geometry import CadGeometry
from luce_3d.graphics import Vector3
from view_projection import project
import node_commands
import sketch_commands
from luce_std import math
import time
from sketch_support import click, near, render
import tool_prompts

pub func sketch_node_tests() -> unit!:
    let editor = Luce3D()
    editor.app.layout(1600.0, 1000.0)
    let w = editor.workspace
    # Clicks land where the tests put them: no grid snapping (one check
    # below turns it on).
    w.snaps.flags[1] = false
    node_commands.delete_selected(w)
    node_commands.add_node(w, "CAD", 0.0, 0.0)
    node_commands.enter_group(w, w.network.selected)
    node_commands.add_node(w, "Sketch", 0.0, 0.0)
    let id = w.network.selected
    node_commands.display_selected(w)
    # Look straight down on the ground plane, 40 mm across.
    w.view.yaw = 0.0
    w.view.pitch = 1.5
    w.view.distance = 120.0
    w.view.target = Vector3(20.0, 0.0, -10.0)
    w.view.update_camera()
    editor.app.layout(1600.0, 1000.0)
    let sketch_editor = editor.panels.view.sketch.editor
    assert(w.sketching() and sketch_editor.active())
    # A rectangle from (0, 0) to (40, 20).
    sketch_editor.choose("2-Point Rectangle")
    click(editor, 0.0, 0.0)
    click(editor, 40.0, 20.0)
    var s = CadSketch.parse(w.network.require(id).texts[0])
    assert(s.entity_count() == 8 and s.constraint_count() == 4)
    # Its bottom dimensioned: the bottom line, then a click where the
    # value goes.
    sketch_editor.choose("Sketch Dimension")
    click(editor, 20.0, 0.0)
    click(editor, 20.0, -8.0)
    let found = sketch_commands.dimensions(w.network.require(id).texts[0])
    assert(found.length == 1 and near(found[0].value, 40.0) and not found[0].angle)
    # Its value box opened where it was placed, the measured value selected:
    # typed "50 mm" and Enter, the rectangle (fixed nowhere) widens by the
    # least motion, both sides out.
    assert(editor.panels.sketch_value.is_open())
    render(editor)
    for c in [53, 48, 32, 109, 109]:
        editor.app.dispatch(Event(kind = EventKind.text_input, codepoint = c))
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
    assert(not editor.panels.sketch_value.is_open())
    s = CadSketch.parse(w.network.require(id).texts[0])
    var left = 1000.0
    var right = -1000.0
    for at in 0..<s.entity_count():
        if s.kind(at) == point_kind:
            left = math.min(left, s.x(at))
            right = math.max(right, s.x(at))
    assert(near(right - left, 50.0) and near(left, -5.0))
    # Extruded 10 mm (the default): a 50 x 20 x 10 block.
    node_commands.add_node(w, "Extrude", 0.0, 120.0)
    let body = CadGeometry.model(w.network.evaluate_data(w.network.selected).geometry, 0)
    let box = body.tessellate(8).bounds()
    assert(near(box.maximum.x - box.minimum.x, 50.0) and near(box.maximum.y - box.minimum.y, 10.0) and near(box.maximum.z - box.minimum.z, 20.0))
    # Undo the extrusion and the dimension's change: 40 wide again.
    w.undo()
    w.undo()
    node_commands.select_node(w, id)
    node_commands.display_selected(w)
    assert(near(sketch_commands.dimensions(w.network.require(id).texts[0])[0].value, 40.0))
    # A line from the rectangle's corner snaps to that corner: it shares
    # the point (no new point made for its start).
    sketch_editor.sync()
    let before = CadSketch.parse(w.network.require(id).texts[0]).entity_count()
    sketch_editor.choose("Line")
    click(editor, 40.0, 0.0)
    click(editor, 60.0, -15.0)
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.escape))
    assert(CadSketch.parse(w.network.require(id).texts[0]).entity_count() == before + 2)
    # Horizontal/Vertical on the slanted line makes it level (more level
    # than upright); then Perpendicular between it and the level bottom
    # cannot hold: refused, with Fusion's notice.
    let line = CadSketch.parse(w.network.require(id).texts[0]).entity_count() - 1
    sketch_editor.choose("Horizontal/Vertical")
    let middle = middle_of(w.network.require(id).texts[0], line)
    click(editor, middle[0], middle[1])
    let held = CadSketch.parse(w.network.require(id).texts[0]).constraint_count()
    sketch_editor.choose("Perpendicular")
    let moved = middle_of(w.network.require(id).texts[0], line)
    click(editor, moved[0], moved[1])
    click(editor, 20.0, 0.0)
    assert(CadSketch.parse(w.network.require(id).texts[0]).constraint_count() == held)
    assert(w.notice.contains("over constrained"))
    # Saved and opened again: the sketch comes back as it was.
    let kept = w.network.require(id).texts[0]
    editor.panels.projects.save_to("sketch.prisma")
    editor.panels.projects.load_from("sketch.prisma")
    assert(w.network.require(id).texts[0] == kept)
    profile_picking(editor)
    face_sketch(editor)
    modify_tools(editor)
    create_tools(editor)
    editor.close()
    print("PASS Sketch node: a rectangle drawn on the ground, dimensioned and changed, extruded, undone; a line snapping to a corner; an over-constraining constraint refused; saved and opened; Pick profiles takes a click's region, again leaves it; a sketch on a box's top face; Trim and a typed fillet; a 3-point circle, an inscribed hexagon, a slot and a mirrored line; D's constraints; Q to Select; the Shapes level")

## Line `line`'s middle in the sketch `text` describes.
func middle_of(text: str, line: int) -> list[float]!:
    let s = CadSketch.parse(text)
    let a = s.part(line, 0)
    let b = s.part(line, 1)
    return [(s.x(a) + s.x(b)) * 0.5, (s.y(a) + s.y(b)) * 0.5]

## Pick profiles: a 40 x 20 rectangle split by a line at x 15, extruded;
## a click in the left part takes it alone (15 x 20), a second click there
## leaves it out again (back to the whole), Enter finishes.
func profile_picking(editor: Luce3D) -> unit!:
    let w = editor.workspace
    let s = CadSketch.parse(sketch_commands.rectangle_text(20.0, -10.0, 40.0, 20.0))
    _ = s.add_line(s.add_point(15.0, 0.0), s.add_point(15.0, -20.0))
    node_commands.add_node(w, "Sketch", 300.0, 0.0)
    let sketch = w.network.selected
    node_commands.set_text(w, 0, s.text())
    node_commands.add_node(w, "Extrude", 300.0, 120.0)
    let extrude = w.network.selected
    node_commands.display_selected(w)
    editor.app.layout(1600.0, 1000.0)
    tool_prompts.pick_profiles(w, extrude)
    assert(editor.panels.view.profiles.active())
    # The ground plane seen from above (XZ: sketch y runs along -Z).
    let view = editor.panels.view
    let bounds = view.layout().bounds()
    let at = project(w, Vector3(5.0, 0.0, 10.0), bounds.width, bounds.height)
    for kind in [EventKind.pointer_moved, EventKind.pointer_down, EventKind.pointer_up]:
        editor.app.dispatch(Event(kind = kind, x = bounds.x + at[0], y = bounds.y + at[1], button = 0))
    assert(w.network.require(extrude).texts[0].split(";").length == 1 and w.network.require(extrude).texts[0] != "")
    let left = CadGeometry.model(w.network.evaluate_data(extrude).geometry, 0).tessellate(8).bounds()
    assert(near(left.maximum.x - left.minimum.x, 15.0))
    for kind in [EventKind.pointer_down, EventKind.pointer_up]:
        editor.app.dispatch(Event(kind = kind, x = bounds.x + at[0], y = bounds.y + at[1], button = 0))
    assert(w.network.require(extrude).texts[0] == "")
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
    assert(w.prompt == none and w.network.require(sketch).kind == "Sketch")

## A sketch on a face: a 20 mm box into a Sketch, Pick face clicking the
## box's top; its plane is that face (y 10), and a rectangle drawn there
## lies on it.
func face_sketch(editor: Luce3D) -> unit!:
    let w = editor.workspace
    node_commands.add_node(w, "Solid Box", 600.0, 0.0)
    let box = w.network.selected
    node_commands.add_node(w, "Sketch", 600.0, 120.0)
    let id = w.network.selected
    assert(w.network.require(id).inputs[0] == box)
    node_commands.display_selected(w)
    w.view.yaw = 0.4
    w.view.pitch = 0.6
    w.view.distance = 90.0
    w.view.target = Vector3(0.0, 0.0, 0.0)
    w.view.update_camera()
    render(editor)
    tool_prompts.pick_face(w, id)
    render(editor)
    let bounds = editor.panels.view.layout().bounds()
    let top = project(w, Vector3(0.0, 10.0, 0.0), bounds.width, bounds.height)
    for kind in [EventKind.pointer_moved, EventKind.pointer_down, EventKind.pointer_up]:
        editor.app.dispatch(Event(kind = kind, x = bounds.x + top[0], y = bounds.y + top[1], button = 0))
    let node = w.network.require(id)
    assert(w.prompt == none and int(node.values[0]) == 3 and node.texts[1] != "")
    # Drawn on the face: a rectangle from (-5, -5) to (5, 5) of its plane.
    render(editor)
    let sketch_editor = editor.panels.view.sketch.editor
    sketch_editor.choose("2-Point Rectangle")
    click(editor, -5.0, -5.0)
    click(editor, 5.0, 5.0)
    let drawn = w.network.copy().evaluate_data(id)
    let points = drawn.geometry.bounds()
    assert(near(points.minimum.y, 10.0) and near(points.maximum.y, 10.0) and near(points.maximum.x - points.minimum.x, 10.0))

## MODIFY: a 40 x 20 rectangle crossed by a line at x 20 from y 5 to -25
## (sketch y down the screen on the ground); Trim on the line above the
## rectangle takes that piece; Fillet on the top-left corner, then "3 mm"
## typed: an arc of 3.
func modify_tools(editor: Luce3D) -> unit!:
    let w = editor.workspace
    let s = CadSketch.parse(sketch_commands.rectangle_text(20.0, -10.0, 40.0, 20.0))
    _ = s.add_line(s.add_point(20.0, 5.0), s.add_point(20.0, -25.0))
    node_commands.add_node(w, "Sketch", 900.0, 0.0)
    let id = w.network.selected
    node_commands.set_text(w, 0, s.text())
    node_commands.display_selected(w)
    render(editor)
    let e = editor.panels.view.sketch.editor
    e.choose("Trim")
    click(editor, 20.0, 3.0)
    var now = CadSketch.parse(w.network.require(id).texts[0])
    var top = -100.0
    for at in 0..<now.entity_count():
        if now.kind(at) == line_kind and now.x(now.part(at, 0)) == 20.0 and now.x(now.part(at, 1)) == 20.0:
            top = math.max(now.y(now.part(at, 0)), now.y(now.part(at, 1)))
    assert(near(top, 0.0))
    e.choose("Fillet")
    click(editor, 0.0, 0.0)
    assert(editor.panels.sketch_value.is_open())
    render(editor)
    for c in [51, 32, 109, 109]:
        editor.app.dispatch(Event(kind = EventKind.text_input, codepoint = c))
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
    now = CadSketch.parse(w.network.require(id).texts[0])
    var arcs = 0
    var radius = 0.0
    for at in 0..<now.entity_count():
        if now.kind(at) == arc_kind:
            arcs += 1
            radius = now.radius(at)
    assert(arcs == 1 and near(radius, 3.0))
    # Each step one undo: the fillet's typed radius, the fillet, the trim.
    w.undo()
    w.undo()
    w.undo()
    assert(CadSketch.parse(w.network.require(id).texts[0]).entity_count() == s.entity_count())

## CREATE variants on an empty sketch: a 3-Point Circle through (0, 0),
## (20, 0) and (10, 10) (center (10, 0), radius 10); an Inscribed Polygon
## centered at (60, 0) with a corner at (70, 0) (six sides of 10); a slot
## from (0, -40) to (30, -40), 5 wide; a line mirrored about the slot's
## a line from (100, 0) to (110, 10) mirrored about x 120.
func create_tools(editor: Luce3D) -> unit!:
    let w = editor.workspace
    node_commands.add_node(w, "Sketch", 1200.0, 0.0)
    let id = w.network.selected
    node_commands.display_selected(w)
    w.view.yaw = 0.0
    w.view.pitch = 1.5
    w.view.distance = 300.0
    w.view.target = Vector3(60.0, 0.0, 20.0)
    w.view.update_camera()
    render(editor)
    let e = editor.panels.view.sketch.editor
    e.choose("3-Point Circle")
    click(editor, 0.0, 0.0)
    click(editor, 20.0, 0.0)
    click(editor, 10.0, 10.0)
    var now = CadSketch.parse(w.network.require(id).texts[0])
    var circles = 0
    for at in 0..<now.entity_count():
        if now.kind(at) == circle_kind:
            circles += 1
            assert(near(now.radius(at), 10.0) and near(now.x(now.part(at, 0)), 10.0) and near(now.y(now.part(at, 0)), 0.0))
    assert(circles == 1)
    e.choose("Inscribed Polygon")
    click(editor, 60.0, 0.0)
    click(editor, 70.0, 0.0)
    now = CadSketch.parse(w.network.require(id).texts[0])
    var sides = 0
    for at in 0..<now.entity_count():
        if now.kind(at) == line_kind and now.x(now.part(at, 0)) > 45.0:
            sides += 1
            let a = now.part(at, 0)
            let b = now.part(at, 1)
            assert(near(math.hypot(now.x(b) - now.x(a), now.y(b) - now.y(a)), 10.0))
    assert(sides == 6)
    e.choose("Center to Center Slot")
    click(editor, 0.0, -40.0)
    click(editor, 30.0, -40.0)
    click(editor, 15.0, -42.5)
    now = CadSketch.parse(w.network.require(id).texts[0])
    var arcs = 0
    for at in 0..<now.entity_count():
        if now.kind(at) == arc_kind:
            arcs += 1
            assert(near(now.radius(at), 2.5))
    assert(arcs == 2)
    # A line, then an upright axis at x 120; Mirror: the line, then the axis.
    e.choose("Line")
    click(editor, 100.0, 0.0)
    click(editor, 110.0, 10.0)
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.escape))
    click(editor, 120.0, -20.0)
    click(editor, 120.0, 20.0)
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.escape))
    let before = CadSketch.parse(w.network.require(id).texts[0]).entity_count()
    e.choose("Mirror")
    click(editor, 105.0, 5.0)
    click(editor, 120.0, 0.0)
    now = CadSketch.parse(w.network.require(id).texts[0])
    assert(now.entity_count() == before + 3)
    var mirrored = false
    for at in 0..<now.entity_count():
        if now.kind(at) == line_kind and near(now.x(now.part(at, 0)), 140.0) and near(now.x(now.part(at, 1)), 130.0):
            mirrored = true
    assert(mirrored)
    # D shows the constraints while held. Held A, the rectangles are one
    # icon: a long press shows them, 3-Point Rectangle let go on is picked,
    # and A's repeats and release pick nothing else (A was once Center
    # Point Arc's letter); next time a click on the family picks 3-Point
    # again.
    let marking = editor.panels.view.marking
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.d))
    assert(marking.is_open() and marking.title == "Constraints")
    editor.app.dispatch(Event(kind = EventKind.key_up, key = Key.d))
    assert(not marking.is_open())
    let view_area = editor.panels.view.layout().bounds()
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.a))
    var rectangle_item = -1
    for at in 0..<marking.items.length:
        if marking.items[at].name == "Rectangle":
            rectangle_item = at
    assert(rectangle_item >= 0 and marking.items[rectangle_item].variants.length == 3)
    let item_at = marking.center_of(rectangle_item, view_area.width, view_area.height)
    editor.app.dispatch(Event(kind = EventKind.pointer_down, x = view_area.x + item_at[0], y = view_area.y + item_at[1], button = 0))
    assert(marking.poll(time.now() + 1000000000) and marking.family == rectangle_item)
    let variant_at = marking.variant_center(1, view_area.width, view_area.height)
    editor.app.dispatch(Event(kind = EventKind.pointer_up, x = view_area.x + variant_at[0], y = view_area.y + variant_at[1], button = 0))
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.a, repeated = true))
    editor.app.dispatch(Event(kind = EventKind.key_up, key = Key.a))
    assert(not marking.is_open() and e.tool == "3-Point Rectangle")
    e.choose("Select")
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.a))
    let again_at = marking.center_of(rectangle_item, view_area.width, view_area.height)
    editor.app.dispatch(Event(kind = EventKind.pointer_down, x = view_area.x + again_at[0], y = view_area.y + again_at[1], button = 0))
    editor.app.dispatch(Event(kind = EventKind.pointer_up, x = view_area.x + again_at[0], y = view_area.y + again_at[1], button = 0))
    editor.app.dispatch(Event(kind = EventKind.key_up, key = Key.a))
    assert(e.tool == "3-Point Rectangle")
    # Q is Select; at the Shapes level (4), Select takes the hexagon's whole
    # outline.
    e.choose("Trim")
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.q))
    assert(e.tool == "Select")
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.digit_4))
    assert(w.sketch_level == 3)
    click(editor, 65.0, 8.660254)
    assert(e.selection.length == 6)
    editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.digit_5))