dymokomi/luced-3d
Source of release 0.11.9
## The document's length unit: lengths are kept in millimeters everywhere
## and shown in the unit the document chooses (millimeters or inches).
from luce_ui import ui
from luce_ui.ui import Unit
from workspace import Workspace
from luce_std import math
from angles import degrees
## The unit lengths show in.
pub func document_unit(w: Workspace) -> Unit:
return ui.unit_named(w.network.units) else ui.millimeters()
## Length `value` (mm) as the document shows it: "12.50 mm", "0.492 in".
pub func length_text(value: float, shown_in: Unit) -> str:
let places = 3 if shown_in.suffix.trim() == "in" else 2
return f"{fixed(shown_in.shown(value), places)}{shown_in.suffix}"
## An angle (radians) in degrees: "90.0°".
pub func angle_text(value: float) -> str:
return f"{fixed(degrees(value), 1)}°"
## `value` with `places` decimals.
func fixed(value: float, places: int) -> str:
var scale = 1.0
for _ in 0..<places:
scale *= 10.0
let negative = value < 0.0
let whole = int(math.round(math.abs(value) * scale))
let integer = whole // int(scale)
var fraction = str(whole % int(scale))
while fraction.length < places:
fraction = "0" + fraction
return f"{"-" if negative and whole != 0 else ""}{integer}.{fraction}"