@bitbybit-dev/occt
Version:
Bit By Bit Developers CAD algorithms using OpenCascade Technology kernel. Run in Node and in Browser.
207 lines (206 loc) • 7.8 kB
JavaScript
import * as Inputs from "../../api/inputs";
export class EntitiesService {
constructor(occ) {
this.occ = occ;
}
createCircle(radius, center, direction, type) {
const ax = this.gpAx2(center, direction);
if (type === Inputs.OCCT.typeSpecificityEnum.edge) {
const edge = this.occ.MakeCircleEdge(ax, radius);
ax.delete();
return edge;
}
else if (type === Inputs.OCCT.typeSpecificityEnum.wire) {
const wire = this.occ.MakeCircleWire(ax, radius);
ax.delete();
return wire;
}
else if (type === Inputs.OCCT.typeSpecificityEnum.face) {
const wire = this.occ.MakeCircleWire(ax, radius);
ax.delete();
const face = this.bRepBuilderAPIMakeFaceFromWire(wire, true);
wire.delete();
return face;
}
// Default: return wire for curve type as well (Geom_Curve not exposed)
const wire = this.occ.MakeCircleWire(ax, radius);
ax.delete();
return wire;
}
createEllipse(minorRadius, majorRadius, center, direction, type) {
const ax = this.gpAx2(center, direction);
if (type === Inputs.OCCT.typeSpecificityEnum.edge) {
const edge = this.occ.MakeEllipseEdge(ax, majorRadius, minorRadius);
ax.delete();
return edge;
}
else if (type === Inputs.OCCT.typeSpecificityEnum.wire) {
const wire = this.occ.MakeEllipseWire(ax, majorRadius, minorRadius);
ax.delete();
return wire;
}
else if (type === Inputs.OCCT.typeSpecificityEnum.face) {
const wire = this.occ.MakeEllipseWire(ax, majorRadius, minorRadius);
ax.delete();
const face = this.bRepBuilderAPIMakeFaceFromWire(wire, true);
wire.delete();
return face;
}
// Default: return wire for curve type as well (Geom_Curve not exposed)
const wire = this.occ.MakeEllipseWire(ax, majorRadius, minorRadius);
ax.delete();
return wire;
}
makeVertex(pt) {
const gpPnt = this.gpPnt(pt);
const vert = new this.occ.BRepBuilderAPI_MakeVertex(gpPnt);
const vrt = vert.Vertex();
gpPnt.delete();
vert.delete();
return vrt;
}
gpPnt2d(point) {
return new this.occ.gp_Pnt2d(point[0], point[1]);
}
gpPnt(point) {
return new this.occ.gp_Pnt(point[0], point[1], point[2]);
}
gpVec(vec) {
return new this.occ.gp_Vec(vec[0], vec[1], vec[2]);
}
gpXYZ(point) {
return new this.occ.gp_XYZ(point[0], point[1], point[2]);
}
gpVec2d(vec) {
return new this.occ.gp_Vec2d(vec[0], vec[1]);
}
gpDir(direction) {
return new this.occ.gp_Dir(direction[0], direction[1], direction[2]);
}
gpDir2d(direction) {
return new this.occ.gp_Dir2d(direction[0], direction[1]);
}
bRepBuilderAPIMakeWire(edge) {
const wire = new this.occ.BRepBuilderAPI_MakeWire(edge);
const w = wire.Wire();
wire.delete();
return w;
}
bRepBuilderAPIMakeFaceFromWires(wires, planar, guideFace, inside) {
let face;
const faces = [];
wires.forEach(currentWire => {
if (faces.length > 0) {
const faceBuilder = new this.occ.BRepBuilderAPI_MakeFace(faces[faces.length - 1], currentWire);
faces.push(faceBuilder.Face());
faceBuilder.delete();
}
else {
if (!guideFace) {
// Use factory function to avoid constructor overload conflicts
const newFace = this.occ.MakeFaceFromWireOnlyPlane(currentWire, planar);
faces.push(newFace);
}
else {
// Use factory function for surface-based face creation
const newFace = this.occ.MakeFaceFromFaceSurfaceAndWire(guideFace, currentWire, inside !== null && inside !== void 0 ? inside : true);
faces.push(newFace);
}
}
});
if (faces.length > 0) {
face = faces.pop();
faces.forEach(f => f.delete());
}
return face;
}
bRepBuilderAPIMakeFaceFromWire(wire, planar) {
const face = this.occ.MakeFaceFromWireOnlyPlane(wire, planar);
return face;
}
bRepBuilderAPIMakeFacesFromWiresOnFace(face, wires, inside) {
return wires.map(wire => this.occ.MakeFaceFromFaceSurfaceAndWire(face, wire, inside));
}
bRepBuilderAPIMakeFaceFromWireOnFace(face, wire, inside) {
return this.occ.MakeFaceFromFaceSurfaceAndWire(face, wire, inside);
}
bRepPrimAPIMakeSphere(center, direction, radius) {
const ax = this.gpAx2(center, direction);
const sphereMaker = this.occ.MakeSphereFromAx2(ax, radius);
const sphere = sphereMaker.Shape();
sphereMaker.delete();
ax.delete();
return sphere;
}
gpAx3_3(point, normal, direction) {
return new this.occ.gp_Ax3(this.gpPnt(point), this.gpDir(normal), this.gpDir(direction));
}
gpAx3_4(point, direction) {
return new this.occ.gp_Ax3(this.gpPnt(point), this.gpDir(direction));
}
gpAx2(point, direction) {
return new this.occ.gp_Ax2(this.gpPnt(point), this.gpDir(direction));
}
gpAx2FromTwoVectors(point, directionFirst, directionSecond) {
return new this.occ.gp_Ax2(this.gpPnt(point), this.gpDir(directionFirst), this.gpDir(directionSecond));
}
gpAx1(point, direction) {
return new this.occ.gp_Ax1(this.gpPnt(point), this.gpDir(direction));
}
gpAx2d(point, direction) {
const pt = this.gpPnt2d(point);
const dir = this.gpDir2d(direction);
return new this.occ.gp_Ax2d(pt, dir);
}
gpAx22d(point, direction1, direction2) {
const pt = this.gpPnt2d(point);
const dir1 = this.gpDir2d(direction1);
const dir2 = this.gpDir2d(direction2);
const ax = new this.occ.gp_Ax22d(pt, dir1, dir2);
dir1.delete();
dir2.delete();
pt.delete();
return ax;
}
gpPln(point, direction) {
const gpPnt = this.gpPnt(point);
const gpDir = this.gpDir(direction);
const pln = new this.occ.gp_Pln(gpPnt, gpDir);
gpPnt.delete();
gpDir.delete();
return pln;
}
bRepPrimAPIMakeCylinder(center, direction, radius, height, angle) {
const ax = this.gpAx2(center, direction);
const cylinderMaker = new this.occ.BRepPrimAPI_MakeCylinder(ax, radius, height, angle);
const cylinder = cylinderMaker.Shape();
cylinderMaker.delete();
ax.delete();
return cylinder;
}
bRepPrimAPIMakeCylinderBetweenPoints(start, end, radius) {
const center = this.gpPnt(start);
const pt = this.gpPnt(end);
const vec = this.occ.gp_Vec_fromPoints(center, pt);
const distance = vec.Magnitude();
const ax = this.gpAx2(start, [vec.X(), vec.Y(), vec.Z()]);
const cylinderMaker = new this.occ.BRepPrimAPI_MakeCylinder(ax, radius, distance);
const cylinder = cylinderMaker.Shape();
cylinderMaker.delete();
ax.delete();
center.delete();
pt.delete();
vec.delete();
return cylinder;
}
bRepPrimAPIMakeBox(width, length, height, center) {
const pt = this.gpPnt([
-width / 2 + center[0],
-height / 2 + center[1],
-length / 2 + center[2]
]);
const box = this.occ.MakeBoxFromPntAndDims(pt, width, height, length);
pt.delete();
return box;
}
}