All files / lib/services/base operations.service.ts

89.69% Statements 357/398
80.76% Branches 84/104
91.52% Functions 54/59
90.15% Lines 339/376

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710                                                                            8x 1x   7x 7x 7x 7x 1x 1x 1x   7x 1x 6x 1x 1x 3x     3x 3x       1x 33x 11x 11x 11x     7x 6x 19x     7x 7x 1x 1x 1x   7x     7x 7x   7x 7x 1x 6x 5x 1x 1x   7x 7x   7x 7x 7x 7x 7x 11x 7x 7x 7x         22x 22x             22x 22x 22x 22x 22x 22x             5x 5x 5x       5x 1x 1x 10x 10x   1x       6x 6x 1x 6x         3x 3x 9x     9x   3x 3x 3x 3x 3x 3x       8x       8x 8x   8x   8x   8x   8x     5x 1x 1x   4x   5x 5x 5x 3x   2x   5x 5x 5x 5x                               3x 3x 3x                       8x 8x 8x 8x 8x   8x 8x       6x         6x         6x 6x   6x 6x   6x 6x 140x 32x 32x         6x 6x 6x 6x           6x         1x 2x 2x 2x 2x         14x 14x           14x 14x 14x 14x                         2x 2x   2x 1x 1x 1x 1x     1x 1x 1x 1x 1x   1x 1x 1x 1x     1x 1x 1x 1x 1x   2x 2x 2x       1x       1x               1x                 1x 1x 1x 31x 31x             1x     1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x       2x 2x 3x   2x 2x 2x 2x 2x 2x 2x       2x 2x 2x 2x 5x 5x   5x   5x 2x 2x 2x   5x 5x 5x 2x 2x 2x 2x       2x 2x 7x     2x 2x 2x 2x 2x 2x 2x       4x 4x 4x 4x 10x 10x   10x   10x 4x 4x 4x   10x 10x 10x 4x 4x 4x 4x       4x 4x 14x     4x 4x 4x 4x 4x 4x 4x       1x 2x         7x 7x 7x 7x   7x 7x 7x 4x 84x 4x 4x   7x 7x 7x                                                               8x   8x 8x                     8x                                             8x 2x   6x 6x 6x 6x   6x 6x 327x 327x     6x   4x 4x       6x 2x   4x 4x 4x 4x   4x   4x 4x 215x 215x 215x 173x   42x       4x   2x 2x                 10x             10x 10x 10x       10x 10x 10x 10x   10x 10x   10x 10x   10x 10x   10x 10x 10x 10x         10x 10x 2x   8x 8x     10x 4x     6x 10x 10x 10x   10x 10x   10x 10x 10x 10x             10x            
import {
    Approx_ParametrizationType, BRepFill_TypeOfContact, BRepOffsetAPI_MakeOffsetShape,
    BRepOffsetAPI_MakeOffset_1, BRepOffset_Mode, Bnd_Box_1, Extrema_ExtAlgo, Extrema_ExtFlag,
    GeomAbs_JoinType, OpenCascadeInstance, TopoDS_Compound, TopoDS_Edge, TopoDS_Face, TopoDS_Shape, TopoDS_Vertex, TopoDS_Wire
} from "../../../bitbybit-dev-occt/bitbybit-dev-occt";
import { VectorHelperService } from "../../api/vector-helper.service";
import * as Inputs from "../../api/inputs/inputs";
import { Base } from "../../api/inputs/base-inputs";
import { EnumService } from "./enum.service";
import { EntitiesService } from "./entities.service";
import { ConverterService } from "./converter.service";
import { BooleansService } from "./booleans.service";
import { TransformsService } from "./transforms.service";
import { ShapeGettersService } from "./shape-getters";
import { EdgesService } from "./edges.service";
import { WiresService } from "./wires.service";
import { FacesService } from "./faces.service";
import { ShellsService } from "./shells.service";
 
export class OperationsService {
 
    constructor(
        private readonly occ: OpenCascadeInstance,
        private readonly enumService: EnumService,
        private readonly entitiesService: EntitiesService,
        private readonly converterService: ConverterService,
        private readonly booleansService: BooleansService,
        private readonly shapeGettersService: ShapeGettersService,
        private readonly edgesService: EdgesService,
        private readonly transformsService: TransformsService,
        private readonly vecHelper: VectorHelperService,
        private readonly wiresService: WiresService,
        private readonly facesService: FacesService,
        private readonly shellsService: ShellsService,
 
    ) { }
 
