dymokomi/luced-3d
Source of release 0.11.9
0.11.9 / src / sketch_frames.luc
## A Sketch node's plane and text for the editor on the UI thread
## (sketch_node.luc), from what its last cook reported (SketchState): the
## body is cooked by the compute worker, never here. An origin plane's frame
## is known at once; a face's comes with the cook, and the last one known
## stands while the next cooks. And the view looking square at a sketch.
from workspace import Workspace
from node import Node
from sketch_node import SketchFrame, SketchState, node_frame, face_plane, parse_state
from luce_cad.sketch import CadSketch
import sketch_kinds
import sketch_camera
from luce_std import math
pub class FrameCache:
## The state last read, by the node and the text it was read from.
var read_from: str = ""
var last_state: SketchState? = none
## The last face frame known of each node.
let faces: map[int, SketchFrame]
pub func init(self):
self.faces = {}
## Sketch node `node`'s state as its last cook reported it, when that
## cook is current (none while it cooks, or without one).
pub func state(self, node: Node) -> SketchState?!:
if node.dirty or node.sketch_state == "":
return none
let key = f"{node.id}|{node.sketch_state}"
if key != self.read_from:
self.read_from = key
self.last_state = parse_state(node.sketch_state)
return self.last_state
## Sketch node `node`'s frame in `w`: an origin plane's at once, a
## face's as last cooked (the ground until a cook said).
pub func frame(self, node: Node) -> SketchFrame!:
if int(node.values[0]) != face_plane:
return node_frame(node.values)
if let found = self.state(node):
self.faces[node.id] = found.plane
return found.plane
return self.faces[node.id] else node_frame([1.0, node.values[1]])
## Look square at `sketch` on plane `f` in the active pane, orthographic,
## x right and y up as the frame says, at the middle of what is drawn (as
## Fusion's Look At does), the whole body in view when nothing is drawn yet.
## The pane's view before the sketch is kept to return to when the sketch is
## left (sketch_camera.luc).
pub func look_at(w: Workspace, sketch: CadSketch, f: SketchFrame) -> unit!:
sketch_camera.keep(w)
let view = w.panes.view()
var low = [1.0e30, 1.0e30]
var high = [-1.0e30, -1.0e30]
for at in 0..<sketch.entity_count():
if sketch.kind(at) == sketch_kinds.point_kind:
low = [math.min(low[0], sketch.x(at)), math.min(low[1], sketch.y(at))]
high = [math.max(high[0], sketch.x(at)), math.max(high[1], sketch.y(at))]
if high[0] >= low[0]:
view.target = f.at((low[0] + high[0]) * 0.5, (low[1] + high[1]) * 0.5)
view.distance = math.max(20.0, 2.2 * math.max(high[0] - low[0], high[1] - low[1]))
else:
view.target = f.origin
if let mesh = w.geometry:
if mesh.face_count() > 0:
let b = mesh.bounds()
let middle = f.coordinates(b.minimum.add(b.maximum).multiply_scalar(0.5))
view.target = f.at(middle[0], middle[1])
view.distance = math.max(view.distance, 1.6 * b.maximum.subtract(b.minimum).length())
view.look_square(f.normal(), f.v)
w.touch()