Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.9

0.11.9 / src / tool_combine.luc

## Combine (Fusion 360's): a target body joined with, cut by or
## intersected with tool bodies, kept with Keep tools (edit_cad_faces.luc).
## The node keeps its target and tools by name, its second input's bodies
## being tools too; an Edit CAD step takes the target picked, then waits
## for the tool bodies.
from luce_cad.cad_geometry import CadGeometry
from tool_record import Tool, ToolEntry, Pick, Invocation
from geometry_data import GeometryData
from parameter_rows import Parameter, toggle, menu
from edit_types import object_level
import pick_kinds
import edit_cad_faces
import cad_cook

pub func entry() -> ToolEntry:
    return ToolEntry("Combine", "Combine", "boolean-union", "Modify", [cad_cook.context], [cad_cook.edit_kind], made)

func made() -> Tool:
    return entry().tool("Modify", "Bodies", "Join tool bodies to a target body, cut them from it, or keep what they share", picks(), rows(), cook, inputs = 2, waits = waits)

## The target body, then the tool bodies (a step keeps them as a group).
func picks() -> list[Pick]:
    return [Pick("Target body", [object_level], kind = pick_kinds.body, slot = 0, needed = unwired, doc = "The body the tools change."),
            Pick("Tool bodies", [object_level], kind = pick_kinds.bodies, slot = 1, step = edit_cad_faces.target_text, rest = true, needed = unwired,
                 doc = "The bodies joined to, cut from or intersected with the target (the second input's bodies are tools too).")]

func unwired(values: list[float], texts: list[str], wired: bool) -> bool:
    return not wired

func waits(values: list[float], texts: list[str], wired: bool) -> str:
    if texts[0].trim() == "" and not wired:
        return "Pick the target body"
    if texts[1].trim() == "" and not wired:
        return "Pick the tool bodies"
    return ""

## A node: its target (none picked, with a second input wired: the chain's
## first body) and its tools, the second input's bodies among them.
func cook(call: Invocation, data: GeometryData, tag: str) -> GeometryData!:
    if not call.node:
        return cad_cook.face_step(call, data, tag)
    var input = data
    var wired = 0
    if let more = call.second:
        wired = CadGeometry.model_count(more.geometry)
        input = data.merged(more)
    let waiting = waits(call.numbers, call.texts, wired > 0)
    if waiting != "":
        return data.passed(waiting)
    let mesh = cad_cook.node_mesh(call, input)
    let group = cad_cook.picked(call, mesh, 0, true) if call.text(0).trim() != "" else "@cad_model=0"
    let tools: list[str] = []
    if call.text(1).trim() != "":
        tools.append(cad_cook.picked(call, mesh, 1, true))
    let count = CadGeometry.model_count(input.geometry)
    for index in count - wired..<count:
        tools.append(f"@cad_model={index}")
    return cad_cook.node_face_step(call, "Combine", input, mesh, group, [cad_cook.joined(tools), ""], 8, tag)

func rows() -> list[Parameter]:
    return [menu("Operation", edit_cad_faces.combine_slot, "Join|Cut|Intersect", 0.0, "Join: the tool bodies added to the target. Cut: taken out of it. Intersect: only what the target shares with them."),
            toggle("Keep tools", edit_cad_faces.keep_tools_slot, 0.0, "Keep the tool bodies after combining.")]