Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.6

0.11.6 / src / sketch_chains.luc

## Chains of a sketch's curves: curves joined end to end, as far as each
## joint is just two curves meeting (a shape, for Offset and the Shapes
## selection level).
from luce_cad.sketch import CadSketch
from sketch_kinds import point_kind
from sketch_geometry import start_point, end_point

## The curves joined end to end with `curve` (as far as each joint is two
## curves meeting), in order along the chain.
pub func chain_of(sketch: CadSketch, curve: int) -> list[int]:
    let chain = [curve]
    # Forward from the curve's end, then back from its start.
    var tail = end_point(sketch, curve)
    var going = true
    while going:
        going = false
        let next = only_other(sketch, tail, chain)
        if next >= 0:
            chain.append(next)
            tail = end_point(sketch, next) if start_point(sketch, next) == tail else start_point(sketch, next)
            going = true
    var head = start_point(sketch, curve)
    going = true
    while going:
        going = false
        let previous = only_other(sketch, head, chain)
        if previous >= 0:
            chain.insert(0, previous)
            head = start_point(sketch, previous) if end_point(sketch, previous) == head else end_point(sketch, previous)
            going = true
    return chain

## The one curve besides those in `chain` at point `point`, or -1 (none,
## or a branch).
func only_other(sketch: CadSketch, point: int, chain: list[int]) -> int:
    if point < 0:
        return -1
    var found = -1
    for at in 0..<sketch.entity_count():
        # (The origin's axes run through shapes without joining them.)
        if sketch.kind(at) == point_kind or at in chain or sketch.origin_part(at) >= 0:
            continue
        if start_point(sketch, at) == point or end_point(sketch, at) == point:
            if found >= 0:
                return -1
            found = at
    return found