Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.6

0.11.6 / 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 (Remove Faces; Delete heals, refusing a box's side), 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
import cad_blends
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, "CAD", 0.0, 0.0)
    node_commands.enter_group(w, w.network.selected)
    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", "Smart", "Patch", "Curves"])
    # 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)
    # Delete heals, as Fusion's: a box's side has nothing to close it, so
    # the step says why; Remove Faces leaves the box open.
    modeling_commands.model(w, "Delete")
    let node = w.selected() else trap("node")
    assert(node.warning.contains("could not be built"))
    w.undo()
    selection_commands.choose_component(w, 0, false)
    modeling_commands.model(w, "Remove Faces")
    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()), 0, dome, "#1", none)
    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; a G2 strip's control net drawn at the CVs level alone")

## 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, Tangency weight and Tangent chain, and a radius set
## there reruns it; a weight set there changes the step's fillet (2: more of
## the corner kept), 1 giving back the plain one.
func fillet_tool() -> unit!:
    let editor = Luce3D()
    let w = editor.workspace
    node_commands.delete_selected(w)
    node_commands.add_node(w, "CAD", 0.0, 0.0)
    node_commands.enter_group(w, w.network.selected)
    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
    # Its rows (the hidden ones counted), More options among them,
    # Overflow's Smooth, Cliff and Notch behind it, Limits' At overlap, the
    # corner's Edge shape and Fullness, and Selection sets with its eight
    # Set k radius rows.
    assert(bar.is_open() and bar.panel.count() == 31, f"{bar.panel.count()} rows")
    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)
    # The step's Tangency weight: 2 keeps more of the corner; 1 the plain.
    let plain = step_volume(w, node.id)
    modeling_commands.set_last_step(w, cad_blends.tangency_slot, 2.0, 0.1, 2.0)
    assert(step_volume(w, node.id) > plain + 1e-4)
    modeling_commands.set_last_step(w, cad_blends.tangency_slot, 1.0, 0.1, 2.0)
    assert(math.abs(step_volume(w, node.id) - plain) < 1e-9)
    # At G2 the strip is a B-spline face a feature made: its control net is
    # drawn at the CVs level, where its cvs are edited, and nowhere else
    # (Fusion shows a modeled body no control frame).
    modeling_commands.set_last_step(w, 1, 2.0, 1.0, 3.0)
    assert(hull_points(w) == 0)
    selection_commands.set_mode(w, 1)
    assert(hull_points(w) > 0)
    selection_commands.set_mode(w, 3)
    assert(hull_points(w) == 0)
    editor.close()

## The tessellated volume of node `id`'s first body.
func step_volume(w: Workspace, id: int) -> float!:
    let mesh = CadGeometry.model(w.network.evaluate_data(id).geometry, 0).tessellate(32)
    var total = 0.0
    for triangle in 0..<mesh.index_count() // 3:
        let a = mesh.vertex(mesh.index(triangle * 3)).position
        let b = mesh.vertex(mesh.index(triangle * 3 + 1)).position
        let c = mesh.vertex(mesh.index(triangle * 3 + 2)).position
        total += a.dot(b.cross(c)) / 6.0
    return total

## Control hull points the viewport draws.
func hull_points(w: Workspace) -> int:
    var total = 0
    for batch in w.batches.hull_batches:
        total += batch.count()
    return total