Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.9

0.11.9 / src / cad_commands.luc

## The CAD hand-offs of docs/research/FUSION-CAD-UX.md, from the viewport's
## selection to a feature, as Fusion makes them: the right-click menu, the
## A and S marking menus and the Create and Modify menus all run these.
##   Finish Sketch: leave the sketch edited; the node shown before it shows
##     again (rolled forward).
##   Extrude (in a sketch): an Extrude after the sketch taking its picked
##     profiles (none: its closed shapes), Operation Auto; it is shown and
##     selected, its arrow and distance box in the viewport.
##   Sketch on face: a Sketch after the displayed node on the selected flat
##     face, edited at once.
##   Revolve (in a sketch): a Revolve after the sketch taking its picked
##     profiles, waiting for its axis (Pick axis: a sketch line or a body
##     edge clicked).
##   Extrude face: a Sketch on the selected flat face (its loops projected)
##     and an Extrude of it after, as Fusion extrudes a face as a profile.
##   Slice: while a sketch is edited, the body in front of its plane is cut
##     away from view (display only; not an undo step).
##   Redefine Sketch Plane: the edited sketch onto another face or plane.
## Each is one undo step, and says why when it does not apply.
from luce_geocore.geocore import Domain
from workspace import Workspace
from tools import ToolRun
from edit_steps import insert_after_display
import node_commands
import sketch_node
import tool_picks
import tool_registry
import edit_cad_commands
import extrude_inputs
import cad_features
from luce_cad.sketch import CadSketch
from sketch_kinds import line_kind

## Whether Finish Sketch and the sketch's Extrude apply: a sketch is edited.
pub func in_sketch(w: Workspace) -> bool:
    return w.sketching()

## Whether the shown sketch (not edited) has profiles picked on it
## (sketch_shown.luc), which Extrude and Revolve take as in the editor.
pub func shown_profiles(w: Workspace) -> bool:
    return not w.sketching() and w.sketch_picked_on == w.network.display and w.sketch_profiles.length > 0

## Finish Sketch: the node displayed before the sketch was edited is
## selected and shown again; without one, nothing is selected (the sketch
## stays shown).
pub func finish_sketch(w: Workspace) -> unit!:
    if not w.sketching():
        return
    if edit_cad_commands.sketching(w):
        edit_cad_commands.finish_sketch(w)
        return
    let back = w.sketch_return
    w.sketch_profiles = []
    if back >= 0 and w.network.find(back) != none:
        node_commands.select_node(w, back)
    else:
        w.sketch_return = -1
        node_commands.select_nothing(w)
    w.touch()

## Slice on or off: the edited sketch's body in front of its plane hidden
## (the cook clips what it shows of the sketch).
pub func toggle_slice(w: Workspace) -> unit!:
    w.slice = not w.slice
    w.rebuild()

## Redefine Sketch Plane (Fusion's) of the sketch edited: a Sketch node
## waits for the face it goes onto (its input body shown; Pick face, an
## origin plane in its Plane row), an Edit CAD sketch takes a step for it;
## the drawing keeps its own coordinates.
pub func redefine_plane(w: Workspace) -> unit!:
    if edit_cad_commands.sketching(w):
        edit_cad_commands.redefine_sketch(w, w.edit_sketch)
        return
    if not w.sketching():
        return
    let node = w.selected() else return
    let tool = tool_registry.cad(sketch_node.kind_name) else return
    tool_picks.start(w, node, tool, 0)

## Extrude from the edited sketch: after it, with its picked profiles.
pub func extrude_profiles(w: Workspace) -> unit!:
    if edit_cad_commands.sketching(w):
        edit_cad_commands.feature_from_sketch(w, "Extrude")
        return
    if not w.sketching() and not shown_profiles(w):
        w.notice = "Extrude takes a sketch's profiles: click inside its closed regions (in the sketch, or the sketch shown)"
        w.touch()
        return
    let keys = w.sketch_profiles.join(";")
    w.perform("Extrude", func () -> unit!:
        let node = insert_after_display(w, ToolRun("Extrude", []))
        node.texts[0] = keys
        w.network.picked = [node.id]
        w.sketch_profiles = []
        w.sketch_picks = []
        w.selection = none
        # The distance box opens by the arrow (panels).
        w.ask_value = true
        w.rebuild())

