UNPKG

@bitbybit-dev/occt

Version:

Bit By Bit Developers CAD algorithms using OpenCascade Technology kernel. Run in Node and in Browser.

151 lines (150 loc) 6.04 kB
export class OCCTTransforms { constructor(occ, och) { this.occ = occ; this.och = och; } transform(inputs) { const scaledShape = this.scale({ shape: inputs.shape, factor: inputs.scaleFactor }); const rotatedShape = this.rotate({ shape: scaledShape, axis: inputs.rotationAxis, angle: inputs.rotationAngle }); const translatedShape = this.translate({ shape: rotatedShape, translation: inputs.translation }); scaledShape.delete(); rotatedShape.delete(); return translatedShape; } rotate(inputs) { return this.och.transformsService.rotate(inputs); } rotateAroundCenter(inputs) { const shapeTranslated = this.translate({ shape: inputs.shape, translation: inputs.center.map(c => -c) }); const angle = inputs.angle; const rotatedShape = this.rotate({ shape: shapeTranslated, axis: inputs.axis, angle }); const result = this.translate({ shape: rotatedShape, translation: inputs.center }); rotatedShape.delete(); shapeTranslated.delete(); return result; } align(inputs) { return this.och.transformsService.align(inputs); } alignNormAndAxis(inputs) { return this.och.transformsService.alignNormAndAxis(inputs); } alignAndTranslate(inputs) { return this.och.transformsService.alignAndTranslate(inputs); } translate(inputs) { return this.och.transformsService.translate(inputs); } scale(inputs) { const transformation = new this.occ.gp_Trsf_1(); const gpPnt = this.och.entitiesService.gpPnt([0.0, 0.0, 0.0]); transformation.SetScale(gpPnt, inputs.factor); const transf = new this.occ.BRepBuilderAPI_Transform_2(inputs.shape, transformation, true); const s = transf.Shape(); const result = this.och.converterService.getActualTypeOfShape(s); gpPnt.delete(); transformation.delete(); transf.delete(); s.delete(); return result; } scale3d(inputs) { return this.och.transformsService.scale3d(inputs); } mirror(inputs) { return this.och.transformsService.mirror(inputs); } mirrorAlongNormal(inputs) { return this.och.transformsService.mirrorAlongNormal(inputs); } transformShapes(inputs) { this.checkIfListsEqualLength([inputs.shapes, inputs.translations, inputs.rotationAxes, inputs.rotationAngles, inputs.scaleFactors]); return inputs.shapes.map((s, index) => this.transform({ shape: s, translation: inputs.translations[index], rotationAxis: inputs.rotationAxes[index], rotationAngle: inputs.rotationAngles[index], scaleFactor: inputs.scaleFactors[index], })); } rotateShapes(inputs) { this.checkIfListsEqualLength([inputs.shapes, inputs.axes, inputs.angles]); return inputs.shapes.map((s, index) => this.rotate({ shape: s, axis: inputs.axes[index], angle: inputs.angles[index], })); } rotateAroundCenterShapes(inputs) { this.checkIfListsEqualLength([inputs.shapes, inputs.axes, inputs.angles]); return inputs.shapes.map((s, index) => this.rotateAroundCenter({ shape: s, axis: inputs.axes[index], angle: inputs.angles[index], center: inputs.centers[index], })); } alignShapes(inputs) { this.checkIfListsEqualLength([inputs.shapes, inputs.fromOrigins, inputs.fromDirections, inputs.toOrigins, inputs.toDirections]); return inputs.shapes.map((s, index) => this.align({ shape: s, fromOrigin: inputs.fromOrigins[index], fromDirection: inputs.fromDirections[index], toOrigin: inputs.toOrigins[index], toDirection: inputs.toDirections[index] })); } alignAndTranslateShapes(inputs) { this.checkIfListsEqualLength([inputs.shapes, inputs.centers, inputs.directions]); return inputs.shapes.map((s, index) => this.alignAndTranslate({ shape: s, center: inputs.centers[index], direction: inputs.directions[index], })); } translateShapes(inputs) { this.checkIfListsEqualLength([inputs.shapes, inputs.translations]); return inputs.shapes.map((s, index) => this.translate({ shape: s, translation: inputs.translations[index], })); } scaleShapes(inputs) { this.checkIfListsEqualLength([inputs.shapes, inputs.factors]); return inputs.shapes.map((s, index) => this.scale({ shape: s, factor: inputs.factors[index], })); } scale3dShapes(inputs) { this.checkIfListsEqualLength([inputs.shapes, inputs.scales, inputs.centers]); return inputs.shapes.map((s, index) => this.scale3d({ shape: s, scale: inputs.scales[index], center: inputs.centers[index], })); } mirrorShapes(inputs) { this.checkIfListsEqualLength([inputs.shapes, inputs.directions, inputs.origins]); return inputs.shapes.map((s, index) => this.mirror({ shape: s, origin: inputs.origins[index], direction: inputs.directions[index], })); } mirrorAlongNormalShapes(inputs) { this.checkIfListsEqualLength([inputs.shapes, inputs.normals, inputs.origins]); return inputs.shapes.map((s, index) => this.mirrorAlongNormal({ shape: s, normal: inputs.normals[index], origin: inputs.origins[index], })); } checkIfListsEqualLength(lists) { const firstLength = lists[0].length; const notSameLength = lists.some(s => s.length !== firstLength); if (notSameLength) { throw new Error("Some of the list lengths are not the same. For this operation to work all lists need to be of equal length"); } } }