Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.8

0.11.8 / src / profile_pick.luc

## Pick profiles: choosing which of a sketch's regions an Extrude or
## Revolve takes, by clicking in them as in Fusion 360. While the prompt is
## open the feature's result shows (live as profiles change), with its
## input sketch's curves over it, the chosen profiles outlined and the one
## under the pointer lit. A click in a region adds it or takes it away; the
## node keeps the profiles' keys (luce-cad's: they hold as dimensions
## change). An Extrude's click off the regions on a flat face of the body
## takes that face as its profile, as Fusion's Extrude takes planar faces
## (extrude_inputs.face_profile). Enter finishes, Esc undoes the picking.
from luce_cad.sketch import CadSketch
from luce_cad.modeling import CadModeling
from luce_cad.cad_edits import CadEdits
from luce_cad.cad_geometry import CadGeometry
from luce_geocore.geocore import Mesh
import extrude_inputs
from luce_geocore.geocore import GeometrySet
from luce_gpu.gpu import RenderTarget
from luce_ui.ui import Event
from luce_window.input import EventKind
from workspace import Workspace
from luce_3d.graphics import CurveLines
from overlay_lines import OverlayLines
from sketch_node import kind_name
from sketch_frames import FrameCache
import view_projection
import theme

pub let label = "Pick profiles"

pub class ProfilePick:
    let w: Workspace
    ## The input sketch's curves, as of its text and plane.
    var curves: GeometrySet? = none
    var made_from: str = ""
    ## The key under the pointer ("": none).
    pub var hover: str = ""
    let frames: FrameCache
    ## The input's bodies picked on for a face, as of the input's stamp.
    var bodies: Mesh? = none
    var bodies_from: int = 0

    pub func init(self, w: Workspace):
        self.w = w
        self.frames = FrameCache()

    ## Whether profiles are being picked.
    pub func active(self) -> bool:
        let prompt = self.w.prompt else return false
        return prompt.label == label

    ## The input Sketch's curves, made again when its text or plane changed.
    func sketch(self) -> GeometrySet?!:
        let prompt = self.w.prompt else return none
        let input = self.w.network.find(prompt.input) else return none
        if input.kind != kind_name:
            return none
        let f = self.frames.frame(input)
        let key = f"{input.texts[0]}|{f.origin.x},{f.origin.y},{f.origin.z}|{f.u.x},{f.u.y},{f.u.z}|{f.v.x},{f.v.y},{f.v.z}"
        if key != self.made_from or self.curves == none:
            let s = CadSketch.parse(input.texts[0])
            _ = s.solve()
            self.curves = s.curves(f.origin, f.u, f.v)
            self.made_from = key
        return self.curves

    ## The key of the profile under the pointer at (`x`, `y`), or "".
    func key_at(self, x: float, y: float, width: float, height: float) -> str!:
        let prompt = self.w.prompt else return ""
        let input = self.w.network.find(prompt.input) else return ""
        let curves = self.sketch() else return ""
        let f = self.frames.frame(input)
        let at = view_projection.plane(self.w, x, y, width, height, f.origin, f.normal())
        return CadModeling.profile_at(curves, at)

    ## The name of the flat face of the input's body under (`x`, `y`), or
    ## "" (none there, or not flat).
    func face_at(self, x: float, y: float, width: float, height: float) -> str!:
        let prompt = self.w.prompt else return ""
        let input = self.w.network.find(prompt.input) else return ""
        let data = self.w.network.evaluate_data(input.id)
        if CadGeometry.model_count(data.geometry) == 0:
            return ""
        if self.bodies == none or self.bodies_from != input.stamp:
            self.bodies = CadEdits.pick_mesh(data.geometry)
            self.bodies_from = input.stamp
        let mesh = self.bodies else return ""
        let face = mesh.ray_face(view_projection.origin(self.w, x, y, width, height), view_projection.ray(self.w, x, y, width, height).normalized())
        if face < 0:
            return ""
        let name = mesh.attribute_text(mesh.find_attribute("cad_face_name", 2), face)
        if name == "" or not CadModeling.face_frame(CadGeometry.model(data.geometry, 0), name).found:
            return ""
        return name

    ## Pointer events while picking; whether one was used.
    pub func event(self, event: Event, width: float, height: float) -> bool!:
        if not self.active() or event.alt:
            return false
        if event.kind == EventKind.pointer_moved:
            let before = self.hover
            self.hover = self.key_at(event.x, event.y, width, height)
            return before != self.hover
        if event.kind != EventKind.pointer_down or event.button != 0 or not event.inside:
            return false
        let prompt = self.w.prompt else return true
        let node = self.w.network.find(prompt.node) else return true
        let key = self.key_at(event.x, event.y, width, height)
        var keys = toggled(node.texts[0], key)
        if key == "" and node.kind == "Extrude":
            # Off the regions: a flat face of the body as the profile.
            let face = self.face_at(event.x, event.y, width, height)
            if face == "":
                return true
            keys = extrude_inputs.face_profile(face)
        elif key == "":
            return true
        elif extrude_inputs.profile_face(node.texts[0]) != "":
            keys = key
        self.w.perform(label, func () -> unit!:
            node.texts[0] = keys
            self.w.network.invalidate(node.id)
            self.w.rebuild())
        return true

    ## The sketch's curves, the chosen profiles and the hovered one.
    pub func draw_lines(self, target: RenderTarget) -> unit!:
        if not self.active():
            return
        let curves = self.sketch() else return
        let prompt = self.w.prompt else return
        let node = self.w.network.find(prompt.node) else return
        let camera = self.w.view.camera
        CurveLines.batch(curves).draw(camera, target, theme.grey(200.0), 1.5)
        # Outlines over the curves they run along, lit over them.
        if node.texts[0].trim() != "":
            let chosen = OverlayLines.make()
            chosen.add_curves(CadModeling.profile_outlines(curves, node.texts[0]))
            chosen.draw_lit(camera, target, theme.accent(), 3.0)
        if self.hover != "":
            let hovered = OverlayLines.make()
            hovered.add_curves(CadModeling.profile_outlines(curves, self.hover))
            hovered.draw_lit(camera, target, theme.grey(245.0), 2.0)

## `keys` (apart by `;`) with `key` added, or taken away when it was there.
pub func toggled(keys: str, key: str) -> str:
    let kept: list[str] = []
    var found = false
    for k in keys.split(";"):
        let t = k.trim()
        if t == "":
            continue
        if t == key:
            found = true
        else:
            kept.append(t)
    if not found:
        kept.append(key)
    return kept.join(";")