    loftAdvanced(inputs: Inputs.OCCT.LoftAdvancedDto<TopoDS_Wire | TopoDS_Edge>) {
        if (inputs.periodic && !inputs.closed) {
            throw new Error("Cant construct periodic non closed loft.");
        }
        const pipe = new this.occ.BRepOffsetAPI_ThruSections(inputs.makeSolid, inputs.straight, inputs.tolerance);
        const wires: TopoDS_Wire[] = [];
        const vertices: TopoDS_Vertex[] = [];
        if (inputs.startVertex) {
            const v = this.entitiesService.makeVertex(inputs.startVertex);
            pipe.AddVertex(v);
            vertices.push(v);
        }
        if (inputs.closed && !inputs.periodic) {
            inputs.shapes.push(inputs.shapes[0]);
        } else if (inputs.closed && inputs.periodic) {
            const pointsOnCrvs: Inputs.Base.Point3[][] = [];
            inputs.shapes.forEach((s: TopoDS_Wire | TopoDS_Edge) => {
                Iif (this.enumService.getShapeTypeEnum(s) === Inputs.OCCT.shapeTypeEnum.edge) {
                    s = this.entitiesService.bRepBuilderAPIMakeWire(s);
                }
                const pts = this.wiresService.divideWireByParamsToPoints({ shape: s, nrOfDivisions: inputs.nrPeriodicSections, removeStartPoint: false, removeEndPoint: false });
                pointsOnCrvs.push(pts);
            });
 
            // <= needed due to start and end points that are added
            for (let i = 0; i <= inputs.nrPeriodicSections; i++) {
                const ptsForPerpWire = pointsOnCrvs.map(p => p[i]);
                const periodicWire = this.wiresService.interpolatePoints({ points: ptsForPerpWire, tolerance: inputs.tolerance, periodic: true });
                pipe.AddWire(periodicWire);
                wires.push(periodicWire);
            }
        }
        if (!inputs.periodic) {
            inputs.shapes.forEach((wire) => {
                pipe.AddWire(wire);
            });
        }
        const endVertices: TopoDS_Vertex[] = [];
        if (inputs.endVertex) {
            const v = this.entitiesService.makeVertex(inputs.endVertex);
            pipe.AddVertex(v);
            endVertices.push(v);
        }
        Iif (inputs.useSmoothing) {
            pipe.SetSmoothing(inputs.useSmoothing);
        }
        Eif (inputs.maxUDegree) {
            pipe.SetMaxDegree(inputs.maxUDegree);
        }
        let parType: Approx_ParametrizationType | undefined = undefined;
        if (inputs.parType === Inputs.OCCT.approxParametrizationTypeEnum.approxChordLength) {
            parType = this.occ.Approx_ParametrizationType.Approx_ChordLength as Approx_ParametrizationType;
        } else if (inputs.parType === Inputs.OCCT.approxParametrizationTypeEnum.approxCentripetal) {
            parType = this.occ.Approx_ParametrizationType.Approx_Centripetal as Approx_ParametrizationType;
        } else Eif (inputs.parType === Inputs.OCCT.approxParametrizationTypeEnum.approxIsoParametric) {
            parType = this.occ.Approx_ParametrizationType.Approx_IsoParametric as Approx_ParametrizationType;
        }
        Eif (parType) {
            pipe.SetParType(parType);
        }
        pipe.CheckCompatibility(false);
        const pipeShape = pipe.Shape();
        const res = this.converterService.getActualTypeOfShape(pipeShape);
        pipeShape.delete();
        pipe.delete();
        wires.forEach(w => w.delete());
        vertices.forEach(v => v.delete());
        endVertices.forEach(v => v.delete());
        return res;
    }
 
 
    closestPointsBetweenTwoShapes(shape1: TopoDS_Shape, shape2: TopoDS_Shape): [Base.Point3, Base.Point3] {
        const messageProgress = new this.occ.Message_ProgressRange_1();
        const extrema = new this.occ.BRepExtrema_DistShapeShape_2(
            shape1,
            shape2,
            this.occ.Extrema_ExtFlag.Extrema_ExtFlag_MIN as Extrema_ExtFlag,
            this.occ.Extrema_ExtAlgo.Extrema_ExtAlgo_Grad as Extrema_ExtAlgo,
            messageProgress
        );
        const messageProgress1 = new this.occ.Message_ProgressRange_1();
        extrema.Perform(messageProgress1);
        if (extrema.IsDone() && extrema.NbSolution() > 0) {
            const closestPoint1 = extrema.PointOnShape1(1);
            const closestPoint2 = extrema.PointOnShape2(1);
            return [[closestPoint1.X(), closestPoint1.Y(), closestPoint1.Z()], [closestPoint2.X(), closestPoint2.Y(), closestPoint2.Z()]];
        } else E{
            throw new Error("Closest points could not be found.");
        }
    }
 
