Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.9

0.11.9 / src / held.luc

## What an Edit node holds (edit_held.luc has the model): its result, the
## result before its adjustable last action, their content keys and where
## it started. Immutable: a change makes a new one, so undo snapshots and
## node copies share it.
from geometry_data import GeometryData
from luce_prism.prism import Document, Value, Encoding
from luce_ui import ui

## What an Edit node holds.
pub class Held:
    ## The result before the adjustable last action, and its content key.
    pub let base: GeometryData
    pub let base_key: int
    ## The node's result: the last action run on `base` (`base` with none),
    ## its content key, and what that action's run said (its failure leaves
    ## `base` with this warning).
    pub let result: GeometryData
    pub let key: int
    pub let warning: str
    ## The key of the step `result` was made by (0: none).
    pub let made_for: int
    ## The input's stamp when the node started from it (0: unwired or
    ## loaded).
    pub let input_stamp: int
    ## How many actions are baked into `base`: the last action runs as
    ## action number `actions`, which names what it makes apart from every
    ## earlier action's (Edit CAD's `#<node>s<n>` faces).
    pub let actions: int

    pub func init(self, base: GeometryData, base_key: int, result: GeometryData, key: int, warning: str, made_for: int, input_stamp: int, actions: int = 0):
        self.base = base
        self.base_key = base_key
        self.result = result
        self.key = key
        self.warning = warning
        self.made_for = made_for
        self.input_stamp = input_stamp
        self.actions = actions

## `data` held as it is, under `key`: nothing to adjust.
pub func held_as(data: GeometryData, key: int, input_stamp: int = 0, actions: int = 0) -> Held:
    return Held(data, key, data, key, "", 0, input_stamp, actions)


## What `data` says beside its geometry, as HeldStore keeps it with a held
## result: its warning, sketches' state, handles, Auto choice, report, what
## its last action created, console and Aligns ("" when it says nothing, as
## a move's result does).
pub func meta_of(data: GeometryData) -> str!:
    let fields = [data.warning, data.sketch_state, data.handles, data.automatic, data.report, data.created, data.console, data.aligns.join("\n")]
    var said = false
    for field in fields:
        if field != "": said = true
    if not said:
        return ""
    let doc = Document()
    doc.add("/meta", "meta")
    doc.set("/meta", "fields", Value.strings([fields.length], fields))
    return (doc.encode(Encoding.text).bytes()).text()

## `data` saying what `meta` (meta_of's text) says.
pub func with_meta(data: GeometryData, meta: str) -> GeometryData!:
    if meta == "":
        return data
    let doc = Document.parse(meta)
    let fields = doc.get("/meta", "fields")
    if fields.rank() != 1 or fields.count() != 8:
        error(ui.invalid, "A held result's notes are damaged")
    data.warning = fields.text_at(0)
    data.sketch_state = fields.text_at(1)
    data.handles = fields.text_at(2)
    data.automatic = fields.text_at(3)
    data.report = fields.text_at(4)
    data.created = fields.text_at(5)
    data.console = fields.text_at(6)
    let aligns = fields.text_at(7)
    data.aligns = aligns.split("\n") if aligns != "" else []
    return data