Skip to content

Commit c3b3106

Browse files
wip memory management
1 parent 92536f4 commit c3b3106

File tree

18 files changed

+831
-277
lines changed

18 files changed

+831
-277
lines changed

src/lib/api/bitbybit/occt/shapes/face.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export class OCCTFace {
1010
private readonly occWorkerManager: OCCTWorkerManager,
1111
) {
1212
}
13-
1413
/**
1514
* Creates a face from wire
1615
* <div>

src/lib/workers/occ/occ-helper.ts

Lines changed: 385 additions & 113 deletions
Large diffs are not rendered by default.

src/lib/workers/occ/occ-service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import * as Inputs from '../../api/inputs/inputs';
33
import { OccHelper } from './occ-helper';
44
import { OCCTBooleans } from './services/booleans';
55
import { OCCTGeom } from './services/geom/geom';
6-
// import { OCCTAdvanced } from './services/advanced/advanced';
7-
import { OCCTAdvanced } from './services/advanced-mock/advanced';
6+
import { OCCTAdvanced } from './services/advanced/advanced';
7+
// import { OCCTAdvanced } from './services/advanced-mock/advanced';
88
import { OCCTIO } from './services/io';
99
import { OCCTOperations } from './services/operations';
1010
import { OCCTShapes } from './services/shapes/shapes';
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
// import { OpenCascadeInstance } from '../../../../../bitbybit-dev-occt/bitbybit-dev-occt';
2-
// import { OccHelper } from '../../occ-helper';
3-
// import { OCCTIntersections } from './intersections';
1+
import { OpenCascadeInstance } from '../../../../../bitbybit-dev-occt/bitbybit-dev-occt';
2+
import { OccHelper } from '../../occ-helper';
3+
import { OCCTIntersections } from './intersections';
44

5-
// export class OCCTAdvanced {
6-
// public readonly intersections: OCCTIntersections;
5+
export class OCCTAdvanced {
6+
public readonly intersections: OCCTIntersections;
77

8-
// constructor(
9-
// occ: OpenCascadeInstance,
10-
// och: OccHelper
11-
// ) {
12-
// this.intersections = new OCCTIntersections(occ, och);
13-
// }
8+
constructor(
9+
occ: OpenCascadeInstance,
10+
och: OccHelper
11+
) {
12+
this.intersections = new OCCTIntersections(occ, och);
13+
}
1414

15-
// }
15+
}
Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
// import { OpenCascadeInstance, TopoDS_Shape } from '../../../../../bitbybit-dev-occt/bitbybit-dev-occt';
2-
// import { OccHelper } from '../../occ-helper';
3-
// import * as Inputs from '../../../../api/inputs/inputs';
4-
// // import { AdvIntersections } from 'projects/bitbybit-advanced/src/adv-intersections';
1+
import { OpenCascadeInstance, TopoDS_Shape } from '../../../../../bitbybit-dev-occt/bitbybit-dev-occt';
2+
import { OccHelper } from '../../occ-helper';
3+
import * as Inputs from '../../../../api/inputs/inputs';
4+
import { AdvIntersections } from 'projects/bitbybit-advanced/src/adv-intersections';
5+
import { TopoDS_Compound } from 'opencascade.js';
56

6-
// export class OCCTIntersections {
7+
export class OCCTIntersections {
78

8-
// private readonly advIntersections: AdvIntersections;
9+
private readonly advIntersections: AdvIntersections;
910

10-
// constructor(
11-
// private readonly occ: OpenCascadeInstance,
12-
// private readonly och: OccHelper
13-
// ) {
14-
// this.advIntersections = new AdvIntersections(occ, och);
15-
// }
11+
constructor(
12+
private readonly occ: OpenCascadeInstance,
13+
private readonly och: OccHelper
14+
) {
15+
this.advIntersections = new AdvIntersections(occ, och);
16+
}
1617

17-
// // Source code of this method is only available in proprietary bitbybit library that is not opensourced
18-
// slice(inputs: Inputs.OCCT.SliceDto<TopoDS_Shape>): TopoDS_Shape[] {
19-
// return this.advIntersections.slice(inputs);
20-
// }
18+
// Source code of this method is only available in proprietary bitbybit library that is not opensourced
19+
slice(inputs: Inputs.OCCT.SliceDto<TopoDS_Shape>): TopoDS_Compound {
20+
return this.advIntersections.slice(inputs);
21+
}
2122

22-
// }
23+
}

src/lib/workers/occ/services/booleans.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,21 @@ export class OCCTBooleans {
1313
union(inputs: Inputs.OCCT.UnionDto<TopoDS_Shape>): TopoDS_Shape {
1414
let combined = inputs.shapes[0];
1515
for (let i = 0; i < inputs.shapes.length; i++) {
16-
const combinedFuse = new this.occ.BRepAlgoAPI_Fuse_3(combined, inputs.shapes[i], new this.occ.Message_ProgressRange_1());
17-
combinedFuse.Build(new this.occ.Message_ProgressRange_1());
16+
const messageProgress1 = new this.occ.Message_ProgressRange_1();
17+
const combinedFuse = new this.occ.BRepAlgoAPI_Fuse_3(combined, inputs.shapes[i], messageProgress1);
18+
const messageProgress2 = new this.occ.Message_ProgressRange_1()
19+
combinedFuse.Build(messageProgress2);
1820
combined = combinedFuse.Shape();
21+
messageProgress1.delete();
22+
messageProgress2.delete();
23+
combinedFuse.delete();
1924
}
2025

2126
if (!inputs.keepEdges) {
2227
const fusor = new this.occ.ShapeUpgrade_UnifySameDomain_2(combined, true, true, false);
2328
fusor.Build();
2429
combined = fusor.Shape();
30+
fusor.delete();
2531
}
2632

2733
return combined;
@@ -32,25 +38,35 @@ export class OCCTBooleans {
3238
const objectsToSubtract = inputs.shapes;
3339
for (let i = 0; i < objectsToSubtract.length; i++) {
3440
if (!objectsToSubtract[i] || objectsToSubtract[i].IsNull()) { console.error('Tool in Difference is null!'); }
35-
const differenceCut = new this.occ.BRepAlgoAPI_Cut_3(difference, objectsToSubtract[i], new this.occ.Message_ProgressRange_1());
36-
differenceCut.Build(new this.occ.Message_ProgressRange_1());
41+
const messageProgress1 = new this.occ.Message_ProgressRange_1();
42+
const differenceCut = new this.occ.BRepAlgoAPI_Cut_3(difference, objectsToSubtract[i], messageProgress1);
43+
const messageProgress2 = new this.occ.Message_ProgressRange_1();
44+
differenceCut.Build(messageProgress2);
3745
difference = differenceCut.Shape();
46+
messageProgress1.delete();
47+
messageProgress2.delete();
48+
differenceCut.delete();
3849
}
3950

4051
if (!inputs.keepEdges) {
4152
const fusor = new this.occ.ShapeUpgrade_UnifySameDomain_2(difference, true, true, false);
4253
fusor.Build();
43-
difference = fusor.Shape();
54+
let fusedShape = fusor.Shape();
55+
difference.delete();
56+
difference = fusedShape;
57+
fusor.delete();
4458
}
4559

4660
if (this.och.getNumSolidsInCompound(difference) === 1) {
47-
difference = this.och.getSolidFromCompound(difference, 0);
61+
let solid = this.och.getSolidFromCompound(difference, 0);
62+
difference.delete();
63+
difference = solid;
4864
}
4965

5066
return difference;
5167
}
5268

53-
intersection(inputs: Inputs.OCCT.IntersectionDto<TopoDS_Shape>): TopoDS_Shape {
69+
intersection(inputs: Inputs.OCCT.IntersectionDto<TopoDS_Shape>): TopoDS_Shape[] {
5470
return this.och.intersection(inputs);
5571
}
5672

src/lib/workers/occ/services/fillets.ts

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@ export class OCCTFillets {
1919
inputs.shape, (this.occ.TopAbs_ShapeEnum.TopAbs_EDGE as TopAbs_ShapeEnum),
2020
(this.occ.TopAbs_ShapeEnum.TopAbs_SHAPE as TopAbs_ShapeEnum)
2121
);
22+
let edges = [];
2223
while (anEdgeExplorer.More()) {
2324
const anEdge = this.occ.TopoDS.Edge_1(anEdgeExplorer.Current());
25+
edges.push(anEdge);
2426
mkFillet.Add_2(inputs.radius, anEdge);
2527
anEdgeExplorer.Next();
2628
}
27-
inputs.shape = mkFillet.Shape();
28-
return inputs.shape;
29+
const result = mkFillet.Shape();
30+
mkFillet.delete();
31+
anEdgeExplorer.delete();
32+
edges.forEach(e => e.delete());
33+
return result;
2934
} else if (inputs.indexes && inputs.indexes.length > 0) {
3035
const mkFillet = new this.occ.BRepFilletAPI_MakeFillet(
3136
inputs.shape, (this.occ.ChFi3d_FilletShape.ChFi3d_Rational as ChFi3d_FilletShape)
@@ -50,7 +55,10 @@ export class OCCTFillets {
5055
else {
5156
curFillet = mkFillet.Shape();
5257
}
53-
return this.och.getActualTypeOfShape(curFillet);
58+
mkFillet.delete();
59+
let result = this.och.getActualTypeOfShape(curFillet);
60+
curFillet.delete();
61+
return result;
5462
}
5563
return undefined;
5664
}
@@ -64,19 +72,24 @@ export class OCCTFillets {
6472
inputs.shape, (this.occ.TopAbs_ShapeEnum.TopAbs_EDGE as TopAbs_ShapeEnum),
6573
(this.occ.TopAbs_ShapeEnum.TopAbs_SHAPE as TopAbs_ShapeEnum)
6674
);
75+
const edges = [];
6776
while (anEdgeExplorer.More()) {
6877
const anEdge = this.occ.TopoDS.Edge_1(anEdgeExplorer.Current());
78+
edges.push(anEdge);
6979
mkChamfer.Add_2(inputs.distance, anEdge);
7080
anEdgeExplorer.Next();
7181
}
72-
inputs.shape = mkChamfer.Shape();
73-
return inputs.shape;
82+
const result = mkChamfer.Shape();
83+
mkChamfer.delete();
84+
anEdgeExplorer.delete();
85+
edges.forEach(e => e.delete());
86+
return result;
7487
} else if (inputs.indexes && inputs.indexes.length > 0) {
7588
const mkChamfer = new this.occ.BRepFilletAPI_MakeChamfer(
7689
inputs.shape
7790
);
7891
let foundEdges = 0;
79-
let curFillet;
92+
let curChamfer;
8093
let distanceIndex = 0;
8194
this.och.forEachEdge(inputs.shape, (index, edge) => {
8295
if (inputs.indexes.includes(index)) {
@@ -90,13 +103,16 @@ export class OCCTFillets {
90103
}
91104
});
92105
if (foundEdges === 0) {
93-
console.error('Fillet Edges Not Found! Make sure you are looking at the object _before_ the Fillet is applied!');
94-
curFillet = inputs.shape;
106+
console.error('Chamfer Edges Not Found! Make sure you are looking at the object _before_ the Fillet is applied!');
107+
curChamfer = inputs.shape;
95108
}
96109
else {
97-
curFillet = mkChamfer.Shape();
110+
curChamfer = mkChamfer.Shape();
98111
}
99-
return this.och.getActualTypeOfShape(curFillet);
112+
mkChamfer.delete();
113+
let result = this.och.getActualTypeOfShape(curChamfer);
114+
curChamfer.delete();
115+
return result;
100116
}
101117
return undefined;
102118
}
@@ -115,7 +131,14 @@ export class OCCTFillets {
115131
}
116132
const filletedEdge = fil.Result(pt, edge1, edge2, solution);
117133

118-
return this.och.combineEdgesAndWiresIntoAWire({ shapes: [edge1, filletedEdge, edge2] });
134+
let result = this.och.combineEdgesAndWiresIntoAWire({ shapes: [edge1, filletedEdge, edge2] });
135+
fil.delete();
136+
pt.delete();
137+
pln.delete();
138+
edge1.delete();
139+
edge2.delete();
140+
filletedEdge.delete();
141+
return result;
119142
}
120143

121144
fillet2d(inputs: Inputs.OCCT.FilletDto<TopoDS_Wire | TopoDS_Face>): TopoDS_Face | TopoDS_Wire {
@@ -129,8 +152,13 @@ export class OCCTFillets {
129152
isShapeFace = true;
130153
} else if (inputs.shape.ShapeType() === this.occ.TopAbs_ShapeEnum.TopAbs_WIRE) {
131154
const faceBuilder = new this.occ.BRepBuilderAPI_MakeFace_15(inputs.shape, true);
132-
faceBuilder.Build(new this.occ.Message_ProgressRange_1());
133-
face = this.och.getActualTypeOfShape(faceBuilder.Shape());
155+
const messageProgress = new this.occ.Message_ProgressRange_1();
156+
faceBuilder.Build(messageProgress);
157+
let shape = faceBuilder.Shape();
158+
face = this.och.getActualTypeOfShape(shape);
159+
shape.delete();
160+
messageProgress.delete();
161+
faceBuilder.delete();
134162
} else {
135163
throw new Error(`You can only fillet a 2d wire or a 2d face.`);
136164
}
@@ -165,18 +193,25 @@ export class OCCTFillets {
165193
radiusAddedCounter++;
166194
}
167195
})
168-
filletMaker.Build(new this.occ.Message_ProgressRange_1());
196+
const messageProgress = new this.occ.Message_ProgressRange_1();
197+
filletMaker.Build(messageProgress);
198+
let result;
169199
if (isShapeFace) {
170-
return filletMaker.Shape();
200+
result = filletMaker.Shape();
171201
} else {
172202
const filletedWires = this.och.getWires({ shape: filletMaker.Shape() });
173203
if (filletedWires.length === 1) {
174-
return filletedWires[0];
204+
result = filletedWires[0];
175205
}
176206
else {
177207
throw new Error('There was an error when computing fillet.')
178208
}
179209
}
210+
anVertexExplorer.delete();
211+
filletMaker.delete();
212+
messageProgress.delete();
213+
cornerVertices.forEach(cvx => cvx.delete());
214+
return result;
180215
}
181216

182217
private applyRadiusToVertex(inputs: Inputs.OCCT.FilletDto<TopoDS_Shape>, filletMaker: BRepFilletAPI_MakeFillet2d_2, cvx: TopoDS_Vertex, index: number) {

src/lib/workers/occ/services/geom/curves.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,43 @@ export class OCCTCurves {
1212

1313
geom2dEllipse(inputs: Inputs.OCCT.Geom2dEllipseDto) {
1414
const axis2d = this.och.gpAx2d(inputs.center, inputs.direction);
15-
return new this.occ.Geom2d_Ellipse_2(axis2d, inputs.radiusMajor, inputs.radiusMinor, inputs.sense);
15+
let res = new this.occ.Geom2d_Ellipse_2(axis2d, inputs.radiusMajor, inputs.radiusMinor, inputs.sense);
16+
axis2d.delete();
17+
return res;
1618
}
1719

1820
geom2dCircle(inputs: Inputs.OCCT.Geom2dCircleDto) {
1921
const axis2d = this.och.gpAx2d(inputs.center, inputs.direction);
20-
return new this.occ.Geom2d_Circle_2(axis2d, inputs.radius, inputs.sense);
22+
let res = new this.occ.Geom2d_Circle_2(axis2d, inputs.radius, inputs.sense);
23+
axis2d.delete();
24+
return res;
2125
}
2226

2327
geom2dTrimmedCurve(inputs: Inputs.OCCT.Geom2dTrimmedCurveDto<Geom2d_Curve>) {
2428
const handleCurve = new this.occ.Handle_Geom2d_Curve_2(inputs.shape);
2529
const trimmed = new this.occ.Geom2d_TrimmedCurve(handleCurve, inputs.u1, inputs.u2, inputs.sense, inputs.theAdjustPeriodic);
30+
handleCurve.delete();
2631
return trimmed;
2732
}
2833

2934
geom2dSegment(inputs: Inputs.OCCT.Geom2dSegmentDto) {
3035
const pt1 = this.och.gpPnt2d(inputs.start);
3136
const pt2 = this.och.gpPnt2d(inputs.end);
3237
const res = new this.occ.GCE2d_MakeSegment_1(pt1, pt2);
33-
return res.Value().get();
38+
let resValue = res.Value();
39+
let r = resValue.get();
40+
pt1.delete();
41+
pt2.delete();
42+
resValue.delete();
43+
res.delete();
44+
return r;
3445
}
3546

3647
get2dPointFrom2dCurveOnParam(inputs: Inputs.OCCT.DataOnGeometryAtParamDto<Geom2d_Curve>) {
3748
const pt2d = inputs.shape.Value(inputs.param);
38-
return { result: [pt2d.X(), pt2d.Y()] };
49+
const pt = [pt2d.X(), pt2d.Y()]
50+
pt2d.delete();
51+
return { result: pt};
3952
}
4053

4154
geomCircleCurve(inputs: Inputs.OCCT.CircleDto): any {

src/lib/workers/occ/services/geom/surfaces.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ export class OCCTSurfaces {
1212

1313
cylindricalSurface(inputs: Inputs.OCCT.GeomCylindricalSurfaceDto) {
1414
const ax = this.och.gpAx3(inputs.center, inputs.direction);
15-
return new this.occ.Geom_CylindricalSurface_1(ax, inputs.radius);
15+
let res = new this.occ.Geom_CylindricalSurface_1(ax, inputs.radius);
16+
ax.delete();
17+
return res;
1618
}
1719

1820
surfaceFromFace(inputs: Inputs.OCCT.ShapeDto<TopoDS_Face>) {

0 commit comments

Comments
 (0)