    closestPointsOnShapeFromPoints(inputs: Inputs.OCCT.ClosestPointsOnShapeFromPointsDto<TopoDS_Shape>): Inputs.Base.Point3[] {
        const vertexes = inputs.points.map(p => this.entitiesService.makeVertex(p));
        const pointsOnShape = vertexes.map(v => this.closestPointsBetweenTwoShapes(v, inputs.shape));
        return pointsOnShape.map(p => p[1]);
    }
 
    closestPointsOnShapesFromPoints(inputs: Inputs.OCCT.ClosestPointsOnShapesFromPointsDto<TopoDS_Shape>): Inputs.Base.Point3[] {
        const vertexes = inputs.points.map(p => this.entitiesService.makeVertex(p));
        const result: Inputs.Base.Point3[] = [];
        inputs.shapes.forEach((s) => {
            const pointsOnShape = vertexes.map(v => this.closestPointsBetweenTwoShapes(v, s));
            result.push(...pointsOnShape.map(p => p[1]));
        });
        return result;
    }
 
    distancesToShapeFromPoints(inputs: Inputs.OCCT.ClosestPointsOnShapeFromPointsDto<TopoDS_Shape>): number[] {
        const vertexes = inputs.points.map(p => this.entitiesService.makeVertex(p));
        const pointsOnShape = vertexes.map(v => this.closestPointsBetweenTwoShapes(v, inputs.shape));
        return pointsOnShape.map(p => {
            return this.vecHelper.distanceBetweenPoints(p[0], p[1]);
        });
    }
 
    loft(inputs: Inputs.OCCT.LoftDto<TopoDS_Wire | TopoDS_Edge>) {
        const pipe = new this.occ.BRepOffsetAPI_ThruSections(inputs.makeSolid, false, 1.0e-06);
        inputs.shapes.forEach((wire) => {
            Iif (this.enumService.getShapeTypeEnum(wire) === Inputs.OCCT.shapeTypeEnum.edge) {
                wire = this.entitiesService.bRepBuilderAPIMakeWire(wire);
            }
            pipe.AddWire(wire);
        });
        pipe.CheckCompatibility(false);
        const pipeShape = pipe.Shape();
        const res = this.converterService.getActualTypeOfShape(pipeShape);
        pipeShape.delete();
        pipe.delete();
        return res;
    }
 
    offset(inputs: Inputs.OCCT.OffsetDto<TopoDS_Shape, TopoDS_Face>) {
        return this.offsetAdv({ shape: inputs.shape, face: inputs.face, distance: inputs.distance, tolerance: inputs.tolerance, joinType: Inputs.OCCT.joinTypeEnum.arc, removeIntEdges: false });
    }
 
