Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.6

0.11.6 / tools / preview_parity.luc

## The captures of the Fusion parity round (preview_features.luc hands these
## scenes here; the second round's are preview_parity2.luc's):
##   cad_crossing: a box at the Faces level, a box dragged right to left
##     over half its front face and held: the dashed crossing box, the face
##     it touches taken.
##   cad_repeat: after a Fillet from the right-click menu, S held: the
##     Modify menu led by Repeat Fillet.
##   cad_filter: an Extrude's Pick object waiting while Smart is the
##     level, the pointer on the box's top edge: only the face lights (the
##     caption says Faces only).
##   cad_thin: a rectangle and an open L thin extruded, Center: its Type,
##     Wall thickness and Wall location rows.
##   cad_face_extrude: a box's top face extruded as its own profile
##     (Extrude face): its arrow and box.
##   cad_through: a square under a box extruded To object, Extend Through
##     body, cut: the hole; the Extend row.
##   cad_bodies: two boxes, an Extrude's Pick bodies waiting at the Object
##     level, the left box picked.
##   cad_revolve_to: a rectangle revolved To a wall's face: the quarter
##     turn, its Type To and Object rows.
##   cad_full_round: a rib's top rounded whole (Full round): its Type and
##     Center face rows.
##   cad_chamfer_corner: three chamfers at a box's corner, Corner type
##     Chamfer: the corner's triangle; its rows.
##   cad_chamfer_blend: the same, Corner type Blend: the chamfers set back
##     and one flat hexagon closing the corner (Fusion's).
##   cad_setback: three G1 fillets at a box's corner, Corner type
##     Setback: the patch; its rows.
##   cad_variable: a box's front top edge filleted with a variable radius,
##     2 at its start, 6 at its end and 3 in the middle: its rows.
##   cad_shown_sketch: a finished sketch on a box's top, shown, at Smart:
##     its disc's profile picked (over the face beneath), the pointer on
##     its line (lit).
import tool_picks
import tool_run
import preview_support
from luce_ui.ui import Event
from luce_window.input import EventKind, Key
from luce_3d.graphics import Vector3
from luce_cad.cad_geometry import CadGeometry
from luce_cad.modeling import CadModeling
from app import Luce3D
from view_projection import project
from edit_types import smart_level
import node_commands
import sketch_commands
import selection_commands
import cad_features
import cad_blends
from workspace import Workspace
from luce_cad.sketch import CadSketch
from luce_std import math
import preview_parity2

pub func scenes() -> list[str]:
    return ["cad_crossing", "cad_repeat", "cad_filter", "cad_thin", "cad_face_extrude", "cad_through", "cad_bodies", "cad_revolve_to", "cad_full_round", "cad_chamfer_corner", "cad_chamfer_blend", "cad_shown_sketch", "cad_variable", "cad_setback"] + preview_parity2.scenes()

func look(editor: Luce3D, yaw: float, pitch: float, distance: float, target: Vector3) -> unit!:
    let w = editor.workspace
    w.view.yaw = yaw
    w.view.pitch = pitch
    w.view.distance = distance
    w.view.target = target
    w.view.update_camera()

## A new CAD node, entered, holding a Solid Box (20 mm).
func box_in_cad(editor: Luce3D) -> unit!:
    let w = editor.workspace
    node_commands.delete_selected(w)
    node_commands.add_node(w, "CAD", 40.0, 40.0)
    node_commands.enter_group(w, w.network.selected)
    node_commands.add_node(w, "Solid Box", 40.0, 40.0)

## The screen point (window coordinates) of world point `p`.
func screen(editor: Luce3D, p: Vector3) -> list[float]!:
    let area = editor.panels.view.layout().bounds()
    let s = project(editor.workspace, p, area.width, area.height)
    return [area.x + s[0], area.y + s[1]]

