Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.8

0.11.8 / src / selection_commands.luc

## Commands on the component selection of the displayed geometry: picking
## components (whole pieces at a level of pieces), selecting all or none,
## inverting, the Select tools (the registry's: loops, Similar, …) with
## their rows on the tool bar, and switching the selection level (the
## selection carries over to the new level's domain). During a tool prompt
## each change goes to the prompted node at once (follow_prompt). The
## viewport's click gestures are selection_gestures.luc's.
from luce_geocore.geocore import Selection, Domain
from select_run import SelectRun
from luce_std import math
from workspace import Workspace
from tool_prompts import follow_prompt
from tool_record import Tool
from edit_types import smart_level, face_level, edge_level, domain_number
import edit_cad
from luce_cad.nurbs import CadNurbs

pub func invert_selection(w: Workspace) -> unit!:
    let chosen = w.current_selection() else return
    w.perform("Invert selection", func () -> unit!:
        let inverted = chosen.inverted()
        let mesh = w.geometry else return
        w.selection = inverted.combined(mesh, "@cad_edge>=0", 2) if w.network.context() == "cad" and w.level_index() == edge_level else w.kept(inverted, chosen))
    follow_prompt(w)

## A Select tool (the registry's Select section: Loop, Similar, Sharp
## Edges, …) on the selection, or on everything when nothing is selected
## and the tool looks at everything: the selection its rule makes replaces
## the selection, one undo step, on the rule's own level (Boundary Loop of
## faces gives edges); the geometry stays as it was. Its rows stay on the
## tool bar while that selection does (set_last_select).
pub func select_with(w: Workspace, tool: Tool, numbers: list[float]) -> unit!:
    let mesh = w.geometry else return
    let chosen = w.current_selection() else return
    var seed = chosen
    if chosen.count() == 0:
        if not tool.whole:
            w.notice = f"Select components for {tool.label}"
            w.touch()
            return
        seed = chosen.filled(true)
    let made = w.kept(seed.ruled(mesh, tool.name, numbers), chosen)
    w.perform(tool.label, func () -> unit!:
        adopt(w, made))
    w.last_select = SelectRun(tool, seed, numbers, made)
    follow_prompt(w)

## The Select tool run last with row `slot` set to `value` (within
## `minimum`..`maximum`): run again on the same seed, its selection
## replacing the one it made, inside the tool bar's transaction.
pub func set_last_select(w: Workspace, slot: int, value: float, minimum: float, maximum: float) -> unit!:
    let run = w.last_select else return
    let mesh = w.geometry else return
    if not run.current(w.selection) or slot < 0 or slot >= run.numbers.length:
        return
    let numbers = run.numbers.copy()
    numbers[slot] = math.max(minimum, math.min(maximum, value))
    if numbers == run.numbers:
        return
    let made = w.kept(run.seed.ruled(mesh, run.tool.name, numbers), run.seed)
    w.perform("Change " + run.tool.label, func () -> unit!:
        adopt(w, made))
    w.last_select = SelectRun(run.tool, run.seed, numbers, made)
    # Repeat runs it with what its bar was set to.
    if w.last_tool == run.tool.label:
        w.last_tool_numbers = numbers.copy()
    follow_prompt(w)

## Take `made` as the selection, switching to the level of its components
## when they are of another type.
pub func adopt(w: Workspace, made: Selection) -> unit!:
    if made.domain() != w.domain():
        w.adopt_level(w.level_of(made.domain()))
    w.selection = made

pub func select_none(w: Workspace) -> unit!:
    if w.current_selection() == none:
        return
    w.perform("Select none", func () -> unit!:
        w.selection = none)
    follow_prompt(w)

## Switching the component type carries the selection over (a face is
## kept when all its points were selected; a point when any face was).
## Smart keeps the level picked on, when it is one of Smart's.
pub func set_mode(w: Workspace, mode: int) -> unit!:
    if w.sketching():
        if mode in w.shown_levels():
            w.sketch_level = mode
            w.touch()
        return
    if mode == w.component_mode or not w.level_available(mode):
        return
    let mesh = w.geometry
    let chosen = w.selection
    let before = w.level_index()
    w.component_mode = mode
    if mode == smart_level:
        w.smart_pick = before if before in w.smart_levels() else face_level
        if w.level_index() == before:
            w.touch()
            return
    w.selection = none
    if let geometry = mesh:
        if let previous = chosen:
            if w.network.context() == "geometry" and previous.fits(geometry) and previous.count() > 0:
                let domain = w.domain()
                let strict = w.on_domain(Domain.face) or (w.on_domain(Domain.edge) and previous.domain() == domain_number(Domain.point))
                w.selection = previous.on_domain(geometry, domain, strict)
    w.touch()
    follow_prompt(w)
    # An Edit kind may pick each level on other geometry (Edit CAD).
    if w.selection_kind() != none:
        w.rebuild()