    offsetAdv(inputs: Inputs.OCCT.OffsetAdvancedDto<TopoDS_Shape, TopoDS_Face>) {
        Iif (!inputs.tolerance) { inputs.tolerance = 0.1; }
        Iif (inputs.distance === 0.0) { return inputs.shape; }
        let offset: BRepOffsetAPI_MakeOffset_1 | BRepOffsetAPI_MakeOffsetShape;
        const joinType: GeomAbs_JoinType = this.getJoinType(inputs.joinType);
        // only this mode is implemented currently, so we cannot expose others...
        const brepOffsetMode: BRepOffset_Mode = this.occ.BRepOffset_Mode.BRepOffset_Skin as BRepOffset_Mode;
 
        const wires: TopoDS_Wire[] = [];
 
        if ((this.enumService.getShapeTypeEnum(inputs.shape) === Inputs.OCCT.shapeTypeEnum.wire ||
            this.enumService.getShapeTypeEnum(inputs.shape) === Inputs.OCCT.shapeTypeEnum.edge)) {
            let wire: TopoDS_Wire;
            if (this.enumService.getShapeTypeEnum(inputs.shape) === Inputs.OCCT.shapeTypeEnum.edge) {
                wire = this.entitiesService.bRepBuilderAPIMakeWire(inputs.shape);
                wires.push(wire);
            } else {
                wire = inputs.shape;
            }
            try {
                offset = new this.occ.BRepOffsetAPI_MakeOffset_1();
                if (inputs.face) {
                    offset.Init_1(inputs.face, joinType, false);
                } else {
                    offset.Init_2(joinType, false);
                }
                offset.AddWire(wire);
                const messageProgress1 = new this.occ.Message_ProgressRange_1();
                offset.Build(messageProgress1);
                offset.Perform(inputs.distance, 0.0);
            } catch (ex) {
                offset = new this.occ.BRepOffsetAPI_MakeOffsetShape();
                (offset as BRepOffsetAPI_MakeOffsetShape).PerformByJoin(
                    wire,
                    inputs.distance,
                    inputs.tolerance,
                    brepOffsetMode,
                    false,
                    false,
                    joinType,
                    inputs.removeIntEdges,
                    new this.occ.Message_ProgressRange_1()
                );
            }
        } else {
            const shapeToOffset = inputs.shape;
            offset = new this.occ.BRepOffsetAPI_MakeOffsetShape();
            (offset as BRepOffsetAPI_MakeOffsetShape).PerformByJoin(
                shapeToOffset,
                inputs.distance,
                inputs.tolerance,
                brepOffsetMode,
                false,
                false,
                joinType,
                inputs.removeIntEdges,
                new this.occ.Message_ProgressRange_1()
            );
        }
        const offsetShape = offset.Shape();
        const result = this.converterService.getActualTypeOfShape(offsetShape);
        offsetShape.delete();
        Eif (offset) {
            offset.delete();
        }
        wires.forEach(w => w.delete());
        return result;
    }
 
    offset3DWire(inputs: Inputs.OCCT.Offset3DWireDto<TopoDS_Wire>): TopoDS_Wire | TopoDS_Edge[] {
        const extrusion = this.extrude({
            shape: inputs.shape,
            direction: inputs.direction,
        });
 
        const thickSolid = this.makeThickSolidSimple({
            shape: extrusion,
            offset: inputs.offset,
        });
 
        const nrOfEdges = this.shapeGettersService.getEdges({ shape: inputs.shape }).length;
        const predictedNrOfFaces = nrOfEdges * 4 + 2;
 
        const lastFaceIndex = predictedNrOfFaces / 2 - 1;
        const firstFaceIndex = lastFaceIndex - nrOfEdges + 1;
 
        const faceEdges: TopoDS_Edge[] = [];
        this.shapeGettersService.getFaces({ shape: thickSolid }).forEach((f, index) => {
            if (index >= firstFaceIndex && index <= lastFaceIndex) {
                const firstEdge = this.shapeGettersService.getEdges({ shape: f })[2];
                faceEdges.push(firstEdge);
            }
        });
 
        let result: TopoDS_Wire | TopoDS_Edge[];
        try {
            result = this.converterService.combineEdgesAndWiresIntoAWire({ shapes: faceEdges });
            result = result.Reversed();
            result = this.converterService.getActualTypeOfShape(result);
        }
        catch {
            result = faceEdges;
            return result;
        }
        return result;
 
    }
 
    extrudeShapes(inputs: Inputs.OCCT.ExtrudeShapesDto<TopoDS_Shape>): TopoDS_Shape[] {
        return inputs.shapes.map(shape => {
            const extruded = this.extrude({ shape, direction: inputs.direction });
            const result = this.converterService.getActualTypeOfShape(extruded);
            extruded.delete();
            return result;
        });
    }
 
    extrude(inputs: Inputs.OCCT.ExtrudeDto<TopoDS_Shape>): TopoDS_Shape {
        const gpVec = new this.occ.gp_Vec_4(inputs.direction[0], inputs.direction[1], inputs.direction[2]);
        const prismMaker = new this.occ.BRepPrimAPI_MakePrism_1(
            inputs.shape,
            gpVec,
            false,
            true
        );
        const prismShape = prismMaker.Shape();
        prismMaker.delete();
        gpVec.delete();
        return prismShape;
    }
 
    splitShapeWithShapes(inputs: Inputs.OCCT.SplitDto<TopoDS_Shape>): TopoDS_Shape {
        const listOfShapes = new this.occ.TopTools_ListOfShape_1();
        inputs.shapes.forEach(shape => {
            listOfShapes.Append_1(shape);
        });
        const shape = this.occ.BitByBitDev.BitSplit(inputs.shape, listOfShapes);
        return shape;
    }
 
