dymokomi/luced-3d
Source of release 0.11.9
0.11.9 / src / sketch_modify.luc
## The Sketch editor's MODIFY tools (luce-cad's sketch/modify.lucb), click
## by click as Fusion's: Trim, Extend and Break act on the curve clicked
## (where it was clicked); Fillet and Chamfer take two lines meeting at a
## corner (or the corner's point), a fifth of the shorter one by default,
## the fillet's radius then typed in its value box; Offset takes the chain
## a clicked curve is part of, then a click to its side (that far away);
## Move/Copy takes the selection (or the entity clicked) from one click to
## the next (Shift copies); Sketch Scale takes a base point, a point and
## where that point goes; Blend Curve two curves' ends (sketch_freeform).
## Change Parameters says where dimensions live.
from sketch_editor import SketchEditor
from sketch_kinds import point_kind, line_kind
from sketch_chains import chain_of
from sketch_geometry import length_of, carrier_distance
from sketch_freeform import blend_click
from luce_std import math
## A MODIFY tool's click (Shift held: `shift`).
pub func modify_click(e: SketchEditor, shift: bool) -> unit!:
let sketch = e.sketch else return
let tool = e.tool
let x = e.cursor[0]
let y = e.cursor[1]
let hovered = e.hover
let curve = hovered if hovered >= 0 and sketch.kind(hovered) != point_kind else -1
if tool == "Change Parameters":
e.say("A sketch's dimensions are rows of the Parameters panel (d1, d2, …): change them there")
return
if tool in ["Trim", "Extend", "Break"]:
if curve < 0:
return
let done = act(e, tool, curve, x, y)
if done:
e.commit(tool)
return
if tool == "Fillet" or tool == "Chamfer":
corner_click(e, hovered)
return
if tool == "Offset":
offset_click(e, curve, x, y)
return
if tool.starts_with("Blend Curve"):
blend_click(e, curve, x, y)
return
if tool == "Move/Copy" or tool == "Sketch Scale":
transform_click(e, hovered, shift)
return
if tool in ["Mirror", "Circular Pattern", "Rectangular Pattern"]:
copy_click(e, hovered)
## Mirror, Circular and Rectangular Pattern: their entities (the selection,
## else what the first click is on), then the mirror line, the pattern's
## center (six round it), or the pattern's step (three along it).
func copy_click(e: SketchEditor, hovered: int) -> unit!:
let sketch = e.sketch else return
if e.selection.length == 0:
if hovered < 0:
return
e.selection = [hovered]
e.revision += 1
e.say(f"{e.tool}: " + ("click the line to mirror about" if e.tool == "Mirror" else ("click the pattern's center" if e.tool == "Circular Pattern" else "click from and to: one step of the pattern")))
return
var failed = ""
if e.tool == "Mirror":
if hovered < 0 or sketch.kind(hovered) != line_kind:
return
let picked: list[int] = []
for at in e.selection:
if at != hovered:
picked.append(at)
_ = sketch.mirror(picked, hovered) catch failure:
failed = failure.message
recover 0
elif e.tool == "Circular Pattern":
_ = sketch.pattern_circular(e.selection, e.cursor[0], e.cursor[1], 6, 2.0 * math.pi) catch failure:
failed = failure.message
recover 0
else:
e.clicks.append(e.cursor.copy())
if e.clicks.length < 2:
e.revision += 1
return
let step = [e.clicks[1][0] - e.clicks[0][0], e.clicks[1][1] - e.clicks[0][1]]
e.clicks = []
_ = sketch.pattern_rectangular(e.selection, step[0], step[1], 3) catch failure:
failed = failure.message
recover 0
if failed != "":
e.say(failed)
return
_ = sketch.solve()
e.commit(e.tool)
## Trim, Extend or Break curve `curve` at (`x`, `y`); false (and a notice)
## when it cannot be.
func act(e: SketchEditor, tool: str, curve: int, x: float, y: float) -> bool!:
let sketch = e.sketch else return false
var failed = ""
if tool == "Trim":
sketch.trim(curve, x, y) catch failure:
failed = failure.message
elif tool == "Extend":
sketch.extend(curve, x, y) catch failure:
failed = failure.message
else:
sketch.break_apart(curve, x, y) catch failure:
failed = failure.message
if failed != "":
e.say(failed)
return false
_ = sketch.solve()
return true
## Fillet or Chamfer: two lines (or the point they share).
func corner_click(e: SketchEditor, hovered: int) -> unit!:
let sketch = e.sketch else return
if hovered < 0:
return
var lines: list[int] = []
if sketch.kind(hovered) == point_kind:
# The two lines on the corner point.
for at in 0..<sketch.entity_count():
if sketch.kind(at) == line_kind and (sketch.part(at, 0) == hovered or sketch.part(at, 1) == hovered):
lines.append(at)
if lines.length != 2:
e.say(f"{e.tool} rounds a corner of two lines; this point has {lines.length}")
return
elif sketch.kind(hovered) == line_kind:
e.picks.append(hovered)
e.revision += 1
if e.picks.length < 2:
return
lines = [e.picks[0], e.picks[1]]
e.picks = []
else:
return
let size = 0.2 * math.min(length_of(sketch, lines[0]), length_of(sketch, lines[1]))
var failed = ""
var made = -1
if e.tool == "Fillet":
made = sketch.fillet(lines[0], lines[1], size) catch failure:
failed = failure.message
recover -1
else:
made = sketch.chamfer(lines[0], lines[1], size) catch failure:
failed = failure.message
recover -1
if failed != "":
e.say(failed)
return
_ = sketch.solve()
let filleted = e.tool == "Fillet"
e.commit(e.tool)
# A fillet's radius is its last dimension: typed in its value box.
if filleted and made >= 0:
if let open = e.on_value:
open(sketch.constraint_count() - 1, e.pointer_x, e.pointer_y, false)
## Offset: the first click picks the chain, the second its side and
## distance.
func offset_click(e: SketchEditor, curve: int, x: float, y: float) -> unit!:
let sketch = e.sketch else return
if e.picks.length == 0:
if curve < 0:
return
e.picks = chain_of(sketch, curve)
e.say("Offset: click to the side as far away as the copy goes, or type the distance and Enter")
e.revision += 1
# Its distance typed instead: the last one, selected.
if let ask = e.on_ask:
ask("Offset", e.last_offset, func (typed: float) -> unit!: apply_offset(e, typed))
return
apply_offset(e, offset_distance(e))
## The offset as far from its chain as the cursor is.
pub func offset_distance(e: SketchEditor) -> float!:
let sketch = e.sketch else return 0.0
if e.picks.length == 0:
return 0.0
return carrier_distance(sketch, e.picks[0], e.cursor[0], e.cursor[1])
## Offset the picked chain `distance` toward the cursor's side, as one
## undo: one Offset relation holds every copy at the one distance, a single
## dimension to change after (a projected loop's copy follows the loop as
## the body changes). The distance is kept for the next offset.
func apply_offset(e: SketchEditor, distance: float) -> unit!:
let sketch = e.sketch else return
let chain = e.picks
e.picks = []
if chain.length == 0 or distance <= 0.0:
return
var failed = ""
_ = sketch.offset(chain, distance, e.cursor[0], e.cursor[1], relation = true) catch failure:
failed = failure.message
recover 0
if failed != "":
e.say(failed)
return
e.last_offset = distance
_ = sketch.solve()
e.commit("Offset")
## Move/Copy and Sketch Scale: their entities (the selection, else what
## the first click is on), then their clicks.
func transform_click(e: SketchEditor, hovered: int, shift: bool) -> unit!:
let sketch = e.sketch else return
if e.selection.length == 0:
if hovered < 0:
return
e.selection = [hovered]
e.revision += 1
return
e.clicks.append(e.cursor.copy())
let wanted = 2 if e.tool == "Move/Copy" else 3
if e.clicks.length < wanted:
e.revision += 1
return
let base = e.clicks[0]
if e.tool == "Move/Copy":
sketch.move(e.selection, e.clicks[1][0] - base[0], e.clicks[1][1] - base[1], shift)
else:
let reference = math.hypot(e.clicks[1][0] - base[0], e.clicks[1][1] - base[1])
let target = math.hypot(e.clicks[2][0] - base[0], e.clicks[2][1] - base[1])
if reference > 0.0 and target > 0.0:
sketch.scale(e.selection, base[0], base[1], target / reference)
e.clicks = []
_ = sketch.solve()
e.commit(e.tool)