@bitbybit-dev/occt
Version:
Bit By Bit Developers CAD algorithms using OpenCascade Technology kernel. Run in Node and in Browser.
332 lines (331 loc) • 13.1 kB
JavaScript
import * as Inputs from "../../api/inputs/inputs";
export class OCCTWire {
constructor(occ, och) {
this.occ = occ;
this.och = och;
}
fromBaseLine(inputs) {
return this.createLineWire(inputs.line);
}
fromBaseLines(inputs) {
return inputs.lines.map(line => this.createLineWire(line));
}
fromBaseSegment(inputs) {
return this.createLineWire({ start: inputs.segment[0], end: inputs.segment[1] });
}
fromBaseSegments(inputs) {
return inputs.segments.map(segment => this.createLineWire({ start: segment[0], end: segment[1] }));
}
fromPoints(inputs) {
let wire;
if (inputs.points.length > 1) {
const start = inputs.points[0];
const end = inputs.points[1];
if (this.och.base.point.twoPointsAlmostEqual({ point1: start, point2: end })) {
wire = this.createPolygonWire({ points: inputs.points });
}
else {
wire = this.createPolylineWire({ points: inputs.points });
}
}
return wire;
}
fromBasePolyline(inputs) {
let wire;
const points = inputs.polyline.points;
if (inputs.polyline.isClosed) {
wire = this.createPolygonWire({ points: inputs.polyline.points });
}
else {
wire = this.createPolylineWire({ points: inputs.polyline.points });
}
return wire;
}
fromBaseTriangle(inputs) {
const points = inputs.triangle;
return this.fromBasePolyline({ polyline: { points, isClosed: true } });
}
fromBaseMesh(inputs) {
const wires = [];
inputs.mesh.forEach((triangle) => {
try {
wires.push(this.fromBaseTriangle({ triangle }));
}
catch (e) {
console.warn("Failed to make wire for triangle", triangle);
}
});
return wires.flat();
}
createPolygonWire(inputs) {
return this.och.wiresService.createPolygonWire(inputs);
}
createPolygons(inputs) {
const wires = inputs.polygons.map(p => this.createPolygonWire(p)).filter(s => s !== undefined);
return this.och.converterService.makeCompoundIfNeeded(wires, inputs.returnCompound);
}
createPolylineWire(inputs) {
return this.och.wiresService.createPolylineWire(inputs);
}
createPolylines(inputs) {
const wires = inputs.polylines.map(p => this.createPolylineWire(p)).filter(s => s !== undefined);
return this.och.converterService.makeCompoundIfNeeded(wires, inputs.returnCompound);
}
createLineWire(inputs) {
return this.och.wiresService.createLineWire(inputs);
}
createLineWireWithExtensions(inputs) {
return this.och.wiresService.createLineWireWithExtensions(inputs);
}
createLines(inputs) {
const wires = inputs.lines.map(p => this.createLineWire(p)).filter(s => s !== undefined);
return this.och.converterService.makeCompoundIfNeeded(wires, inputs.returnCompound);
}
createBezier(inputs) {
return this.och.wiresService.createBezier(inputs);
}
createBezierWires(inputs) {
const wires = inputs.bezierWires.map(p => this.createBezier(p)).filter(s => s !== undefined);
return this.och.converterService.makeCompoundIfNeeded(wires, inputs.returnCompound);
}
createBezierWeights(inputs) {
return this.och.wiresService.createBezierWeights(inputs);
}
interpolatePoints(inputs) {
return this.och.wiresService.interpolatePoints(inputs);
}
interpolateWires(inputs) {
const wires = inputs.interpolations.map(p => this.interpolatePoints(p)).filter(s => s !== undefined);
return this.och.converterService.makeCompoundIfNeeded(wires, inputs.returnCompound);
}
splitOnPoints(inputs) {
return this.och.wiresService.splitOnPoints(inputs);
}
combineEdgesAndWiresIntoAWire(inputs) {
return this.och.converterService.combineEdgesAndWiresIntoAWire(inputs);
}
createWireFromEdge(inputs) {
return this.och.wiresService.createWireFromEdge(inputs);
}
addEdgesAndWiresToWire(inputs) {
return this.och.wiresService.addEdgesAndWiresToWire(inputs);
}
createBSpline(inputs) {
return this.och.wiresService.createBSpline(inputs);
}
createBSplines(inputs) {
const wires = inputs.bSplines.map(p => this.createBSpline(p)).filter(s => s !== undefined);
return this.och.converterService.makeCompoundIfNeeded(wires, inputs.returnCompound);
}
hexagonsInGrid(inputs) {
return this.och.wiresService.hexagonsInGrid(inputs);
}
createZigZagBetweenTwoWires(inputs) {
return this.och.wiresService.createZigZagBetweenTwoWires(inputs);
}
divideWireByParamsToPoints(inputs) {
return this.och.wiresService.divideWireByParamsToPoints(inputs);
}
divideWiresByParamsToPoints(inputs) {
return inputs.shapes.map(s => this.divideWireByParamsToPoints(Object.assign(Object.assign({}, inputs), { shape: s })));
}
divideWireByEqualDistanceToPoints(inputs) {
return this.och.wiresService.divideWireByEqualDistanceToPoints(inputs);
}
divideWiresByEqualDistanceToPoints(inputs) {
return inputs.shapes.map(s => this.divideWireByEqualDistanceToPoints(Object.assign(Object.assign({}, inputs), { shape: s })));
}
pointOnWireAtParam(inputs) {
return this.och.wiresService.pointOnWireAtParam(inputs);
}
pointOnWireAtLength(inputs) {
return this.och.wiresService.pointOnWireAtLength(inputs);
}
pointsOnWireAtLengths(inputs) {
return this.och.wiresService.pointsOnWireAtLengths(inputs);
}
pointsOnWireAtEqualLength(inputs) {
return this.och.wiresService.pointsOnWireAtEqualLength(inputs);
}
pointsOnWireAtPatternOfLengths(inputs) {
return this.och.wiresService.pointsOnWireAtPatternOfLengths(inputs);
}
tangentOnWireAtParam(inputs) {
return this.och.wiresService.tangentOnWireAtParam(inputs);
}
tangentOnWireAtLength(inputs) {
return this.och.wiresService.tangentOnWireAtLength(inputs);
}
derivativesOnWireAtLength(inputs) {
const wire = inputs.shape;
const curve = new this.occ.BRepAdaptor_CompCurve_2(wire, false);
const absc = new this.occ.GCPnts_AbscissaPoint_2(curve, inputs.length, curve.FirstParameter());
const param = absc.Parameter();
const gpPnt = this.och.entitiesService.gpPnt([0, 0, 0]);
const der1 = this.och.entitiesService.gpVec([0, 0, 0]);
const der2 = this.och.entitiesService.gpVec([0, 0, 0]);
const der3 = this.och.entitiesService.gpVec([0, 0, 0]);
curve.D3(param, gpPnt, der1, der2, der3);
const der = [[der1.X(), der1.Y(), der1.Z()], [der2.X(), der2.Y(), der2.Z()], [der3.X(), der3.Y(), der3.Z()]];
der1.delete();
der2.delete();
der3.delete();
curve.delete();
absc.delete();
gpPnt.delete();
return der;
}
derivativesOnWireAtParam(inputs) {
const wire = inputs.shape;
const curve = new this.occ.BRepAdaptor_CompCurve_2(wire, false);
const gpPnt = this.och.entitiesService.gpPnt([0, 0, 0]);
const der1 = this.och.entitiesService.gpVec([0, 0, 0]);
const der2 = this.och.entitiesService.gpVec([0, 0, 0]);
const der3 = this.och.entitiesService.gpVec([0, 0, 0]);
const param = this.och.vecHelper.remap(inputs.param, 0, 1, curve.FirstParameter(), curve.LastParameter());
curve.D3(param, gpPnt, der1, der2, der3);
const der = [[der1.X(), der1.Y(), der1.Z()], [der2.X(), der2.Y(), der2.Z()], [der3.X(), der3.Y(), der3.Z()]];
der1.delete();
der2.delete();
der3.delete();
curve.delete();
gpPnt.delete();
return der;
}
startPointOnWire(inputs) {
return this.och.wiresService.startPointOnWire(inputs);
}
midPointOnWire(inputs) {
return this.och.wiresService.midPointOnWire(inputs);
}
endPointOnWire(inputs) {
return this.och.wiresService.endPointOnWire(inputs);
}
createCircleWire(inputs) {
return this.och.entitiesService.createCircle(inputs.radius, inputs.center, inputs.direction, Inputs.OCCT.typeSpecificityEnum.wire);
}
createEllipseWire(inputs) {
return this.och.entitiesService.createEllipse(inputs.radiusMinor, inputs.radiusMajor, inputs.center, inputs.direction, Inputs.OCCT.typeSpecificityEnum.wire);
}
textWires(inputs) {
return this.och.wiresService.textWires(inputs);
}
textWiresWithData(inputs) {
return this.och.wiresService.textWiresWithData(inputs);
}
createSquareWire(inputs) {
return this.och.wiresService.createSquareWire(inputs);
}
createStarWire(inputs) {
return this.och.wiresService.createStarWire(inputs);
}
createChristmasTreeWire(inputs) {
return this.och.wiresService.createChristmasTreeWire(inputs);
}
createParallelogramWire(inputs) {
return this.och.wiresService.createParallelogramWire(inputs);
}
createHeartWire(inputs) {
return this.och.wiresService.createHeartWire(inputs);
}
createNGonWire(inputs) {
return this.och.wiresService.createNGonWire(inputs);
}
createRectangleWire(inputs) {
return this.och.wiresService.createRectangleWire(inputs);
}
createLPolygonWire(inputs) {
return this.och.wiresService.createLPolygonWire(inputs);
}
getWire(inputs) {
return this.och.shapeGettersService.getWire(inputs);
}
getWires(inputs) {
return this.och.shapeGettersService.getWires(inputs);
}
getWireLength(inputs) {
return this.och.wiresService.getWireLength(inputs);
}
getWiresLengths(inputs) {
return this.och.wiresService.getWiresLengths(inputs);
}
isWireClosed(inputs) {
return this.och.wiresService.isWireClosed(inputs);
}
getWireCenterOfMass(inputs) {
return this.och.wiresService.getWireCenterOfMass(inputs);
}
getWiresCentersOfMass(inputs) {
return inputs.shapes.map(w => this.och.wiresService.getWireCenterOfMass({
shape: w
}));
}
reversedWire(inputs) {
return this.och.wiresService.reversedWire(inputs);
}
reversedWireFromReversedEdges(inputs) {
return this.och.wiresService.reversedWireFromReversedEdges(inputs);
}
placeWireOnFace(inputs) {
const wire = inputs.wire;
const face = inputs.face;
const srf = this.och.surfaceFromFace({ shape: face });
const result = this.och.wiresService.placeWire(wire, srf);
return result;
}
placeWiresOnFace(inputs) {
const wires = inputs.wires;
const face = inputs.face;
const srf = this.och.surfaceFromFace({ shape: face });
const result = wires.map(wire => this.och.wiresService.placeWire(wire, srf));
return result;
}
closeOpenWire(inputs) {
const wire = inputs.shape;
const firstPoint = this.och.wiresService.startPointOnWire({ shape: wire });
const lastPoint = this.och.wiresService.endPointOnWire({ shape: wire });
const tolerance = 1.0e-7;
if (this.och.vecHelper.vectorsTheSame(firstPoint, lastPoint, tolerance)) {
return wire;
}
const edgeWire = this.createLineWire({ start: lastPoint, end: firstPoint });
const result = this.addEdgesAndWiresToWire({ shape: wire, shapes: [edgeWire] });
edgeWire.delete();
return result;
}
project(inputs) {
const wire = inputs.wire;
const gpDir = this.och.entitiesService.gpDir(inputs.direction);
const proj = new this.occ.BRepProj_Projection_1(wire, inputs.shape, gpDir);
const shape = proj.Shape();
gpDir.delete();
proj.delete();
return shape;
}
wiresToPoints(inputs) {
const wires = this.getWires({ shape: inputs.shape });
const allWirePoints = [];
wires.forEach(w => {
const edgePoints = this.och.edgesService.edgesToPoints(Object.assign(Object.assign({}, inputs), { shape: w }));
const flatPoints = edgePoints.flat();
const dupsRemoved = this.och.vecHelper.removeConsecutiveDuplicates(flatPoints, false);
allWirePoints.push(dupsRemoved);
});
return allWirePoints;
}
projectWires(inputs) {
const shapes = [];
inputs.wires.forEach(wire => {
const gpDir = this.och.entitiesService.gpDir(inputs.direction);
const proj = new this.occ.BRepProj_Projection_1(wire, inputs.shape, gpDir);
const shape = proj.Shape();
shapes.push(shape);
gpDir.delete();
proj.delete();
});
return shapes;
}
createWireFromTwoCirclesTan(inputs) {
return this.och.wiresService.createWireFromTwoCirclesTan(inputs);
}
}