    revolve(inputs: Inputs.OCCT.RevolveDto<TopoDS_Shape>) {
        Iif (!inputs.angle) { inputs.angle = 360.0; }
        Iif (!inputs.direction) { inputs.direction = [0, 0, 1]; }
        let result;
        if (inputs.angle >= 360.0) {
            const pt1 = new this.occ.gp_Pnt_3(0, 0, 0);
            const dir = new this.occ.gp_Dir_4(inputs.direction[0], inputs.direction[1], inputs.direction[2]);
            const ax1 = new this.occ.gp_Ax1_2(pt1, dir);
            const makeRevol = new this.occ.BRepPrimAPI_MakeRevol_2(inputs.shape,
                ax1,
                inputs.copy);
            result = makeRevol.Shape();
            makeRevol.delete();
            pt1.delete();
            dir.delete();
            ax1.delete();
        } else {
            const pt1 = new this.occ.gp_Pnt_3(0, 0, 0);
            const dir = new this.occ.gp_Dir_4(inputs.direction[0], inputs.direction[1], inputs.direction[2]);
            const ax1 = new this.occ.gp_Ax1_2(pt1, dir);
            const makeRevol = new this.occ.BRepPrimAPI_MakeRevol_1(inputs.shape,
                ax1,
                inputs.angle * 0.0174533, inputs.copy);
            result = makeRevol.Shape();
            makeRevol.delete();
            pt1.delete();
            dir.delete();
            ax1.delete();
        }
        const actual = this.converterService.getActualTypeOfShape(result);
        result.delete();
        return actual;
    }
 
    rotatedExtrude(inputs: Inputs.OCCT.RotationExtrudeDto<TopoDS_Shape>) {
        const translatedShape = this.transformsService.translate({
            translation: [0, inputs.height, 0],
            shape: inputs.shape,
        });
        const upperPolygon = this.transformsService.rotate(
            {
                axis: [0, 1, 0],
                angle: inputs.angle,
                shape: translatedShape
            });
 
        // Define the straight spine going up the middle of the sweep
        const spineWire = this.wiresService.createBSpline({
            points: [
                [0, 0, 0],
                [0, inputs.height, 0]
            ],
            closed: false,
        });
 
        // Define the guiding helical auxiliary spine (which controls the rotation)
        const steps = 30;
        const aspinePoints: Inputs.Base.Point3[] = [];
        for (let i = 0; i <= steps; i++) {
            const alpha = i / steps;
            aspinePoints.push([
                20 * Math.sin(alpha * inputs.angle * 0.0174533),
                inputs.height * alpha,
                20 * Math.cos(alpha * inputs.angle * 0.0174533),
            ]);
        }
 
        const aspineWire = this.wiresService.createBSpline({ points: aspinePoints, closed: false });
 
        // Sweep the face wires along the spine to create the extrusion
        const pipe = new this.occ.BRepOffsetAPI_MakePipeShell(spineWire);
        pipe.SetMode_5(aspineWire, true, (this.occ.BRepFill_TypeOfContact.BRepFill_NoContact as BRepFill_TypeOfContact));
        pipe.Add_1(inputs.shape, false, false);
        pipe.Add_1(upperPolygon, false, false);
        pipe.Build(new this.occ.Message_ProgressRange_1());
        pipe.MakeSolid();
 
        const pipeShape = pipe.Shape();
        const result = this.converterService.getActualTypeOfShape(pipeShape);
        pipeShape.delete();
        pipe.delete();
        aspineWire.delete();
        spineWire.delete();
        upperPolygon.delete();
        translatedShape.delete();
        return result;
    }
 
    pipe(inputs: Inputs.OCCT.ShapeShapesDto<TopoDS_Wire, TopoDS_Shape>) {
        const pipe = new this.occ.BRepOffsetAPI_MakePipeShell(inputs.shape);
        inputs.shapes.forEach(sh => {
            pipe.Add_1(sh, false, false);
        });
        pipe.Build(new this.occ.Message_ProgressRange_1());
        pipe.MakeSolid();
        const pipeShape = pipe.Shape();
        const result = this.converterService.getActualTypeOfShape(pipeShape);
        pipeShape.delete();
        pipe.delete();
        return result;
    }
 
