diff --git a/packages/base/src/commands.ts b/packages/base/src/commands.ts index 4301e932..74abc533 100644 --- a/packages/base/src/commands.ts +++ b/packages/base/src/commands.ts @@ -345,13 +345,19 @@ const OPERATORS = { default: (model: IJupyterCadModel) => { const objects = model.getAllObject(); const selected = model.localState?.selected.value || {}; - const sel0 = getSelectedMeshName(selected, 0); - const sel1 = getSelectedMeshName(selected, 1); - const baseName = sel0 || objects[0].name || ''; - const baseModel = model.sharedModel.getObjectByName(baseName); + + const selectedShapes = Object.keys(selected).map(key => key); + + // Fallback to at least two objects if selection is empty + const baseShapes = + selectedShapes.length > 0 + ? selectedShapes + : [objects[0].name || '', objects[1].name || '']; + + const baseModel = model.sharedModel.getObjectByName(baseShapes[0]); return { Name: newName('Union', model), - Shapes: [baseName, sel1 || objects[1].name || ''], + Shapes: baseShapes, Refine: false, Color: baseModel?.parameters?.Color || DEFAULT_MESH_COLOR, Placement: { Position: [0, 0, 0], Axis: [0, 0, 1], Angle: 0 } diff --git a/packages/occ-worker/src/occapi/fuse.ts b/packages/occ-worker/src/occapi/fuse.ts index dc4f6d42..62387b01 100644 --- a/packages/occ-worker/src/occapi/fuse.ts +++ b/packages/occ-worker/src/occapi/fuse.ts @@ -30,13 +30,27 @@ export function _Fuse( } } }); - const operator = new oc.BRepAlgoAPI_Fuse_3( - occShapes[0], - occShapes[1], - new oc.Message_ProgressRange_1() - ); - if (operator.IsDone()) { - return setShapePlacement(operator.Shape(), Placement); + + if (occShapes.length === 0) { + return; + } + + let fusedShape = occShapes[0]; + + for (let i = 1; i < occShapes.length; i++) { + const operator = new oc.BRepAlgoAPI_Fuse_3( + fusedShape, + occShapes[i], + new oc.Message_ProgressRange_1() + ); + + if (operator.IsDone()) { + fusedShape = operator.Shape(); + } else { + console.error(`Fusion failed at index ${i}`); + return; + } } - return; + + return setShapePlacement(fusedShape, Placement); }