All files / lib/services/base shape-getters.ts

90.1% Statements 82/91
86.84% Branches 66/76
100% Functions 21/21
90% Lines 72/80

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                                          17x       1x   16x 16x 16x       1x             1x 1x     1x 1x 1x 1x   1x 1x 1x       618x     618x     618x 618x 2482x   618x       4x 1x   3x 3x 3x 3x 36x 1x 1x       3x 2x   1x         30x 30x 461x   30x       10x 1x   9x 9x     2x   7x   7x 17x   7x     7x       82x 82x 1083x   82x       9x 9x 11x   9x       29x     29x 29x     1x   28x 28x 28x 38x   28x 2x     26x         6x     6x     6x 6x 168x   6x        
import {
    OpenCascadeInstance, TopoDS_Edge, TopoDS_Face,
    TopoDS_Shape, TopoDS_Solid, TopoDS_Vertex, TopoDS_Wire
} from "../../../bitbybit-dev-occt/bitbybit-dev-occt";
import * as Inputs from "../../api/inputs/inputs";
import { EnumService } from "./enum.service";
import { IteratorService } from "./iterator.service";
 
interface TopoDS_ShapeHash extends TopoDS_Shape {
    hash?: number;
}
 
export class ShapeGettersService {
 
    constructor(
        private readonly occ: OpenCascadeInstance,
        private readonly enumService: EnumService,
        private readonly iteratorService: IteratorService,
    ) { }
 
    getNumSolidsInCompound(shape: TopoDS_Shape): number {
        if (!shape ||
            this.enumService.getShapeTypeEnum(shape) !== Inputs.OCCT.shapeTypeEnum.compound ||
            shape.IsNull()
        ) {
            throw new Error("Shape is not a compound or is null.");
        }
        let solidsFound = 0;
        this.iteratorService.forEachSolid(shape, () => { solidsFound++; });
        return solidsFound;
    }
 
    getSolidFromCompound(shape: TopoDS_ShapeHash, index: number) {
        Iif (!shape ||
            shape.ShapeType() > this.occ.TopAbs_ShapeEnum.TopAbs_COMPSOLID ||
            shape.IsNull()
        ) {
            console.error("Not a compound shape!");
            return shape;
        }
        Eif (!index) {
            index = 0;
        }
 
        let innerSolid = shape;
        let solidsFound = 0;
        this.iteratorService.forEachSolid(shape, (i, s) => {
            Eif (i === index) { innerSolid = this.occ.TopoDS.Solid_1(s); } solidsFound++;
        });
        Iif (solidsFound === 0) { console.error("NO SOLIDS FOUND IN SHAPE!"); }
        innerSolid.hash = shape.hash + 1;
        return innerSolid;
    }
 
    getEdges(inputs: Inputs.OCCT.ShapeDto<TopoDS_Shape>): TopoDS_Edge[] {
        Iif (inputs.shape && this.enumService.getShapeTypeEnum(inputs.shape) === Inputs.OCCT.shapeTypeEnum.edge) {
            return [inputs.shape];
        }
        Iif (!inputs.shape || inputs.shape.IsNull()) {
            throw (new Error("Shape is not provided or is of incorrect type"));
        }
        const edges: TopoDS_Edge[] = [];
        this.iteratorService.forEachEdge(inputs.shape, (i, edge) => {
            edges.push(edge);
        });
        return edges;
    }
 
    getEdge(inputs: Inputs.OCCT.EdgeIndexDto<TopoDS_Shape>): TopoDS_Edge {
        if (!inputs.shape || (inputs.shape.ShapeType && inputs.shape.ShapeType() > this.occ.TopAbs_ShapeEnum.TopAbs_WIRE) || inputs.shape.IsNull()) {
            throw (new Error("Edge can not be found for shape that is not provided or is of incorrect type"));
        }
        if (!inputs.index) { inputs.index = 0; }
        let innerEdge = {};
        let foundEdge = false;
        this.iteratorService.forEachEdge(inputs.shape, (i: number, s: TopoDS_Edge) => {
            if (i === inputs.index) {
                innerEdge = s;
                foundEdge = true;
            }
        });
 
        if (!foundEdge) {
            throw (new Error(`Edge can not be found for shape on index ${inputs.index}`));
        } else {
            return innerEdge as TopoDS_Edge;
        }
    }
 