## Click: select `id` alone, or toggle it with `extend`, or take it out
## with `remove` (Ctrl or Cmd); -1 clears, but not while extending or
## removing. At a level of pieces (Object) the whole piece around `id`;
## with `row` (0 to 2) the CV's hull row along V, along U or its net
## (Edit CAD's CVs, Alias's hull picking).
pub func choose_component(w: Workspace, id: int, extend: bool, chain: bool = false, remove: bool = false, row: int = -1) -> unit!:
    if not w.selecting():
        return
    let mesh = w.geometry else return
    let chosen = w.current_selection() else return
    let tangent = chain and w.network.context() == "cad" and w.level_index() == edge_level
    var piece = (edit_cad.edge_chain(mesh, id) if tangent else w.level().whole(mesh, id)) if id >= 0 else ""
    if id >= 0 and row >= 0:
        piece = CadNurbs.cloud_row(mesh, id, row)
    w.perform("Select components", func () -> unit!:
        if id < 0:
            if not extend and not remove: w.selection = none
            return
        if piece == "":
            w.selection = chosen.changed(id, 1 if remove else (2 if extend else 3))
        elif remove:
            w.selection = chosen.combined(mesh, piece, 1)
        elif not extend:
            w.selection = Selection.of(mesh, piece, w.domain())
        else:
            w.selection = chosen.combined(mesh, piece, 1 if chosen.contains(id) else 0)
        # Mirrored across the Edit Mesh's Symmetry, hidden faces left out.
        if let picked = w.selection:
            w.selection = w.kept(picked, chosen))
    follow_prompt(w)

## A box over the components: `boxed(current, operation, wholly, through)`
## is the Base selection of what the box touches (the points inside it, the
## edges and faces it meets; with `wholly` the edges and faces wholly
## inside it; with `through` hidden ones too) changed from `current` (0
## replaced, 1 added, 2 taken away). At a level of pieces each touched
## element brings its whole piece, one Base pass per piece; a CAD edge
## level counts B-rep edges only. On CAD (a CAD network, Edit CAD) a box
## follows Fusion's rule: dragged left to right it is a window, a B-rep
## face or edge taken only when all of it lies inside the box (and some of
## it shows); right to left a crossing, taking what it touches.
## Whether boxes follow CAD's rule, Fusion's: in a CAD network or Edit
## CAD, a box dragged left to right is a window, right to left a crossing.
pub func cad_boxes(w: Workspace) -> bool:
    if w.network.context() == "cad":
        return true
    let kind = w.active_kind() else return false
    return kind.name == edit_cad.kind_name

pub func choose_boxed(w: Workspace, operation: int, crossing: bool, boxed: func(Selection, int, bool, bool) -> Selection!) -> unit!:
    if not w.selecting():
        return
    let mesh = w.geometry else return
    let chosen = w.current_selection() else return
    let level = w.level()
    var made = chosen
    # A CAD window takes what lies wholly inside it; a crossing, as boxes
    # elsewhere, what it touches.
    let window = cad_boxes(w) and not crossing
    # What lies wholly inside the box, hidden or not (a piece must be all
    # in it).
    var outside: Selection? = none
    if window and level.piece != none:
        outside = boxed(Selection.empty(mesh, w.domain()), 0, true, true).inverted()
    if level.piece == none:
        made = boxed(chosen, operation, window, false)
    else:
        var rest = boxed(Selection.empty(mesh, w.domain()), 0, false, false)
        if w.network.context() == "cad" and w.level_index() == edge_level:
            rest = rest.combined(mesh, "@cad_edge>=0", 2)
        if operation == 0:
            made = Selection.empty(mesh, w.domain())
        var at = rest.next(-1)
        while at >= 0:
            let piece = level.whole(mesh, at)
            var whole = true
            if let out = outside:
                whole = out.combined(mesh, piece, 2).count() == 0
            if whole:
                made = made.combined(mesh, piece, 1 if operation == 2 else 0)
            rest = rest.combined(mesh, piece, 1).changed(at, 1)
            at = rest.next(-1)
    let shown = w.kept(made, chosen)
    w.perform("Select components", func () -> unit!:
        w.selection = shown if shown.count() > 0 else none)
    follow_prompt(w)

pub func select_all(w: Workspace) -> unit!:
    let chosen = w.current_selection() else return
    w.perform("Select all", func () -> unit!:
        if w.network.context() == "cad" and w.level_index() == edge_level:
            w.selection = Selection.of(w.geometry else trap("CAD"), "@cad_edge>=0", w.domain())
        else:
            w.selection = w.kept(chosen.filled(true), chosen))
    follow_prompt(w)

## Smart's click: component `id` of level `level` (what was under the
## pointer), alone, added or taken away (`operation` 0, 1, 2). Another level
## than the selection's starts a new selection on it; Smart's levels share
## their pick geometry, so nothing is cooked.
pub func choose_smart(w: Workspace, level: int, id: int, operation: int) -> unit!:
    if id >= 0 and level != w.smart_pick:
        if operation == 2:
            return
        w.smart_pick = level
        w.selection = none
    choose_component(w, id, operation == 1 and id >= 0, remove = operation == 2)