Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.9

0.11.9 / tools / preview_parity2.luc

## The captures of the second Fusion parity round (preview_parity.luc hands
## these scenes here; the surfacing plan's are preview_surfacing.luc's):
##   cad_variable_chain: a box's uprights rounded 6, then its top loop
##     filleted with a variable radius along the chain (1 at its start, 4
##     halfway round, 1 at its end), G2.
##   cad_variable_slanted: a prism's top edge, ending on a face leaning 45°,
##     filleted 1 to 4 (G2): the strip runs on and the slanted face trims
##     it.
##   cad_round_rib: a curved rib (a quarter ring extruded) rounded whole
##     (Full round of its top), G1; cad_round_rib_g2 the same at G2 (one
##     face: the halves joined where the top was); cad_round_drafted the
##     rib extruded with a draft of -5° (its ends leaning), G1, its round
##     trimmed by the ends; cad_round_rib_end the G1 rib seen from its end
##     (its edges run where the round meets the sides, none on its top).
##   cad_cv_nets: Edit CAD on a box, an edge filleted G2, at the CVs level:
##     the strip's control net shows (hidden at every other level).
##   cad_sketch_point: a finished sketch on a box's top, shown, at Smart:
##     its point picked (the accent square), its line under the pointer.
##   cad_tangency: a box's front top edge filleted 8 at G2 with a Tangency
##     weight of 2 (the blend hugging the faces, tight in the middle): its
##     rows.
##   cad_dome_chamfer: a dome (a quarter disc turned round Y) chamfered 3
##     at its rim, Fusion's checked case: the cone between the circles of 7
##     and 9.55.
##   cad_dome_fillet: the same dome filleted 2 at its rim (G1): Fusion's
##     torus between the circles of 7.746 and 9.682; cad_dome_fillet_g2 the
##     same at G2 (its section matches the sphere's curvature there).
##   cad_flat_shaft_g2: a D-shaft (a circle of 10 cut flat at 5) whose
##     edges along it, where the flat meets the cylinder, are filleted 2
##     at G2 with a Tangency weight of 2; cad_flat_shaft_g1 at G1 with that
##     weight (Fusion's conic, touching the cylinder only tangentially);
##     cad_flat_shaft_g3 at G3 (following the cylinder's curvature and its
##     change); cad_dome_fillet_g3 the dome's rim at G3.
##   cad_dome_chamfer_angle: the dome chamfered Distance and angle, 3 on
##     the sphere leaving it at 30° from its tangent there (Fusion's).
##   cad_variable_cone: a box's front top edge filleted 2 to 6 straight at
##     G1: a ball rolled, its face a cone (Fusion's at a weight of 1);
##     cad_variable_chain_rolled the rounded box's top loop (1.5 a quarter
##     round, 3 three quarters round) and cad_dome_rolled the dome's rim
##     (1 and 2.5) rolled at G1; cad_variable_slanted_rolled the slanted
##     prism's ridge (1, 3.5 at its middle, 4) rolled into its slanted end.
##   cad_plate_chain: Fusion's plate (a line, an arc and a line on its top,
##     ending at its sides) filleted 3 with Tangent chain, the strips flush
##     in the sides; cad_plate_chain_variable 2 to 4 along it at G2.
import preview_support
from luce_ui.ui import Event
from luce_window.input import EventKind
from luce_3d.graphics import Vector3
from luce_cad.cad_geometry import CadGeometry
from luce_cad.modeling import CadModeling
from luce_cad.sketch import CadSketch
from app import Luce3D
from view_projection import project
from edit_types import smart_level
from workspace import Workspace
import node_commands
import selection_commands
import modeling_commands
import cad_blends
import cad_features
import preview_surfacing
from luce_std import math

pub func scenes() -> list[str]:
    return ["cad_variable_chain", "cad_variable_slanted", "cad_round_rib", "cad_round_rib_g2", "cad_round_drafted", "cad_round_rib_end", "cad_cv_nets", "cad_sketch_point", "cad_tangency", "cad_dome_chamfer", "cad_dome_fillet", "cad_dome_fillet_g2", "cad_flat_shaft_g2"] + fillet3_scenes() + ["cad_plate_chain", "cad_plate_chain_variable"] + preview_surfacing.scenes()

