dymokomi/luce-cad
Source of release 0.9.3
0.9.3 / tests / regressions / cad_edits_tests.luc
## CadEdits: the pick mesh tags polygons with their model and B-rep face
## and edges with their B-rep edge; whole models move about their centroid
## and part of a model does not; faces delete, and a model losing every
## face goes; a model is added. On a set of two tagged models (and one
## untagged), picked edges give each model its own references, a feature
## made from them replaces its model, and an untagged model has none.
from luce_cad.solids import CadSolids
from luce_cad.model import CadModel, BrepModel
from luce_cad.modeling import CadModeling
from luce_geocore.geocore import Vector3, Selection
from luce_cad.cad_edits import CadEdits
from luce_cad.cad_geometry import CadGeometry
from checks import near
pub func cad_edits_tests() -> unit!:
let box = CadGeometry.of_model(CadSolids.box(2.0, 2.0, 2.0))
let pick = CadEdits.pick_mesh(box)
assert(pick.face_count() == 6 and pick.find_attribute("cad_model", 2) >= 0)
let edge = pick.find_attribute("cad_edge", 4)
var tagged = 0
for at in 0..<pick.edge_count():
if pick.attribute_value(edge, at) >= 0.0: tagged += 1
assert(tagged == 12)
# Moved and turned about its centroid.
let moved = CadEdits.transformed_parts(box, pick, "*", [3.0, 0.0, 0.0, 0.0, 90.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0])
let placed = CadGeometry.model(moved, 0).tessellate(8)
near(placed.bounds().minimum.x, 2.0)
near(placed.bounds().maximum.x, 4.0)
var refused = false
_ = CadEdits.transformed_parts(box, pick, "0", [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]) catch failure:
refused = true
assert(refused)
# Faces go; the last face takes the model with it.
let open_box = CadEdits.without_parts(box, pick, "0 1")
assert(CadGeometry.model_count(open_box) == 1 and CadGeometry.model(open_box, 0).face_count() == 4)
assert(CadGeometry.model_count(CadEdits.without_parts(box, pick, "*")) == 0)
# Another model after it.
let two = CadEdits.with_model(box, CadSolids.sphere(1.0))
assert(CadGeometry.model_count(two) == 2 and CadEdits.pick_mesh(two).face_count() > 6)
edge_references()
face_moves()
open_patches()
print("PASS CAD edits: tagged pick mesh, whole-model moves (parts refused), face and model deletes, added models, per-model edge references, replaced models; picked faces by name, a model's share, faces moved and turned (Move Face), whole models placed, faces not scaled; unnamed models named; a picked face carried across a move")
func edge_references() -> unit!:
let first = CadModeling.tagged(CadSolids.box(2.0, 2.0, 2.0), "#a")
let second = CadModeling.tagged(CadSolids.box(1.0, 1.0, 1.0), "#b").transformed(Vector3(4.0, 0.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
let models = CadEdits.with_model(CadEdits.with_model(CadGeometry.of_model(first), second), CadSolids.sphere(0.5))
let pick = CadEdits.pick_mesh(models)
let a = CadEdits.model_edges(pick, "*", 0)
let b = CadEdits.model_edges(pick, "*", 1)
assert(a.split(";").length == 12 and b.split(";").length == 12)
assert(a.contains("#a") and not a.contains("#b") and b.contains("#b") and not b.contains("#a"))
# The untagged sphere's edges have no names; no model 3.
assert(CadEdits.model_edges(pick, "*", 2) == "" and CadEdits.model_edges(pick, "*", 3) == "")
# The second box's edges all filleted, in its place.
let rounded = CadEdits.replaced(models, 1, CadModeling.filleted(CadGeometry.model(models, 1), b, 0.1, "#f"))
assert(CadGeometry.model_count(rounded) == 3)
assert(CadGeometry.model(rounded, 1).face_count() == 26 and CadGeometry.model(rounded, 0).face_count() == 6)
var refused = false
_ = CadEdits.replaced(models, 3, first) catch failure:
refused = true
assert(refused)
## A tagged 2-box's +X face picked: its name, the model's share of the
## group; moved 1 along x (the box grows to 2 × 2 × 3), turned 10° about
## its middle (the volume kept); the whole model placed; faces never scaled.
func face_moves() -> unit!:
let box = CadGeometry.of_model(CadModeling.tagged(CadSolids.box(2.0, 2.0, 2.0), "#a"))
let pick = CadEdits.pick_mesh(box)
let faces = pick.find_attribute("cad_face", 2)
var side = -1
for polygon in 0..<pick.face_count():
if side < 0 and pick.face_center(polygon).x > 0.999: side = int(pick.attribute_value(faces, polygon))
let group = f"@cad_model=0 &@cad_face={side}"
let name = CadEdits.model_faces(pick, group, 0)
assert(name.starts_with("#a/") and not name.contains(";"))
assert(CadEdits.model_faces(pick, group, 1) == "")
assert(CadEdits.model_share(pick, group, 0) == 1 and CadEdits.model_share(pick, "*", 0) == 2 and CadEdits.model_share(pick, "!*", 0) == 0)
let longer = CadGeometry.model(CadEdits.transformed_faces(box, pick, group, [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]), 0).tessellate(8)
near(longer.bounds().maximum.x, 2.0)
near(longer.bounds().minimum.x, -1.0)
let turned = CadGeometry.model(CadEdits.transformed_faces(box, pick, group, [0.0, 0.0, 0.0, 0.0, 0.0, 10.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]), 0).tessellate(8)
assert(turned.bounds().maximum.x > 1.1 and turned.bounds().maximum.x < 1.2)
let whole = CadGeometry.model(CadEdits.transformed_faces(box, pick, "*", [0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]), 0).tessellate(8)
near(whole.bounds().minimum.y, 4.0)
var refused = false
_ = CadEdits.transformed_faces(box, pick, group, [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]) catch failure:
refused = failure.message.contains("do not scale")
assert(refused)
# The face picked carried onto the moved box's pick mesh (meshed again).
let carried = CadEdits.carried(pick, Selection.of(pick, group, 2), CadEdits.pick_mesh(CadEdits.transformed_faces(box, pick, group, [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0])))
assert(carried != "")
let moved_pick = CadEdits.pick_mesh(CadEdits.transformed_faces(box, pick, group, [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]))
let again = Selection.of(moved_pick, carried, 2)
var all_side = again.count() > 0
var polygon = again.next(-1)
while polygon >= 0:
all_side = all_side and moved_pick.face_center(polygon).x > 1.999
polygon = again.next(polygon)
assert(all_side)
# Unnamed faces named for direct modeling; named ones kept.
let plain = CadEdits.with_model(box, CadSolids.box(1.0, 1.0, 1.0))
let named = CadEdits.named_models(plain, "#m")
assert(CadModeling.face_name_of(CadGeometry.model(named, 1), 0) == "#m1/f0")
assert(CadModeling.face_name_of(CadGeometry.model(named, 0), 0) == CadModeling.face_name_of(CadGeometry.model(box, 0), 0))
assert(CadEdits.model_faces(CadEdits.pick_mesh(named), "@cad_model=1", 1).split(";").length == 6)
## Two trimmed regions share a plane. One rim contains two collinear
## edges: whole edges differ, but their tangent chain is shared.
func open_patches() -> unit!:
let b = BrepModel()
for p in [Vector3(), Vector3(1.0, 0.0, 0.0), Vector3(2.0, 0.0, 0.0), Vector3(2.0, 2.0, 0.0), Vector3(0.0, 2.0, 0.0),
Vector3(3.0, 0.0, 0.0), Vector3(4.0, 0.0, 0.0), Vector3(4.0, 1.0, 0.0), Vector3(3.0, 1.0, 0.0)]:
_ = b.add_vertex(p)
for at in 0..<5: _ = b.add_line(at, (at + 1) % 5)
for at in 0..<4: _ = b.add_line(5 + at, 5 + (at + 1) % 4)
_ = b.add_surface(0, Vector3(), Vector3(0.0, 0.0, 1.0), Vector3(1.0, 0.0, 0.0))
_ = b.add_face(0, [1, 2, 3, 4, 5], [5])
_ = b.add_face(0, [6, 7, 8, 9], [4])
let model = CadModel()
model.attach_brep(b)
assert(model.face_surface_id(0) == model.face_surface_id(1))
let pick = CadEdits.pick_mesh(CadGeometry.of_model(model))
let surfaces = pick.find_attribute("cad_surface", 2)
for face in 0..<pick.face_count(): assert(pick.attribute_value(surfaces, face) == 0.0)
let edges = pick.find_attribute("cad_edge", 4)
let chains = pick.find_attribute("cad_edge_chain", 4)
let seen: list[int] = []
for edge in 0..<pick.edge_count():
let id = int(pick.attribute_value(edges, edge))
if id < 0: continue
if not (id in seen): seen.append(id)
let chain = int(pick.attribute_value(chains, edge))
assert(chain == (0 if id == 1 else id))
assert(CadEdits.selected_entities(pick, Selection.of(pick, "*", 2), 0) == 1)
assert(CadEdits.selected_entities(pick, Selection.of(pick, "*", 2), 3) == 2)
assert(CadEdits.selected_entities(pick, Selection.of(pick, "*", 2), 5) == 1)
assert(CadEdits.selected_entities(pick, Selection.of(pick, "@cad_edge_chain=0", 4), 2) == 2)
assert(seen.length == 9, f"open boundary owners: {seen}")
# Removing a box face keeps its four open rims pickable too.
let box = CadSolids.box(2.0, 2.0, 2.0).subset([1, 2, 3, 4, 5])
let open_pick = CadEdits.pick_mesh(CadGeometry.of_model(box))
let owners = open_pick.find_attribute("cad_edge", 4)
for edge in 0..<open_pick.edge_count(): assert(open_pick.attribute_value(owners, edge) >= 0.0)
print("PASS CAD pick topology: open boundaries, surface patch identity and tangent chains stopping at corners")