    pipePolylineWireNGon(inputs: Inputs.OCCT.PipePolygonWireNGonDto<TopoDS_Wire>) {
        const wire = inputs.shape;
        const shapesToPassThrough: TopoDS_Shape[] = [];
        const edges = this.shapeGettersService.getEdges({ shape: wire });
        edges.forEach((e, index) => {
            const edgeStartPt = this.edgesService.startPointOnEdge({ shape: e });
            const tangent = this.edgesService.tangentOnEdgeAtParam({ shape: e, param: 0 });
            let tangentPreviousEdgeEnd: Inputs.Base.Vector3;
            let averageTangentVec = tangent;
 
            if (index > 0 && index < edges.length - 1) {
                const previousEdge = edges[index - 1];
                tangentPreviousEdgeEnd = this.edgesService.tangentOnEdgeAtParam({ shape: previousEdge, param: 1 });
                averageTangentVec = [tangent[0] + tangentPreviousEdgeEnd[0] / 2, tangent[1] + tangentPreviousEdgeEnd[1] / 2, tangent[2] + tangentPreviousEdgeEnd[2] / 2];
            }
            const ngon = this.wiresService.createNGonWire({ radius: inputs.radius, center: edgeStartPt, direction: averageTangentVec, nrCorners: inputs.nrCorners }) as TopoDS_Wire;
            shapesToPassThrough.push(ngon);
            if (index === edges.length - 1) {
                const edgeEndPt = this.edgesService.endPointOnEdge({ shape: e });
                const tangentEndPt = this.edgesService.tangentOnEdgeAtParam({ shape: e, param: 1 });
                const ngon = this.wiresService.createNGonWire({ radius: inputs.radius, center: edgeEndPt, direction: tangentEndPt, nrCorners: inputs.nrCorners }) as TopoDS_Wire;
                shapesToPassThrough.push(ngon);
            }
        });
 
        const pipe = new this.occ.BRepOffsetAPI_MakePipeShell(wire);
        shapesToPassThrough.forEach(s => {
            pipe.Add_1(s, false, false);
        });
 
        pipe.Build(new this.occ.Message_ProgressRange_1());
        pipe.MakeSolid();
        const pipeShape = pipe.Shape();
        const result = this.converterService.getActualTypeOfShape(pipeShape);
        pipeShape.delete();
        pipe.delete();
        return result;
    }
 
    pipeWireCylindrical(inputs: Inputs.OCCT.PipeWireCylindricalDto<TopoDS_Wire>) {
        const wire = inputs.shape;
        const shapesToPassThrough: TopoDS_Shape[] = [];
        const edges = this.shapeGettersService.getEdges({ shape: wire });
        edges.forEach((e, index) => {
            const edgeStartPt = this.edgesService.startPointOnEdge({ shape: e });
            const tangent = this.edgesService.tangentOnEdgeAtParam({ shape: e, param: 0 });
            let tangentPreviousEdgeEnd: Inputs.Base.Vector3;
            let averageTangentVec = tangent;
 
            if (index > 0 && index < edges.length - 1) {
                const previousEdge = edges[index - 1];
                tangentPreviousEdgeEnd = this.edgesService.tangentOnEdgeAtParam({ shape: previousEdge, param: 1 });
                averageTangentVec = [tangent[0] + tangentPreviousEdgeEnd[0] / 2, tangent[1] + tangentPreviousEdgeEnd[1] / 2, tangent[2] + tangentPreviousEdgeEnd[2] / 2];
            }
            const circle = this.entitiesService.createCircle(inputs.radius, edgeStartPt, averageTangentVec, Inputs.OCCT.typeSpecificityEnum.wire) as TopoDS_Wire;
            shapesToPassThrough.push(circle);
            if (index === edges.length - 1) {
                const edgeEndPt = this.edgesService.endPointOnEdge({ shape: e });
                const tangentEndPt = this.edgesService.tangentOnEdgeAtParam({ shape: e, param: 1 });
                const line = this.entitiesService.createCircle(inputs.radius, edgeEndPt, tangentEndPt, Inputs.OCCT.typeSpecificityEnum.wire) as TopoDS_Wire;
                shapesToPassThrough.push(line);
            }
        });
 
        const pipe = new this.occ.BRepOffsetAPI_MakePipeShell(wire);
        shapesToPassThrough.forEach(s => {
            pipe.Add_1(s, false, false);
        });
 
        pipe.Build(new this.occ.Message_ProgressRange_1());
        pipe.MakeSolid();
        const pipeShape = pipe.Shape();
        const result = this.converterService.getActualTypeOfShape(pipeShape);
        pipeShape.delete();
        pipe.delete();
        return result;
    }
 
