dymokomi/luced-3d
Source of release 0.11.2
0.11.2 / tests / editor / edit_cad_tests.luc
## Edit CAD: Object, Faces, Edges and CVs levels on analytic models.
## Primitives are recipe steps selected as their Object; whole models move
## and go, faces go, a B-rep edge is picked whole, and CV steps move control
## vertices (the other CVs keep their exact positions).
from workspace import Workspace
from app import Luce3D
from geometry_data import cad_data
from edit_operations import EditStep
from cad_display_tests import dome_model
from luce_cad.cad_geometry import CadGeometry
from luce_3d.graphics import Vector3
import edit_cad
from luce_std import math
import modeling_commands
import node_commands
import selection_commands
import component_moves
func near(a: float, b: float):
assert(math.abs(a - b) < 0.00001)
func models(w: Workspace) -> int!:
let node = w.selected() else trap("node")
return CadGeometry.model_count(w.network.evaluate_data(node.id).geometry)
pub func edit_cad_tests() -> unit!:
let w = Workspace()
node_commands.delete_selected(w)
node_commands.add_node(w, "Edit CAD", 0.0, 0.0)
assert(w.editing())
let names: list[str] = []
for level in w.levels():
names.append(level.name)
assert(names == ["Object", "CVs", "Edges", "Faces"])
# A solid is a step, selected as its Object; moved right after, the
# move's group is read on the mesh it was picked on (which names the
# new solid `.selection`).
modeling_commands.create(w, "Box")
assert(models(w) == 1 and w.component_mode == 0 and w.selected_count() == 6)
component_moves.move_selection(w, Vector3(3.0, 0.0, 0.0))
near((w.geometry else trap("moved")).bounds().maximum.x, 4.0)
near((w.geometry else trap("moved")).bounds().minimum.x, 2.0)
modeling_commands.create(w, "Sphere")
let both = w.geometry else trap("both")
assert(models(w) == 2 and w.selected_count() == both.face_count() - 6)
# Moved right after it was made: the sphere alone moves.
component_moves.move_selection(w, Vector3(0.0, 5.0, 0.0))
let lifted = w.geometry else trap("lifted")
near(lifted.bounds().maximum.y, 6.0)
near(lifted.bounds().minimum.y, -1.0)
w.undo()
# Object picks select a whole model; Faces picks a B-rep face.
selection_commands.choose_component(w, 0, false)
assert(w.selected_count() == 6)
selection_commands.set_mode(w, 3)
selection_commands.choose_component(w, 0, false)
assert(w.selected_count() == 1)
modeling_commands.model(w, "Delete")
let node = w.selected() else trap("node")
let data = w.network.evaluate_data(node.id)
assert(CadGeometry.model(data.geometry, 0).face_count() == 5)
# Edges picks every mesh edge of one B-rep edge.
selection_commands.set_mode(w, 2)
let mesh = w.geometry else trap("edges")
let tag = mesh.find_attribute("cad_edge", 4)
var edge = -1
for at in 0..<(mesh.edge_count()):
if edge < 0 and mesh.attribute_value(tag, at) >= 0.0: edge = at
selection_commands.choose_component(w, edge, false)
assert(w.selected_count() >= 1)
# Whole models go at the Object level.
selection_commands.set_mode(w, 0)
selection_commands.choose_component(w, 0, false)
modeling_commands.model(w, "Delete")
assert(models(w) == 1)
# A CV step moves one control vertex of a NURBS dome.
let kind = edit_cad.kind()
let dome = cad_data(dome_model())
_ = dome.preview()
let cloud = kind.pick(dome, 1) else trap("cloud")
assert(cloud.point_count() == 9 and cloud.face_count() == 0)
let moved = kind.run(EditStep("Transform Components", "4", 1, [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3.0], cloud.topology_hash()), dome)
let model = CadGeometry.model(moved.geometry, 0)
near(model.cv_position(4).z, 5.0)
near(model.cv_position(0).z, 0.0)
# A drag's frames show the quick preview; the release asks for the full
# tessellation, which replaces it.
assert(moved.preview(quick = true).quick)
let full = moved.preview(quick = false)
assert(not full.quick and full.meshes.length == 1)
fillet_tool()
print("PASS Edit CAD: Object, Faces, Edges and CVs levels; solids as steps; models moved and deleted, faces deleted, edges picked whole, CVs moved, quick drag previews; Fillet on picked edges with its tool bar")
## Fillet as an Edit CAD tool: a box's edge picked, Fillet makes the step
## the Fillet node's code runs (a seventh face); the tool bar shows its
## Radius, Continuity and Tangent chain, and a radius set there reruns it.
func fillet_tool() -> unit!:
let editor = Luce3D()
let w = editor.workspace
node_commands.delete_selected(w)
node_commands.add_node(w, "Edit CAD", 0.0, 0.0)
modeling_commands.create(w, "Box")
selection_commands.set_mode(w, 2)
let mesh = w.geometry else trap("edges")
let tag = mesh.find_attribute("cad_edge", 4)
var edge = -1
for at in 0..<(mesh.edge_count()):
if edge < 0 and mesh.attribute_value(tag, at) >= 0.0: edge = at
selection_commands.choose_component(w, edge, false)
modeling_commands.model(w, "Fillet")
let node = w.selected() else trap("node")
assert(w.notice == "" and node.edits[-1].verb == "Fillet")
let filleted = CadGeometry.model(w.network.evaluate_data(node.id).geometry, 0)
assert(filleted.face_count() == 7)
editor.panels.refresh()
let bar = editor.panels.tool_bar
assert(bar.is_open() and bar.panel.count() == 3)
near(bar.panel.component(0, 0), 1.0)
modeling_commands.set_last_step(w, 0, 0.4, 0.0001, 100000.0)
editor.panels.refresh()
near(bar.panel.component(0, 0), 0.4)
assert(CadGeometry.model(w.network.evaluate_data(node.id).geometry, 0).face_count() == 7)
editor.close()