## Revolve from the edited sketch: after it, with its picked profiles,
## then the axis picked (tool_picks.luc).
pub func revolve_profiles(w: Workspace) -> unit!:
    if edit_cad_commands.sketching(w):
        edit_cad_commands.feature_from_sketch(w, "Revolve")
        return
    if not w.sketching() and not shown_profiles(w):
        w.notice = "Revolve takes a sketch's profiles: click inside its closed regions (in the sketch, or the sketch shown)"
        w.touch()
        return
    let keys = w.sketch_profiles.join(";")
    # A line of the shown sketch picked with them is the axis, as Fusion
    # takes a selected line.
    var axis = -1
    if shown_profiles(w):
        let shown = w.network.require(w.network.display)
        let sketch = CadSketch.parse(shown.texts[0])
        for e in w.sketch_picks:
            if axis < 0 and e < sketch.entity_count() and sketch.kind(e) == line_kind:
                axis = e
    w.sketch_picks = []
    w.perform("Revolve", func () -> unit!:
        let node = insert_after_display(w, ToolRun("Revolve", []))
        node.texts[0] = keys
        w.network.picked = [node.id]
        w.sketch_profiles = []
        w.selection = none
        w.rebuild())
    let made = w.selected() else return
    if made.kind != "Revolve":
        return
    if axis >= 0:
        node_commands.set_text(w, cad_features.axis_text, cad_features.line_axis(axis))
        return
    let tool = tool_registry.cad("Revolve") else return
    tool_picks.start(w, made, tool, tool.picks.length - 1)

## Extrude face: an Extrude of the first selected face itself, its loops
## the profile (Fusion takes a planar face as a profile); one undo step.
pub func extrude_face(w: Workspace) -> unit!:
    face_feature(w, "Extrude")

## Extrude or Revolve (`kind`) of the first selected face as its profile,
## as Fusion takes a planar face: one undo step; a Revolve then waits for
## its axis.
pub func face_feature(w: Workspace, kind: str) -> unit!:
    if edit_cad_commands.active(w):
        edit_cad_commands.face_feature(w, kind)
        return
    let faces = selected_faces(w)
    if faces.length == 0:
        w.notice = f"{kind} face: select a flat face of the body (the Faces level) first"
        w.touch()
        return
    let name = faces[0]
    w.perform(f"{kind} face", func () -> unit!:
        let node = insert_after_display(w, ToolRun(kind, []))
        node.texts[0] = extrude_inputs.face_profile(name)
        w.network.picked = [node.id]
        w.selection = none
        w.ask_value = kind == "Extrude"
        w.rebuild())
    if kind != "Revolve":
        return
    let made = w.selected() else return
    let tool = tool_registry.cad("Revolve") else return
    if made.kind == "Revolve":
        tool_picks.start(w, made, tool, tool.picks.length - 1)

## The selected faces' or edges' attribute `name` texts on the displayed
## CAD body's pick geometry, each once, at the level picking `domain`.
func selected_texts(w: Workspace, domain: Domain, name: str, attribute_domain: int) -> list[str]!:
    let found: list[str] = []
    if w.sketching() or w.network.context() != "cad" or not w.selecting() or not w.on_domain(domain):
        return found
    let mesh = w.geometry else return found
    let column = mesh.find_attribute(name, attribute_domain)
    if column < 0:
        return found
    let chosen = w.current_selection() else return found
    var at = chosen.next(-1)
    while at >= 0:
        let text = mesh.attribute_text(column, at)
        if text != "" and not (text in found):
            found.append(text)
        at = chosen.next(at)
    return found

## The names of the selected faces of the displayed CAD body.
pub func selected_faces(w: Workspace) -> list[str]!:
    return selected_texts(w, Domain.face, "cad_face_name", 2)

## References to the selected edges of the displayed CAD body.
pub func selected_edges(w: Workspace) -> list[str]!:
    return selected_texts(w, Domain.edge, "cad_edge_reference", 4)

## Sketch on face: a Sketch on the first selected face, after the
## displayed node, edited at once (the editor looks straight at it).
pub func sketch_on_face(w: Workspace) -> unit!:
    if edit_cad_commands.active(w):
        edit_cad_commands.create_sketch(w)
        return
    let faces = selected_faces(w)
    if faces.length == 0:
        w.notice = "Sketch on face: select a flat face of the body (the Faces level) first"
        w.touch()
        return
    let name = faces[0]
    w.perform("Sketch on face", func () -> unit!:
        let node = insert_after_display(w, ToolRun(sketch_node.kind_name, []))
        node.values[0] = float(sketch_node.face_plane)
        node.texts[1] = name
        w.network.picked = [node.id]
        w.selection = none
        w.rebuild())