    pipeWiresCylindrical(inputs: Inputs.OCCT.PipeWiresCylindricalDto<TopoDS_Wire>) {
        return inputs.shapes.map(wire => {
            return this.pipeWireCylindrical({ shape: wire, radius: inputs.radius });
        });
    }
 
    makeThickSolidSimple(inputs: Inputs.OCCT.ThisckSolidSimpleDto<TopoDS_Shape>) {
        const maker = new this.occ.BRepOffsetAPI_MakeThickSolid();
        maker.MakeThickSolidBySimple(inputs.shape, inputs.offset);
        maker.Build(new this.occ.Message_ProgressRange_1());
        const makerShape = maker.Shape();
 
        const result = this.converterService.getActualTypeOfShape(makerShape);
        let res2 = result;
        if (inputs.offset > 0) {
            const faces = this.shapeGettersService.getFaces({ shape: result });
            const revFaces = faces.map(face => face.Reversed());
            res2 = this.shellsService.sewFaces({ shapes: revFaces, tolerance: 1e-7 });
            result.delete();
        }
        maker.delete();
        makerShape.delete();
        return res2;
    }
 
    makeThickSolidByJoin(inputs: Inputs.OCCT.ThickSolidByJoinDto<TopoDS_Shape>) {
        const facesToRemove = new this.occ.TopTools_ListOfShape_1();
        inputs.shapes.forEach(shape => {
            facesToRemove.Append_1(shape);
        });
        const myBody = new this.occ.BRepOffsetAPI_MakeThickSolid();
        const jointType: GeomAbs_JoinType = this.getJoinType(inputs.joinType);
 
        myBody.MakeThickSolidByJoin(
            inputs.shape,
            facesToRemove,
            inputs.offset,
            inputs.tolerance,
            this.occ.BRepOffset_Mode.BRepOffset_Skin as BRepOffset_Mode, // currently a single option
            inputs.intersection,
            inputs.selfIntersection,
            jointType,
            inputs.removeIntEdges,
            new this.occ.Message_ProgressRange_1());
        const makeThick = myBody.Shape();
        const result = this.converterService.getActualTypeOfShape(makeThick);
        makeThick.delete();
        myBody.delete();
        facesToRemove.delete();
        return result;
    }
 
    private getJoinType(jointType: Inputs.OCCT.joinTypeEnum): GeomAbs_JoinType {
        let res: GeomAbs_JoinType;
        switch (jointType) {
            case Inputs.OCCT.joinTypeEnum.arc: {
                res = this.occ.GeomAbs_JoinType.GeomAbs_Arc as GeomAbs_JoinType;
                break;
            }
            case Inputs.OCCT.joinTypeEnum.intersection: {
                res = this.occ.GeomAbs_JoinType.GeomAbs_Intersection as GeomAbs_JoinType;
                break;
            }
            case Inputs.OCCT.joinTypeEnum.tangent: {
                res = this.occ.GeomAbs_JoinType.GeomAbs_Tangent as GeomAbs_JoinType;
                break;
            }
        }
        return res;
    }
 
    private getBRepOffsetMode(offsetMode: Inputs.OCCT.bRepOffsetModeEnum): BRepOffset_Mode {
        let res: BRepOffset_Mode;
        switch (offsetMode) {
            case Inputs.OCCT.bRepOffsetModeEnum.skin: {
                res = this.occ.BRepOffset_Mode.BRepOffset_Skin as BRepOffset_Mode;
                break;
            }
            case Inputs.OCCT.bRepOffsetModeEnum.pipe: {
                res = this.occ.BRepOffset_Mode.BRepOffset_Pipe as BRepOffset_Mode;
                break;
            }
            case Inputs.OCCT.bRepOffsetModeEnum.rectoVerso: {
                res = this.occ.BRepOffset_Mode.BRepOffset_RectoVerso as BRepOffset_Mode;
                break;
            }
        }
        return res;
    }
 