## Scene `scene` before its first frame.
pub func setup(editor: Luce3D, scene: str) -> unit!:
    let w = editor.workspace
    if scene in preview_parity2.scenes():
        preview_parity2.setup(editor, scene)
        return
    if scene == "cad_crossing":
        box_in_cad(editor)
        look(editor, 0.0, 0.0, 80.0, Vector3())
        selection_commands.set_mode(w, w.level_of(2))
    elif scene == "cad_repeat":
        box_in_cad(editor)
        look(editor, 0.5, 0.6, 75.0, Vector3())
        let model = CadGeometry.model(preview_support.cooked(w, w.network.selected).geometry, 0)
        var top: list[int] = []
        for face in 0..<model.face_count():
            for use in 0..<model.face_edge_count(face):
                let a = model.face_edge_point(face, use, 0.0)
                let b = model.face_edge_point(face, use, 1.0)
                if top.length == 0 and a.y > 9.99 and b.y > 9.99:
                    top = [model.face_edge_id(face, use)]
        node_commands.add_node(w, "Fillet", 40.0, 140.0)
        node_commands.display_selected(w)
        node_commands.set_text(w, 0, CadModeling.edge_references(model, top))
        node_commands.set_parameter(w, 0, 3.0)
        tool_run.remember(w, "Fillet")
    elif scene == "cad_filter":
        box_in_cad(editor)
        node_commands.add_node(w, "Sketch", 40.0, 140.0)
        node_commands.set_parameter(w, 0, 1.0)
        node_commands.set_parameter(w, 1, -16.0)
        node_commands.set_text(w, 0, sketch_commands.rectangle_text(0.0, 0.0, 6.0, 6.0))
        node_commands.add_node(w, "Extrude", 40.0, 240.0)
        node_commands.display_selected(w)
        node_commands.set_parameter(w, 0, 4.0)
        look(editor, 0.6, 0.35, 95.0, Vector3(0.0, -4.0, 0.0))
        selection_commands.set_mode(w, smart_level)
        tool_picks.start_named(w, w.network.selected, "Object")
    elif scene == "cad_thin":
        node_commands.delete_selected(w)
        node_commands.add_node(w, "CAD", 40.0, 40.0)
        node_commands.enter_group(w, w.network.selected)
        node_commands.add_node(w, "Sketch", 40.0, 40.0)
        node_commands.set_parameter(w, 0, 1.0)
        let s = CadSketch.parse(sketch_commands.rectangle_text(0.0, 0.0, 30.0, 20.0))
        let a = s.add_point(-15.0, -25.0)
        let b = s.add_point(15.0, -25.0)
        let c = s.add_point(15.0, -35.0)
        _ = s.add_line(a, b)
        _ = s.add_line(b, c)
        node_commands.set_text(w, 0, s.text())
        node_commands.add_node(w, "Extrude", 40.0, 160.0)
        node_commands.display_selected(w)
        node_commands.set_parameter(w, cad_features.type_slot, 1.0)
        node_commands.set_parameter(w, cad_features.thickness_slot, 2.0)
        node_commands.set_parameter(w, cad_features.wall_slot, 2.0)
        node_commands.set_parameter(w, cad_features.distance_slot, 12.0)
        look(editor, 0.6, 0.6, 110.0, Vector3(0.0, 4.0, 8.0))
    elif scene == "cad_face_extrude":
        box_in_cad(editor)
        look(editor, 0.5, 0.6, 85.0, Vector3(0.0, 6.0, 0.0))
        selection_commands.set_mode(w, w.level_of(2))
        let mesh = preview_support.shown(w) else trap("no pick mesh")
        for face in 0..<mesh.face_count():
            if math.abs(mesh.face_center(face).y - 10.0) < 1e-6:
                selection_commands.choose_component(w, face, false)
                break
        editor.panels.refresh()
        editor.panels.actions.extrude_face.trigger()
        node_commands.set_parameter(w, cad_features.distance_slot, 8.0)
    elif scene == "cad_through":
        box_in_cad(editor)
        let box = w.network.selected
        let top = facing(w, box, Vector3(0.0, 1.0, 0.0))
        node_commands.add_node(w, "Sketch", 40.0, 140.0)
        node_commands.set_parameter(w, 0, 1.0)
        node_commands.set_parameter(w, 1, -16.0)
        node_commands.set_text(w, 0, sketch_commands.rectangle_text(0.0, 0.0, 8.0, 8.0))
        node_commands.add_node(w, "Extrude", 40.0, 240.0)
        node_commands.display_selected(w)
        node_commands.set_parameter(w, cad_features.extent_slot, 1.0)
        node_commands.set_text(w, cad_features.face_text, top)
        node_commands.set_parameter(w, cad_features.extend_slot, 3.0)
        node_commands.set_parameter(w, cad_features.operation_slot, 3.0)
        look(editor, 0.6, -0.45, 90.0, Vector3(0.0, -4.0, 0.0))
    elif scene == "cad_bodies":
        node_commands.delete_selected(w)
        node_commands.add_node(w, "CAD", 40.0, 40.0)
        node_commands.enter_group(w, w.network.selected)
        let moved: list[int] = []
        for side in [-1.0, 1.0]:
            node_commands.add_node(w, "Solid Box", 40.0 + side * 100.0, 40.0)
            node_commands.add_node(w, "Transform", 40.0 + side * 100.0, 140.0)
            node_commands.set_parameter(w, 1, side * 15.0)
            moved.append(w.network.selected)
        node_commands.add_node(w, "Merge", 40.0, 240.0)
        let both = w.network.selected
        node_commands.connect(w, moved[0], both, 0)
        node_commands.connect(w, moved[1], both, 1)
        node_commands.add_node(w, "Sketch", 40.0, 340.0)
        node_commands.set_parameter(w, 0, 1.0)
        node_commands.set_parameter(w, 1, -30.0)
        node_commands.set_text(w, 0, sketch_commands.rectangle_text(0.0, 0.0, 60.0, 4.0))
        node_commands.add_node(w, "Extrude", 40.0, 440.0)
        node_commands.display_selected(w)
        node_commands.set_parameter(w, cad_features.extent_slot, 2.0)
        node_commands.set_parameter(w, cad_features.operation_slot, 3.0)
        look(editor, 0.5, 0.5, 120.0, Vector3(0.0, 0.0, 0.0))
        tool_picks.start_named(w, w.network.selected, "Objects To Cut")
        let mesh = preview_support.shown(w) else trap("no pick mesh")
        for face in 0..<mesh.face_count():
            if mesh.face_center(face).x < -20.0:
                selection_commands.choose_component(w, face, false)
                break
    elif scene == "cad_revolve_to":
        node_commands.delete_selected(w)
        node_commands.add_node(w, "CAD", 40.0, 40.0)
        node_commands.enter_group(w, w.network.selected)
        node_commands.add_node(w, "Solid Box", 40.0, 40.0)
        node_commands.set_parameter(w, 0, 4.0)
        node_commands.set_parameter(w, 1, 20.0)
        node_commands.set_parameter(w, 2, 30.0)
        node_commands.add_node(w, "Transform", 40.0, 140.0)
        node_commands.set_parameter(w, 1, -2.0)
        let wall = w.network.selected
        let face = facing(w, wall, Vector3(1.0, 0.0, 0.0))
        node_commands.add_node(w, "Sketch", 40.0, 240.0)
        node_commands.set_parameter(w, 0, 0.0)
        node_commands.set_text(w, 0, sketch_commands.rectangle_text(9.0, 2.0, 6.0, 4.0))
        node_commands.add_node(w, "Revolve", 40.0, 340.0)
        node_commands.display_selected(w)
        node_commands.set_parameter(w, cad_features.axis_slot, 2.0)
        node_commands.set_parameter(w, cad_features.revolve_type_slot, 2.0)
        node_commands.set_text(w, cad_features.revolve_face_text, face)
        look(editor, 0.9, 0.55, 90.0, Vector3(0.0, 2.0, 0.0))
    elif scene == "cad_full_round":
        node_commands.delete_selected(w)
        node_commands.add_node(w, "CAD", 40.0, 40.0)
        node_commands.enter_group(w, w.network.selected)
        node_commands.add_node(w, "Solid Box", 40.0, 40.0)
        node_commands.set_parameter(w, 0, 6.0)
        node_commands.set_parameter(w, 1, 14.0)
        node_commands.set_parameter(w, 2, 30.0)
        let rib = w.network.selected
        let top = facing(w, rib, Vector3(0.0, 1.0, 0.0))
        node_commands.add_node(w, "Fillet", 40.0, 140.0)
        node_commands.display_selected(w)
        node_commands.set_parameter(w, cad_blends.type_slot, 2.0)
        node_commands.set_text(w, cad_blends.faces_text, top)
        look(editor, 0.7, 0.45, 75.0, Vector3(0.0, 2.0, 0.0))
    elif scene in ["cad_chamfer_corner", "cad_chamfer_blend", "cad_setback"]:
        box_in_cad(editor)
        let box = w.network.selected
        let model = CadGeometry.model(preview_support.cooked(w, box).geometry, 0)
        let corner: list[int] = []
        for face in 0..<model.face_count():
            for use in 0..<model.face_edge_count(face):
                let id = model.face_edge_id(face, use)
                let a = model.face_edge_point(face, use, 0.0)
                let b = model.face_edge_point(face, use, 1.0)
                if (a.subtract(Vector3(10.0, 10.0, 10.0)).length() < 1e-9 or b.subtract(Vector3(10.0, 10.0, 10.0)).length() < 1e-9) and not (id in corner):
                    corner.append(id)
        node_commands.add_node(w, "Fillet" if scene == "cad_setback" else "Chamfer", 40.0, 140.0)
        node_commands.display_selected(w)
        node_commands.set_text(w, 0, CadModeling.edge_references(model, corner))
        node_commands.set_parameter(w, 0, 4.0)
        if scene == "cad_setback":
            node_commands.set_parameter(w, cad_blends.fillet_corner_slot, 1.0)
        else:
            node_commands.set_parameter(w, cad_blends.corner_slot, 2.0 if scene == "cad_chamfer_blend" else 0.0)
        look(editor, 0.75, 0.6, 55.0, Vector3(4.0, 5.0, 4.0))
    elif scene == "cad_variable":
        box_in_cad(editor)
        let model = CadGeometry.model(preview_support.cooked(w, w.network.selected).geometry, 0)
        var front: list[int] = []
        for face in 0..<model.face_count():
            for use in 0..<model.face_edge_count(face):
                let a = model.face_edge_point(face, use, 0.0)
                let b = model.face_edge_point(face, use, 1.0)
                if front.length == 0 and a.y > 9.99 and b.y > 9.99 and a.z > 9.99 and b.z > 9.99:
                    front = [model.face_edge_id(face, use)]
        node_commands.add_node(w, "Fillet", 40.0, 140.0)
        node_commands.display_selected(w)
        node_commands.set_text(w, 0, CadModeling.edge_references(model, front))
        node_commands.set_parameter(w, cad_blends.chain_slot, 0.0)
        node_commands.set_parameter(w, cad_blends.radius_type_slot, 2.0)
        node_commands.set_parameter(w, cad_blends.size_slot, 2.0)
        node_commands.set_parameter(w, cad_blends.end_radius_slot, 6.0)
        node_commands.set_text(w, cad_blends.points_text, "0.5 3")
        look(editor, 0.45, 0.45, 60.0, Vector3(0.0, 4.0, 4.0))
    elif scene == "cad_shown_sketch":
        box_in_cad(editor)
        node_commands.add_node(w, "Sketch", 40.0, 140.0)
        let sketch = w.network.selected
        node_commands.set_parameter(w, 0, 1.0)
        node_commands.set_parameter(w, 1, 10.0)
        let s = CadSketch.parse("")
        _ = s.add_circle(s.add_point(3.0, 0.0), 4.0)
        _ = s.add_line(s.add_point(-6.0, -6.0), s.add_point(-6.0, 6.0))
        _ = s.add_circle(s.add_point(3.0, 0.0), 2.0)
        node_commands.set_text(w, 0, s.text())
        node_commands.select_nothing(w)
        w.network.set_display(sketch)
        look(editor, 0.4, 0.8, 60.0, Vector3(0.0, 6.0, 0.0))
        selection_commands.set_mode(w, smart_level)
    editor.panels.refresh()

