All files / lib/services io.ts

50% Statements 41/82
40% Branches 14/35
80% Functions 4/5
50% Lines 41/82

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                          4x   4x 1x 1x     1x   4x 4x   4x 1x   3x       4x   4x 4x                   4x   4x 4x   4x 4x     4x             4x 1x   4x   4x                                                                                                                     2x 2x 2x 2x     2x                 2x     2x 2x             2x 2x   2x 2x     2x 2x   2x                 2x 2x       2x              
import { IGESControl_Reader_1, OpenCascadeInstance, STEPControl_Reader_1, STEPControl_StepModelType, TopoDS_Shape } from "../../bitbybit-dev-occt/bitbybit-dev-occt";
import { OccHelper } from "../occ-helper";
import * as Inputs from "../api/inputs/inputs";
 
export class OCCTIO {
 
    constructor(
        private readonly occ: OpenCascadeInstance,
        private readonly och: OccHelper
    ) {
    }
 
    saveShapeSTEP(inputs: Inputs.OCCT.SaveStepDto<TopoDS_Shape>): string {
        const shapeToUse = inputs.shape;
        let adjustedShape;
        if (inputs.adjustYtoZ) {
            const rotatedShape = this.och.transformsService.rotate({ shape: inputs.shape, axis: [1, 0, 0], angle: -90 });
            adjustedShape = this.och.transformsService.mirrorAlongNormal(
                { shape: rotatedShape, origin: [0, 0, 0], normal: [0, 0, 1] }
            );
            rotatedShape.delete();
        }
        const fileName = "x";
        const writer = new this.occ.STEPControl_Writer_1();
        let transferShape;
        if (adjustedShape) {
            transferShape = adjustedShape;
        } else {
            transferShape = shapeToUse;
        }
        // Convert to a .STEP File
 
        const messageProgress = new this.occ.Message_ProgressRange_1();
        let transferResult;
        try {
            transferResult = writer.Transfer(
                transferShape,
                (this.occ.STEPControl_StepModelType.STEPControl_AsIs as STEPControl_StepModelType),
                true,
                messageProgress
            );
        } catch (ex) {
            throw (new Error("Failed when calling writer.Transfer."));
        }
        let result: string;
        if (transferResult === this.occ.IFSelect_ReturnStatus.IFSelect_RetDone) {
            // Write the STEP File to the virtual Emscripten Filesystem Temporarily
            const writeResult = writer.Write(fileName);
            if (writeResult === this.occ.IFSelect_ReturnStatus.IFSelect_RetDone) {
                // Read the STEP File from the filesystem and clean up
                const stepFileText = this.occ.FS.readFile("/" + fileName, { encoding: "utf8" });
                this.occ.FS.unlink("/" + fileName);
 
                // Return the contents of the STEP File
                result = stepFileText;
            } else E{
                throw (new Error("Failed when writing step file."));
            }
        } else E{
            throw (new Error("Failed when transfering to step writer."));
        }
        if (adjustedShape) {
            adjustedShape.delete();
        }
        messageProgress.delete();
 
        return result;
    }
 
    saveShapeStl(inputs: Inputs.OCCT.SaveStlDto<TopoDS_Shape>): string {
        const shapeToUse = inputs.shape;
 
        // This could be made optional...
        // Clean cached triangulation data for the shape.
        // This allows to get lower res models out of higher res that was once computed and cached.
        this.occ.BRepTools.Clean(shapeToUse, true);
 
        let adjustedShape;
        if (inputs.adjustYtoZ) {
            const rotatedShape = this.och.transformsService.rotate({ shape: inputs.shape, axis: [1, 0, 0], angle: -90 });
            adjustedShape = this.och.transformsService.mirrorAlongNormal(
                { shape: rotatedShape, origin: [0, 0, 0], normal: [0, 0, 1] }
            );
            rotatedShape.delete();
        }
        const fileName = "x";
        const writer = new this.occ.StlAPI_Writer();
        let transferShape;
        if (adjustedShape) {
            transferShape = adjustedShape;
        } else {
            transferShape = shapeToUse;
        }
        const messageProgress = new this.occ.Message_ProgressRange_1();
        let result: string;
        const incrementalMeshBuilder = new this.occ.BRepMesh_IncrementalMesh_2(transferShape, inputs.precision, false, 0.5, false);
 
        // Write the STL File to the virtual Emscripten Filesystem Temporarily
        const writeResult = writer.Write(transferShape, fileName, messageProgress);
        if (writeResult) {
            // Read the STL File from the filesystem and clean up
            const stlFile = this.occ.FS.readFile("/" + fileName, { encoding: "utf8" });
            this.occ.FS.unlink("/" + fileName);
            // Return the contents of the STL File
            result = stlFile;
        } else {
            throw (new Error("Failed when writing stl file."));
        }
 
        if (adjustedShape) {
            adjustedShape.delete();
        }
        messageProgress.delete();
 
        if (incrementalMeshBuilder) {
            incrementalMeshBuilder.Delete();
        }
        
        return result;
    }
 
    /** This function parses the ASCII contents of a `.STEP` or `.IGES`
     * File as a Shape into the `externalShapes` dictionary.
     */
    loadSTEPorIGES(inputs: Inputs.OCCT.LoadStepOrIgesDto): TopoDS_Shape | undefined {
        const fileName = inputs.fileName;
        const fileText = inputs.filetext;
        const fileType = (() => {
            switch (fileName.toLowerCase().split(".").pop()) {
                case "step":
                case "stp":
                    return "step";
                case "iges":
                case "igs":
                    return "iges";
                default:
                    return undefined;
            }
        })();
        // Writes the uploaded file to Emscripten's Virtual Filesystem
        this.occ.FS.createDataFile("/", `file.${fileType}`, fileText as string, true, true, true);
        // Choose the correct OpenCascade file parsers to read the CAD file
        let reader: STEPControl_Reader_1 | IGESControl_Reader_1;
        if (fileType === "step") {
            reader = new this.occ.STEPControl_Reader_1();
        } else Eif (fileType === "iges") {
            reader = new this.occ.IGESControl_Reader_1();
        } else {
            console.error("opencascade can't parse this extension! (yet)");
            return undefined;
        }
        const readResult = reader.ReadFile(`file.${fileType}`);            // Read the file
        if (readResult === this.occ.IFSelect_ReturnStatus.IFSelect_RetDone) {
            // Translate all transferable roots to OpenCascade
            const messageProgress = new this.occ.Message_ProgressRange_1();
            reader.TransferRoots(
                messageProgress
            );
            messageProgress.delete();
            let stepShape = reader.OneShape();
            let adjustedShape;
            Iif (inputs.adjustZtoY) {
                const mirroredShape = this.och.transformsService.mirrorAlongNormal(
                    { shape: stepShape, origin: [0, 0, 0], normal: [0, 0, 1] }
                );
                adjustedShape = this.och.transformsService.rotate({ shape: mirroredShape, axis: [1, 0, 0], angle: 90 });
                mirroredShape.delete();
            }
            // Out with the old, in with the new!
            // Remove the file when we're done (otherwise we run into errors on reupload)
            this.occ.FS.unlink(`/file.${fileType}`);
            Iif (adjustedShape) {
                stepShape.delete();
                stepShape = adjustedShape;
            }
            return stepShape;
        } else E{
            console.error("Something in OCCT went wrong trying to read " + fileName);
        }
        return undefined;
    }
}