Skip to content

Commit

Permalink
Fuse multiple objects at once (#659)
Browse files Browse the repository at this point in the history
* Consider multiple selection

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Can fuse multiple objects now

* cleanup

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
arjxn-py and pre-commit-ci[bot] authored Jan 6, 2025
1 parent 7dd07e0 commit 3210ee6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
16 changes: 11 additions & 5 deletions packages/base/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
30 changes: 22 additions & 8 deletions packages/occ-worker/src/occapi/fuse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 3210ee6

Please sign in to comment.