Skip to content

Commit

Permalink
port to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
erossignon committed Jun 12, 2023
1 parent e135bd1 commit a81e44d
Show file tree
Hide file tree
Showing 92 changed files with 5,118 additions and 4,758 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ npm-debug*
occt-*

bower_components
*.tsbuildinfo
7 changes: 7 additions & 0 deletions .mocharc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require:
- ./node_modules/ts-node/register
extensions:
- .js
- .ts
recursive: true
enable-source-map: true
2 changes: 1 addition & 1 deletion bin/STEPtoBREP.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const occ = require("../lib/occ");
import { occ } from "..";

const pace = require("pace")(1000);

Expand Down
5 changes: 5 additions & 0 deletions dist-test/helpers.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export declare function getTemporaryFilePath({ prefix, suffix }: {
prefix?: string;
suffix: string;
}): string;
export declare function removeFile(filename: string): void;
26 changes: 26 additions & 0 deletions dist-test/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeFile = exports.getTemporaryFilePath = void 0;
const node_fs_1 = __importDefault(require("node:fs"));
const node_os_1 = __importDefault(require("node:os"));
const node_crypto_1 = __importDefault(require("node:crypto"));
const node_path_1 = __importDefault(require("node:path"));
function getTemporaryFilePath({ prefix, suffix }) {
const name = node_crypto_1.default.randomUUID();
return node_path_1.default.join(node_os_1.default.tmpdir(), (prefix || "") + name + suffix);
}
exports.getTemporaryFilePath = getTemporaryFilePath;
;
function removeFile(filename) {
if (node_fs_1.default.existsSync(filename)) {
node_fs_1.default.unlinkSync(filename);
}
else {
//Show in red
console.log("File " + filename + " not found, so not deleting.");
}
}
exports.removeFile = removeFile;
1 change: 1 addition & 0 deletions dist-test/test_BREP.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
138 changes: 138 additions & 0 deletions dist-test/test_BREP.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const should_1 = __importDefault(require("should"));
const __1 = require("..");
const __2 = require("..");
const helpers_1 = require("./helpers");
const assert_1 = __importDefault(require("assert"));
describe("testing BREP input output ", function () {
let b1_brep;
let b2_brep;
let b3_brep;
let b1_volume = 0;
let b1_area = 0;
before(() => {
b1_brep = (0, helpers_1.getTemporaryFilePath)({ prefix: "b1_", suffix: ".brep" });
b2_brep = (0, helpers_1.getTemporaryFilePath)({ prefix: "b2_", suffix: ".brep" });
b3_brep = (0, helpers_1.getTemporaryFilePath)({ prefix: "b3_", suffix: ".brep" });
create_shapes();
});
after((done) => {
(0, helpers_1.removeFile)(b1_brep);
(0, helpers_1.removeFile)(b2_brep);
(0, helpers_1.removeFile)(b3_brep);
done();
});
function create_shapes() {
let box = __2.occ.makeBox([0, 0, 0], [100, 200, 300]);
let b1_result = __2.occ.writeBREP(b1_brep, box);
b1_volume = box.volume;
b1_area = box.area;
let cyl = __2.occ.makeCylinder([0, 0, 0], [0, 0, 10], 5);
let b2_result = __2.occ.writeBREP(b2_brep, cyl);
let b3_result = __2.occ.writeBREP(b3_brep, [box, cyl]);
b1_result.should.eql(true);
b2_result.should.eql(true);
b3_result.should.eql(true);
}
it("should write a simple shape", function () {
create_shapes();
});
describe(" readBREP ", function () {
it("ZZ1 - should throw an error if used with no argument", function () {
(0, should_1.default)(function () {
__2.occ.readBREP(null, (err) => {
err.message.should.match(/expecting a filename/);
});
}).throwError();
});
it("ZZ2 - should call the callback method with an error if used with an invalid arguments", (done) => {
__2.occ.readBREP("||this is a invalid filename||", (err, _shapes) => {
err.message.should.match(/cannot read/);
done();
});
});
it("ZZ3 - should call the callback with an error if the file doesn't exist", (done) => {
__2.occ.readBREP("invalid file name", (err, _shapes) => {
console.log(" intercepting error ", err);
(0, assert_1.default)(err !== undefined);
done();
});
});
it("ZZ4 - should read the shape back", (done) => {
__2.occ.readBREP(b1_brep, (err, shapes) => {
(0, should_1.default)(err).eql(null);
if (!err) {
shapes.length.should.equal(1);
shapes[0].numFaces.should.equal(6);
shapes[0].volume.should.equal(b1_volume);
shapes[0].area.should.equal(b1_area);
}
done(err);
});
});
it("ZZ5 - should read the shape back", (done) => {
__2.occ.readBREP(b2_brep, (err, shapes) => {
if (!err) {
shapes.length.should.equal(1);
shapes[0].numFaces.should.equal(3);
}
done(err);
});
});
it("ZZ6 - should read the shape back", (done) => {
__2.occ.readBREP(b3_brep, function (err, shapes) {
if (!err) {
shapes.length.should.equal(2);
shapes[0].numFaces.should.equal(6);
shapes[1].numFaces.should.equal(3);
}
done();
});
});
});
});
function build_large_part() {
let lego_filename = (0, helpers_1.getTemporaryFilePath)({ prefix: "legoPlate3x2_2x2", suffix: "" });
let legoPlate = (0, __1.makeLegoBrick)(__2.occ, 3, 2, "thin");
let solids = [];
for (let x = 0; x < 100; x += 50) {
for (let y = 0; y < 100; y += 50) {
solids.push(legoPlate.translate([x, y, 0]));
}
}
__2.occ.writeBREP(lego_filename + ".brep", solids);
/*
occ.writeSTL(lego_filename + ".stl", solids);
let obj = {solids: []};
let counter = 0;
solids.forEach(function (solid) {
solid.name = "S" + counter;
counter++;
obj.solids.push(occ.buildSolidMesh(solid));
});
fs.writeFile(lego_filename + ".3js", JSON.stringify(obj, null, ""), function (err) {
console.log("OK");
});
*/
return lego_filename;
}
describe("it should write and read a large brep file", function () {
this.timeout(15000);
let filename = build_large_part();
it("should read a large BREP file quickly", (done) => {
console.log(" lego file ", filename);
__2.occ.readBREP(filename + ".brep", (err, solids) => {
console.log(" read !!!");
if (!err) {
console.log(" num Faces = ", solids[0].numFaces);
}
done(err);
});
});
});
1 change: 1 addition & 0 deletions dist-test/test_BoundingBox.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "should";
69 changes: 69 additions & 0 deletions dist-test/test_BoundingBox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const __1 = require("..");
require("should");
describe("testing bounding box", function () {
describe("an empty BoundingBox", function () {
let bbox;
before(function () {
bbox = new __1.BoundingBox();
});
it("should be void", function () {
bbox.isVoid.should.equal(true);
});
});
describe("an BoundingBox built with a single point in constructor", function () {
let bbox;
before(function () {
bbox = new __1.BoundingBox([10, 20, 30]);
});
it("should not be void", function () {
bbox.isVoid.should.equal(false);
});
it("should have nearPt to be correct", function () {
bbox.nearPt.x.should.equal(10);
bbox.nearPt.y.should.equal(20);
bbox.nearPt.z.should.equal(30);
});
it("should have farPt to be correct", function () {
bbox.farPt.x.should.equal(10);
bbox.farPt.y.should.equal(20);
bbox.farPt.z.should.equal(30);
});
});
describe("adding a single point to an empty bounding box", function () {
let bbox;
before(function () {
bbox = new __1.BoundingBox();
bbox.addPoint([10, 20, 30]);
});
it("should not be void", function () {
bbox.isVoid.should.equal(false);
});
it("should have nearPt to be correct", function () {
bbox.nearPt.x.should.equal(10);
bbox.nearPt.y.should.equal(20);
bbox.nearPt.z.should.equal(30);
});
it("should have farPt to be correct", function () {
bbox.farPt.x.should.equal(10);
bbox.farPt.y.should.equal(20);
bbox.farPt.z.should.equal(30);
});
});
describe("checking calling isOut on a empty box", function () {
it("should return isOut = true for any point ", function () {
let bbox = new __1.BoundingBox();
bbox.isOut([10, 20, 30]).should.equal(true);
});
});
describe("checking calling isOut this box [-10,-10,-10],[5,5,5]", function () {
let bbox = new __1.BoundingBox([-10, -10, -10], [5, 5, 5]);
it("should return isOut = true for [10,20,30] ", function () {
bbox.isOut([10, 20, 30]).should.equal(true);
});
it("should return isOut = false for [1,2,3] ", function () {
bbox.isOut([1, 2, 3]).should.equal(false);
});
});
});
1 change: 1 addition & 0 deletions dist-test/test_ReadWriteSTEP.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "should";
102 changes: 102 additions & 0 deletions dist-test/test_ReadWriteSTEP.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/*eslint-env node mocha*/
/*global require*/
// test_STEP
const assert_1 = __importDefault(require("assert"));
require("should");
const __1 = require("..");
const helpers_1 = require("./helpers");
describe("testing STEP input output ", function () {
let b1_step;
let b2_step;
let b3_step;
before(function () {
b1_step = (0, helpers_1.getTemporaryFilePath)({ prefix: "b1_", suffix: ".step" });
b2_step = (0, helpers_1.getTemporaryFilePath)({ prefix: "b2_", suffix: ".step" });
b3_step = (0, helpers_1.getTemporaryFilePath)({ prefix: "b3_", suffix: ".step" });
let box = __1.occ.makeBox([0, 0, 0], [100, 200, 300]);
let b1 = __1.occ.writeSTEP(b1_step, box);
let cyl = __1.occ.makeCylinder([0, 0, 0], [0, 0, 10], 5);
let b2 = __1.occ.writeSTEP(b2_step, cyl);
let b3 = __1.occ.writeSTEP(b3_step, [box, cyl]);
b1.should.eql(true);
b2.should.eql(true);
b3.should.eql(true);
});
after(function () {
(0, helpers_1.removeFile)(b1_step);
(0, helpers_1.removeFile)(b2_step);
(0, helpers_1.removeFile)(b3_step);
});
it("AZ0 - should write a simple shape", function (done) {
let box = __1.occ.makeBox([0, 0, 0], [100, 200, 300]);
let b1 = __1.occ.writeSTEP(b1_step, box);
done();
});
it("AZ1 - readSTEP with callback ", function (done) {
__1.occ.readSTEP(b3_step, (err, shapes) => {
console.log(err, shapes);
shapes.length.should.equal(2);
shapes[0].numFaces.should.equal(6);
shapes[1].numFaces.should.equal(3);
done();
});
});
it("AZ2 - should raise an exception with invalid arguments", function () {
(function () {
__1.occ.readSTEP();
}).should.throwError();
(function () {
__1.occ.readSTEP("filename");
}).should.throwError();
});
it("AZ3 - should call the callback with an error if the file doesn't exist", function (done) {
__1.occ.readSTEP("invalid file name", function (err, shapes) {
if (err) {
err.message.should.match(/invalid file name/);
}
else {
return done(new Error("Expecting Error"));
}
done();
});
});
it("AZ4 - should read file one", function (done) {
__1.occ.readSTEP(b1_step, function (err, shapes) {
if (err) {
console.log(" err = ", err, shapes);
}
(0, assert_1.default)(!err);
shapes.length.should.equal(1);
shapes[0].numFaces.should.equal(6);
done();
});
});
it("AZ5 - should read file two", function (done) {
__1.occ.readSTEP(b2_step, function (err, shapes) {
if (err) {
console.log(" err = ", err, shapes);
}
(0, assert_1.default)(!err);
shapes.length.should.equal(1);
shapes[0].numFaces.should.equal(3);
done();
});
});
it("AZ6 - should read file three", function (done) {
__1.occ.readSTEP(b3_step, function (err, shapes) {
if (err) {
console.log(" err = ", err, shapes);
}
(0, assert_1.default)(!err);
shapes.length.should.equal(2);
shapes[0].numFaces.should.equal(6);
shapes[1].numFaces.should.equal(3);
done();
});
});
});
1 change: 1 addition & 0 deletions dist-test/test_applyTransform.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "should";
27 changes: 27 additions & 0 deletions dist-test/test_applyTransform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const __1 = require("..");
require("should");
describe("testing various transformation", function () {
function getVerticeData(box) {
let vert = box.getVertices();
const triplets = vert.map((v) => [v.x, v.y, v.z]);
triplets.length.should.eql(8);
return triplets;
}
function add(v1, v2) {
return [v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]];
}
it("#applyTransform", function () {
let box = __1.occ.makeBox([0, 0, 0], [100, 200, 300]);
let trsf = new __1.Transformation();
trsf.makeTranslation([10, 20, 30]);
let vert = getVerticeData(box);
vert[1].should.eql([0, 0, 0]);
box.applyTransform(trsf);
let vert_after = getVerticeData(box);
// translate vertex
vert = vert.map((v) => add(v, [10, 20, 30]));
vert_after.should.eql(vert);
});
});
Loading

0 comments on commit a81e44d

Please sign in to comment.