Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.7

0.11.7 / src / sketch_selecting.luc

## The Sketch editor's selections (sketch_editor.luc) as lists: entities,
## constraints picked by their glyphs, and profiles, each taken alone,
## added or taken away; and what Select's box holds at the level, as
## Fusion's box: dragged left to right a window (what lies wholly inside
## it), right to left a crossing (what it touches too); at Shapes, whole
## chains.
from luce_cad.sketch import CadSketch
from sketch_chains import Joints
from sketch_kinds import point_kind
from sketch_node import SketchFrame
from workspace import Workspace
import view_projection
import marquee

## `values` with `picked` alone (`operation` 0), added (1) or taken away (2).
pub func changed(values: list[int], picked: list[int], operation: int) -> list[int]:
    if operation == 0:
        return picked
    var result = values.copy()
    for each in picked:
        if operation == 2:
            result = without(result, each)
        elif not (each in result):
            result.append(each)
    return result

## `values` with `value` alone (`operation` 0), added or taken away again
## (1, Shift), or taken away (2, Ctrl or Cmd).
pub func toggled(values: list[int], value: int, operation: int) -> list[int]:
    if operation == 0:
        return [value]
    if operation == 2 or value in values:
        return without(values, value)
    let result = values.copy()
    result.append(value)
    return result

## The same for profile keys.
pub func toggled_key(keys: list[str], key: str, operation: int) -> list[str]:
    if operation == 0:
        return [key]
    let result: list[str] = []
    for each in keys:
        if each != key:
            result.append(each)
    if operation != 2 and not (key in keys):
        result.append(key)
    return result

## `values` without `value`.
pub func without(values: list[int], value: int) -> list[int]:
    let kept: list[int] = []
    for v in values:
        if v != value:
            kept.append(v)
    return kept

## What Select's box (screen corners box[0], box[1] and box[2], box[3] on a
## viewport `width` by `height`) holds of `sketch` on plane `f`: entities
## of the pick filter `only` (CadSketch.select_box's) wholly inside, as
## the view casts the box onto the plane, or touched when it was dragged
## right to left (a crossing); with `shapes` (the Shapes level) curves
## only of chains wholly inside (a crossing: every chain it touches).
pub func boxed(w: Workspace, sketch: CadSketch, f: SketchFrame, box: list[float], only: int, shapes: bool, width: float, height: float) -> list[int]!:
    let corners: list[float] = []
    let normal = f.normal()
    for (x, y) in [(box[0], box[1]), (box[2], box[1]), (box[2], box[3]), (box[0], box[3])]:
        let place = f.coordinates(view_projection.plane(w, x, y, width, height, f.origin, normal))
        corners.append(place[0])
        corners.append(place[1])
    let held: list[int] = []
    let crossing = marquee.crosses(box)
    for at in 0..<sketch.select_box(corners, only, crossing = crossing):
        held.append(sketch.boxed(at))
    let picked: list[int] = []
    let chosen: set[int] = {}
    let inside_box: set[int] = {}
    for entity in held:
        inside_box.insert(entity)
    # Shapes: each chain traced once, from the joints found once.
    var joints: Joints? = none
    let traced: set[int] = {}
    for entity in held:
        if not shapes or sketch.kind(entity) == point_kind:
            if not (entity in chosen):
                picked.append(entity)
                chosen.insert(entity)
            continue
        if entity in traced:
            continue
        # A chain is taken when every curve of it is inside (a crossing:
        # when one is touched, all of it).
        if joints == none:
            joints = Joints(sketch)
        let chain = (joints else trap("joints")).chain(sketch, entity)
        var inside = true
        for each in chain:
            traced.insert(each)
            inside = inside and each in inside_box
        for each in chain:
            if (crossing or (inside and each in inside_box)) and not (each in chosen):
                picked.append(each)
                chosen.insert(each)
    return picked