## The third fillet round's (Fusion's rolled ball, tangential touching,
## G3 on curved faces, Distance and angle on a sphere).
func fillet3_scenes() -> list[str]:
    return ["cad_flat_shaft_g1", "cad_flat_shaft_g3", "cad_dome_fillet_g3", "cad_dome_chamfer_angle", "cad_variable_cone", "cad_variable_chain_rolled", "cad_variable_slanted_rolled", "cad_dome_rolled"]

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 and empty.
func 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)

## 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]]

## The ids of node `id`'s first body's edges whose start and middle `keep`
## takes.
func edges_where(w: Workspace, id: int, keep: func(Vector3, Vector3) -> bool) -> list[int]!:
    let model = CadGeometry.model(preview_support.cooked(w, id).geometry, 0)
    let found: list[int] = []
    for face in 0..<model.face_count():
        for use in 0..<model.face_edge_count(face):
            let edge = model.face_edge_id(face, use)
            if keep(model.face_edge_point(face, use, 0.0), model.face_edge_point(face, use, 0.5)) and not (edge in found):
                found.append(edge)
    return found

## The references of `edges` on node `id`'s first body.
func references(w: Workspace, id: int, edges: list[int]) -> str!:
    return CadModeling.edge_references(CadGeometry.model(preview_support.cooked(w, id).geometry, 0), edges)

## The name of the flat 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 model.face_surface_kind(face) == 0 and at.found and at.normal.dot(normal) > 0.999:
            return name
    return ""

## A sketch node on plane `plane` (0 XY, 1 XZ) holding `s`.
func sketch_of(w: Workspace, plane: float, s: CadSketch) -> int!:
    node_commands.add_node(w, "Sketch", 40.0, 140.0)
    node_commands.set_parameter(w, 0, plane)
    node_commands.set_text(w, 0, s.text())
    return w.network.selected

