Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.6

0.11.6 / src / edit_cad_faces.luc

## Fusion 360's Modify tools on faces and bodies, as Edit CAD steps (direct
## modeling: each one luce-cad operation on the model as the steps before
## it left it) and as the CAD feature nodes that run the same step code on
## the faces and bodies their names find (their tool_<name>.luc). A step's group
## names faces or whole models on the pick geometry it was selected on.
##   Move (the gizmo, Move/Copy; cad_moves.luc): models named whole are
##     placed, faces of a model named in part move and turn with the faces
##     around following; Create copy leaves the originals and adds moved
##     copies (of bodies, or of faces as a surface body).
##   Offset Face (Press Pull on faces): along their normals, tangent faces
##     following.
##   Delete: whole models go; faces are deleted and healed, the faces around
##     extended to close the gap (Fusion's Delete). Remove: whole models go,
##     recorded as their own step (Fusion's Remove). Remove Faces takes
##     faces away and leaves the model open.
##   Replace Face: the faces (with Tangent chain, those meeting them
##     smoothly) moved onto the target faces' surface.
##   Split Face: the faces split where a picked face's surface (extended,
##     or with Extend splitting tool off only the face itself, right across
##     each face it meets) or an origin plane crosses them; the pieces can
##     be picked apart afterwards.
##   Shell: hollowed, the faces named opened (Tangent chain: with the faces
##     meeting them smoothly; a whole model: a closed void), the wall
##     Inside, Outside or Both (a thickness each way).
##   Combine: the target model joined with, cut by or intersected with the
##     tool models (kept with Keep tools).
##   Split Body: a model split by a picked face's surface (extended, or
##     with Extend splitting tool off only where the face reaches) or an
##     origin plane, each piece a model of its own.
##   Reblend (Parasolid's; Fusion's Press Pull on a fillet face): the
##     picked blends (constant rolling-ball fillets, an imported body's
##     too) given another radius, the blends meeting them keeping theirs.
from luce_cad.cad_edits import CadEdits
from luce_cad.modeling import CadModeling, CadDirect
from luce_cad.model import CadModel
from luce_geocore.geocore import Mesh
from luce_3d.graphics import Vector3
from geometry_data import GeometryData
from edit_operations import EditStep
from handle_lines import HandleLine
from edit_cad_sketches import with_models
import handle_lines
import cad_moves
from luce_ui import ui

## Reblend: the picked blends (luce-cad's identified_blends) given the
## step's radius (number 0), the blends meeting them keeping theirs.
pub let reblend_verb = "Reblend"

## Replace Face's, Split Face's and Split Body's texts: the faces picked as
## the target or splitting tool (by name); Combine's: the tool models'
## group.
pub let target_text = 0
## Shell's numbers: the inside thickness, the direction (0 inside, 1
## outside, 2 both), Tangent chain, the outside thickness.
pub let thickness_slot = 0
pub let shell_side_slot = 1
pub let shell_chain_slot = 2
pub let outside_slot = 3
## Replace Face's number: Tangent chain.
pub let replace_chain_slot = 0
## Combine's numbers: operation (0 join, 1 cut, 2 intersect), keep tools.
pub let combine_slot = 0
pub let keep_tools_slot = 1
## Split Body's and Split Face's numbers: the tool (0 the picked face, 1
## XY, 2 XZ, 3 YZ) and an origin plane's offset along its normal.
pub let split_tool_slot = 0
pub let split_offset_slot = 1
## Split Body's and Split Face's Extend splitting tool (Fusion's, on by
## default): off, a picked face cuts only where it reaches.
pub let split_extend_slot = 2

## The pick geometry step `step` was selected on: `data`'s, when the
## models are as they were then.
pub func picked(step: EditStep, data: GeometryData, pick: func(GeometryData) -> Mesh!) -> Mesh!:
    let shown = pick(data)
    if step.topology != 0 and step.topology != shown.topology_hash():
        error(ui.invalid, "The CAD input changed. Undo the upstream change or clear this node's edits.")
    return shown