    slice(inputs: Inputs.OCCT.SliceDto<TopoDS_Shape>): TopoDS_Compound {
        if (inputs.step <= 0) {
            throw new Error("Step needs to be positive.");
        }
        const { bbox, transformedShape } = this.createBBoxAndTransformShape(inputs.shape, inputs.direction);
        const intersections = [];
        Eif (!bbox.IsThin(0.0001)) {
            const { minY, maxY, maxDist } = this.computeBounds(bbox);
 
            const planes = [];
            for (let i = minY; i < maxY; i += inputs.step) {
                const pq = this.facesService.createSquareFace({ size: maxDist, center: [0, i, 0], direction: [0, 1, 0] });
                planes.push(pq);
            }
 
            this.applySlices(transformedShape, planes, inputs.direction, intersections);
        }
        const res = this.converterService.makeCompound({ shapes: intersections });
        return res;
    }
 
    sliceInStepPattern(inputs: Inputs.OCCT.SliceInStepPatternDto<TopoDS_Shape>): TopoDS_Compound {
        if (!inputs.steps || inputs.steps.length === 0) {
            throw new Error("Steps must be provided with at elast one positive value");
        }
        const { bbox, transformedShape } = this.createBBoxAndTransformShape(inputs.shape, inputs.direction);
        const intersections = [];
        Eif (!bbox.IsThin(0.0001)) {
            const { minY, maxY, maxDist } = this.computeBounds(bbox);
 
            const planes = [];
 
            let index = 0;
            for (let i = minY; i < maxY; i += inputs.steps[index]) {
                const pq = this.facesService.createSquareFace({ size: maxDist, center: [0, i, 0], direction: [0, 1, 0] });
                planes.push(pq);
                if (inputs.steps[index + 1] === undefined) {
                    index = 0;
                } else {
                    index++;
                }
            }
 
            this.applySlices(transformedShape, planes, inputs.direction, intersections);
        }
        const res = this.converterService.makeCompound({ shapes: intersections });
        return res;
    }
 
    private createBBoxAndTransformShape(shape: TopoDS_Shape, direction: Inputs.Base.Vector3) {
 
        // we orient the given shape to the reverse direction of sections so that slicing
        // would always happen in flat bbox aligned orientation
        // after algorithm computes, we turn all intersections to original shape so that it would match a given shape.
        // const fromDir
        const transformedShape = this.transformsService.align({
            shape,
            fromOrigin: [0, 0, 0],
            fromDirection: direction,
            toOrigin: [0, 0, 0],
            toDirection: [0, 1, 0],
        });
        const bbox = new this.occ.Bnd_Box_1();
        this.occ.BRepBndLib.Add(transformedShape, bbox, false);
        return { bbox, transformedShape };
    }
 
    private computeBounds(bbox: Bnd_Box_1) {
        const cornerMin = bbox.CornerMin();
        const cornerMax = bbox.CornerMax();
        const minY = cornerMin.Y();
        const maxY = cornerMax.Y();
 
        const minX = cornerMin.X();
        const maxX = cornerMax.X();
 
        const minZ = cornerMin.Z();
        const maxZ = cornerMax.Z();
 
        const distX = maxX - minX;
        const distZ = maxZ - minZ;
 
        const percentage = 1.2;
        let maxDist = distX >= distZ ? distX : distZ;
        maxDist *= percentage;
        return { minY, maxY, maxDist };
    }
 
 
    private applySlices(transformedShape: TopoDS_Shape, planes: any[], direction: Inputs.Base.Vector3, intersections: any[]) {
        const shapesToSlice = [];
        if (this.enumService.getShapeTypeEnum(transformedShape) === Inputs.OCCT.shapeTypeEnum.solid) {
            shapesToSlice.push(transformedShape);
        } else {
            const solids = this.shapeGettersService.getSolids({ shape: transformedShape });
            shapesToSlice.push(...solids);
        }
 
        if (shapesToSlice.length === 0) {
            throw new Error("No solids found to slice.");
        }
 
        shapesToSlice.forEach(s => {
            const intInputs = new Inputs.OCCT.IntersectionDto<TopoDS_Shape>();
            intInputs.keepEdges = true;
            intInputs.shapes = [s];
 
            const compound = this.converterService.makeCompound({ shapes: planes });
            intInputs.shapes.push(compound);
 
            const ints = this.booleansService.intersection(intInputs);
            ints.forEach(int => {
                Eif (int && !int.IsNull()) {
                    const transformedInt = this.transformsService.align({
                        shape: int,
                        fromOrigin: [0, 0, 0],
                        fromDirection: [0, 1, 0],
                        toOrigin: [0, 0, 0],
                        toDirection: direction,
                    });
                    intersections.push(transformedInt);
                }
            });
        });
    }
}