dymokomi/luce-cad
Source of release 0.9.3
0.9.3 / tests / regressions / sketch_object_tests.luc
## Sketches as objects of a geometry set (CadSketches): added beside a
## body, replaced by name, hidden, removed; placed with the set (the plane
## moves, the sketch keeps its sizes), joined, and saved in a geometry file.
from luce_cad.cad_geometry import CadGeometry
from luce_cad.cad_sketches import CadSketches
from luce_cad.solids import CadSolids
from luce_cad.sketch import CadSketch
from luce_geocore.geocore import Vector3, GeometryFile
from checks import near
from luce_std import files, paths
pub func sketch_object_tests() -> unit!:
let body = CadGeometry.of_model(CadSolids.box(2.0, 2.0, 2.0))
assert(CadSketches.count(body) == 0 and CadSketches.find(body, "Sketch1") == -1)
let square = CadSketch.parse("")
let a = square.add_point(0.0, 0.0)
let b = square.add_point(4.0, 0.0)
_ = square.add_line(a, b)
let one = CadSketches.with_sketch(body, "Sketch1", square.text(), Vector3(0.0, 1.0, 0.0), Vector3(1.0, 0.0, 0.0), Vector3(0.0, 0.0, -1.0))
let two = CadSketches.with_sketch(one, "Sketch2", "", Vector3(), Vector3(1.0, 0.0, 0.0), Vector3(0.0, 1.0, 0.0))
assert(CadSketches.count(two) == 2 and CadGeometry.model_count(two) == 1)
assert(CadSketches.find(two, "Sketch2") == 1 and CadSketches.name(two, 0) == "Sketch1")
assert(CadSketches.text(two, 0) == square.text() and CadSketches.visible(two, 0))
near(CadSketches.origin(two, 0).y, 1.0)
near(CadSketches.v(two, 0).z, -1.0)
# Replaced in place by name; hidden; the first set unchanged.
let edited = CadSketches.with_sketch(two, "Sketch1", "", Vector3(), Vector3(1.0, 0.0, 0.0), Vector3(0.0, 1.0, 0.0))
assert(CadSketches.count(edited) == 2 and CadSketches.find(edited, "Sketch1") == 0 and CadSketches.text(edited, 0) == "")
assert(CadSketches.text(two, 0) == square.text())
let hidden = CadSketches.with_visible(two, "Sketch2", false)
assert(not CadSketches.visible(hidden, 1) and CadSketches.visible(two, 1))
let gone = CadSketches.without_sketch(two, "Sketch1")
assert(CadSketches.count(gone) == 1 and CadSketches.name(gone, 0) == "Sketch2")
assert(CadSketches.count(CadSketches.without_sketches(two)) == 0 and CadGeometry.model_count(CadSketches.without_sketches(two)) == 1)
# Placed with the set: moved 3 along x and turned a quarter about y;
# the axes stay unit length under a scale.
let placed = two.transformed(Vector3(3.0, 0.0, 0.0), Vector3(0.0, 1.5707963267948966, 0.0), Vector3(2.0, 2.0, 2.0))
near(CadSketches.origin(placed, 0).x, 3.0)
near(CadSketches.origin(placed, 0).y, 2.0)
near(CadSketches.u(placed, 0).length(), 1.0)
near(CadSketches.u(placed, 0).z, -1.0)
# Joined: both sets' sketches.
assert(CadSketches.count(two.merged(one)) == 3)
# Saved and read back.
_ = CadGeometry.register_codec()
let scratch = files.TemporaryDirectory()
let saved = paths.join(scratch.path(), "sketches.prism")
_ = GeometryFile.save(hidden, saved)
let loaded = GeometryFile.load(saved)
assert(CadSketches.count(loaded) == 2 and CadGeometry.model_count(loaded) == 1)
assert(CadSketches.name(loaded, 0) == "Sketch1" and CadSketches.text(loaded, 0) == square.text())
assert(not CadSketches.visible(loaded, 1))
near(CadSketches.v(loaded, 0).z, -1.0)
print("PASS sketch objects: added beside a body, replaced by name, hidden and removed; placed with the set (unit axes), joined, saved and read back")