## The name of the face of node `id`'s first body facing `normal`.
func facing(w: Workspace, id: int, normal: Vector3) -> str!:
    let model = CadGeometry.model(preview_support.cooked(w, id).geometry, 0)
    for face in 0..<model.face_count():
        let name = CadModeling.face_name_of(model, face)
        let at = CadModeling.face_point(model, name)
        if at.found and at.normal.dot(normal) > 0.999:
            return name
    return ""

## Frame `frame` of scene `scene` (from 2; `last` is the frame captured).
pub func act(editor: Luce3D, scene: str, frame: int, last: int) -> unit!:
    if scene in preview_parity2.scenes():
        preview_parity2.act(editor, scene, frame, last)
        return
    if frame != last - 1:
        return
    if scene == "cad_crossing":
        # Right to left from below the front face's right side to above its
        # middle: let go once (the face taken), then held again.
        let a = screen(editor, Vector3(14.0, -13.0, 10.0))
        let b = screen(editor, Vector3(0.0, 13.0, 10.0))
        for round in 0..<2:
            editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = a[0], y = a[1]))
            editor.app.dispatch(Event(kind = EventKind.pointer_down, x = a[0], y = a[1], button = 0, shift = round == 1))
            editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = b[0], y = b[1], shift = round == 1))
            if round == 0:
                editor.app.dispatch(Event(kind = EventKind.pointer_up, x = b[0], y = b[1], button = 0))
    elif scene == "cad_repeat":
        let at = screen(editor, Vector3(0.0, 10.0, 0.0))
        editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = at[0], y = at[1]))
        editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.s))
    elif scene == "cad_shown_sketch":
        # The ring between the circles clicked, then the pointer on the line.
        let ring = screen(editor, Vector3(6.0, 10.0, 0.5))
        for kind in [EventKind.pointer_moved, EventKind.pointer_down, EventKind.pointer_up]:
            editor.app.dispatch(Event(kind = kind, x = ring[0], y = ring[1], button = 0))
        let line = screen(editor, Vector3(-6.0, 10.0, 2.0))
        editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = line[0], y = line[1]))
    elif scene == "cad_filter":
        # On the box's top front edge: Smart would take the edge; the
        # prompt takes faces only.
        let at = screen(editor, Vector3(0.0, 10.0, 10.0))
        editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = at[0], y = at[1]))