## Scene `scene` before its first frame.
pub func setup(editor: Luce3D, scene: str) -> unit!:
    if scene in preview_surfacing.scenes():
        preview_surfacing.setup(editor, scene)
        return
    let w = editor.workspace
    if scene == "cad_variable_chain" or scene == "cad_variable_chain_rolled":
        cad(editor)
        node_commands.add_node(w, "Solid Box", 40.0, 40.0)
        let box = w.network.selected
        let upright = func (a: Vector3, b: Vector3) -> bool:
            return math.abs(a.x - b.x) < 1e-9 and math.abs(a.z - b.z) < 1e-9
        node_commands.add_node(w, "Fillet", 40.0, 140.0)
        node_commands.set_text(w, 0, references(w, box, edges_where(w, box, upright)))
        node_commands.set_parameter(w, cad_blends.size_slot, 6.0)
        let rounded = w.network.selected
        let top_line = func (a: Vector3, b: Vector3) -> bool:
            return a.y > 9.99 and b.y > 9.99 and math.abs(a.z - 10.0) < 1e-9 and math.abs(b.z - 10.0) < 1e-9
        let line = edges_where(w, rounded, top_line)
        node_commands.add_node(w, "Fillet", 40.0, 240.0)
        node_commands.display_selected(w)
        node_commands.set_text(w, 0, references(w, rounded, line))
        node_commands.set_parameter(w, cad_blends.radius_type_slot, 2.0)
        node_commands.set_parameter(w, cad_blends.size_slot, 1.0)
        node_commands.set_parameter(w, cad_blends.end_radius_slot, 1.0)
        if scene == "cad_variable_chain_rolled":
            node_commands.set_text(w, cad_blends.points_text, "0.25 1.5; 0.75 3")
        else:
            node_commands.set_text(w, cad_blends.points_text, "0.5 4")
            node_commands.set_parameter(w, cad_blends.continuity_slot, 1.0)
        look(editor, 0.75, 0.62, 62.0, Vector3(0.0, 4.0, 0.0))
    elif scene == "cad_variable_slanted" or scene == "cad_variable_slanted_rolled":
        cad(editor)
        let s = CadSketch.parse("")
        let p = [s.add_point(0.0, 0.0), s.add_point(30.0, 0.0), s.add_point(20.0, 10.0), s.add_point(0.0, 10.0)]
        for at in 0..<4:
            _ = s.add_line(p[at], p[(at + 1) % 4])
        _ = sketch_of(w, 0.0, s)
        node_commands.add_node(w, "Extrude", 40.0, 240.0)
        node_commands.set_parameter(w, 0, 16.0)
        let prism = w.network.selected
        let ridge = func (a: Vector3, b: Vector3) -> bool:
            return math.abs(a.y - 10.0) < 1e-9 and math.abs(b.y - 10.0) < 1e-9 and math.abs(a.z - 16.0) < 1e-6 and math.abs(b.z - 16.0) < 1e-6
        node_commands.add_node(w, "Fillet", 40.0, 340.0)
        node_commands.display_selected(w)
        node_commands.set_text(w, 0, references(w, prism, edges_where(w, prism, ridge)))
        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.smooth_slot, 0.0)
        node_commands.set_parameter(w, cad_blends.size_slot, 1.0)
        node_commands.set_parameter(w, cad_blends.end_radius_slot, 4.0)
        if scene == "cad_variable_slanted_rolled": node_commands.set_text(w, cad_blends.points_text, "0.5 3.5")
        else: node_commands.set_parameter(w, cad_blends.continuity_slot, 1.0)
        look(editor, 1.25, 0.5, 48.0, Vector3(18.0, 6.0, 10.0))
    elif scene == "cad_round_rib" or scene == "cad_round_rib_g2" or scene == "cad_round_drafted" or scene == "cad_round_rib_end":
        cad(editor)
        let s = CadSketch.parse("")
        let o = s.add_point(0.0, 0.0)
        let p = [s.add_point(16.0, 0.0), s.add_point(22.0, 0.0), s.add_point(0.0, 22.0), s.add_point(0.0, 16.0)]
        _ = s.add_line(p[0], p[1])
        _ = s.add_arc(o, p[1], p[2])
        _ = s.add_line(p[2], p[3])
        _ = s.add_arc(o, p[0], p[3])
        _ = sketch_of(w, 0.0, s)
        node_commands.add_node(w, "Extrude", 40.0, 240.0)
        node_commands.set_parameter(w, 0, 12.0)
        if scene == "cad_round_drafted":
            node_commands.set_parameter(w, cad_features.taper_slot, -5.0)
        let rib = w.network.selected
        let top = facing(w, rib, Vector3(0.0, 0.0, 1.0))
        node_commands.add_node(w, "Fillet", 40.0, 340.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)
        if scene == "cad_round_rib_g2":
            node_commands.set_parameter(w, cad_blends.continuity_slot, 1.0)
        if scene == "cad_round_rib_end":
            look(editor, -1.3, 0.25, 34.0, Vector3(0.0, 19.0, 9.0))
        else:
            look(editor, 0.6, 0.55, 75.0, Vector3(10.0, 10.0, 6.0))
    elif scene == "cad_cv_nets":
        cad(editor)
        node_commands.add_node(w, "Edit CAD", 40.0, 40.0)
        # Each command acts on what the worker published for the one
        # before: wait for it between them.
        preview_support.settle(w)
        modeling_commands.create(w, "Box")
        preview_support.settle(w)
        selection_commands.set_mode(w, 2)
        let mesh = preview_support.shown(w) else trap("edges")
        let tag = mesh.find_attribute("cad_edge", 4)
        var edge = -1
        for at in 0..<(mesh.edge_count()):
            if edge < 0 and mesh.attribute_value(tag, at) >= 0.0: edge = at
        selection_commands.choose_component(w, edge, false)
        modeling_commands.model(w, "Fillet")
        modeling_commands.set_last_step(w, 0, 0.5, 0.0001, 100000.0)
        modeling_commands.set_last_step(w, 1, 1.0, 0.0, 2.0)
        preview_support.settle(w)
        selection_commands.set_mode(w, 1)
        preview_support.settle(w)
        look(editor, 0.9, -0.45, 4.2, Vector3(-0.4, -0.6, 0.2))
    elif scene == "cad_sketch_point":
        cad(editor)
        node_commands.add_node(w, "Solid Box", 40.0, 40.0)
        let s = CadSketch.parse("")
        _ = s.add_point(4.0, -3.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, 2.0), 3.0)
        let sketch = sketch_of(w, 1.0, s)
        node_commands.set_parameter(w, 1, 10.0)
        node_commands.select_nothing(w)
        w.network.set_display(sketch)
        look(editor, 0.4, 0.8, 50.0, Vector3(0.0, 6.0, 0.0))
        selection_commands.set_mode(w, smart_level)
    elif scene == "cad_tangency":
        cad(editor)
        node_commands.add_node(w, "Solid Box", 40.0, 40.0)
        let box = w.network.selected
        let front_top = func (a: Vector3, b: Vector3) -> bool:
            return a.y > 9.99 and b.y > 9.99 and a.z > 9.99 and b.z > 9.99
        node_commands.add_node(w, "Fillet", 40.0, 140.0)
        node_commands.display_selected(w)
        node_commands.set_text(w, 0, references(w, box, edges_where(w, box, front_top)))
        node_commands.set_parameter(w, cad_blends.size_slot, 8.0)
        node_commands.set_parameter(w, cad_blends.continuity_slot, 1.0)
        node_commands.set_parameter(w, cad_blends.tangency_slot, 2.0)
        look(editor, -1.35, 0.15, 55.0, Vector3(0.0, 3.0, 3.0))
    elif scene == "cad_dome_chamfer" or scene == "cad_dome_fillet" or scene == "cad_dome_fillet_g2" or scene == "cad_dome_fillet_g3" or scene == "cad_dome_chamfer_angle" or scene == "cad_dome_rolled":
        cad(editor)
        let s = CadSketch.parse("")
        let o = s.add_point(0.0, 0.0)
        let rim = s.add_point(10.0, 0.0)
        let top = s.add_point(0.0, 10.0)
        _ = s.add_line(o, rim)
        _ = s.add_arc(o, rim, top)
        _ = s.add_line(top, o)
        _ = sketch_of(w, 0.0, s)
        node_commands.add_node(w, "Revolve", 40.0, 240.0)
        node_commands.set_parameter(w, cad_features.axis_slot, 2.0)
        node_commands.set_parameter(w, cad_features.revolve_type_slot, 1.0)
        let dome = w.network.selected
        let on_rim = func (a: Vector3, b: Vector3) -> bool:
            return math.abs(a.y) < 1e-9 and math.abs(b.y) < 1e-9 and math.abs(math.hypot(b.x, b.z) - 10.0) < 1e-6
        let chamfer = scene == "cad_dome_chamfer" or scene == "cad_dome_chamfer_angle"
        node_commands.add_node(w, "Chamfer" if chamfer else "Fillet", 40.0, 340.0)
        node_commands.display_selected(w)
        node_commands.set_text(w, 0, references(w, dome, edges_where(w, dome, on_rim)))
        node_commands.set_parameter(w, cad_blends.size_slot, 3.0 if chamfer else 2.0)
        if scene == "cad_dome_fillet_g2":
            node_commands.set_parameter(w, cad_blends.continuity_slot, 1.0)
        if scene == "cad_dome_fillet_g3":
            node_commands.set_parameter(w, cad_blends.continuity_slot, 2.0)
        if scene == "cad_dome_rolled":
            node_commands.set_parameter(w, cad_blends.radius_type_slot, 2.0)
            node_commands.set_text(w, cad_blends.points_text, "0.25 1; 0.75 2.5")
        if scene == "cad_dome_chamfer_angle":
            node_commands.set_parameter(w, cad_blends.chamfer_type_slot, 2.0)
            node_commands.set_parameter(w, cad_blends.angle_slot, 30.0)
        look(editor, 0.6, 0.12, 40.0, Vector3(0.0, 4.0, 0.0))
    elif scene == "cad_flat_shaft_g2" or scene == "cad_flat_shaft_g1" or scene == "cad_flat_shaft_g3":
        cad(editor)
        let s = CadSketch.parse("")
        let x = math.sqrt(75.0)
        let a = s.add_point(-x, 5.0)
        let b = s.add_point(x, 5.0)
        _ = s.add_line(a, b)
        _ = s.add_arc(s.add_point(0.0, 0.0), a, b)
        _ = sketch_of(w, 0.0, s)
        node_commands.add_node(w, "Extrude", 40.0, 240.0)
        node_commands.set_parameter(w, 0, 20.0)
        let shaft = w.network.selected
        let along = func (p: Vector3, q: Vector3) -> bool:
            return math.abs(p.y - 5.0) < 1e-6 and math.abs(q.y - 5.0) < 1e-6 and math.abs(math.abs(p.x) - math.sqrt(75.0)) < 1e-6 and math.abs(p.x - q.x) < 1e-9
        node_commands.add_node(w, "Fillet", 40.0, 340.0)
        node_commands.display_selected(w)
        node_commands.set_text(w, 0, references(w, shaft, edges_where(w, shaft, along)))
        node_commands.set_parameter(w, cad_blends.size_slot, 2.0)
        node_commands.set_parameter(w, cad_blends.continuity_slot, 0.0 if scene == "cad_flat_shaft_g1" else (2.0 if scene == "cad_flat_shaft_g3" else 1.0))
        node_commands.set_parameter(w, cad_blends.tangency_slot, 1.0 if scene == "cad_flat_shaft_g3" else 2.0)
        look(editor, 0.5, 0.35, 45.0, Vector3(0.0, 2.0, 10.0))
    elif scene == "cad_plate_chain" or scene == "cad_plate_chain_variable":
        cad(editor)
        let s = CadSketch.parse("")
        let a = s.add_point(0.0, 0.0)
        let b = s.add_point(20.0, 0.0)
        let c = s.add_point(20.0, 10.0)
        let d = s.add_point(10.0, 20.0)
        let e = s.add_point(0.0, 20.0)
        _ = s.add_line(a, b)
        _ = s.add_line(b, c)
        _ = s.add_arc(s.add_point(10.0, 10.0), c, d)
        _ = s.add_line(d, e)
        _ = s.add_line(e, a)
        _ = sketch_of(w, 0.0, s)
        node_commands.add_node(w, "Extrude", 40.0, 240.0)
        node_commands.set_parameter(w, 0, 10.0)
        let plate = w.network.selected
        # The top edge on x = 20 (its chain: the arc and the line on y = 20).
        let side = func (p: Vector3, q: Vector3) -> bool:
            return math.abs(p.x - 20.0) < 1e-6 and math.abs(q.x - 20.0) < 1e-6 and math.abs(p.z - q.z) < 1e-9 and math.abs(p.z) > 1.0
        node_commands.add_node(w, "Fillet", 40.0, 340.0)
        node_commands.display_selected(w)
        node_commands.set_text(w, 0, references(w, plate, edges_where(w, plate, side)))
        if scene == "cad_plate_chain":
            node_commands.set_parameter(w, cad_blends.size_slot, 3.0)
        else:
            node_commands.set_parameter(w, cad_blends.continuity_slot, 1.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, 4.0)
            node_commands.set_parameter(w, cad_blends.smooth_slot, 0.0)
        look(editor, 0.75, 0.35, 60.0, Vector3(10.0, 9.0, 5.0))
    elif scene == "cad_variable_cone":
        cad(editor)
        node_commands.add_node(w, "Solid Box", 40.0, 40.0)
        let box = w.network.selected
        let front_top = func (a: Vector3, b: Vector3) -> bool:
            return a.y > 9.99 and b.y > 9.99 and a.z > 9.99 and b.z > 9.99
        node_commands.add_node(w, "Fillet", 40.0, 140.0)
        node_commands.display_selected(w)
        node_commands.set_text(w, 0, references(w, box, edges_where(w, box, front_top)))
        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_parameter(w, cad_blends.smooth_slot, 0.0)
        look(editor, 0.45, 0.45, 60.0, Vector3(0.0, 4.0, 4.0))
    editor.panels.refresh()

## 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_surfacing.scenes():
        preview_surfacing.act(editor, scene, frame, last)
        return
    if frame != last - 1:
        return
    if scene == "cad_sketch_point":
        # The point clicked, then the pointer on the line (lit).
        let point = screen(editor, Vector3(4.0, 10.0, 3.0))
        for kind in [EventKind.pointer_moved, EventKind.pointer_down, EventKind.pointer_up]:
            editor.app.dispatch(Event(kind = kind, x = point[0], y = point[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]))