Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.7

0.11.7 / src / cad_nodes.luc

## CAD nodes: what a CAD node holds inside, Fusion 360's way of modeling.
## Sketches (sketch_node.luc) give profiles, features make and change solid
## bodies (luce-cad's B-reps: CadModeling and CadSolids), and a few
## geometry nodes (Transform, Merge, Null, Switch, Edit CAD) work on bodies
## too. CAD features exist only inside a CAD node; inside one, the menu
## offers only these.
from luce_cad.cad_geometry import CadGeometry
from luce_cad.modeling import CadModeling
from luce_cad.model import CadModel, CadTolerances
from geometry_data import GeometryData
from luce_ui import ui
from cad_blends import BlendMemo
from tool_record import Invocation
import tool_registry
from kind_memo import KindMemo

## The solid primitives (analytic B-reps, centered on the origin along +Y):
## the CAD tools of the Solids family.
pub func solid_kinds() -> list[str]:
    let result: list[str] = []
    for entry in tool_registry.cad_nodes():
        if entry.name.starts_with("Solid "):
            result.append(entry.name)
    return result

## Nodes only a CAD node holds: the CAD tools' nodes (tool_registry.luc).
pub func cad_only_kinds() -> list[str]:
    let result: list[str] = []
    for entry in tool_registry.cad_nodes():
        result.append(entry.name)
    return result

## Features with a second input (Combine's tool bodies, Extrude's target
## bodies, Check's reference for its deviation map).
pub func two_inputs(kind: str) -> bool:
    let tool = tool_registry.cad_node(kind) else return false
    return tool.inputs > 1

## Whether `kind` is one of cad_only_kinds(), which are made once
## (kind_memo.lucb): every Node made asks.
pub func is_cad_only(kind: str) -> bool:
    if not KindMemo.filled(1):
        # Out of memory: the list made here answers.
        KindMemo.fill(1, cad_only_kinds().join("\n")) catch failure:
            return kind in cad_only_kinds()
    return KindMemo.has(1, kind)

## Whether node `kind`'s curves show their control hulls: not a CAD
## network's (its sketches' circles and arcs are rational curves whose
## control polygons Fusion never draws), nor a CAD node's result.
pub func shows_curve_hulls(kind: str) -> bool:
    return not is_cad_only(kind) and kind != "CAD"

## Geometry nodes a CAD node may hold too.
pub func shared_kinds() -> list[str]:
    return ["Edit CAD", "Transform", "Merge", "Null", "Output", "Switch"]

## Whether `kind` may be added where the network's context is `context`
## ("geometry", "cad" inside a CAD node, "shading" inside a Material).
pub func allowed(kind: str, context: str, shader: bool) -> bool:
    if context == "shading":
        return shader
    if context == "cad":
        return is_cad_only(kind) or kind in shared_kinds()
    return not shader and not is_cad_only(kind)

pub func cad_categories() -> list[str]:
    return ["Sketch", "Create", "Curves", "Surfaces", "Modify", "Inspect", "Utility", "All"]

## A node's category in the CAD menu.
pub func cad_category(kind: str) -> str:
    for entry in tool_registry.cad_nodes():
        if entry.name == kind:
            return entry.category
    if kind in ["Edit CAD", "Transform"]:
        return "Modify"
    return "Utility"

## The bodies of `data` as one: each CAD model of its set, unioned.
func body_of(data: GeometryData, role: str) -> CadModel!:
    let count = CadGeometry.model_count(data.geometry)
    if count == 0:
        error(ui.invalid, f"The {role} input has no solid body")
    var body = CadGeometry.model(data.geometry, 0)
    for at in 1..<count:
        body = CadModeling.combined(body, CadGeometry.model(data.geometry, at), 0)
    return body

## The tag that names the faces node `id` makes (luce-cad's naming).
pub func tag_of(id: int) -> str:
    return f"#{id}"

## A CAD feature's result from its input `data`, its second input, values
## `v` and texts `texts`; the faces it makes are named for `tag` (tag_of).
## `memo` keeps a Fillet's plan between evaluations, when given. `sketch`
## is the nearest Sketch's text upstream (a Revolve's axis on a sketch
## line reads it). A feature with viewport handles reports where they
## stand (GeometryData.handles).
pub func evaluate_cad(kind: str, v: list[float], data: GeometryData, tag: str, second: GeometryData? = none, texts: list[str]? = none, memo: BlendMemo? = none, sketch: str = "", tolerances: CadTolerances? = none) -> GeometryData!:
    if let tool = tool_registry.cad_node(kind):
        return tool.cook(Invocation(kind, v, [], 0, texts = texts else ["", "", "", "", ""], node = true, second = second, memo = memo, sketch = sketch, tolerances = tolerances), data, tag)
    error(ui.invalid, f"{kind} is not a CAD feature")