SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Plastic dialogs: dimension fields show their units (#1936) (#2107)
Vodmordia55bfeb71a7c9
diff --git a/assets/locales/en.json b/assets/locales/en.jsonindex 2fa7d927e6..f22bb40873 100644--- a/assets/locales/en.json+++ b/assets/locales/en.json@@ -726,9 +726,11 @@"new configuration": "new configuration","new name": "new name","new parameter": "new parameter",+ "no hole": "no hole","no interference": "no interference","no joint that moves yet": "no joint that moves yet","no picture": "no picture",+ "none": "none","pattern.alignment_2": "Alignment 2","pattern.alignment_hint": "Counts include the original. Center balances copies; Flip chooses the extra side for even totals.","pattern.direction_1": "Direction 1",@@ -815,6 +817,7 @@"sketch_text.path_clear": "Take the text off its curve","sketch_text.path_hint": "A curve of the sketch the text follows: click here, then the curve","sketch_text.use_font_file": "Use the font file {path}",+ "solid": "solid","the parts library is not available": "the parts library is not available","thread.automatic_size": "Automatic size","thread.designation": "Designation",@@ -826,6 +829,7 @@"timeline.rename_group": "Rename Feature Folder","timeline.reorder_many": "Reorder Features","timeline.ungroup": "Remove Feature Folders",+ "to the face": "to the face","to the next face": "to the next face","toolbar.image": "IMAGE","toolbar.render": "RENDER",diff --git a/assets/locales/ja.json b/assets/locales/ja.jsonindex 8685df9e97..5da8fa9827 100644--- a/assets/locales/ja.json+++ b/assets/locales/ja.json@@ -726,9 +726,11 @@"new configuration": "新規構成","new name": "新しい名前","new parameter": "新規パラメータ",+ "no hole": "穴なし","no interference": "干渉なし","no joint that moves yet": "可動ジョイントがまだありません","no picture": "画像なし",+ "none": "なし","pattern.alignment_2": "配置 2","pattern.alignment_hint": "個数には元の図形が含まれます。中央配置ではコピーを両側に配置し、反転で偶数個の場合に多く配置する側を切り替えます。","pattern.direction_1": "方向 1",@@ -815,6 +817,7 @@"sketch_text.path_clear": "文字を曲線から外す","sketch_text.path_hint": "文字が沿うスケッチの曲線: ここをクリックしてから曲線をクリック","sketch_text.use_font_file": "フォント ファイル {path} を使用",+ "solid": "中実","the parts library is not available": "パーツライブラリは利用できません","thread.automatic_size": "サイズを自動選択","thread.designation": "ねじの呼び",@@ -826,6 +829,7 @@"timeline.rename_group": "フィーチャーフォルダの名前を変更","timeline.reorder_many": "フィーチャーの順序を変更","timeline.ungroup": "フィーチャーフォルダを削除",+ "to the face": "面まで","to the next face": "次の面まで","toolbar.image": "画像","toolbar.render": "レンダリング",diff --git a/assets/locales/zh.json b/assets/locales/zh.jsonindex c2abf2aa21..a1fe448ad7 100644--- a/assets/locales/zh.json+++ b/assets/locales/zh.json@@ -726,9 +726,11 @@"new configuration": "新配置","new name": "新名称","new parameter": "新参数",+ "no hole": "无孔","no interference": "无干涉","no joint that moves yet": "尚无运动接头","no picture": "无图片",+ "none": "无","pattern.alignment_2": "对齐 2","pattern.alignment_hint": "数量包含原始图形。居中时副本分布在两侧;数量为偶数时,反转可切换多一个副本的一侧。","pattern.direction_1": "方向 1",@@ -815,6 +817,7 @@"sketch_text.path_clear": "将文字移离其曲线","sketch_text.path_hint": "文字所跟随的草图曲线:先单击此处,再选择曲线","sketch_text.use_font_file": "使用字体文件 {path}",+ "solid": "实心","the parts library is not available": "零件库不可用","thread.automatic_size": "自动尺寸","thread.designation": "螺纹标记",@@ -826,6 +829,7 @@"timeline.rename_group": "重命名特征文件夹","timeline.reorder_many": "特征重新排序","timeline.ungroup": "移除特征文件夹",+ "to the face": "到面","to the next face": "到下一个面","toolbar.image": "图像","toolbar.render": "渲染",diff --git a/crates/engine/src/sample.rs b/crates/engine/src/sample.rsindex 2e95267b17..1bec6eb255 100644--- a/crates/engine/src/sample.rs+++ b/crates/engine/src/sample.rs@@ -89,11 +89,11 @@ fn enclosure() -> Value {json!({"commands": [{"command": "solid.box", "params": {"length": 80, "width": 50, "height": 30, "body_name": "Enclosure"}},{"command": "solid.shell", "params": {"faces": [[40, 25, 30]], "thickness": 2}},- {"command": "plastic.lip", "params": {"face": [1, 25, 30], "width": 1, "height": 2}},- {"command": "plastic.snap_fit", "params": {"position": [40, 44, 2], "hook": [0, -1, 0], "length": 14, "thickness": 1.5, "width": 6, "catch_depth": 1, "catch_length": 2}},- {"command": "plastic.boss", "params": {"position": [10, 10, 2], "diameter": 7, "height": 12, "hole_diameter": 2.5}},- {"command": "plastic.boss", "params": {"position": [70, 40, 2], "diameter": 7, "height": 12, "hole_diameter": 2.5}},- {"command": "plastic.rest", "params": {"position": [40, 25, 2], "width": 16, "length": 24, "height": 3}},+ {"command": "plastic.lip", "params": {"face": [1, 25, 30], "width": "1 mm", "height": "2 mm"}},+ {"command": "plastic.snap_fit", "params": {"position": [40, 44, 2], "hook": [0, -1, 0], "length": "14 mm", "thickness": "1.5 mm", "width": "6 mm", "catch_depth": "1 mm", "catch_length": "2 mm"}},+ {"command": "plastic.boss", "params": {"position": [10, 10, 2], "diameter": "7 mm", "height": "12 mm", "hole_diameter": "2.5 mm"}},+ {"command": "plastic.boss", "params": {"position": [70, 40, 2], "diameter": "7 mm", "height": "12 mm", "hole_diameter": "2.5 mm"}},+ {"command": "plastic.rest", "params": {"position": [40, 25, 2], "width": "16 mm", "length": "24 mm", "height": "3 mm"}},]})}diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rsindex 2dffaafd0b..67b2700565 100644--- a/crates/ui-egui/src/dialogs.rs+++ b/crates/ui-egui/src/dialogs.rs@@ -1377,7 +1377,7 @@ fn tr(text: &str) -> &str {}/// A dialog row's widget, for scenarios and demos: the handle `dialog:<row label>`.-fn publish_row(r: &egui::Response) {+pub(crate) fn publish_row(r: &egui::Response) {ROW.with(|row| crate::scenario::publish_handle(&format!("dialog:{}", row.borrow()), r.rect.center()));}@@ -3370,6 +3370,19 @@ fn stored_length(s: &Session, e: &str) -> String {if s.doc.with_design_unit(e, ValueKind::Length) == e { e.to_string() } else { format!("{} mm", e.trim()) }}+/// A stored value as its edit field shows it (#1936): a bare number is written with the unit the+/// engine reads it in, "2" as "2 mm" for a length and "5" as "5 deg" for an angle, so the field+/// and the on-canvas tag name the unit as the other dialogs do. The number keeps its stored text.+/// Counts, units already written, parameter names, expressions and empty text show as stored.+pub(crate) fn shown_expr(e: &str, kind: ValueKind) -> String {+ let unit = match kind {+ ValueKind::Length => "mm",+ ValueKind::Angle => "deg",+ _ => return e.to_string(),+ };+ if solidcraft_engine::doc::expr::is_bare_number(e) { format!("{} {unit}", e.trim()) } else { e.to_string() }+}+fn hole_defaults() -> Kind {Kind::Hole {diameter: "5 mm".into(),diff --git a/crates/ui-egui/src/dialogs_plastic.rs b/crates/ui-egui/src/dialogs_plastic.rsindex 8a9d85b415..48dae642ea 100644--- a/crates/ui-egui/src/dialogs_plastic.rs+++ b/crates/ui-egui/src/dialogs_plastic.rs@@ -9,7 +9,7 @@ use solidcraft_engine::doc::plastic::PlasticRule;use solidcraft_engine::geom::Vec3;use crate::SolveApp;-use crate::dialogs::{Kind, combo, face_sel, field, row_label};+use crate::dialogs::{Kind, combo, face_sel, field, row_label, shown_expr};use crate::selection::{BODIES, FACES, PLANAR_FACES, SelInput};#[derive(Clone, Debug)]@@ -140,10 +140,13 @@ pub fn start(app: &SolveApp, id: &str) -> Option<(Kind, Vec<SelInput>)> {Some((Kind::Plastic(pl), inputs))}-/// A value row whose empty value means "from the rule" (or another default).-fn opt_field(ui: &mut egui::Ui, label: &str, v: &mut String, hint: &str) -> bool {+/// A value row whose empty value means "from the rule" (or another default). `unit` leads the+/// hint ("mm, from the rule") so the empty field names the unit a typed number takes.+fn opt_field(ui: &mut egui::Ui, label: &str, v: &mut String, unit: &str, hint: &str) -> bool {row_label(ui, label);+ let hint = format!("{unit}, {}", crate::i18n::tr_label(hint));let r = ui.add(egui::TextEdit::singleline(v).hint_text(hint));+ crate::dialogs::publish_row(&r);crate::params_dialog::complete(ui, &r, v);ui.end_row();r.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter))@@ -182,10 +185,10 @@ pub fn rows(app: &mut SolveApp, ui: &mut egui::Ui, k: &mut Pl, inputs: &[SelInpuPl::Boss { diameter, height, hole_diameter, hole_depth, draft, fillet, ribs, rib_thickness, rib_length, rib_offset } => {enter |= value_row(ui, "Diameter", diameter);enter |= value_row(ui, "Height", height);- enter |= opt_field(ui, "Hole Diameter", hole_diameter, "no hole");- enter |= opt_field(ui, "Hole Depth", hole_depth, "to the face");- enter |= opt_field(ui, "Draft Angle", draft, "from the rule");- enter |= opt_field(ui, "Root Fillet", fillet, "none");+ enter |= opt_field(ui, "Hole Diameter", hole_diameter, "mm", "no hole");+ enter |= opt_field(ui, "Hole Depth", hole_depth, "mm", "to the face");+ enter |= opt_field(ui, "Draft Angle", draft, "deg", "from the rule");+ enter |= opt_field(ui, "Root Fillet", fillet, "mm", "none");enter |= value_row(ui, "Ribs", ribs);if app.session.doc.eval(ribs, ValueKind::Unitless).is_ok_and(|n| n >= 1.0) {enter |= value_row(ui, " Rib Thickness", rib_thickness);@@ -207,7 +210,7 @@ pub fn rows(app: &mut SolveApp, ui: &mut egui::Ui, k: &mut Pl, inputs: &[SelInpuenter |= value_row(ui, "Width", width);enter |= value_row(ui, "Height", height);if *groove {- enter |= opt_field(ui, "Clearance", gap, "from the rule");+ enter |= opt_field(ui, "Clearance", gap, "mm", "from the rule");}}Pl::SnapFit { hook, length, thickness, width, catch_depth, catch_length } => {@@ -235,8 +238,8 @@ pub fn rows(app: &mut SolveApp, ui: &mut egui::Ui, k: &mut Pl, inputs: &[SelInpuenter |= value_row(ui, "Length", length);}enter |= value_row(ui, "Height", height);- enter |= opt_field(ui, "Draft Angle", draft, "from the rule");- enter |= opt_field(ui, "Wall Thickness", thickness, "solid");+ enter |= opt_field(ui, "Draft Angle", draft, "deg", "from the rule");+ enter |= opt_field(ui, "Wall Thickness", thickness, "mm", "solid");}Pl::Rules { pick, name, values, active, loaded } => {let rules = app.session.doc.plastic_rules();@@ -452,16 +455,16 @@ pub fn for_feature(app: &SolveApp, kind: &FeatureKind) -> Option<(Kind, Vec<SelIrib_offset: ro,} = &mut b{- *d = diameter.to_string();- *h = height.to_string();- *hd = s(hole_diameter);- *hp = s(hole_depth);- *dr = s(draft);- *f = s(fillet);+ *d = shown_expr(diameter, ValueKind::Length);+ *h = shown_expr(height, ValueKind::Length);+ *hd = shown_expr(&s(hole_diameter), ValueKind::Length);+ *hp = shown_expr(&s(hole_depth), ValueKind::Length);+ *dr = shown_expr(&s(draft), ValueKind::Angle);+ *f = shown_expr(&s(fillet), ValueKind::Length);*r = ribs.as_deref().map(str::to_string).unwrap_or_else(|| "0".into());for (slot, v) in [(rt, rib_thickness), (rl, rib_length), (ro, rib_offset)] {if let Some(v) = v {- *slot = v.to_string();+ *slot = shown_expr(v, ValueKind::Length);}}}@@ -471,7 +474,17 @@ pub fn for_feature(app: &SolveApp, kind: &FeatureKind) -> Option<(Kind, Vec<SelIif let Some(b) = body {extra.insert("body".into(), json!(b));}- (Pl::Lip { groove: *groove, width: width.to_string(), height: height.to_string(), gap: s(gap), outside: *outside }, "plastic.lip", face.0)+ (+ Pl::Lip {+ groove: *groove,+ width: shown_expr(width, ValueKind::Length),+ height: shown_expr(height, ValueKind::Length),+ gap: shown_expr(&s(gap), ValueKind::Length),+ outside: *outside,+ },+ "plastic.lip",+ face.0,+ )}FeatureKind::SnapFit { position, direction, hook, length, thickness, width, catch_depth, catch_length, body } => {extra.insert("direction".into(), pt(direction.0));@@ -484,11 +497,11 @@ pub fn for_feature(app: &SolveApp, kind: &FeatureKind) -> Option<(Kind, Vec<SelI}let f = Pl::SnapFit {hook: h.unwrap_or(0),- length: length.to_string(),- thickness: thickness.to_string(),- width: width.to_string(),- catch_depth: catch_depth.to_string(),- catch_length: catch_length.to_string(),+ length: shown_expr(length, ValueKind::Length),+ thickness: shown_expr(thickness, ValueKind::Length),+ width: shown_expr(width, ValueKind::Length),+ catch_depth: shown_expr(catch_depth, ValueKind::Length),+ catch_length: shown_expr(catch_length, ValueKind::Length),};(f, "plastic.snap_fit", position.0)}@@ -502,11 +515,11 @@ pub fn for_feature(app: &SolveApp, kind: &FeatureKind) -> Option<(Kind, Vec<SelI}let r = Pl::Rest {round: length.is_none(),- width: width.to_string(),- length: length.as_deref().map(str::to_string).unwrap_or_else(|| "15 mm".into()),- height: height.to_string(),- draft: s(draft),- thickness: s(thickness),+ width: shown_expr(width, ValueKind::Length),+ length: length.as_deref().map(|l| shown_expr(l, ValueKind::Length)).unwrap_or_else(|| "15 mm".into()),+ height: shown_expr(height, ValueKind::Length),+ draft: shown_expr(&s(draft), ValueKind::Angle),+ thickness: shown_expr(&s(thickness), ValueKind::Length),};(r, "plastic.rest", position.0)}diff --git a/crates/ui-egui/src/dialogs_plastic_tests.rs b/crates/ui-egui/src/dialogs_plastic_tests.rsindex 7e054f0b03..eaf0890610 100644--- a/crates/ui-egui/src/dialogs_plastic_tests.rs+++ b/crates/ui-egui/src/dialogs_plastic_tests.rs@@ -102,3 +102,57 @@ fn plastic_rules_manage_and_assign() {assert_eq!(c[0].1["rule"], "ABS (1.5mm)");run_all(&mut app, c);}++/// Features stored with bare numbers (as the old Plastic Enclosure sample did) edit with their+/// units written out, and re-applying keeps the values (#1936).+#[test]+fn bare_numbers_show_their_units() {+ let mut app = app_with(&[+ ("solid.box", json!({"length": 80, "width": 50, "height": 30})),+ ("solid.shell", json!({"faces": [[40, 25, 30]], "thickness": 2})),+ ("plastic.lip", json!({"face": [1, 25, 30], "width": 1, "height": 2})),+ (+ "plastic.snap_fit",+ json!({"position": [40, 44, 2], "hook": [0, -1, 0], "length": 14, "thickness": 1.5, "width": 6, "catch_depth": 1, "catch_length": 2}),+ ),+ (+ "plastic.boss",+ json!({"position": [10, 10, 2], "diameter": 7, "height": 12, "hole_diameter": 2.5, "draft": 1, "ribs": 4, "rib_thickness": 1, "rib_length": 3, "rib_offset": 1}),+ ),+ ]);+ let ids: Vec<_> = app.session.doc.features.iter().rev().take(3).map(|f| f.id).collect();+ let (boss, snap, lip) = (ids[0], ids[1], ids[2]);+ let d = edit_dialog(&app, lip, None).unwrap();+ let before = apply_commands(&app, &d).unwrap();+ assert!(matches!(&d.kind, Kind::Plastic(Pl::Lip { width, height, .. }) if width == "1 mm" && height == "2 mm"));+ let d = edit_dialog(&app, snap, None).unwrap();+ assert!(matches!(&d.kind, Kind::Plastic(Pl::SnapFit { length, thickness, width, catch_depth, catch_length, .. })+ if length == "14 mm" && thickness == "1.5 mm" && width == "6 mm" && catch_depth == "1 mm" && catch_length == "2 mm"));+ let d = edit_dialog(&app, boss, None).unwrap();+ assert!(matches!(&d.kind, Kind::Plastic(Pl::Boss { diameter, height, hole_diameter, draft, ribs, rib_thickness, rib_length, rib_offset, .. })+ if diameter == "7 mm" && height == "12 mm" && hole_diameter == "2.5 mm" && draft == "1 deg" && ribs == "4"+ && rib_thickness == "1 mm" && rib_length == "3 mm" && rib_offset == "1 mm"));+ let c = apply_commands(&app, &d).unwrap();+ assert_eq!(c[0].1["params"]["diameter"], "7 mm");+ assert_eq!(c[0].1["params"]["draft"], "1 deg");+ assert_eq!(c[0].1["params"]["ribs"], "4");+ // Re-applying the dialog redefines the features in place (no new ones, no errors).+ let count = |app: &SolveApp| app.session.doc.features.len();+ let n = count(&app);+ run_all(&mut app, c);+ run_all(&mut app, before);+ assert_eq!(count(&app), n);+}++/// `shown_expr` adds the unit only to a bare number of a length or an angle.+#[test]+fn shown_expr_adds_units_to_bare_numbers() {+ use crate::dialogs::shown_expr;+ assert_eq!(shown_expr("2", ValueKind::Length), "2 mm");+ assert_eq!(shown_expr("-1.5", ValueKind::Length), "-1.5 mm");+ assert_eq!(shown_expr("5", ValueKind::Angle), "5 deg");+ assert_eq!(shown_expr("4", ValueKind::Unitless), "4");+ assert_eq!(shown_expr("2 mm", ValueKind::Length), "2 mm");+ assert_eq!(shown_expr("w * 2", ValueKind::Length), "w * 2");+ assert_eq!(shown_expr("", ValueKind::Length), "");+}