Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.3

0.11.3 / src / selection_commands.luc

## Commands on the component selection of the displayed geometry: picking
## components (whole pieces at a level of pieces), selecting all, inverting,
## growing and walking it, 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).
from luce_geocore.geocore import Selection, Domain
from workspace import Workspace
from tool_prompts import follow_prompt
from edit_types import smart_level, face_level, object_level, domain_number

pub func invert_selection(w: Workspace) -> unit!:
    let chosen = w.current_selection() else return
    w.perform("Invert selection", func () -> unit!:
        w.selection = chosen.inverted())
    follow_prompt(w)

## Grow (0), shrink (1), border (2), flood (3), loop (4) or ring (5).
pub func walk_selection(w: Workspace, operation: int) -> unit!:
    let mesh = w.geometry else return
    let chosen = w.current_selection() else return
    if operation >= 4 and not w.on_domain(Domain.edge):
        w.notice = "Loops and rings need the Edges level"
        w.revision += 1
        return
    w.perform("Change selection", func () -> unit!:
        w.selection = chosen.walked(mesh, operation, 1))
    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 != object_level:
            w.sketch_level = mode
            w.revision += 1
        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.revision += 1
            return
    w.selection = none
    if let geometry = mesh:
        if let previous = chosen:
            if 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.revision += 1
    follow_prompt(w)
    # An Edit kind may pick each level on other geometry (Edit CAD).
    if w.active_kind() != none:
        w.rebuild()

## Click: select `id` alone, or toggle it with `extend`; -1 clears. At a
## level of pieces (Object) the whole piece around `id`.
pub func choose_component(w: Workspace, id: int, extend: bool) -> unit!:
    if not w.selecting():
        return
    let mesh = w.geometry else return
    let chosen = w.current_selection() else return
    let piece = w.level().whole(mesh, id) if id >= 0 else ""
    w.perform("Select components", func () -> unit!:
        if id < 0:
            if not extend: w.selection = none
            return
        if piece == "":
            w.selection = chosen.changed(id, 2 if extend else 3)
        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))
    follow_prompt(w)

pub func select_all(w: Workspace) -> unit!:
    let chosen = w.current_selection() else return
    w.perform("Select all", func () -> unit!:
        w.selection = chosen.filled(true))
    follow_prompt(w)

## Smart's click: component `id` of level `level` (what was under the
## pointer). 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, extend: bool) -> unit!:
    if id >= 0 and level != w.smart_pick:
        w.smart_pick = level
        w.selection = none
    choose_component(w, id, extend and id >= 0)