//! Tool windows for sketch-space modify commands, on the `dialogs_surface` pattern (#19): one //! `Mod` enum behind one `Kind::SketchModify` variant, grown by one variant and one arm per //! command rather than by a new module each time. Scale came first, whose toolbar button //! reached no tool and no dialog at all, so a press ran `sketch.scale` with no parameters and //! the engine answered "`factor` must be a positive number". Offset joins it here (upstream //! issue #162 item 1: "Offset does not create a constraint, the size of the offset seems //! random"), and it now offers the same One Side, Two Sides and Symmetric directions //! `solid.extrude` already names. Mirror joins them here, which ran as a bare click flow: entities until a //! right-click, then one more click for the line, with nothing on screen saying what was //! picked. Move joins them too, which had no toolbar placement, no tool and no dialog: its only //! route into the UI was the sketch context menu, and that went through the same //! empty-parameter fallback and reported an engine error. `Mod::Corner` joins them here, //! serving `sketch.fillet` and the three `sketch.chamfer.*` commands together, the way //! `solid.fillet` and `solid.chamfer` already share one window. Before it, a click committed a //! size guessed at 20 percent of the shorter leg and then opened a bare parameter editor, so //! the design changed before any number was asked for. `sketch.chamfer.distance_angle` sent a //! hard-coded 45 degrees and `sketch.chamfer.two_distance` forced its second distance equal to //! the first, leaving both values unreachable from the UI. The rectangular pattern joins them //! too, whose click tool sent a hard-coded count of 3 and could reach neither the command's //! second count nor either spacing, so a user who wanted five instances got three and deleted //! two. The circular pattern is the same gap and joins this module later. //! //! The equation curve (#8352) has no picks: its window is the form for the formulas, the range and //! the tolerance, and reopens on an existing equation curve to edit them. //! //! The Chamfer Type choice is a row of icon buttons rather than a drop-down: the modes differ //! in shape, and a picture of each shape names it better than a line of text. use egui::Pos2; use serde_json::{Map, Value, json}; use solidcore_record::{Vec2, Vec3}; use solidcraft_engine::Sel; use solidcraft_engine::doc::Document; use solidcraft_engine::doc::expr::Kind as ValueKind; use solidcraft_engine::geom::prelude::*; use solidcraft_engine::sketch::{PatternAlignment, PatternDistance, VariableUnit}; use crate::SolveApp; use crate::dialogs::{CHAMFER_TYPES, Dialog, Kind}; use crate::selection::{AXES, CURVES, PROFILES, SKETCH_POINTS, SelInput}; use crate::tool_modifiers::{Audit, Scope, ToolModifiers}; use crate::ui_kit; use crate::viewport::{Hit, Proj}; const SELECT_MANIP: &[Scope] = &[Scope::Selection, Scope::Manipulator, Scope::FeaturePreview]; const SELECT_ONLY: &[Scope] = &[Scope::Selection, Scope::FeaturePreview]; /// Per-tool modifier descriptors owned by this host file. pub const MODIFIERS: &[ToolModifiers] = &[ ToolModifiers { tool: "sketch.scale", scopes: SELECT_ONLY, bindings: &[], audit: Audit::VerifiedNoOp }, ToolModifiers { tool: "sketch.offset", scopes: SELECT_MANIP, bindings: &[], audit: Audit::VerifiedNoOp }, ToolModifiers { tool: "sketch.mirror", scopes: SELECT_ONLY, bindings: &[], audit: Audit::VerifiedNoOp }, // Sketch Move: dialog/manipulator inherit only - no invented Alt-copy. ToolModifiers { tool: "sketch.move", scopes: SELECT_MANIP, bindings: &[], audit: Audit::VerifiedNoOp }, ToolModifiers { tool: "sketch.transform", scopes: SELECT_MANIP, bindings: &[], audit: Audit::VerifiedNoOp }, // Pattern spacing/count live in the dialog - no viewport modifier chords. ToolModifiers { tool: "sketch.pattern.rectangular", scopes: SELECT_ONLY, bindings: &[], audit: Audit::VerifiedNoOp }, ToolModifiers { tool: "sketch.pattern.circular", scopes: SELECT_ONLY, bindings: &[], audit: Audit::VerifiedNoOp }, ToolModifiers { tool: "sketch.fillet", scopes: SELECT_ONLY, bindings: &[], audit: Audit::VerifiedNoOp }, ToolModifiers { tool: "sketch.chamfer.equal_distance", scopes: SELECT_ONLY, bindings: &[], audit: Audit::VerifiedNoOp }, ToolModifiers { tool: "sketch.chamfer.two_distance", scopes: SELECT_ONLY, bindings: &[], audit: Audit::VerifiedNoOp }, ToolModifiers { tool: "sketch.chamfer.distance_angle", scopes: SELECT_ONLY, bindings: &[], audit: Audit::VerifiedNoOp }, // Formulas only: no picks and no viewport modifier chords. ToolModifiers { tool: "sketch.equation_curve", scopes: SELECT_ONLY, bindings: &[], audit: Audit::VerifiedNoOp }, // Divide Curve: curves and a count; no viewport modifier chords. ToolModifiers { tool: "sketch.point.divide", scopes: SELECT_ONLY, bindings: &[], audit: Audit::VerifiedNoOp }, ]; /// The equation curve's two forms: x(t) and y(t), or y = f(x). pub(crate) const EQUATION_FORMS: [&str; 2] = ["Parametric", "Explicit"]; /// The three chamfer types, drawn as the cut each one makes, in `CHAMFER_TYPES` order. const CHAMFER_TYPE_ICONS: [&str; 3] = ["sketch_chamfer_equal", "sketch_chamfer_two", "sketch_chamfer_angle"]; /// The engine's own cap on the copies one sketch pattern may make. const MAX_COPIES: f64 = 500.0; /// The engine's own cap on the pieces Divide Curve cuts one curve into. const DIVIDE_MAX: i64 = 500; /// The engine's own cap on the offsets `sketch.offset` stacks one behind the other. const MAX_OFFSET_INSTANCES: i64 = 100; /// The offset directions, in the words and the order `solid.extrude` already uses for them. /// One Side offsets the way the distance points. Two Sides adds an independent distance the /// other way. Symmetric applies the one distance each way. pub(crate) const OFFSET_MODES: [&str; 3] = ["One Side", "Two Sides", "Symmetric"]; const OFFSET_MODE_ICONS: [&str; 3] = ["offset_one_side", "offset_two_sides", "offset_symmetric"]; /// Index of Two Sides in [`OFFSET_MODES`] (the mode with a second distance). const TWO_SIDES: usize = 1; /// Index of Symmetric in [`OFFSET_MODES`]. const SYMMETRIC: usize = 2; /// Whether an offset mode index is Symmetric (which hides Flip and offsets both ways). pub(crate) fn offset_symmetric(mode: usize) -> bool { mode == SYMMETRIC } /// Whether an offset mode index is Two Sides (Distance and an independent Distance 2). pub(crate) fn offset_two_sides(mode: usize) -> bool { mode == TWO_SIDES } /// A sketch modify tool window's values. #[derive(Clone, Debug, PartialEq)] pub enum Mod { /// `sketch.scale`: the factor the picked entities scale by. The base point is the second /// input; with none picked the command scales about the sketch origin, as its own default /// already does. Scale { factor: String }, /// `sketch.offset`: the curves to offset, the distance typed and whether the connected /// chain follows. A positive distance is left of the chain, as the command defines it; /// `flip` reverses whatever sign the field holds. `mode` indexes [`OFFSET_MODES`], and /// `distance2` is the other side's distance that Two Sides asks for. `instances` is how many /// offsets stand one behind the other, each the same distance again from the one before it: /// one, the offset by itself, by default. Offset { distance: String, distance2: String, instances: String, mode: usize, chain: bool, flip: bool }, /// `sketch.mirror`: the entities and the line they reflect in. The command has no number of /// its own, so the window's whole job is to show what is picked and let a pick be changed /// before anything is committed. Mirror, /// `sketch.move`: how far the picked entities move along the sketch's own axes, how far they /// turn about the centre, and whether the originals stay behind. The command already /// carries the copy flag, so the Create Copy row needs no engine change. In a 3D sketch /// (`three_d`, taken when the window opens) `z` moves them off the plane along its normal. Move { x: String, y: String, z: String, angle: String, copy: bool, three_d: bool }, /// `sketch.transform`: the picked entities scaled, turned and moved as one, about the picked /// Center point or else the middle of their box. `x` and `y` move that point, `factor` scales /// and `angle` turns about it. /// `frame` is the box (middle and size in the sketch, kept up to date from the picks by /// `sketch_transform_ui`) the window's size rows and the handles work on. Transform { x: String, y: String, factor: String, angle: String, frame: Option<([f64; 2], [f64; 2])> }, /// `sketch.pattern.rectangular`: how many instances along the picked direction and how far /// apart, then the same along a second picked direction (perpendicular by default). /// Each direction's distance type says which of its Quantity, Spacing, Extent and Spacing /// List it is set by, as a solid Rectangular Pattern's does (#23878); the values a type /// doesn't use stay in the window, so switching back finds them. PatternRect { count: String, spacing: String, count2: String, spacing2: String, alignment: PatternAlignment, alignment2: PatternAlignment, reversed: bool, reversed2: bool, distance: PatternDistance, distance2: PatternDistance, extent: String, extent2: String, /// The spacing list as typed, its gaps separated by semicolons. list: String, list2: String, copy_at_end: bool, copy_at_end2: bool, }, /// `sketch.pattern.circular`: how many instances around the picked centre, and the total /// angle they span. PatternCirc { count: String, angle: String, alignment: PatternAlignment, reversed: bool }, /// `sketch.fillet` and the three `sketch.chamfer.*` commands. `chamfer` chooses between a /// fillet and a chamfer and never changes while the window is open; `ctype` chooses which /// of the three chamfer commands runs (0 equal distance, 1 two distances, 2 distance and /// angle). `size` is the radius or the first distance, `distance2` the second distance and /// `angle` the angle from the first line. `flip` names the two lines in the other order, so /// the first distance lands on the other leg of the corner. For a fillet of two parallel /// lines, `flip` joins them at the other end (the half circle's side). Corner { chamfer: bool, ctype: usize, size: String, distance2: String, angle: String, flip: bool }, /// `sketch.equation_curve` (or `.edit` on the curve the window's `curve` extra names): the /// form (an index into [`EQUATION_FORMS`]), x(t) and y(t) (or y(x)), the variable's range and /// how far the curve may stray from the formulas. Equation { form: usize, x: String, y: String, start: String, end: String, tolerance: String, range_unit: bool, radians_trig: bool }, /// `sketch.point.divide` (#8969): the curves picked are each divided into `count` pieces of equal /// length by points held at their shares of it. Divide { count: String }, } impl Mod { pub fn previews(&self) -> bool { matches!( self, Self::Mirror | Self::Move { .. } | Self::PatternRect { .. } | Self::PatternCirc { .. } | Self::Offset { .. } | Self::Corner { .. } | Self::Divide { .. } ) } /// The length a rectangular pattern's second drag arrow sets (#2462): what /// [`Mod::primary`] gives for the first direction, for the second. pub fn pattern_length2(&mut self) -> Option<&mut String> { match self { Mod::PatternRect { distance2: PatternDistance::CountExtent, extent2, .. } => Some(extent2), Mod::PatternRect { distance2: PatternDistance::CountList, .. } => None, Mod::PatternRect { spacing2, .. } => Some(spacing2), _ => None, } } /// Whether a rectangular pattern's drag arrow may go below zero: a spacing does, reversing /// the direction (#5497); an extent is a length the command wants above zero. pub fn pattern_arrow_signed(&self, second: bool) -> bool { match self { Mod::PatternRect { distance, distance2, .. } => !(if second { distance2 } else { distance }).uses_extent(), _ => true, } } pub fn title(&self) -> &'static str { match self { Mod::Scale { .. } => "SKETCH SCALE", Mod::Offset { .. } => "OFFSET", Mod::Mirror => "MIRROR", Mod::Move { .. } => "MOVE/COPY", Mod::Transform { .. } => "TRANSFORM", Mod::PatternRect { .. } => "RECTANGULAR PATTERN", Mod::PatternCirc { .. } => "CIRCULAR PATTERN", Mod::Corner { chamfer: false, .. } => "FILLET", Mod::Corner { .. } => "CHAMFER", Mod::Equation { .. } => "EQUATION CURVE", Mod::Divide { .. } => "DIVIDE CURVE", } } /// The value on the canvas. A move shows its `move_field` (see /// [`crate::dialogs_move::value_field`]). pub fn primary(&mut self, move_field: usize) -> Option<(&'static str, ValueKind, &mut String)> { match self { Mod::Scale { factor } => Some(("Factor", ValueKind::Unitless, factor)), Mod::Offset { distance, .. } => Some(("Distance", ValueKind::Length, distance)), Mod::Move { x, y, z, angle, .. } => Some(match move_field { 1 => ("Y Distance", ValueKind::Length, y), 2 => ("Z Distance", ValueKind::Length, z), 5 => ("Angle", ValueKind::Angle, angle), _ => ("X Distance", ValueKind::Length, x), }), // The handles in the viewport are its manipulator; the window has the numbers. Mod::Transform { .. } => None, Mod::Mirror => None, // The length the first direction's distance type shows: its Extent when that alone // sets the gaps, else its Spacing. A spacing list has no one length to drag. Mod::PatternRect { distance: PatternDistance::CountExtent, extent, .. } => Some(("ui.pattern_extent", ValueKind::Length, extent)), Mod::PatternRect { distance: PatternDistance::CountList, .. } => None, Mod::PatternRect { spacing, .. } => Some(("Spacing", ValueKind::Length, spacing)), Mod::PatternCirc { angle, .. } => Some(("Angle", ValueKind::Angle, angle)), Mod::Corner { chamfer: false, size, .. } => Some(("Radius", ValueKind::Length, size)), Mod::Corner { size, .. } => Some(("Distance", ValueKind::Length, size)), Mod::Equation { .. } | Mod::Divide { .. } => None, } } } fn inputs(k: &Mod) -> Vec { match k { Mod::Scale { .. } => { vec![SelInput::new("Entities", CURVES | SKETCH_POINTS | PROFILES, true), SelInput::new("Base point", SKETCH_POINTS, false)] } Mod::Offset { .. } => vec![SelInput::new("Curves", CURVES, true)], // AXES is the mask that takes a straight sketch curve (`selection::SelInput::accepts`). Mod::Mirror => vec![SelInput::new("Entities", CURVES | SKETCH_POINTS | PROFILES, true), SelInput::new("Mirror line", AXES, false)], // From and To (#21235): with both picked, the entities move by To - From (Point to Point). Mod::Move { .. } => vec![ SelInput::new("Entities", CURVES | SKETCH_POINTS | PROFILES, true), SelInput::new("Center", SKETCH_POINTS, false), SelInput::new("From", SKETCH_POINTS, false), SelInput::new("To", SKETCH_POINTS, false), ], // Center: the point Scale and Angle work about (#11718); empty, the middle of the picks' box. Mod::Transform { .. } => { vec![SelInput::new("Entities", CURVES | SKETCH_POINTS | PROFILES, true), SelInput::new("Center", SKETCH_POINTS, false)] } // The direction is optional: `sketch.pattern.rectangular` documents sketch x as its // default, the way the engine reads it. Mod::PatternRect { .. } => vec![ SelInput::new("Entities", CURVES | SKETCH_POINTS | PROFILES, true), SelInput::new("Direction", AXES, false), SelInput::new("Direction 2", AXES, false), ], // The centre is required: `sketch.pattern.circular` has no default for it. Mod::PatternCirc { .. } => { vec![SelInput::new("Entities", CURVES | SKETCH_POINTS | PROFILES, true), SelInput::new("Center", SKETCH_POINTS, false)] } // A fillet rounds any number of corners to one radius, and a chamfer cuts any number // alike (`points`). Two lines name the corner between them where more lines meet at its // point (#1824), for both. Mod::Corner { .. } => vec![SelInput::new("Corners", SKETCH_POINTS | CURVES, true)], Mod::Equation { .. } => Vec::new(), Mod::Divide { .. } => vec![SelInput::new("Curves", CURVES, true)], } } /// The dialog for a sketch modify command. pub fn start(app: &SolveApp, id: &str) -> Option<(Kind, Vec)> { let corner = |chamfer: bool, ctype: usize| Mod::Corner { chamfer, ctype, size: crate::dialogs::length_default(&app.session, "2"), distance2: crate::dialogs::length_default(&app.session, "2"), angle: "45 deg".into(), flip: false, }; let k = match id { "sketch.scale" => Mod::Scale { factor: "2".into() }, "sketch.offset" => Mod::Offset { distance: crate::dialogs::length_default(&app.session, "2"), distance2: crate::dialogs::length_default(&app.session, "2"), instances: "1".into(), mode: 0, chain: true, flip: false, }, "sketch.mirror" => Mod::Mirror, "sketch.transform" => Mod::Transform { x: crate::dialogs::length_default(&app.session, "0"), y: crate::dialogs::length_default(&app.session, "0"), factor: "1".into(), angle: "0 deg".into(), frame: None, }, "sketch.move" => Mod::Move { x: crate::dialogs::length_default(&app.session, "0"), y: crate::dialogs::length_default(&app.session, "0"), z: crate::dialogs::length_default(&app.session, "0"), angle: "0 deg".into(), copy: false, three_d: crate::sketch3d::active(app), }, "sketch.pattern.rectangular" => Mod::PatternRect { count: "3".into(), spacing: crate::dialogs::length_default(&app.session, "20"), count2: "1".into(), spacing2: crate::dialogs::length_default(&app.session, "20"), alignment: PatternAlignment::Right, alignment2: PatternAlignment::Right, reversed: false, reversed2: false, distance: PatternDistance::default(), distance2: PatternDistance::default(), extent: crate::dialogs::length_default(&app.session, "40"), extent2: crate::dialogs::length_default(&app.session, "40"), list: String::new(), list2: String::new(), copy_at_end: false, copy_at_end2: false, }, "sketch.pattern.circular" => { Mod::PatternCirc { count: "6".into(), angle: "360 deg".into(), alignment: PatternAlignment::Right, reversed: false } } "sketch.fillet" => corner(false, 0), "sketch.chamfer.equal_distance" => corner(true, 0), "sketch.chamfer.two_distance" => corner(true, 1), "sketch.chamfer.distance_angle" => corner(true, 2), // A sine wave to start from: one period, 40 wide and 10 high in the design's unit. "sketch.equation_curve" => Mod::Equation { form: 0, x: "t / 9".into(), y: "5 * sin(t)".into(), start: "0".into(), end: "360".into(), tolerance: "0.01 mm".into(), range_unit: false, radians_trig: false, }, "sketch.point.divide" => Mod::Divide { count: "4".into() }, _ => return None, }; let i = inputs(&k); Some((Kind::SketchModify(k), i)) } /// Reopen the equation curve window on curve `id` of the sketch being edited, with its formulas. /// OK runs `sketch.equation_curve.edit` on it. pub fn edit_equation(app: &mut SolveApp, id: &str) -> bool { let Some(sid) = app.session.active_sketch else { return false }; let state = crate::open_meshing::session_world(&app.session); let Some(eq) = state.sketch(sid).and_then(|s| s.sketch.equation_of(id)).cloned() else { return false }; let kind = Mod::Equation { form: usize::from(eq.x.is_none()), x: eq.x.clone().unwrap_or_else(|| "t".into()), y: eq.y, start: eq.start, end: eq.end, tolerance: format!("{} mm", eq.tolerance), range_unit: eq.variable_unit == VariableUnit::Range, radians_trig: eq.radians_trig, }; let mut dialog = Dialog::new(Kind::SketchModify(kind), Vec::new()); dialog.extra.insert("curve".into(), json!(id)); app.tool = None; app.dialog = Some(dialog); true } /// Merge Lines (#20317): the selected lines become one. It has no picks of its own: with fewer /// than two lines selected, the status bar says what to select. pub fn merge_selected_lines(app: &mut SolveApp) { let curves: Vec = app.session.selection.iter().filter_map(|s| if let Sel::SketchCurve { id, .. } = s { Some(id.clone()) } else { None }).collect(); if curves.len() < 2 { let hint = tr!(app, "ui.merge_lines_pick").to_owned(); app.set_status(hint, false); return; } if let Err(e) = app.run("sketch.merge_lines", json!({ "curves": curves })) { app.set_status(e, true); } } /// The equation curve the selection is, when it is one curve that has formulas. pub(crate) fn selected_equation(app: &SolveApp) -> Option { let [Sel::SketchCurve { id, .. }] = app.session.selection.as_slice() else { return None }; let sid = app.session.active_sketch?; let state = crate::open_meshing::session_world(&app.session); state.sketch(sid)?.sketch.equation_of(id).map(|e| e.curve.clone()) } /// What a click on `hit` picks in a sketch modify window: what the active input takes /// ([`candidate`]), else what the Entities list takes ([`entities_candidate`]). pub(crate) fn pick_candidate(d: &Dialog, session: &solidcraft_engine::Session, hit: &Hit) -> Option { candidate(d, session, hit).or_else(|| entities_candidate(d, session, hit)) } /// The selection a hit adds to the Entities list although another input is active: the commands /// whose first input is that shared list (Mirror, Move/Copy, Scale, Transform, the patterns) take /// a curve, point or region the active input refuses there, so one clicked after the window /// moved on to the mirror line, centre or direction joins the others instead of being dropped /// without a word (#11700). fn entities_candidate(d: &Dialog, session: &solidcraft_engine::Session, hit: &Hit) -> Option { let active = session.active_sketch?; let shared = matches!( &d.kind, Kind::SketchModify( Mod::Scale { .. } | Mod::Mirror | Mod::Move { .. } | Mod::Transform { .. } | Mod::PatternRect { .. } | Mod::PatternCirc { .. } ) ); if !shared || d.active == 0 { return None; } if let Hit::SketchCurve { sketch, .. } | Hit::SketchPoint { sketch, .. } | Hit::Profile { sketch, .. } = hit && *sketch != active { return None; } d.inputs.first().filter(|input| input.multi)?.accepts(hit, Some(active), |_, _| false) } /// Whether a click on `hit` goes to the Entities list rather than the active input (see /// [`entities_candidate`]). pub(crate) fn picks_entity(d: &Dialog, session: &solidcraft_engine::Session, hit: &Hit) -> bool { candidate(d, session, hit).is_none() && entities_candidate(d, session, hit).is_some() } /// What the active input of a sketch modify window takes. Sketch modify inputs accept only the /// sketch being edited. Origin axes remain valid rectangular-pattern directions, while Hole /// continues using its separate POINTS mask. pub fn candidate(d: &Dialog, session: &solidcraft_engine::Session, hit: &Hit) -> Option { let active = session.active_sketch?; match hit { Hit::SketchCurve { sketch, .. } | Hit::SketchPoint { sketch, .. } | Hit::Profile { sketch, .. } if *sketch != active => None, Hit::SketchCurve { id, .. } if matches!(d.kind, Kind::SketchModify(Mod::Corner { .. })) => { let state = crate::open_meshing::session_world(session); let sk = &state.sketch(active)?.sketch; // Fillet rounds the corner of two drawn 3D lines (#21319); it takes no other 3D // curve, and Chamfer takes none. let line_3d = matches!(d.kind, Kind::SketchModify(Mod::Corner { chamfer: false, .. })) && sk.is_drawn_line(id); if !line_3d { if sk.is_spatial_corner_curve(id) { return None; } sk.curve_index(id)?; } d.active_input()?.accepts(hit, Some(active), |_, _| false) } // Once the picked curves close a shape, Offset takes no curve that meets it at a corner: // such a curve neither highlights nor picks. A picked curve still does, so clicking it // opens the shape again, and so does a curve of a shape apart from the closed one. Hit::SketchCurve { id, .. } if matches!(d.kind, Kind::SketchModify(Mod::Offset { .. })) => { let follow = matches!(d.kind, Kind::SketchModify(Mod::Offset { chain: true, .. })); let state = crate::open_meshing::session_world(session); if solidcraft_engine::cmd::offset_leaves_out(&state.sketch(active)?.sketch, &curve_ids(&d.inputs, 0), id, follow) { return None; } d.active_input()?.accepts(hit, Some(active), |_, _| false) } _ => d.active_input()?.accepts(hit, Some(active), |_, _| false), } } /// Reopen the same pattern window with its stored parameters. OK reaches the same engine /// command with the pattern id, so previews and programmatic edits share the implementation. pub fn edit_pattern(app: &mut SolveApp, id: &str) -> bool { use solidcraft_engine::sketch::SketchPatternKind; let Some(sid) = app.session.active_sketch else { return false }; let state = crate::open_meshing::session_world(&app.session); let Some(sk) = state.sketch(sid).map(|s| &s.sketch) else { return false }; let Some(pattern) = sk.patterns.iter().find(|p| p.id == id) else { return false }; let input = |key: &str, fallback: String| pattern.expressions.get(key).cloned().unwrap_or(fallback); let kind = match &pattern.kind { SketchPatternKind::Circular { count, angle, alignment, reversed, .. } => Mod::PatternCirc { count: pattern.count_expression.clone().unwrap_or_else(|| input("count", count.to_string())), angle: input("angle", format!("{angle} deg")), alignment: if *alignment == PatternAlignment::Left { PatternAlignment::Right } else { *alignment }, reversed: *reversed ^ (*alignment == PatternAlignment::Left), }, SketchPatternKind::Rectangular { count, spacing, count2, spacing2, alignment, alignment2, reversed, reversed2, row, row2, .. } => { Mod::PatternRect { distance: row.distance, distance2: row2.distance, extent: row.extent.clone().unwrap_or_else(|| crate::dialogs::length_default(&app.session, "40")), extent2: row2.extent.clone().unwrap_or_else(|| crate::dialogs::length_default(&app.session, "40")), list: row.spacing_list.join("; "), list2: row2.spacing_list.join("; "), copy_at_end: row.copy_at_end, copy_at_end2: row2.copy_at_end, count: input("count", count.to_string()), spacing: input("spacing", format!("{spacing} mm")), count2: input("count2", count2.to_string()), spacing2: input("spacing2", format!("{spacing2} mm")), alignment: if *alignment == PatternAlignment::Left { PatternAlignment::Right } else { *alignment }, alignment2: if *alignment2 == PatternAlignment::Left { PatternAlignment::Right } else { *alignment2 }, reversed: *reversed ^ (*alignment == PatternAlignment::Left), reversed2: *reversed2 ^ (*alignment2 == PatternAlignment::Left), } } }; let mut dialog = Dialog::new(Kind::SketchModify(kind.clone()), inputs(&kind)); if let Some(input) = dialog.inputs.first_mut() { input.items = pattern .entities .iter() .map(|id| { if sk.curve_index(id).is_some() { Sel::SketchCurve { id: id.clone(), sketch: None } } else { Sel::SketchPoint { id: id.clone() } } }) .collect(); } match &pattern.kind { SketchPatternKind::Circular { center, .. } => { if let Some(input) = dialog.inputs.get_mut(1) { input.items = vec![Sel::SketchPoint { id: center.clone() }]; } } SketchPatternKind::Rectangular { dir, dir2, direction, direction2, .. } => { dialog.extra.insert("dir".into(), json!(dir)); dialog.extra.insert("dir2".into(), json!(dir2)); for (index, id) in [(1, direction), (2, direction2)] { if let Some(id) = id && let Some(input) = dialog.inputs.get_mut(index) { input.items = vec![Sel::SketchCurve { id: id.clone(), sketch: None }]; } } } } dialog.extra.insert("pattern".into(), json!(id)); app.tool = None; app.dialog = Some(dialog); true } pub fn double_click_pattern(app: &mut SolveApp, pos: Pos2) -> bool { let Some(glyph) = crate::sketch_tools::glyph_at(pos) else { return false }; let Some(sid) = app.session.active_sketch else { return false }; let id = crate::open_meshing::session_world(&app.session) .sketch(sid) .and_then(|ss| ss.sketch.patterns.iter().find(|p| p.instances.iter().any(|i| i.constraints.contains(&glyph))).map(|p| p.id.clone())); id.is_some_and(|id| edit_pattern(app, &id)) } /// Double-click a sketch curve to add its whole connected chain to the shared Entities input of /// Mirror, Move/Copy, Scale and the two patterns, as a profile click adds a whole closed region /// (#8390). The single-curve pick of the double-click's two clicks cancels itself out (each click /// toggles the same curve), so the chain toggle lands on the state before the double-click. pub fn double_click_entities(app: &mut SolveApp) -> bool { let Some(Hit::SketchCurve { sketch, id, .. }) = app.viewport.hover.clone() else { return false; }; let Some(dialog) = app.dialog.as_mut() else { return false; }; // Sweep paths accept a chain from a sketch other than the profile sketch. if matches!(dialog.kind, Kind::Sweep { .. } | Kind::Surface(crate::dialogs_surface::Surf::Sweep(_))) { if dialog.active != 1 || app.viewport.hover.as_ref().and_then(|hit| dialog.candidate_on(&app.session, hit, None)).is_none() { return false; } // The sketch is finished, so the editing-only sketch.chain command is // unavailable. Use the same chain traversal on the picked sketch. let Ok(sk) = app.session.doc.sketch(sketch) else { return false; }; let Some(index) = sk.curve_index(&id) else { return false; }; let Some(input) = dialog.inputs.get_mut(1) else { return false; }; let items: Vec = sk .chain(index) .into_iter() .filter_map(|c| sk.curves.get(c)) .filter(|c| !c.construction) .map(|c| crate::selection::curve_pick(&c.id, sketch, app.session.active_sketch)) .filter(|s| !input.rejects(s)) .collect(); if items.is_empty() { return false; } input.toggle(items); return true; } // Project takes the curves of the other sketches, never the one being edited: the chain // joined to the curve goes into Geometry as one toggle, as for the Entities inputs below. if matches!(dialog.kind, Kind::SketchProject { .. }) { if app.session.active_sketch == Some(sketch) || dialog.active != 0 { return false; } let Ok(v) = app.session.execute("sketch.chain", &json!({ "sketch": sketch, "curve": id })) else { return false; }; let Some(input) = dialog.inputs.get_mut(0) else { return false; }; let items: Vec = v["curves"] .as_array() .into_iter() .flatten() .filter_map(|c| c.as_str()) .map(|id| Sel::ExternalSketchCurve { sketch, id: id.to_string() }) .filter(|s| !input.rejects(s)) .collect(); if items.is_empty() { return false; } input.toggle(items); return true; } if app.session.active_sketch != Some(sketch) { return false; } // Only the commands whose first input is the shared Entities list take a chain here; Offset // has its own Chain option, and the corner tools take one corner at a time. let entities = matches!( &dialog.kind, Kind::SketchModify( Mod::Scale { .. } | Mod::Mirror | Mod::Move { .. } | Mod::Transform { .. } | Mod::PatternRect { .. } | Mod::PatternCirc { .. } ) ); if !entities || dialog.active != 0 { return false; } let Ok(v) = app.session.execute("sketch.chain", &json!({ "curve": id })) else { return false; }; let items: Vec = v["curves"] .as_array() .into_iter() .flatten() .filter_map(|c| c.as_str()) .map(|id| Sel::SketchCurve { id: id.to_string(), sketch: None }) .collect(); if items.is_empty() { return false; } if let Some(input) = dialog.inputs.get_mut(0) { input.toggle(items); } true } /// Place a circular pattern centre in empty sketch space without creating a document point. pub fn pick_position(d: &mut Dialog, session: &solidcraft_engine::Session, point: Vec3) -> bool { if !matches!(d.kind, Kind::SketchModify(Mod::PatternCirc { .. })) || d.active != 1 { return false; } d.pick(session, Sel::Vertex { body: String::new(), point }); true } /// Command metadata for the active variant, including an explicitly chosen chamfer type. pub(crate) fn command(k: &Mod) -> &'static str { match k { Mod::Scale { .. } => "sketch.scale", Mod::Offset { .. } => "sketch.offset", Mod::Mirror => "sketch.mirror", Mod::Move { .. } => "sketch.move", Mod::Transform { .. } => "sketch.transform", Mod::PatternRect { .. } => "sketch.pattern.rectangular", Mod::PatternCirc { .. } => "sketch.pattern.circular", Mod::Corner { chamfer: false, .. } => "sketch.fillet", Mod::Corner { ctype: 1, .. } => "sketch.chamfer.two_distance", Mod::Corner { ctype: 2, .. } => "sketch.chamfer.distance_angle", Mod::Corner { .. } => "sketch.chamfer.equal_distance", Mod::Equation { .. } => "sketch.equation_curve", Mod::Divide { .. } => "sketch.point.divide", } } /// Repeatable operations offer Apply for another selection, with the same typed values. pub(crate) fn repeatable(k: &Mod) -> bool { matches!(k, Mod::Offset { .. } | Mod::Mirror | Mod::PatternRect { .. } | Mod::PatternCirc { .. } | Mod::Divide { .. }) } /// A labelled value row. The value carries its own unit text ("2 mm", "45 deg"). fn number(ui: &mut egui::Ui, label: &str, value: &mut String) -> bool { ui_kit::row(ui, label, |ui| ui_kit::field(ui, value)) } /// A pattern quantity row with step arrows. They keep the count from `min` up to `max` /// copies, evaluated against the design's parameters as `whole` evaluates it on OK. fn quantity(ui: &mut egui::Ui, doc: &Document, label: &str, value: &mut String, min: i64, max: i64) -> bool { let eval = |text: &str| doc.eval(text.trim(), ValueKind::Unitless).ok(); ui_kit::count_row(ui, label, value, &ui_kit::Count { min, max: max.max(min), eval: &eval }) } /// How many copies the other direction of a rectangular pattern leaves this one, so that /// both together stay within `MAX_COPIES`. All of them while the other count is not a number. fn copies_left(doc: &Document, other: &str) -> i64 { let other = doc.eval(other.trim(), ValueKind::Unitless).ok().filter(|v| v.is_finite() && *v >= 1.0).map_or(1.0, f64::round); (MAX_COPIES / other).floor() as i64 } fn icons(ui: &mut egui::Ui, label: &str, labels: &[&'static str], icons: &[&'static str], selected: &mut usize) { ui_kit::row(ui, label, |ui| { let choices: Vec<_> = labels .iter() .zip(icons) .map(|(key, icon)| ui_kit::IconSegment { key, icon, tip: solidcraft_ui_text::tr_label(key).to_string() }) .collect(); ui_kit::segmented_icons(ui, selected, &choices, &[]); }); } fn alignment(ui: &mut egui::Ui, label: &str, alignment: &mut PatternAlignment, reversed: &mut bool) { ui_kit::row(ui, label, |ui| { ui_kit::toggle_icon(ui, label, "Flip direction", reversed, "pattern_flip"); let mut centered = *alignment == PatternAlignment::Center; ui_kit::toggle_icon(ui, label, "Center", &mut centered, "pattern_center"); *alignment = if centered { PatternAlignment::Center } else { PatternAlignment::Right }; }); } /// One direction's group in the sketch Rectangular Pattern window: the message ids of its rows /// (Quantity, Spacing, Alignment, Distance Type, Extent, Spacing List, Copy at the end) and how /// many copies the other direction leaves it. struct PatternRowUi { key: &'static str, title: &'static str, labels: [&'static str; 7], type_key: &'static str, left: i64, } /// The values of one direction of the sketch Rectangular Pattern window. struct PatternRowFields<'a> { distance: &'a mut PatternDistance, count: &'a mut String, spacing: &'a mut String, extent: &'a mut String, list: &'a mut String, copy_at_end: &'a mut bool, alignment: &'a mut PatternAlignment, reversed: &'a mut bool, } impl PatternRowUi { /// The direction's Distance Type, then the values that type takes, in the order the solid /// Rectangular Pattern shows them (#23878), then its Alignment. True when a field accepts /// Enter. fn show(&self, ui: &mut egui::Ui, doc: &Document, fields: PatternRowFields<'_>) -> bool { let PatternRowFields { distance, count, spacing, extent, list, copy_at_end, alignment: align, reversed } = fields; let [quantity_label, spacing_label, align_label, type_label, extent_label, list_label, end_label] = self.labels; ui_kit::section(ui, self.key, self.title, true, |ui| { let mut enter = false; let types = crate::dialogs::PATTERN_DISTANCES; let mut chosen = types.iter().position(|(kind, _)| kind == distance).unwrap_or(0); ui_kit::choice_row(ui, type_label, self.type_key, &mut chosen, &types.map(|(_, label)| label)); *distance = types.get(chosen).map(|(kind, _)| *kind).unwrap_or_default(); if distance.uses_count() { enter |= quantity(ui, doc, quantity_label, count, 1, self.left); } if distance.uses_spacing() { enter |= number(ui, spacing_label, spacing); } if distance.uses_extent() { enter |= number(ui, extent_label, extent); } if distance.uses_list() { let row = ui.scope(|ui| ui_kit::row(ui, list_label, |ui| ui_kit::hint_field(ui, list, "10 mm; 20 mm; 30 mm"))); row.response.on_hover_text(solidcraft_ui_text::tr_by_id("ui.pattern_spacing_list_tip")); enter |= row.inner; } if *distance == PatternDistance::SpacingExtent { ui_kit::checkbox_grid(ui, &mut [(end_label, copy_at_end)], 1); } alignment(ui, align_label, align, reversed); enter }) .unwrap_or(false) } } fn pattern_hint(ui: &mut egui::Ui) { ui_kit::text( ui, solidcraft_ui_text::tr_label("Counts include the original. Center balances copies; Flip chooses the extra side for even totals."), ui_kit::Text::Note("pattern"), ); } /// Dense rows after the selection inputs; true when an expression field accepts Enter. /// `doc` evaluates the pattern counts the step arrows change. /// Whether the window's picks are exactly two parallel lines of the sketch being edited: only then /// does a Fillet join them with a half circle and its "Other end" option mean anything (#16238). pub(crate) fn picks_two_parallel_lines(app: &SolveApp, inputs: &[SelInput]) -> bool { let ids = line_ids(inputs, 0); let [a, b] = ids.as_slice() else { return false }; let Some(sid) = app.session.active_sketch else { return false }; let state = crate::open_meshing::session_world(&app.session); let Some(sk) = state.sketch(sid).map(|s| &s.sketch) else { return false }; let dir = |id: &str| match sk.curve_index(id).and_then(|i| sk.shape(i)) { Some(solidcraft_engine::sketch::Shape::Line { a, b }) => Some(b - a), _ => None, }; let (Some(u), Some(v)) = (dir(a), dir(b)) else { return false }; let (lu, lv) = (u.len(), v.len()); lu > 1e-12 && lv > 1e-12 && ((u.x * v.y - u.y * v.x) / (lu * lv)).abs() < 1e-9 } pub fn rows(ui: &mut egui::Ui, k: &mut Mod, doc: &Document, two_parallel_lines: bool) -> bool { let mut enter = false; match k { Mod::Scale { factor } => enter |= number(ui, "Factor", factor), Mod::Offset { distance, distance2, instances, mode, chain, flip } => { icons(ui, "Direction", &OFFSET_MODES, &OFFSET_MODE_ICONS, mode); enter |= number(ui, "Distance", distance); if *mode == TWO_SIDES { enter |= number(ui, "Distance 2", distance2); } // Every instance after the first offsets the one before it by Distance again, so the // arrows walk the offset outwards one ring at a time. enter |= quantity(ui, doc, "Instances", instances, 1, MAX_OFFSET_INSTANCES); ui_kit::section(ui, "offset_options", "Options", true, |ui| { if *mode != SYMMETRIC { ui_kit::checkbox_grid(ui, &mut [("Flip", flip), ("Chain", chain)], 2); } else { ui_kit::checkbox_grid(ui, &mut [("Chain", chain)], 2); } }); } Mod::Mirror => {} Mod::Move { x, y, z, angle, copy, three_d } => { enter |= ui_kit::value_pair(ui, "Position", [x, y], ["X", "Y"], Some(["X Distance", "Y Distance"])); if *three_d { enter |= number(ui, "Z Distance", z); } enter |= number(ui, "Angle", angle); ui_kit::options(ui, "move_options", &mut [("Create Copy", copy)]); } Mod::Transform { x, y, factor, angle, frame } => { enter |= ui_kit::value_pair(ui, "Position", [x, y], ["X", "Y"], Some(["X Distance", "Y Distance"])); // The size in millimetres follows the factor and the other way round; the aspect is kept. if let Some((_, size)) = frame { for (label, natural) in [("Width", size[0]), ("Height", size[1])] { let k = crate::ui_kit::parse_number(factor.trim()).filter(|k| *k > 0.0).unwrap_or(1.0); let mut shown = format!("{} mm", crate::sketch_transform_ui::plain(natural * k)); if number(ui, label, &mut shown) && let Some(mm) = crate::ui_kit::parse_number(shown.trim_end_matches("mm").trim()) && mm > 0.0 && natural > 1e-9 { *factor = format!("{}", ((mm / natural) * 1e6).round() / 1e6); } } } enter |= number(ui, "Scale", factor); enter |= number(ui, "Angle", angle); } Mod::PatternRect { count, spacing, count2, spacing2, alignment: a, alignment2, reversed, reversed2, distance, distance2, extent, extent2, list, list2, copy_at_end, copy_at_end2, } => { // A direction that works its count out leaves the other all the copies. let left = |uses_count: bool, other: &str| if uses_count { copies_left(doc, other) } else { MAX_COPIES as i64 }; let (left1, left2) = (left(distance2.uses_count(), count2), left(distance.uses_count(), count)); let first = PatternRowUi { key: "direction1", title: "Direction 1", labels: [ "ui.quantity", "ui.spacing", "pattern.alignment", "ui.pattern_distance_type", "ui.pattern_extent", "ui.pattern_spacing_list", "ui.pattern_copy_at_end", ], type_key: "sketch_pattern_distance1", left: left1, }; let second = PatternRowUi { key: "direction2", title: "Direction 2", labels: [ "ui.quantity_2", "ui.spacing_2", "pattern.alignment_2", "ui.pattern_distance_type_2", "ui.pattern_extent_2", "ui.pattern_spacing_list_2", "ui.pattern_copy_at_end_2", ], type_key: "sketch_pattern_distance2", left: left2, }; enter |= first.show(ui, doc, PatternRowFields { distance, count, spacing, extent, list, copy_at_end, alignment: a, reversed }); enter |= second.show( ui, doc, PatternRowFields { distance: distance2, count: count2, spacing: spacing2, extent: extent2, list: list2, copy_at_end: copy_at_end2, alignment: alignment2, reversed: reversed2, }, ); pattern_hint(ui); } Mod::PatternCirc { count, angle, alignment: a, reversed } => { enter |= quantity(ui, doc, "Quantity", count, 2, MAX_COPIES as i64); enter |= number(ui, "Total angle", angle); alignment(ui, "Alignment", a, reversed); pattern_hint(ui); } Mod::Divide { count } => { enter |= quantity(ui, doc, "Segments", count, 2, DIVIDE_MAX); ui_kit::text(ui, tr!("sketch.divide_hint"), ui_kit::Text::Note("divide")); } Mod::Equation { form, x, y, start, end, tolerance, range_unit, radians_trig } => { let note = if *radians_trig { "ui.equation_angles_legacy" } else { "ui.equation_angles_degrees" }; ui_kit::text(ui, solidcraft_ui_text::tr_by_id(note), ui_kit::Text::Note("equation")); ui_kit::choice_row(ui, "Form", "equation_form", form, &EQUATION_FORMS); if *form == 0 { enter |= number(ui, "x(t)", x); enter |= number(ui, "y(t)", y); } else { enter |= number(ui, "y(x)", y); } enter |= number(ui, "Start", start); enter |= number(ui, "End", end); enter |= number(ui, "Tolerance", tolerance); ui_kit::checkbox_grid(ui, &mut [(tr!("ui.equation_variable_unit"), range_unit)], 1); if *range_unit { ui_kit::text(ui, tr!("ui.equation_note_range"), ui_kit::Text::Note("equation_range")); } } Mod::Corner { chamfer, ctype, size, distance2, angle, flip } => { if *chamfer { icons(ui, "Chamfer Type", &CHAMFER_TYPES, &CHAMFER_TYPE_ICONS, ctype); } enter |= number(ui, size_label(*chamfer, *ctype), size); if *chamfer && *ctype == 1 { enter |= number(ui, "Distance 2", distance2); } if *chamfer && *ctype == 2 { enter |= number(ui, "Angle", angle); } if *chamfer && *ctype > 0 { ui_kit::options(ui, "corner_options", &mut [("Flip", flip)]); } // Two parallel lines are joined by a half circle at one of their ends (#16238); the // option shows only then, so a window rounding corners keeps its size. if !*chamfer && two_parallel_lines { ui_kit::options(ui, "corner_options", &mut [("Other end", flip)]); } } } enter } /// The in-plane direction a picked origin axis or straight sketch curve gives the active /// sketch, as `sketch.pattern.rectangular` reads its `dir`. None when there is no active /// sketch, or when the axis stands perpendicular to the sketch plane and so has no direction /// in it. fn sketch_dir(app: &SolveApp, sel: &Sel) -> Option { let dir = crate::dialogs::axis_of(app, sel)?.1; let plane = crate::open_meshing::session_world(&app.session).sketch(app.session.active_sketch?)?.plane; Vec2::new(dir.dot(plane.x), dir.dot(plane.y)).normalized() } /// The world direction a sketch modify window's dragged value grows along, for the viewport's /// value box and its drag arrow (the same machinery `dialogs_sheet::arrow` feeds). The /// rectangular pattern's spacing follows the picked direction, or the sketch's own x axis, which /// is what the command uses when nothing is picked. Every other variant keeps the arrow /// `canvas::anchor` already works out for it. pub fn arrow(app: &SolveApp, d: &Dialog) -> Option { let Kind::SketchModify(k) = &d.kind else { return None }; match k { Mod::PatternRect { alignment, reversed, .. } => { let plane = crate::open_meshing::session_world(&app.session).sketch(app.session.active_sketch?)?.plane; let dir = pattern_dir(app, d)?; Some(plane.dir_to_world(if (*alignment == PatternAlignment::Left) ^ *reversed { -dir } else { dir })) } // A total angle is a rotation. The viewport draws a straight drag arrow and a rotation // handle only for Revolve, through machinery tied to its own extents, so the circular // pattern takes the value box alone until that manipulator is made general. _ => None, } } /// The rectangular pattern's first direction in the sketch: the picked one, or the sketch's x axis. fn pattern_dir(app: &SolveApp, d: &Dialog) -> Option { match d.inputs.get(1).and_then(|i| i.items.first()) { Some(sel) => sketch_dir(app, sel), None => Some(Vec2::X), } } /// The world direction the rectangular pattern's Spacing 2 grows along, for its own drag arrow /// (#2462): the picked second direction, the one an edited pattern was made with, or square to /// the first, as the command does. pub fn arrow2(app: &SolveApp, d: &Dialog) -> Option { let Kind::SketchModify(Mod::PatternRect { alignment2, reversed2, .. }) = &d.kind else { return None }; let plane = crate::open_meshing::session_world(&app.session).sketch(app.session.active_sketch?)?.plane; let dir = match d.inputs.get(2).and_then(|i| i.items.first()) { Some(sel) => sketch_dir(app, sel)?, None => match d.extra.get("dir2").and_then(|v| serde_json::from_value::(v.clone()).ok()).and_then(Vec2::normalized) { Some(kept) => kept, None => pattern_dir(app, d)?.perp(), }, }; Some(plane.dir_to_world(if (*alignment2 == PatternAlignment::Left) ^ *reversed2 { -dir } else { dir })) } /// What the first value means for this corner type. fn size_label(chamfer: bool, ctype: usize) -> &'static str { if !chamfer { "Radius" } else if ctype == 0 { "Distance" } else { "Distance 1" } } /// The corner under the cursor while the Fillet or Chamfer window is open: the hover hit and /// the selection it stands for. /// /// Corners are resolved through `sketch_tools::corner_point` rather than the ordinary /// nearest-point test. A rectangle drawn from the origin leaves its own endpoint on top of the /// fixed origin point, and the nearest-point test returns the origin, which ends no two curves. /// That resolution chooses among the endpoints that actually make a corner instead. pub(crate) fn corner_hit(app: &SolveApp, proj: &Proj, pos: Pos2) -> Option<(Hit, Sel)> { let Some(Kind::SketchModify(Mod::Corner { chamfer, .. })) = app.dialog.as_ref().map(|d| &d.kind) else { return None }; let sketch = app.session.active_sketch?; let (at, id) = crate::sketch_tools::corner_point(app, proj, pos, !*chamfer)?; let sel = Sel::SketchPoint { id: id.clone() }; Some((Hit::SketchPoint { sketch, id, at }, sel)) } /// The Move window's Point to Point step (#21235): To minus From, on the sketch, when both rows /// hold a point of the sketch being edited. pub(crate) fn point_to_point(app: &SolveApp, inputs: &[SelInput]) -> Option { let state = crate::open_meshing::session_world(&app.session); let sk = &state.sketch(app.session.active_sketch?)?.sketch; let at = |i: usize| { let id = point_id(&app.session, items(inputs, i).first()?)?; sk.resolve_point(id).and_then(|p| sk.point(p)) }; Some(at(3)? - at(2)?) } /// Keep the Move window's X and Y on To minus From while both are picked, so the rows, the value /// box and the preview show the step OK will make. pub fn refresh_move(app: &mut SolveApp) { let Some(d) = app.dialog.as_ref().filter(|d| matches!(d.kind, Kind::SketchModify(Mod::Move { .. }))) else { return }; let Some(step) = point_to_point(app, &d.inputs) else { return }; // Whole nanometres: a step of 40 reads "40 mm", not "40.00000000000001 mm". let mm = |v: f64| format!("{} mm", (v * 1e6).round() / 1e6 + 0.0); if let Some(Kind::SketchModify(Mod::Move { x, y, .. })) = app.dialog.as_mut().map(|d| &mut d.kind) { *x = mm(step.x); *y = mm(step.y); } } /// What input `i` holds. fn items(inputs: &[SelInput], i: usize) -> &[Sel] { inputs.get(i).map(|x| x.items.as_slice()).unwrap_or(&[]) } /// A local point id. Qualified ids supplied by scripts or older selections are also accepted; /// commands check that their sketch prefix identifies the sketch being edited. pub(crate) fn point_id<'a>(session: &solidcraft_engine::Session, sel: &'a Sel) -> Option<&'a str> { let Sel::SketchPoint { id } = sel else { return None }; let Some((owner, point)) = id.split_once(':') else { return Some(id) }; let active = session.active_sketch?; // Custom local ids may themselves contain ':'. Preserve them before interpreting a // legacy qualified selection, whose owner must match the sketch being edited. if crate::open_meshing::session_world(session).sketch(active).is_some_and(|sk| sk.sketch.point_index(id).is_some()) { Some(id) } else { (owner.parse::().ok() == Some(active)).then_some(point) } } /// The curves and lone points picked in input `i`, as the engine's `entities` list. A profile /// pick expands to the curves of its outline (#8390): a shape drawn inside it is a region of its /// own, picked by a click inside that shape, so a click between two nested rectangles takes the /// outer one alone (#14496). With the `region_pick_takes_holes` preference on, the pick also /// takes the curves of the region's holes, as it did before. pub(crate) fn entity_ids(app: &SolveApp, inputs: &[SelInput], i: usize) -> Vec { let session = &app.session; let holes = app.preferences.region_pick_takes_holes; let mut out = Vec::new(); for x in items(inputs, i) { match x { Sel::SketchCurve { id, .. } => out.push(id.clone()), Sel::Profile { sketch, index } => { if let Some(ids) = crate::open_meshing::session_world(session) .sketch(*sketch) .and_then(|ss| ss.profiles.get(*index)) .map(|p| if holes { p.curve_ids() } else { p.outer_curves.clone() }) { out.extend(ids); } } _ => { if let Some(id) = point_id(session, x) { out.push(id.to_string()); } } } } out } /// Sketch curve ids picked in input `i`. fn curve_ids(inputs: &[SelInput], i: usize) -> Vec { items(inputs, i).iter().filter_map(|x| if let Sel::SketchCurve { id, .. } = x { Some(id.clone()) } else { None }).collect() } /// A typed scale factor as the command wants it: the field evaluated against the document's /// parameters, in the range the command accepts. Checking the range here lets the window say /// so in its own error line instead of the status bar. pub(crate) fn factor(app: &SolveApp, text: &str) -> Result { crate::length_input::no_definition(text, "Factor")?; let v = app.session.doc.eval(text.trim(), ValueKind::Unitless).map_err(|e| format!("Factor: {e}"))?; if !(v.is_finite() && v > 1e-9 && v < 1e6) { return Err("Factor must be a positive number".into()); } Ok(v) } /// A typed field as the command wants it: evaluated against the document's parameters, so an /// expression works, with a bare number read in the design's own units. An empty field is zero, /// the way an untouched row should leave the geometry where it is. pub(crate) fn value(app: &SolveApp, text: &str, kind: ValueKind, label: &str) -> Result { let text = text.trim(); if text.is_empty() { return Ok(0.0); } crate::length_input::no_definition(text, label)?; let doc = &app.session.doc; let v = crate::length_input::eval(doc, text, kind).map_err(|e| format!("{label}: {e}"))?; if !v.is_finite() { return Err(format!("{label} must be a number")); } Ok(v) } /// A typed distance as the command wants it: the field evaluated against the document's /// parameters, with the sign reversed when Flip is set. A bare number means the design's units, /// as a typed sketch value does everywhere else. fn signed_length(app: &SolveApp, label: &str, text: &str, flip: bool) -> Result { let doc = &app.session.doc; let v = crate::length_input::eval(doc, text.trim(), ValueKind::Length).map_err(|e| format!("{label}: {e}"))?; if !v.is_finite() || v.abs() <= 1e-9 { return Err(format!("{label} must be a non-zero number")); } Ok(if flip { -v } else { v }) } /// Validate a typed count before sending its expression to the command. fn whole(app: &SolveApp, label: &str, text: &str) -> Result { let v = app.session.doc.eval(text.trim(), ValueKind::Unitless).map_err(|e| format!("{label}: {e}"))?; if !v.is_finite() || !(1.0..=MAX_COPIES).contains(&v) || (v - v.round()).abs() > 1e-9 { return Err(format!("{label} must be a whole number from 1 to {MAX_COPIES:.0}")); } Ok(v.round() as u64) } /// Validate a typed offset instance count before sending it to the command. fn offset_instances(app: &SolveApp, text: &str) -> Result { let v = app.session.doc.eval(text.trim(), ValueKind::Unitless).map_err(|e| format!("Instances: {e}"))?; if !v.is_finite() || !(1.0..=MAX_OFFSET_INSTANCES as f64).contains(&v) || (v - v.round()).abs() > 1e-9 { return Err(format!("Instances must be a whole number from 1 to {MAX_OFFSET_INSTANCES}")); } Ok(v.round() as u64) } /// A typed length as the command wants it, in millimetres. A bare number means the design's /// units, as a typed sketch value does everywhere else. `value` above reads an empty row as /// zero, which a Move window wants and a pattern spacing does not. fn length(app: &SolveApp, label: &str, text: &str) -> Result { let doc = &app.session.doc; let v = crate::length_input::eval(doc, text.trim(), ValueKind::Length).map_err(|e| format!("{label}: {e}"))?; if !v.is_finite() { return Err(format!("{label} must be a number")); } Ok(v) } /// The curve ids picked in input `i` (a corner's two lines, #1824). fn line_ids(inputs: &[SelInput], i: usize) -> Vec { items(inputs, i) .iter() .filter_map(|x| match x { Sel::SketchCurve { id, .. } => Some(id.clone()), _ => None, }) .collect() } /// Corner point ids within the active sketch, including qualified script selections. fn corner_ids(app: &SolveApp, inputs: &[SelInput], i: usize) -> Vec { items(inputs, i).iter().filter_map(|sel| point_id(&app.session, sel).map(str::to_string)).collect() } /// A corner size evaluated against the document's parameters, which every corner command needs /// to be strictly positive. A bare number means the design's units for a length and degrees for /// an angle, as every other typed sketch value does. `value` above is the Move window's reading /// of a field, where an empty row means zero and a negative distance is a direction; neither is /// a radius or a chamfer leg, so this one is its own. fn corner_size(app: &SolveApp, label: &str, text: &str, kind: ValueKind) -> Result { crate::length_input::no_definition(text, label)?; let doc = &app.session.doc; let v = crate::length_input::eval(doc, text.trim(), kind).map_err(|e| format!("{label}: {e}"))?; if !v.is_finite() || v <= 1e-9 { return Err(format!("{label} must be a positive number")); } Ok(v) } /// The corner as a chamfer command takes it. Unflipped it is the point itself, and the command /// resolves the two lines in curve order. Flipped it names those two lines in the other order, /// so `distance` applies to the other leg. fn corner_ref(app: &SolveApp, point: &str, flip: bool) -> Result { if !flip { return Ok(json!({ "point": point })); } let gone = || "that corner is no longer in the sketch".to_string(); let sketch = app.session.active_sketch.ok_or("open a sketch first")?; let state = crate::open_meshing::session_world(&app.session); let sk = &state.sketch(sketch).ok_or_else(gone)?.sketch; let index = sk.resolve_point(point).ok_or_else(gone)?; let (a, b) = crate::sketch_tools::corner_curves(sk, index, false).ok_or("a chamfer needs a corner of exactly two lines")?; let id = |c: usize| sk.curves.get(c).map(|x| x.id.clone()).ok_or_else(gone); Ok(json!({"a": id(b)?, "b": id(a)?})) } /// A typed total angle as the command wants it, in degrees. A bare number means degrees, as a /// typed angle does everywhere else. `sketch.pattern.circular` takes the full turn as 360 and /// refuses anything outside that range. fn total_angle(app: &SolveApp, label: &str, text: &str) -> Result { let v = app.session.doc.eval(text.trim(), ValueKind::Angle).map_err(|e| format!("{label}: {e}"))?.to_degrees(); if !v.is_finite() || v.abs() <= 1e-9 || v.abs() > 360.0 { return Err(format!("{label} must be more than 0 and at most 360 degrees")); } Ok(v) } /// The bare sketch point id of a single point pick, as a command's `center` or `base` reads it. /// `point_id` above takes one `Sel`; this one takes the first pick of an input. fn picked_point(session: &solidcraft_engine::Session, inputs: &[SelInput], i: usize) -> Option { items(inputs, i).first().and_then(|sel| point_id(session, sel)).map(str::to_string) } /// Tab and Shift+Tab in sketch Mirror, with no box holding the keyboard, make the next or /// previous pick field the active one (#24829): the curves and then the line are picked from the /// viewport without a click into the window. Runs before layout, so egui does not first hand the /// keyboard to the window's first button. pub(crate) fn tab_between_picks(app: &mut SolveApp, ctx: &egui::Context) { let Some(d) = app.dialog.as_mut().filter(|d| matches!(d.kind, Kind::SketchModify(Mod::Mirror)) && d.inputs.len() > 1) else { return; }; if ctx.memory(|m| m.focused().is_some()) || ctx.egui_wants_keyboard_input() { return; } // Shift+Tab first: a plain Tab pattern also matches a press with Shift held. let back = ctx.input_mut(|i| i.consume_key(egui::Modifiers::SHIFT, egui::Key::Tab)); if !back && !ctx.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Tab)) { return; } ctx.memory_mut(|m| m.move_focus(egui::FocusDirection::None)); let last = d.inputs.len().saturating_sub(1); let at = d.active.min(last); let to = if back { at.checked_sub(1).unwrap_or(last) } else if at >= last { 0 } else { at.saturating_add(1) }; activate_pick(d, to); } /// Enter in sketch Mirror with the curves picked and the line still empty goes on to the line /// field (#24829) instead of refusing with "Select the mirror line". Returns whether it did. pub(crate) fn enter_moves_on(app: &SolveApp, d: &mut Dialog) -> bool { if !matches!(d.kind, Kind::SketchModify(Mod::Mirror)) || d.active_input().is_none_or(|i| i.items.is_empty()) { return false; } let next = d.inputs.iter().enumerate().skip(d.active.saturating_add(1)).find(|(_, i)| i.items.is_empty()).map(|(n, _)| n); let Some(next) = next.filter(|_| crate::dialogs::apply_commands(app, d).is_err()) else { return false }; activate_pick(d, next); true } fn activate_pick(d: &mut Dialog, i: usize) { d.active = i; d.awaiting_pick = true; d.pick_refused = None; d.error = None; } /// The command a sketch modify dialog makes. Scale, Move, and patterns send evaluated /// numbers, except circular Quantity, which keeps its authored expression. Offset and Fillet /// retain expressions in persistent driving dimensions. pub fn commands(app: &SolveApp, k: &Mod, inputs: &[SelInput], extra: &Map) -> Result, String> { if inputs.iter().flat_map(|input| &input.items).any(|sel| matches!(sel, Sel::SketchPoint { .. }) && point_id(&app.session, sel).is_none()) { return Err("Select points of the sketch being edited".into()); } let (cmd, mut params) = match k { Mod::Scale { factor: text } => { let entities = entity_ids(app, inputs, 0); if entities.is_empty() { return Err("Select the sketch curves or points to scale first".into()); } let f = factor(app, text)?; let mut p = json!({"entities": entities, "factor": f}); if let Some(base) = items(inputs, 1).first().and_then(|sel| point_id(&app.session, sel)) { p["base"] = json!(base); } ("sketch.scale", p) } Mod::Transform { x, y, factor: text, angle, frame } => { let entities = entity_ids(app, inputs, 0); if entities.is_empty() { return Err("Select the sketch curves or points to transform first".into()); } let Some((middle, _)) = frame else { return Err("Select the sketch curves or points to transform first".into()) }; let f = factor(app, text)?; let turn = value(app, angle, ValueKind::Angle, "Angle")?.to_degrees(); let (dx, dy) = (value(app, x, ValueKind::Length, "X")?, value(app, y, ValueKind::Length, "Y")?); let center = items(inputs, 1).first().and_then(|sel| point_id(&app.session, sel)).map_or_else(|| json!(middle), |id| json!(id)); ("sketch.transform", json!({"entities": entities, "factor": f, "angle": turn, "center": center, "translate": [dx, dy]})) } Mod::Offset { distance, distance2, instances, mode, chain, flip } => { let curves = curve_ids(inputs, 0); if curves.is_empty() { return Err("Select the curves to offset first".into()); } // Symmetric hides Flip, so a flip left over from another mode does not travel with it. signed_length(app, "Distance", distance, *flip && *mode != SYMMETRIC)?; // The count is not kept in the design, so the command gets the number it evaluates to // rather than the text, the way Scale and the pattern spacings send theirs. let count = offset_instances(app, instances)?; let mut params = json!({"curves": curves, "distance": distance.trim(), "flip": *flip && *mode != SYMMETRIC, "chain": chain, "instances": count}); if let Value::Object(m) = &mut params { if *mode == SYMMETRIC { m.insert("symmetric".into(), json!(true)); } if *mode == TWO_SIDES { let d2 = signed_length(app, "Distance 2", distance2, false)?; // The other way is the value negated; a definition keeps its name (#4071). let negated = match solidcraft_engine::doc::expr::definition(distance2) { Some((name, rhs)) => format!("{name} = -({rhs})"), None => format!("-({})", distance2.trim()), }; m.insert("distance2".into(), json!(if d2 > 0.0 { distance2.trim().to_string() } else { negated })); } } ("sketch.offset", params) } Mod::Mirror => { let entities = entity_ids(app, inputs, 0); if entities.is_empty() { return Err("Select the sketch curves or points to mirror first".into()); } let line = match items(inputs, 1).first() { Some(Sel::SketchCurve { id, .. }) => json!(id), Some(sel @ Sel::Axis { name }) if matches!(name.as_str(), "X" | "Y" | "Z") => { if let Some(axis) = crate::tools::sketch_axis_of(app, name) { json!(axis) } else if sketch_dir(app, sel).is_some() { // An axis outside the plane still gives the line it makes in the plane. json!({"axis":name}) } else { return Err("The origin axis is perpendicular to the sketch plane, so it has no line in the plane".into()); } } Some(_) => return Err("The mirror line must be a straight curve of this sketch or an origin axis in its plane".into()), None => return Err("Select the mirror line".into()), }; // The mirror line may itself be among the entities (a chain or profile the user // picked in one action): the command leaves the line in place and mirrors the rest. ("sketch.mirror", json!({"entities": entities, "line": line})) } Mod::Move { x, y, z, angle, copy, three_d } => { let entities = entity_ids(app, inputs, 0); if entities.is_empty() { return Err("Select the sketch curves or points to move first".into()); } let (dx, dy) = match point_to_point(app, inputs) { Some(d) => (d.x, d.y), None => (value(app, x, ValueKind::Length, "X Distance")?, value(app, y, ValueKind::Length, "Y Distance")?), }; let deg = value(app, angle, ValueKind::Angle, "Angle")?.to_degrees(); let dz = if *three_d { value(app, z, ValueKind::Length, "Z Distance")? } else { 0.0 }; if dx.abs() < 1e-9 && dy.abs() < 1e-9 && dz.abs() < 1e-9 && deg.abs() < 1e-9 { return Err("Type a distance or an angle to move by".into()); } let mut p = json!({"entities": entities, "translate": [dx, dy], "angle": deg, "copy": copy}); if dz.abs() >= 1e-9 { p["z"] = json!(dz); } if let Some(center) = items(inputs, 1).first().and_then(|sel| point_id(&app.session, sel)) { p["center"] = json!(center); } ("sketch.move", p) } Mod::PatternRect { count, spacing, count2, spacing2, alignment, alignment2, reversed, reversed2, distance, distance2, extent, extent2, list, list2, copy_at_end, copy_at_end2, } => { let entities = entity_ids(app, inputs, 0); if entities.is_empty() { return Err("Select the sketch curves or points to pattern first".into()); } // Only the values a direction's distance type shows are checked here; the command // checks the rest (an extent above zero, a spacing that fits it) and says why. let n1 = if distance.uses_count() { whole(app, "Quantity", count)? } else { 1 }; let n2 = if distance2.uses_count() { whole(app, "Quantity 2", count2)? } else { 1 }; for (distance, n, text, label, zero) in [ (distance, n1, spacing, "Spacing", "Spacing must be a non-zero number"), (distance2, n2, spacing2, "Spacing 2", "Spacing 2 must be a non-zero number"), ] { if distance.uses_spacing() && (n > 1 || !distance.uses_count()) && length(app, label, text)?.abs() <= 1e-9 { return Err(zero.into()); } } for (distance, text, label) in [(distance, extent, "Extent"), (distance2, extent2, "Extent 2")] { if distance.uses_extent() { length(app, label, text)?; } } if n1.saturating_mul(n2) > MAX_COPIES as u64 { return Err(format!("Quantity and Quantity 2 together must make at most {MAX_COPIES:.0} instances")); } let mut p = json!({"entities": entities, "count": count, "spacing": spacing, "count2": count2, "spacing2": spacing2, "alignment": alignment, "alignment2": alignment2, "reversed": reversed, "reversed2": reversed2}); // The distance types go with the pattern only when one is chosen, or when an edited // pattern has to learn it is back to Quantity and Spacing (a key left out keeps its // value): a new pattern by Quantity and Spacing is sent as it always was. let editing = extra.contains_key("pattern"); for (k, distance, extent, list, copy_at_end, count_key, spacing_key) in [("1", distance, extent, list, copy_at_end, "count", "spacing"), ("2", distance2, extent2, list2, copy_at_end2, "count2", "spacing2")] { if distance.is_default() && !editing { continue; } // Only the values the type uses, as the solid pattern's window sends them. if let Some(sent) = p.as_object_mut() { if !distance.uses_count() { sent.remove(count_key); } if !distance.uses_spacing() { sent.remove(spacing_key); } } p[format!("distance{k}")] = json!(distance.as_str()); if distance.uses_extent() { p[format!("extent{k}")] = json!(extent.trim()); } if distance.uses_list() { // Each gap with the design's unit made explicit, as the other lengths are sent. p[format!("spacing_list{k}")] = json!(crate::dialogs::spacing_list_values(&app.session.doc, list)); } p[format!("copy_at_end{k}")] = json!(*copy_at_end && *distance == PatternDistance::SpacingExtent); } // A picked direction that has no component in the sketch plane is reported rather // than quietly replaced by the default. for (index, vector_key, line_key) in [(1, "dir", "direction"), (2, "dir2", "direction2")] { if let Some(sel) = inputs.get(index).and_then(|i| i.items.first()) { let d = sketch_dir(app, sel).ok_or("The direction must be an axis or a straight sketch curve that lies in the sketch plane")?; p[vector_key] = json!([d.x, d.y]); p[line_key] = match sel { Sel::SketchCurve { id, .. } => json!(id), _ => Value::Null, }; } else { p[line_key] = Value::Null; } } ("sketch.pattern.rectangular", p) } Mod::Divide { count } => { let curves = curve_ids(inputs, 0); if curves.is_empty() { return Err("Select the curves to divide first".into()); } let v = app.session.doc.eval(count.trim(), ValueKind::Unitless).map_err(|e| format!("Segments: {e}"))?; if !v.is_finite() || !(2.0..=DIVIDE_MAX as f64).contains(&v) || (v - v.round()).abs() > 1e-9 { return Err(format!("Segments must be a whole number from 2 to {DIVIDE_MAX}")); } ("sketch.point.divide", json!({"curves": curves, "segments": v.round()})) } Mod::PatternCirc { count, angle, alignment, reversed } => { let entities = entity_ids(app, inputs, 0); if entities.is_empty() { return Err("Select the sketch curves or points to pattern first".into()); } let center = match items(inputs, 1).first() { Some(Sel::Vertex { body, point }) if body.is_empty() => { let plane = crate::open_meshing::session_world(&app.session) .sketch(app.session.active_sketch.ok_or("open a sketch first")?) .ok_or("sketch is missing")? .plane; let p = plane.to_local(*point); json!([p.x, p.y]) } _ => json!(picked_point(&app.session, inputs, 1).ok_or("Select the centre the pattern turns around first")?), }; let n = whole(app, "Quantity", count)?; if n < 2 { return Err("Quantity must be at least 2".into()); } total_angle(app, "Total angle", angle)?; ( "sketch.pattern.circular", json!({"entities": entities, "center": center, "count": count, "angle": angle, "alignment": alignment, "reversed": reversed}), ) } Mod::Corner { chamfer: false, size, flip, .. } => { let corners = corner_ids(app, inputs, 0); let lines = line_ids(inputs, 0); // The engine keeps its millimetre API; bare UI values use the design unit. // Keep expressions intact so the radius remains a driving dimension. let radius = crate::length_input::expression(&app.session.doc, size, ValueKind::Length); match (corners.is_empty(), lines.as_slice()) { (true, []) => return Err("select the corners to round first".into()), (false, []) => ("sketch.fillet", json!({"points": corners, "radius": radius})), // Two lines name their corner even where other lines end at its point (#1824). // `end` only matters for two parallel lines, which a half circle joins at the // start of the first line, or at its end with "Other end" (#16238). (true, [a, b]) => ("sketch.fillet", json!({"a": a, "b": b, "radius": radius, "end": if *flip { "end" } else { "start" }})), _ => return Err("select corner points, or the two lines of one corner".into()), } } Mod::Corner { ctype, size, distance2, angle, flip, .. } => { let flipped = *flip && *ctype > 0; let mut p = match (corner_ids(app, inputs, 0).as_slice(), line_ids(inputs, 0).as_slice()) { ([point], []) => corner_ref(app, point, flipped)?, // Several corners are cut alike, each flipped as one alone would be. (points, []) if !points.is_empty() => { json!({"points": points.iter().map(|point| corner_ref(app, point, flipped)).collect::, _>>()?}) } // Flip swaps the lines, so `distance` applies to the other leg. ([], [a, b]) if flipped => json!({"a": b, "b": a}), ([], [a, b]) => json!({"a": a, "b": b}), ([], []) => return Err("select the corner to cut first".into()), _ => return Err("select corner points, or the two lines of one corner".into()), }; p["distance"] = json!(corner_size(app, size_label(true, *ctype), size, ValueKind::Length)?); match ctype { 1 => { p["distance2"] = json!(corner_size(app, "Distance 2", distance2, ValueKind::Length)?); ("sketch.chamfer.two_distance", p) } 2 => { // The command takes degrees; an evaluated angle is in radians. p["angle"] = json!(corner_size(app, "Angle", angle, ValueKind::Angle)?.to_degrees()); ("sketch.chamfer.distance_angle", p) } _ => ("sketch.chamfer.equal_distance", p), } } Mod::Equation { form, x, y, start, end, tolerance, range_unit, .. } => { let filled = |label: &str, text: &str| -> Result { let text = text.trim(); if text.is_empty() { Err(format!("{label} is empty")) } else { Ok(text.to_string()) } }; let tol = crate::length_input::eval(&app.session.doc, tolerance.trim(), ValueKind::Length).map_err(|e| format!("Tolerance: {e}"))?; if !(tol.is_finite() && (1e-4..=10.0).contains(&tol)) { return Err("Tolerance must be from 0.0001 mm to 10 mm".into()); } let mut p = json!({"y": filled(if *form == 0 { "y(t)" } else { "y(x)" }, y)?, "start": filled("Start", start)?, "end": filled("End", end)?, "tolerance": tol}); if *form == 0 { p["x"] = json!(filled("x(t)", x)?); } else { p["explicit"] = json!(true); } p["variable_unit"] = json!(if *range_unit { "range" } else { "number" }); (if extra.contains_key("curve") { "sketch.equation_curve.edit" } else { "sketch.equation_curve" }, p) } }; if let Value::Object(m) = &mut params { for (key, value) in extra { m.entry(key.clone()).or_insert_with(|| value.clone()); } } Ok(vec![(cmd.to_string(), params)]) }