Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Add a dialog for Create Components from Bodies (#10919)

Tarek0dd8d9e5ecdd
diff --git a/assets/locales/pending/components-from-bodies-dialog.json b/assets/locales/pending/components-from-bodies-dialog.json
new file mode 100644
index 0000000000..54990f2e91
--- /dev/null
+++ b/assets/locales/pending/components-from-bodies-dialog.json
@@ -0,0 +1,34 @@
+{
+ "en": {
+ "ui.components_from_bodies_hint": "Create one component per selected body, named after that body, under the active component.",
+ "ui.components_from_bodies_required": "Select at least one body."
+ },
+ "ar": {
+ "ui.components_from_bodies_hint": "أنشئ مكونًا لكل جسم محدد، باسم ذلك الجسم، ضمن المكون النشط.",
+ "ui.components_from_bodies_required": "حدد جسمًا واحدًا على الأقل."
+ },
+ "he": {
+ "ui.components_from_bodies_hint": "יצירת רכיב לכל גוף שנבחר, בשם הגוף, תחת הרכיב הפעיל.",
+ "ui.components_from_bodies_required": "יש לבחור גוף אחד לפחות."
+ },
+ "ja": {
+ "ui.components_from_bodies_hint": "選択した各ボディと同じ名前のコンポーネントを、アクティブなコンポーネントの下に作成します。",
+ "ui.components_from_bodies_required": "ボディを1つ以上選択してください。"
+ },
+ "vi": {
+ "ui.components_from_bodies_hint": "Tạo một thành phần cho mỗi vật thể đã chọn, đặt tên theo vật thể đó, bên dưới thành phần đang hoạt động.",
+ "ui.components_from_bodies_required": "Chọn ít nhất một vật thể."
+ },
+ "zh": {
+ "ui.components_from_bodies_hint": "在活动组件下,为每个选定实体创建一个以该实体命名的组件。",
+ "ui.components_from_bodies_required": "请至少选择一个实体。"
+ },
+ "de": {
+ "ui.components_from_bodies_hint": "Für jeden ausgewählten Körper eine gleichnamige Komponente unter der aktiven Komponente erstellen.",
+ "ui.components_from_bodies_required": "Mindestens einen Körper auswählen."
+ },
+ "ko": {
+ "ui.components_from_bodies_hint": "활성 구성요소 아래에 선택한 각 바디와 같은 이름의 구성요소를 만듭니다.",
+ "ui.components_from_bodies_required": "바디를 하나 이상 선택하세요."
+ }
+}
diff --git a/crates/ui-egui/src/dialogs_assembly.rs b/crates/ui-egui/src/dialogs_assembly.rs
index 22e52c9ec7..5110404de2 100644
--- a/crates/ui-egui/src/dialogs_assembly.rs
+++ b/crates/ui-egui/src/dialogs_assembly.rs
@@ -90,6 +90,7 @@ const MOVES: &[&str] = &[
#[derive(Clone, Debug)]
pub enum Asm {
+ FromBodies,
NewComponent {
name: String,
activate: bool,
@@ -196,6 +197,7 @@ impl JointForm {
impl Asm {
pub fn title(&self) -> &'static str {
match self {
+ Asm::FromBodies => "cmd.component_from_bodies",
Asm::NewComponent { .. } => "NEW COMPONENT",
Asm::Joint(j) if j.editing.is_some() => "EDIT JOINT",
Asm::Joint(j) if j.as_built => "AS-BUILT JOINT",
@@ -224,6 +226,7 @@ impl Asm {
pub fn start(app: &SolveApp, id: &str) -> Option<(Kind, Vec<SelInput>)> {
let s = &app.session;
let (asm, inputs) = match id {
+ "component.from_bodies" => (Asm::FromBodies, vec![SelInput::new("Bodies", BODIES, true)]),
"component.create" => (Asm::NewComponent { name: String::new(), activate: true }, vec![SelInput::new("Bodies (optional)", BODIES, true)]),
"joint.create" => (Asm::Joint(Box::new(JointForm::new(false, &app.session.doc))), side_inputs()),
"joint.as_built" => (
@@ -669,6 +672,16 @@ pub fn rows(app: &mut SolveApp, ui: &mut egui::Ui, k: &mut Asm, inputs: &mut [Se
let mut enter = false;
let problem = joint_problem(app, k, inputs);
match k {
+ Asm::FromBodies => {
+ ui.add(egui::Label::new(app.tr("ui.components_from_bodies_hint")).wrap());
+ if crate::ui_kit::button(ui, app.tr("ui.select_all"), crate::ui_kit::Button::Normal).clicked()
+ && let Some(input) = inputs.first_mut()
+ {
+ // Transfers take definition names, rather than the names of repeated placements.
+ input.items = app.session.model.state().bodies.iter().map(|body| Sel::Body { name: body.name.clone() }).collect();
+ *active = 0;
+ }
+ }
Asm::NewComponent { name, activate } => {
enter |= text_row(ui, "Name", name, crate::i18n::tr_label("Component1")).0;
crate::ui_kit::options(ui, "component_options", &mut [("Activate", activate)]);
@@ -1073,6 +1086,13 @@ pub fn commands(app: &SolveApp, k: &Asm, inputs: &[SelInput]) -> Result<Vec<(Str
};
let cmd = |id: &str, p: Value| vec![(id.to_string(), p)];
Ok(match k {
+ Asm::FromBodies => {
+ let bodies = sel_items(inputs, 0).body_names();
+ if bodies.is_empty() {
+ return Err(crate::i18n::tr("ui.components_from_bodies_required").into());
+ }
+ cmd("component.from_bodies", json!({ "bodies": bodies }))
+ }
Asm::NewComponent { name, activate } => {
let bodies: Vec<String> = sel_items(inputs, 0).body_names();
if bodies.is_empty() {
diff --git a/crates/ui-egui/src/dialogs_assembly_tests.rs b/crates/ui-egui/src/dialogs_assembly_tests.rs
index 691ebb3449..04510a3cf6 100644
--- a/crates/ui-egui/src/dialogs_assembly_tests.rs
+++ b/crates/ui-egui/src/dialogs_assembly_tests.rs
@@ -1179,3 +1179,55 @@ fn inch_joint_offsets_limits_and_drive_values_keep_their_units() {
let Kind::Assembly(Asm::Joint(f)) = edit.kind else { panic!("no edit") };
assert_eq!((f.limits[0].1.as_str(), f.limits[0].2.as_str()), ("0 in", "0.5 in"));
}
+
+/// The Assemble ribbon must open a body picker, without executing the JSON command yet.
+#[test]
+fn components_from_bodies_dialog_select_all_cancel_and_ok() {
+ let mut h = crate::scenario::Harness::new();
+ for x in [0, 30] {
+ h.app.run("solid.box", json!({"length":10,"width":10,"height":10,"corner":[x,0,0]})).unwrap();
+ }
+ let before = h.app.session.doc.clone();
+ let names: Vec<_> = h.app.session.model.state().bodies.iter().map(|body| body.name.to_string()).collect();
+ h.step(&json!({"start":"component.from_bodies"})).unwrap();
+ let d = h.app.dialog.as_ref().expect("From Bodies opens a dialog");
+ assert!(matches!(d.kind, Kind::Assembly(Asm::FromBodies)));
+ assert!(apply_commands(&h.app, d).is_err(), "OK requires at least one body");
+ assert_eq!(h.app.session.doc, before);
+ h.step(&json!({"widget":crate::i18n::tr("ui.select_all")})).unwrap();
+ let commands = apply_commands(&h.app, h.app.dialog.as_ref().unwrap()).unwrap();
+ assert_eq!(commands, vec![("component.from_bodies".into(), json!({"bodies":names}))]);
+ if let Ok(path) = std::env::var("SOLIDCRAFT_COMPONENTS_DIALOG_EVIDENCE") {
+ let ctx = crate::fonts::test_context();
+ let bounds = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1600.0, 1000.0));
+ let mut textures = Default::default();
+ for frame in 0..6 {
+ let mut output = ctx.run_ui(egui::RawInput { screen_rect: Some(bounds), ..Default::default() }, |ui| {
+ h.app.logic(ui.ctx());
+ h.app.ui(ui);
+ });
+ crate::ui_kit::test_textures_update(&mut textures, &output.textures_delta);
+ output.textures_delta.clear();
+ if frame == 5 {
+ crate::ui_kit::test_raster(&ctx, output.shapes, output.pixels_per_point, bounds, &textures).save(&path).unwrap();
+ }
+ }
+ }
+ h.step(&json!({"widget":"Cancel"})).unwrap();
+ assert!(h.app.dialog.is_none());
+ assert_eq!(h.app.session.doc, before, "Cancel makes no components");
+ // Existing body picks seed the same standard multi-selection input.
+ h.app.session.selection = vec![Sel::Body { name: names[0].clone() }];
+ h.step(&json!({"start":"component.from_bodies"})).unwrap();
+ let d = h.app.dialog.as_ref().unwrap();
+ assert_eq!(d.inputs[0].items.body_names(), vec![names[0].clone()]);
+ h.step(&json!({"widget":crate::i18n::tr("ui.select_all")})).unwrap();
+ h.step(&json!({"widget":"OK"})).unwrap();
+ assert!(h.app.dialog.is_none());
+ for name in &names {
+ let component = h.app.session.doc.find_component(name).expect("one component per body");
+ assert_eq!(h.app.session.doc.model_body_component(h.app.session.model.state().body(name).unwrap()), component);
+ }
+ h.app.run("edit.undo", json!({})).unwrap();
+ assert_eq!(h.app.session.doc, before, "the dialog applies one undoable command");
+}
diff --git a/crates/ui-egui/src/dialogs_dense.rs b/crates/ui-egui/src/dialogs_dense.rs
index df6d934bbf..72198eb7cc 100644
--- a/crates/ui-egui/src/dialogs_dense.rs
+++ b/crates/ui-egui/src/dialogs_dense.rs
@@ -86,6 +86,7 @@ pub(super) fn command(kind: &Kind) -> Option<&'static str> {
_ => k.simple_metadata().map(|(command, _)| command),
},
Kind::Assembly(k) => match k {
+ crate::dialogs_assembly::Asm::FromBodies => Some("component.from_bodies"),
crate::dialogs_assembly::Asm::NewComponent { .. } => Some("component.create"),
crate::dialogs_assembly::Asm::Joint(f) => Some(if f.as_built { "joint.as_built" } else { "joint.create" }),
crate::dialogs_assembly::Asm::JointOrigin { .. } => Some("joint.origin"),