## A face step (face_verbs, or a move) on `data`, the faces it makes named
## for `tag` (edit_cad's `#<node>s<n>`, a node's `#<node>`); `shown` is the
## pick geometry its group was selected on.
pub func run_face_step(step: EditStep, tag: str, data: GeometryData, shown: Mesh) -> GeometryData!:
    if step.verb == "Transform Components":
        return cad_moves.moved(step, tag, data, shown)
    if step.verb == "Combine":
        return combined(step, data, shown)
    if step.verb == "Remove Faces":
        return GeometryData(geometry = CadEdits.without_parts(data.geometry, shown, step.group))
    let models = data.models()
    let result: list[CadModel] = []
    var any = false
    var handles: list[HandleLine] = []
    for index in 0..<models.length:
        let model = models[index]
        let share = CadEdits.model_share(shown, step.group, index)
        if share == 0:
            result.append(model)
            continue
        any = true
        if step.verb == "Remove":
            # Fusion's Remove: the body goes, whatever of it was picked.
            continue
        let names = CadEdits.model_faces(shown, step.group, index)
        if step.verb == "Delete":
            # A whole model goes; faces are healed over.
            if share == 1:
                result.append(CadDirect.deleted_faces(model, names))
            continue
        if step.verb == "Offset Face":
            if share == 2:
                error(ui.invalid, "Offset Face moves faces: pick them at the Faces level")
            result.append(CadDirect.offset_faces(model, names, step.numbers[0], true))
            if handles.length == 0:
                handles = arrow(model, names, 0, 1.0)
            continue
        if step.verb == "Replace Face":
            let target = step.text(target_text).trim()
            if target == "":
                error(ui.invalid, "Pick the target faces to replace the faces with")
            result.append(CadDirect.replaced_faces(model, names, target_model(models, target), target, number(step, replace_chain_slot, 1.0) != 0.0))
            continue
        if step.verb == "Split Face":
            if share == 2:
                error(ui.invalid, "Split Face splits faces: pick them at the Faces level")
            result.append(split_faces(step, tag, model, names, models))
            continue
        if step.verb == "Shell":
            result.append(shelled(step, tag, model, names if share == 1 else ""))
            if handles.length == 0 and share == 1:
                handles = shell_arrows(step, model, names)
            continue
        if step.verb == "Split Body":
            result.extend(split(step, tag, model, models))
            continue
        if step.verb == reblend_verb:
            if share == 2:
                error(ui.invalid, "Reblend changes blends: pick their faces at the Faces level")
            result.append(CadDirect.reblended(model, names, step.numbers[0], tag))
            continue
        error(ui.invalid, f"{step.verb} does not apply to CAD models or faces")
    if not any:
        error(ui.invalid, f"{step.verb}: pick what it works on first")
    let made = with_models(data, result)
    made.handles = handle_lines.encode(handles)
    return made

## Number `slot` of `step`, or `otherwise` when the step has fewer.
func number(step: EditStep, slot: int, otherwise: float) -> float:
    return step.numbers[slot] if step.numbers.length > slot else otherwise

## An arrow on the first of faces `names` of `model`, along its outward
## normal (`scale` -1: into the model), for the value in `slot`.
func arrow(model: CadModel, names: str, slot: int, scale: float) -> list[HandleLine]!:
    let at = CadModeling.face_point(model, names.split(";")[0].trim())
    if not at.found:
        return []
    return [HandleLine("arrow", slot, scale, at.point, at.normal, -1, Vector3())]

## Shell, Fusion's: the wall Inside (its thickness), Outside (the outside
## thickness) or Both (each), the faces `removed` opened (with Tangent
## chain, the faces meeting them smoothly too; none: a closed void).
func shelled(step: EditStep, tag: str, model: CadModel, removed: str) -> CadModel!:
    let side = int(step.numbers[shell_side_slot])
    let chain = number(step, shell_chain_slot, 1.0) != 0.0
    let inside = step.numbers[thickness_slot]
    let outside = number(step, outside_slot, inside)
    if side == 1:
        return CadModeling.shelled(model, removed, outside, tag, 1, chain)
    if side == 2:
        return CadModeling.shelled(model, removed, inside, tag, 2, chain, outside)
    return CadModeling.shelled(model, removed, inside, tag, 0, chain)

