dymokomi/luced-3d
Source of release 0.11.8
## Snapping: one layer between the pointer and every tool that places or
## moves something. Its settings (the buttons at the viewport's bottom
## left) say what positions snap to: the grid, points (ends, centers,
## vertices), middles of lines and arcs, crossings of curves, places on
## curves, surfaces. A tool asks it where the pointer lands (in a sketch,
## sketch_snap; in the scene, scene_snap; moving, grid_snapped), and what
## it found shows where it is (the workspace's snap mark, drawn by the
## viewport). Cmd (or Ctrl) held places freely.
from luce_3d.graphics import Vector3
from luce_std import math
## The settings' names, in the buttons' order (the first switches them all).
pub func snap_names() -> list[str]:
return ["Snap", "Grid", "Points", "Middles", "Crossings", "Curves", "Surfaces"]
pub func snap_icons() -> list[str]:
return ["snap", "snap-to-grid", "vertex-select", "snap-midpoint", "snap-intersection", "snap-curve", "snap-surface"]
pub class SnapSettings:
## Each setting's state, snap_names' order.
pub var flags: list[bool]
pub func init(self):
self.flags = [true, true, true, true, true, true, true]
pub func on(self) -> bool:
return self.flags[0]
pub func grid(self) -> bool:
return self.flags[0] and self.flags[1]
pub func points(self) -> bool:
return self.flags[0] and self.flags[2]
pub func surfaces(self) -> bool:
return self.flags[0] and self.flags[6]
pub func toggle(self, index: int):
self.flags[index] = not self.flags[index]
## The sketch's kinds to look for (luce-cad CadSketch.snap's mask: 1
## points, 2 middles, 4 crossings, 8 curves).
pub func sketch_mask(self) -> int:
if not self.on():
return 0
return (1 if self.flags[2] else 0) + (2 if self.flags[3] else 0) + (4 if self.flags[4] else 0) + (8 if self.flags[5] else 0)
## What the pointer last snapped to: where (world) and what ("Point",
## "Middle", "Crossing", "Curve", "Grid", "Surface").
pub class SnapMark:
pub let at: Vector3
pub let kind: str
pub func init(self, at: Vector3, kind: str):
self.at = at
self.kind = kind
## The grid's step for a view `distance` away: 1, 2 or 5 × 10ⁿ mm, about a
## tenth of it (the sketch grid's and the scene's).
pub func grid_step(distance: float) -> float:
let rough = distance / 12.0
var step = math.pow(10.0, math.floor(math.log10(math.max(rough, 1e-6))))
if rough >= step * 5.0:
step *= 5.0
elif rough >= step * 2.0:
step *= 2.0
return step
## `value` on a grid of `step`.
pub func on_grid(value: float, step: float) -> float:
return math.round(value / step) * step
## `value` in steps of `step` when `enabled` (Shift's 15° turns).
pub func stepped(value: float, step: float, enabled: bool) -> float:
return on_grid(value, step) if enabled else value
## A sketch snap kind's name (CadSketch.snap's result).
pub func sketch_kind_name(kind: int) -> str:
if kind == 1: return "Point"
if kind == 2: return "Middle"
if kind == 4: return "Crossing"
if kind == 8: return "Curve"
return ""