    getWires(inputs: Inputs.OCCT.ShapeDto<TopoDS_Wire>): TopoDS_Wire[] {
        const wires: TopoDS_Wire[] = [];
        this.iteratorService.forEachWire(inputs.shape, (wireIndex: number, myWire: TopoDS_Wire) => {
            wires.push(myWire);
        });
        return wires;
    }
 
    getWire(inputs: Inputs.OCCT.ShapeIndexDto<TopoDS_Shape>): TopoDS_Wire {
        if (!inputs.shape || inputs.shape.IsNull()) {
            throw (new Error("Shape is not provided or is null"));
        }
        const shapeType = this.enumService.getShapeTypeEnum(inputs.shape);
        if ((shapeType === Inputs.OCCT.shapeTypeEnum.wire ||
            shapeType === Inputs.OCCT.shapeTypeEnum.edge ||
            shapeType === Inputs.OCCT.shapeTypeEnum.vertex)) {
            throw (new Error("Shape is of incorrect type"));
        }
        if (!inputs.index) { inputs.index = 0; }
        let innerWire: TopoDS_Wire | undefined;
        this.iteratorService.forEachWire(inputs.shape, (i, s) => {
            if (i === inputs.index) { innerWire = this.occ.TopoDS.Wire_1(s); }
        });
        Iif (!innerWire) {
            throw (Error("Wire not found"));
        }
        return innerWire;
    }
 
    getFaces(inputs: Inputs.OCCT.ShapeDto<TopoDS_Shape>): TopoDS_Face[] {
        const faces: TopoDS_Face[] = [];
        this.iteratorService.forEachFace(inputs.shape, (faceIndex, myFace) => {
            faces.push(myFace);
        });
        return faces;
    }
 
    getSolids(inputs: Inputs.OCCT.ShapeDto<TopoDS_Shape>): TopoDS_Solid[] {
        const solids: TopoDS_Face[] = [];
        this.iteratorService.forEachSolid(inputs.shape, (faceIndex, myFace) => {
            solids.push(myFace);
        });
        return solids;
    }
 
    getFace(inputs: Inputs.OCCT.ShapeIndexDto<TopoDS_Shape>): TopoDS_Face {
        Iif (!inputs.shape || inputs.shape.IsNull()) {
            throw new Error("Shape is not provided or is null");
        }
        const shapeType = this.enumService.getShapeTypeEnum(inputs.shape);
        if (shapeType === Inputs.OCCT.shapeTypeEnum.wire ||
            shapeType === Inputs.OCCT.shapeTypeEnum.edge ||
            shapeType === Inputs.OCCT.shapeTypeEnum.vertex) {
            throw (new Error("Shape is of incorrect type"));
        }
        if (!inputs.index) { inputs.index = 0; }
        let innerFace = {}; let facesFound = 0;
        this.iteratorService.forEachFace(inputs.shape, (i, s) => {
            if (i === inputs.index) { innerFace = this.occ.TopoDS.Face_1(s); } facesFound++;
        });
        if (facesFound < inputs.index || inputs.index < 0) {
            throw (new Error("Face index is out of range"));
        }
        else {
            return innerFace as TopoDS_Face;
        }
    }
 
    getVertices(inputs: Inputs.OCCT.ShapeDto<TopoDS_Shape>): TopoDS_Vertex[] {
        Iif (inputs.shape && this.enumService.getShapeTypeEnum(inputs.shape) === Inputs.OCCT.shapeTypeEnum.vertex) {
            return [inputs.shape];
        }
        Iif (!inputs.shape || inputs.shape.IsNull()) {
            throw (new Error("Shape is not provided or is of incorrect type"));
        }
        const vertices: TopoDS_Vertex[] = [];
        this.iteratorService.forEachVertex(inputs.shape, (i, vertex) => {
            vertices.push(vertex);
        });
        return vertices;
    }
 
}