@bitbybit-dev/occt
Version:
Bit By Bit Developers CAD algorithms using OpenCascade Technology kernel. Run in Node and in Browser.
88 lines (87 loc) • 3.85 kB
JavaScript
export class BooleansService {
constructor(occ, shapeGettersService) {
this.occ = occ;
this.shapeGettersService = shapeGettersService;
}
intersection(inputs) {
if (inputs.shapes.length < 2) {
throw (new Error("Intersection requires 2 or more shapes to be given"));
}
const intersectShape = inputs.shapes[0];
let intersectionResults = [];
for (let i = 1; i < inputs.shapes.length; i++) {
let intersectionResult;
const messageProgress = new this.occ.Message_ProgressRange_1();
const intersectedCommon = new this.occ.BRepAlgoAPI_Common_3(intersectShape, inputs.shapes[i], messageProgress);
const messageProgress2 = new this.occ.Message_ProgressRange_1();
if (intersectedCommon.HasGenerated()) {
intersectedCommon.Build(messageProgress2);
intersectionResult = intersectedCommon.Shape();
intersectionResults.push(intersectionResult);
}
messageProgress.delete();
intersectedCommon.delete();
messageProgress2.delete();
}
if (!inputs.keepEdges && intersectionResults.length > 0) {
intersectionResults = intersectionResults.map(i => {
const fusor = new this.occ.ShapeUpgrade_UnifySameDomain_2(i, true, true, false);
fusor.Build();
const fusedShape = fusor.Shape();
fusor.delete();
return fusedShape;
});
}
return intersectionResults;
}
difference(inputs) {
let difference = inputs.shape;
const objectsToSubtract = inputs.shapes;
for (let i = 0; i < objectsToSubtract.length; i++) {
if (!objectsToSubtract[i] || objectsToSubtract[i].IsNull()) {
console.error("Tool in Difference is null!");
}
const messageProgress1 = new this.occ.Message_ProgressRange_1();
const differenceCut = new this.occ.BRepAlgoAPI_Cut_3(difference, objectsToSubtract[i], messageProgress1);
const messageProgress2 = new this.occ.Message_ProgressRange_1();
differenceCut.Build(messageProgress2);
difference = differenceCut.Shape();
messageProgress1.delete();
messageProgress2.delete();
differenceCut.delete();
}
if (!inputs.keepEdges) {
const fusor = new this.occ.ShapeUpgrade_UnifySameDomain_2(difference, true, true, false);
fusor.Build();
const fusedShape = fusor.Shape();
difference.delete();
difference = fusedShape;
fusor.delete();
}
if (this.shapeGettersService.getNumSolidsInCompound(difference) === 1) {
const solid = this.shapeGettersService.getSolidFromCompound(difference, 0);
difference = solid;
}
return difference;
}
union(inputs) {
let combined = inputs.shapes[0];
for (let i = 0; i < inputs.shapes.length; i++) {
const messageProgress1 = new this.occ.Message_ProgressRange_1();
const combinedFuse = new this.occ.BRepAlgoAPI_Fuse_3(combined, inputs.shapes[i], messageProgress1);
const messageProgress2 = new this.occ.Message_ProgressRange_1();
combinedFuse.Build(messageProgress2);
combined = combinedFuse.Shape();
messageProgress1.delete();
messageProgress2.delete();
combinedFuse.delete();
}
if (!inputs.keepEdges) {
const fusor = new this.occ.ShapeUpgrade_UnifySameDomain_2(combined, true, true, false);
fusor.Build();
combined = fusor.Shape();
fusor.delete();
}
return combined;
}
}