## Shell's arrows on its first opened face: the inside thickness into the
## body, the outside one out of it, as its direction has them.
func shell_arrows(step: EditStep, model: CadModel, names: str) -> list[HandleLine]!:
    let side = int(step.numbers[shell_side_slot])
    var result: list[HandleLine] = []
    if side != 1:
        result = arrow(model, names, thickness_slot, -1.0)
    if side != 0 and step.numbers.length > outside_slot:
        result = result + arrow(model, names, outside_slot, 1.0)
    return result

## The model holding face `name` (the first of several apart by `;`).
func target_model(models: list[CadModel], name: str) -> CadModel!:
    let first = name.split(";")[0].trim()
    for model in models:
        if CadModeling.face_point(model, first).found:
            return model
    error(ui.invalid, "The target face is no longer in the model: pick it again")

## The origin plane Split Body's or Split Face's tool menu names (1 XY,
## 2 XZ, 3 YZ): its normal.
func plane_normal(tool: int) -> Vector3:
    return [Vector3(0.0, 0.0, 1.0), Vector3(0.0, 1.0, 0.0), Vector3(1.0, 0.0, 0.0)][tool - 1]

## Split Face: faces `names` of `model` split by the picked face's surface
## or an origin plane.
func split_faces(step: EditStep, tag: str, model: CadModel, names: str, models: list[CadModel]) -> CadModel!:
    let tool = int(step.numbers[split_tool_slot])
    if tool == 0:
        let face = step.text(target_text).trim()
        if face == "":
            error(ui.invalid, "Pick the splitting face, or choose an origin plane as the splitting tool")
        return CadDirect.split_faces(model, names, target_model(models, face), face, tag, extend = extended(step))
    let normal = plane_normal(tool)
    return CadDirect.split_faces_by_plane(model, names, normal.multiply_scalar(step.numbers[split_offset_slot]), normal, tag)

## Whether a split's picked face is extended (Extend splitting tool; on,
## Fusion's default, when the step's numbers stop short of it).
func extended(step: EditStep) -> bool:
    return step.numbers.length <= split_extend_slot or step.numbers[split_extend_slot] != 0.0

## Split Body: `model` split by the picked face's surface or an origin
## plane, each lump a model of its own.
func split(step: EditStep, tag: str, model: CadModel, models: list[CadModel]) -> list[CadModel]!:
    let tool = int(step.numbers[split_tool_slot])
    var made = model
    if tool == 0:
        let face = step.text(target_text).trim()
        if face == "":
            error(ui.invalid, "Pick the splitting face, or choose an origin plane as the splitting tool")
        made = CadModeling.split_by_face(model, target_model(models, face), face, tag, extend = extended(step))
    else:
        let normal = plane_normal(tool)
        made = CadModeling.split_by_plane(model, normal.multiply_scalar(step.numbers[split_offset_slot]), normal, tag)
    let pieces: list[CadModel] = []
    for lump in 0..<CadModeling.lump_count(made):
        pieces.append(CadModeling.lump(made, lump))
    if pieces.length < 2:
        error(ui.invalid, "The splitting tool does not cut through the body")
    return pieces

## Combine: the target model (the step's group) with the tool models (its
## target text's group) by the operation; the tools go unless kept.
func combined(step: EditStep, data: GeometryData, shown: Mesh) -> GeometryData!:
    let models = data.models()
    var target = -1
    let tools: list[int] = []
    for index in 0..<models.length:
        if CadEdits.model_share(shown, step.group, index) > 0:
            if target < 0:
                target = index
        elif step.text(target_text) != "" and CadEdits.model_share(shown, step.text(target_text), index) > 0:
            tools.append(index)
    if target < 0:
        error(ui.invalid, "Pick the target body")
    if tools.length == 0:
        error(ui.invalid, "Pick the tool bodies to combine with the target")
    var body = models[target]
    for index in tools:
        body = CadModeling.combined(body, models[index], int(step.numbers[combine_slot]))
    let keep = number(step, keep_tools_slot, 0.0) != 0.0
    let result: list[CadModel] = []
    for index in 0..<models.length:
        if index == target:
            if body.face_count() > 0:
                result.append(body)
        elif keep or not (index in tools):
            result.append(models[index])
    return with_models(data, result)