dymokomi/luced-3d
Source of release 0.11.6
0.11.6 / src / edit_cad_commands.luc
## Edit CAD's commands, Fusion 360's direct modeling from the viewport: the
## right-click menu, the A and S marking menus and Modify › CAD features run
## these while an Edit CAD node is edited (cad_commands.luc sends them
## here). Each adds a step to the node's recipe as one undo step and says
## why when it does not apply.
## Create Sketch: on the selected flat face (its loops projected once),
## else on an origin plane; the sketch is edited at once (the same
## Sketch editor), Finish Sketch leaves it. Edit Sketch edits a sketch
## of the model again, Show or Hide sketch shows it or not.
## Extrude, Revolve (in a sketch): a step taking the picked profiles (none:
## the closed shapes); Extrude's distance box opens by its arrow,
## Revolve waits for its axis.
## Extrude face: a sketch on the face and an Extrude of its loops.
## Add as fillet set, Select blends.
## The tools' own steps are tool_run.luc's, from their declarations.
from luce_geocore.geocore import Domain, Selection
from workspace import Workspace
from node import Node
from edit_types import object_level, face_level, curve_level
from edit_cad_sketches import SketchObject, sketch_step, feature_step, visibility_step, plane_step, next_name
from edit_operations import EditStep
import view_projection
import edit_cad_curves
from sketch_node import face_plane
import edit_cad
import edit_cad_sketches
import tool_picks
import tool_registry
import node_catalog
import cad_blends
import selection_commands
## The Edit CAD node being edited in the viewport (its model, or one of
## its sketches), or none.
pub func model_node(w: Workspace) -> Node?:
let node = w.selected() else return none
if node.kind != edit_cad.kind_name or not node.enabled or node.id != w.network.display:
return none
return node
## Whether the commands here apply: an Edit CAD node is edited.
pub func active(w: Workspace) -> bool:
return model_node(w) != none
## Whether one of its sketches is being edited.
pub func sketching(w: Workspace) -> bool:
let node = model_node(w) else return false
return w.sketching_model(node)
## The model's sketches as its last cook reported them.
pub func sketches(w: Workspace) -> list[SketchObject]!:
let node = model_node(w) else return []
return edit_cad_sketches.parse_objects(node.sketch_state)
# -- Sketches.
## Create Sketch: on the selected flat face, else on origin plane `plane`
## (sketch_node's: 0 XY, 1 XZ, 2 YZ); edited at once.
pub func create_sketch(w: Workspace, plane: int = 1) -> unit!:
let node = model_node(w) else return
let faces = selected_faces(w)
let name = next_name(node.edits)
let face = faces[0] if faces.length > 0 else ""
w.perform("Create Sketch", func () -> unit!:
node.edits.append(sketch_step(name, face_plane if face != "" else plane, 0.0, face))
w.network.invalidate(node.id)
start_editing(w, node, name)
w.rebuild())
## Edit sketch `name` of the model again (the editor looks straight at it).
pub func edit_sketch(w: Workspace, name: str) -> unit!:
let node = model_node(w) else return
start_editing(w, node, name)
w.rebuild()
func start_editing(w: Workspace, node: Node, name: str):
w.edit_sketch = name
w.edit_sketch_node = node.id
w.selection = none
w.sketch_profiles = []
w.end_modal_tool()
w.revision += 1
## Finish Sketch: back to the model.
pub func finish_sketch(w: Workspace) -> unit!:
w.edit_sketch = ""
w.edit_sketch_node = -1
w.sketch_profiles = []
w.selection = none
w.revision += 1
w.rebuild()
## Show or hide sketch `name` (one undo step).
pub func toggle_sketch(w: Workspace, name: str) -> unit!:
let node = model_node(w) else return
var visible = true
for s in sketches(w):
if s.name == name:
visible = s.visible
w.perform(("Hide " if visible else "Show ") + name, func () -> unit!:
node.edits.append(visibility_step(name, not visible))
w.network.invalidate(node.id)
w.rebuild())
## Redefine Sketch Plane (Fusion's): sketch `name` onto the selected flat
## face, else it waits for a face clicked (or an origin plane chosen in the
## tool bar, then Enter); the drawing keeps its own coordinates. One undo
## step with the pick.
pub func redefine_sketch(w: Workspace, name: str) -> unit!:
let node = model_node(w) else return
if w.in_transaction():
return
let faces = selected_faces(w)
if w.sketching():
finish_sketch(w)
w.begin("Redefine Sketch Plane")
node.edits.append(plane_step(name, face_plane, 0.0, faces[0] if faces.length > 0 else ""))
w.network.invalidate(node.id)
if faces.length > 0:
w.selection = none
w.pending_changed = true
w.commit()
w.rebuild()
return
let tool = tool_registry.cad(edit_cad_sketches.plane_verb) else return
tool_picks.ask(w, node, tool, 0)
## Extrude or Revolve (`kind`) of the edited sketch's picked profiles (none
## picked: its closed shapes); the sketch is finished. Extrude's distance
## box opens by its arrow; Revolve waits for its axis.
pub func feature_from_sketch(w: Workspace, kind: str) -> unit!:
let node = model_node(w) else return
if not w.sketching_model(node):
return
let name = w.edit_sketch
let keys = w.sketch_profiles.join(";")
if kind == "Revolve":
w.begin("Revolve")
w.perform(kind, func () -> unit!:
node.edits.append(feature_step(kind, name, keys, node_catalog.starting_values(kind)))
w.network.invalidate(node.id)
w.edit_sketch = ""
w.edit_sketch_node = -1
w.sketch_profiles = []
w.selection = none
w.ask_value = kind == "Extrude"
w.rebuild())
if kind == "Revolve":
let tool = tool_registry.cad("Revolve") else return
tool_picks.ask(w, node, tool, axis_pick)
## Revolve's axis among its picks (tool_revolve.luc).
let axis_pick = 2
## Extrude face: a sketch on the selected flat face (its loops projected)
## and an Extrude of its closed shapes, one undo step.
pub func extrude_face(w: Workspace) -> unit!:
face_feature(w, "Extrude")
## Extrude or Revolve (`kind`) face: a sketch on the selected flat face
## (its loops projected) and the feature of its closed shapes, one undo
## step with a Revolve's axis pick.
pub func face_feature(w: Workspace, kind: str) -> unit!:
let node = model_node(w) else return
let faces = selected_faces(w)
if faces.length == 0:
say(w, f"{kind} face: select a flat face (the Faces level) first")
return
let name = next_name(node.edits)
if kind == "Revolve":
w.begin("Revolve face")
w.perform(f"{kind} face", func () -> unit!:
node.edits.append(sketch_step(name, face_plane, 0.0, faces[0]))
node.edits.append(feature_step(kind, name, "", node_catalog.starting_values(kind)))
w.network.invalidate(node.id)
w.selection = none
w.ask_value = kind == "Extrude"
w.rebuild())
if kind == "Revolve":
let tool = tool_registry.cad("Revolve") else return
tool_picks.ask(w, node, tool, axis_pick)
# -- Steps on the selection.
## A double-click at (`x`, `y`) on a surface at the Curves level with no
## curve under it: the isoparm through the point picked (Alias's), a step,
## the curve selected. While an Align waits for its master (a step's or a
## node's), the isoparm there is its master instead: Alias's temporary pick
## object, kept in the step as the point. Whether it took the click.
pub func pick_isoparm(w: Workspace, x: float, y: float, width: float, height: float) -> bool!:
# A curve list or Align's master waiting takes Alias's temporary
# isoparm through the point instead.
if tool_picks.picking(w):
let way = view_projection.ray(w, x, y, width, height).normalized() catch failure:
return false
let found = w.surface_at(view_projection.origin(w, x, y, width, height), way)
return found.found and tool_picks.isoparm(w, found.point)
if not active(w) or sketching(w) or w.level_index() != curve_level or w.prompt != none or w.placing != none:
return false
let node = model_node(w) else return false
let hit = w.surface_at(view_projection.origin(w, x, y, width, height), view_projection.ray(w, x, y, width, height).normalized())
if not hit.found:
return false
let p = hit.point
w.perform(edit_cad_curves.isoparm_verb, func () -> unit!:
node.edits.append(EditStep(edit_cad_curves.isoparm_verb, "", 0, [0.0, p.x, p.y, p.z], 0))
w.network.invalidate(node.id)
w.adopt_output = true
w.pending_level = curve_level
w.rebuild())
return true
## The selected faces' names (Faces or Patch level) of the edited model.
pub func selected_faces(w: Workspace) -> list[str]!:
if not active(w) or sketching(w) or not w.selecting() or not w.on_domain(Domain.face) or w.level_index() == object_level:
return []
let mesh = w.geometry else return []
let column = mesh.find_attribute("cad_face_name", 2)
let chosen = w.current_selection() else return []
let found: list[str] = []
if column < 0:
return found
var at = chosen.next(-1)
while at >= 0:
let text = mesh.attribute_text(column, at)
if text != "" and not (text in found):
found.append(text)
at = chosen.next(at)
return found
## Whether edges of the edited model are selected (not curves).
pub func selected_edges(w: Workspace) -> bool:
return active(w) and not sketching(w) and w.selecting() and w.on_domain(Domain.edge) and w.level_index() != curve_level and w.selected_count() > 0
## Whether the selected edges can be the last Fillet step's next selection
## set: the step is the recipe's last, a Fillet of Constant or Variable
## radius with room for another set.
pub func fillet_set_open(w: Workspace) -> bool:
if not selected_edges(w):
return false
let node = model_node(w) else return false
if node.edits.length == 0 or node.edits[-1].verb != "Fillet":
return false
let step = node.edits[-1]
let kind = int(step.numbers[cad_blends.radius_type_slot]) if cad_blends.radius_type_slot < step.numbers.length else 0
return kind != 1 and cad_blends.set_count(step.numbers) < cad_blends.most_sets
## Add as fillet set (Fusion's "+" in its Fillet dialog): the selected
## edges, by reference, the last Fillet step's next selection set, at the
## first set's radius to start with (its own row in the step's tool bar).
pub func add_fillet_set(w: Workspace) -> unit!:
if not fillet_set_open(w):
say(w, "Add as fillet set: select edges while a Fillet is the last step")
return
let node = model_node(w) else return
let mesh = w.geometry else return
let chosen = w.current_selection() else return
let column = mesh.find_attribute("cad_edge_reference", 4)
if column < 0:
say(w, "These edges have no names to keep")
return
let references: list[str] = []
var at = chosen.next(-1)
while at >= 0:
let reference = mesh.attribute_text(column, at)
if reference != "" and not (reference in references):
references.append(reference)
at = chosen.next(at)
if references.length == 0:
say(w, "Add as fillet set: select edges of the model")
return
let step = node.edits[-1]
let numbers = step.numbers.copy()
let start = node_catalog.starting_values("Fillet")
while numbers.length < start.length:
numbers.append(start[numbers.length])
let count = cad_blends.set_count(numbers) + 1
numbers[cad_blends.sets_slot] = float(count)
numbers[cad_blends.set_radius_slot + count - 2] = numbers[cad_blends.size_slot]
let texts: list[str] = []
for k in 0..<cad_blends.sets_text + 1:
texts.append(step.text(k))
texts[cad_blends.sets_text] = cad_blends.with_set_edges(texts[cad_blends.sets_text], count, references.join(";"))
w.perform("Add as fillet set", func () -> unit!:
node.edits[-1] = EditStep(step.verb, step.group, step.group_type, numbers, step.topology, step.symmetry, step.keep, texts)
w.network.invalidate(node.id)
w.selection = none
w.rebuild())
## The selected faces' blend radii (the pick mesh's `cad_blend_radius`,
## luce-cad's identify blends: 0 for a face that is no blend), one a face.
pub func selected_blend_radii(w: Workspace) -> list[float]!:
let found: list[float] = []
if not active(w) or sketching(w) or not w.selecting() or not w.on_domain(Domain.face) or w.level_index() == object_level:
return found
let mesh = w.geometry else return found
let radii = mesh.find_attribute("cad_blend_radius", 2)
let faces = mesh.find_attribute("cad_face", 2)
let models = mesh.find_attribute("cad_model", 2)
let chosen = w.current_selection() else return found
if radii < 0 or faces < 0 or models < 0:
return found
let seen: list[(int, int)] = []
var at = chosen.next(-1)
while at >= 0:
let key = (int(mesh.attribute_value(models, at)), int(mesh.attribute_value(faces, at)))
if not (key in seen):
seen.append(key)
found.append(mesh.attribute_value(radii, at))
at = chosen.next(at)
return found
## Whether every selected face is a blend luce-cad identifies (a constant
## rolling-ball fillet).
pub func blends_selected(w: Workspace) -> bool!:
let radii = selected_blend_radii(w)
if radii.length == 0:
return false
for r in radii:
if r <= 0.0:
return false
return true
## Select blends (Parasolid's identify blends): every face of the edited
## models that is a constant rolling-ball blend (the pick mesh's
## `cad_blend_radius`), selected at the Faces level, so an imported body's
## fillets can be deleted or reblended.
pub func select_blends(w: Workspace) -> unit!:
if not active(w) or sketching(w):
return
if w.level_index() != face_level:
selection_commands.set_mode(w, face_level)
let mesh = w.geometry else return
if mesh.find_attribute("cad_blend_radius", 2) < 0:
return
let made = Selection.of(mesh, "@cad_blend_radius>0", w.domain())
if made.count() == 0:
say(w, "Select blends: these models have no constant radius blends")
return
w.perform("Select blends", func () -> unit!:
w.selection = made)
## Whether curves of the edited model are selected (the Curves level).
pub func selected_curves(w: Workspace) -> bool:
return active(w) and not sketching(w) and w.selecting() and w.level_index() == curve_level and w.selected_count() > 0
## Whether whole models are selected (the Object level).
pub func selected_models(w: Workspace) -> bool:
return active(w) and not sketching(w) and w.selecting() and w.level_index() == object_level and w.selected_count() > 0
func say(w: Workspace, text: str):
w.notice = text
w.revision += 1