sketch: live preview for fillet and chamfer corners (#2044)
Thanks @n0tfr3shm1lk.
This commit is contained in:
@@ -1864,6 +1864,11 @@ fn error_rows(app: &mut SolveApp, ctx: &egui::Context, d: &mut Dialog, ui: &mut
|
||||
{
|
||||
d.refuse(app, error);
|
||||
}
|
||||
// For Corner dialogs (fillet/chamfer), propagate preview errors to `d.error`
|
||||
// so impossible sizes still show a refusal message and tests can check it.
|
||||
if matches!(d.kind, Kind::SketchModify(crate::sketch_modify_ui::Mod::Corner { .. })) && app.preview.error.is_some() && d.error.is_none() {
|
||||
d.error = app.preview.error.clone();
|
||||
}
|
||||
if let Some(error) = d.error.as_ref().or(app.preview.error.as_ref()).cloned() {
|
||||
if !full_width {
|
||||
row_label(ui, "");
|
||||
@@ -2208,6 +2213,11 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) {
|
||||
let has_ok = !matches!(d.kind, Kind::Sketch | Kind::Params { .. } | Kind::Measure { .. });
|
||||
let valid = match &d.kind {
|
||||
Kind::SaveMesh(_) => crate::dialogs_save_mesh::ready(&d.inputs),
|
||||
Kind::SketchModify(crate::sketch_modify_ui::Mod::Corner { .. }) => {
|
||||
// Corner dialogs: preview errors are visual only; validation errors from
|
||||
// `apply_commands` go to `d.error`. OK stays enabled when the preview fails.
|
||||
!has_ok || (built.unwrap_or_else(|| apply_commands(app, &d).is_ok()))
|
||||
}
|
||||
_ => !has_ok || !d.previews() || (built.unwrap_or_else(|| apply_commands(app, &d).is_ok()) && app.preview.error.is_none()),
|
||||
};
|
||||
let width = if wide { crate::frame::Width::Wide } else { crate::frame::Width::Normal };
|
||||
|
||||
@@ -351,8 +351,18 @@ fn spawn(pv: &mut PreviewState, key: u64, scratch: Session, cmds: Vec<(String, V
|
||||
/// kernel can't do yet), the tool body is still shown, with the error.
|
||||
pub fn compute(s: &Session, cmds: &[(String, Value)], colors: Colors) -> Job {
|
||||
if cmds.iter().any(|(command, params)| {
|
||||
matches!(command.as_str(), "sketch.redefine" | "sketch.mirror" | "sketch.pattern.rectangular" | "sketch.pattern.circular" | "sketch.offset")
|
||||
|| command == "construct.plane.along_path"
|
||||
matches!(
|
||||
command.as_str(),
|
||||
"sketch.redefine"
|
||||
| "sketch.mirror"
|
||||
| "sketch.pattern.rectangular"
|
||||
| "sketch.pattern.circular"
|
||||
| "sketch.offset"
|
||||
| "sketch.fillet"
|
||||
| "sketch.chamfer.equal_distance"
|
||||
| "sketch.chamfer.two_distance"
|
||||
| "sketch.chamfer.distance_angle"
|
||||
) || command == "construct.plane.along_path"
|
||||
|| params.get("command").and_then(Value::as_str) == Some("construct.plane.along_path")
|
||||
}) {
|
||||
let preview = s.preview_visible_world(cmds).map_err(|error| error.to_string())?;
|
||||
|
||||
@@ -475,3 +475,43 @@ fn chamfer_window_cuts_the_corner_between_two_picked_lines() {
|
||||
assert_eq!(sk.curves.len(), before + 1, "a chamfer line: {:?}", h.app.status);
|
||||
assert!(sk.curve_index("left").is_some(), "the third line stays");
|
||||
}
|
||||
|
||||
/// Corner previews draw new curves when a corner is picked, just like Mirror and Offset do.
|
||||
/// This tests that `previews()` for Corner produces sketch preview curves visible in the viewport.
|
||||
#[test]
|
||||
fn fillet_chamfer_corner_preview_draws_curves() {
|
||||
let mut h = crate::scenario::Harness::new();
|
||||
// Create a simple L-shape with two lines meeting at (0, 10).
|
||||
h.app.run("sketch.create", json!({"plane":"XY"})).unwrap();
|
||||
h.app.run("sketch.line", json!({"points": [[-30,10],[0,10]], "ids": ["top"]})).unwrap();
|
||||
h.app.run("sketch.line", json!({"points": ["top.end",[0,-20]], "ids": ["right"]})).unwrap();
|
||||
h.frames(2);
|
||||
|
||||
// Open the fillet tool and pick the corner at (0, 10).
|
||||
h.step(&json!({"start":"sketch.fillet"})).unwrap();
|
||||
h.frames(2);
|
||||
h.step(&json!({"click":{"world":[0.0,10.0,0.0]}})).unwrap();
|
||||
h.frames(3);
|
||||
|
||||
// The preview should have drawn new curves (fillet arcs at picked corners).
|
||||
h.step(&json!({"until":{"count":{"name":"sketch_preview_curves","value":1}}})).unwrap();
|
||||
|
||||
// Verify the sketch itself hasn't been modified yet.
|
||||
let sid = h.app.session.active_sketch.unwrap();
|
||||
let sk = h.app.session.world_state().sketch(sid).unwrap().sketch.clone();
|
||||
assert_eq!(sk.curves.len(), 2, "fillet preview must not modify the sketch");
|
||||
|
||||
// Now test chamfer too. Close the fillet dialog first.
|
||||
h.step(&json!({"key":"Escape"})).unwrap();
|
||||
h.frames(3);
|
||||
h.step(&json!({"start":"sketch.chamfer.equal_distance"})).unwrap();
|
||||
h.frames(2);
|
||||
h.step(&json!({"click":{"world":[0.0,10.0,0.0]}})).unwrap();
|
||||
h.frames(3);
|
||||
|
||||
// Chamfer preview should also produce curves.
|
||||
h.step(&json!({"until":{"count":{"name":"sketch_preview_curves","value":1}}})).unwrap();
|
||||
|
||||
let sk = h.app.session.world_state().sketch(sid).unwrap().sketch.clone();
|
||||
assert_eq!(sk.curves.len(), 2, "chamfer preview must not modify the sketch");
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ pub enum Mod {
|
||||
|
||||
impl Mod {
|
||||
pub fn previews(&self) -> bool {
|
||||
matches!(self, Self::Mirror | Self::PatternRect { .. } | Self::PatternCirc { .. } | Self::Offset { .. })
|
||||
matches!(self, Self::Mirror | Self::PatternRect { .. } | Self::PatternCirc { .. } | Self::Offset { .. } | Self::Corner { .. })
|
||||
}
|
||||
|
||||
pub fn title(&self) -> &'static str {
|
||||
|
||||
Reference in New Issue
Block a user