Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.2

0.11.2 / src / sketch_node.luc

## The Sketch node: a 2D sketch on a plane inside a CAD node, Fusion 360's
## way. Its entities and constraints are luce-cad's (CadSketch), kept as
## text in the node's first text and solved on every evaluation, so a
## dimension typed in the parameter panel moves the geometry. Its result is
## the sketch's curves on its plane, what Extrude and Revolve read. The
## plane is one of the origin planes (XY, the XZ ground, YZ) or a flat face
## of the body connected to its input (Face: kept by the face's name, so
## the sketch stays on it as features upstream change the body), moved
## along its normal by Offset; drawing and constraining happen in the
## viewport (sketch_editor.luc). Curves projected from the input body's
## edges are cast again on every evaluation, so they follow it. Lengths are
## millimeters.
from luce_cad.sketch import CadSketch
from luce_cad.cad_geometry import CadGeometry
from luce_cad.modeling import CadModeling
from luce_3d.graphics import Vector3
from geometry_data import GeometryData
from luce_ui import ui

pub let kind_name = "Sketch"

## A sketch plane: its origin and the directions of the sketch's x and y
## (the normal is x × y). Seen from the normal's side, x runs right and y
## up: on XY as from the front (+Z), on the ground XZ as from above (+Y,
## with -Z up the screen), on YZ as from the right (+X, with -Z right).
pub class SketchFrame:
    pub let origin: Vector3
    pub let u: Vector3
    pub let v: Vector3

    pub func init(self, origin: Vector3, u: Vector3, v: Vector3):
        self.origin = origin
        self.u = u
        self.v = v

    pub func normal(self) -> Vector3:
        return self.u.cross(self.v)

    ## Whether `other` is the same plane with the same axes.
    pub func equals(self, other: SketchFrame) -> bool:
        return self.origin.subtract(other.origin).length() == 0.0 and self.u.subtract(other.u).length() == 0.0 and self.v.subtract(other.v).length() == 0.0

    ## The frame as text, for keys of what is cached on it.
    pub func key(self) -> str:
        return f"{self.origin.x}|{self.origin.y}|{self.origin.z}|{self.u.x}|{self.u.y}|{self.u.z}|{self.v.x}|{self.v.y}|{self.v.z}"

    ## Sketch coordinates (`x`, `y`) as a world point.
    pub func at(self, x: float, y: float) -> Vector3:
        return self.origin.add(self.u.multiply_scalar(x)).add(self.v.multiply_scalar(y))

    ## World point `p`'s sketch coordinates (it projected onto the plane).
    pub func coordinates(self, p: Vector3) -> list[float]:
        let d = p.subtract(self.origin)
        return [d.dot(self.u), d.dot(self.v)]

pub func plane_names() -> str:
    return "XY|XZ|YZ|Face"

## The Plane menu's Face entry.
pub let face_plane = 3

## The frame of plane `plane` (plane_names' order) moved `offset` along
## its normal.
pub func frame(plane: int, offset: float) -> SketchFrame:
    var u = Vector3(1.0, 0.0, 0.0)
    var v = Vector3(0.0, 1.0, 0.0)
    if plane == 1:
        v = Vector3(0.0, 0.0, -1.0)
    elif plane == 2:
        u = Vector3(0.0, 0.0, -1.0)
        v = Vector3(0.0, 1.0, 0.0)
    return SketchFrame(u.cross(v).multiply_scalar(offset), u, v)

## The frame of a Sketch node with values `v` (plane, offset).
pub func node_frame(v: list[float]) -> SketchFrame:
    return frame(int(v[0]), v[1])

## A Sketch node's result: its text solved, as curves on its plane (on a
## face of `data`'s body for the Face plane).
pub func evaluate_sketch(v: list[float], texts: list[str], data: GeometryData) -> GeometryData!:
    let sketch = CadSketch.parse(texts[0])
    let f = face_frame(v, texts, data) if int(v[0]) == face_plane else node_frame(v)
    # Projected curves cast again from the body as it is now.
    var lost = 0
    if sketch.link_count() > 0:
        if CadGeometry.model_count(data.geometry) == 0:
            error(ui.invalid, "The sketch's projected curves follow its input body: connect the body")
        lost = sketch.relink(CadGeometry.model(data.geometry, 0), f.origin, f.u, f.v)
    if sketch.entity_count() > 0 and not sketch.solve():
        error(ui.invalid, "The sketch's constraints cannot all hold: remove one that conflicts")
    let result = GeometryData(geometry = sketch.curves(f.origin, f.u, f.v))
    if lost > 0:
        result.warning = f"{lost} projected curve(s) lost their edge upstream: they stay where they were"
    return result

## The plane of the face named `texts[1]` of `data`'s body, moved Offset
## along its normal.
pub func face_frame(v: list[float], texts: list[str], data: GeometryData) -> SketchFrame!:
    if texts[1].trim() == "":
        error(ui.invalid, "Pick the face the sketch is on (Pick face)")
    if CadGeometry.model_count(data.geometry) == 0:
        error(ui.invalid, "The sketch's face is on its input body: connect the body")
    let found = CadModeling.face_frame(CadGeometry.model(data.geometry, 0), texts[1])
    if not found.found:
        error(ui.invalid, "The sketch's face is no longer on the body (or not flat): pick a face again")
    let f = SketchFrame(found.origin, found.u, found.v)
    return SketchFrame(f.origin.add(f.normal().multiply_scalar(v[1])), f.u, f.v)