Skip to content

Commit a81e44d

Browse files
committed
port to typescript
1 parent e135bd1 commit a81e44d

File tree

92 files changed

+5118
-4758
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+5118
-4758
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,4 @@ npm-debug*
5757
occt-*
5858

5959
bower_components
60+
*.tsbuildinfo

.mocharc.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require:
2+
- ./node_modules/ts-node/register
3+
extensions:
4+
- .js
5+
- .ts
6+
recursive: true
7+
enable-source-map: true

bin/STEPtoBREP.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const occ = require("../lib/occ");
1+
import { occ } from "..";
22

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

dist-test/helpers.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export declare function getTemporaryFilePath({ prefix, suffix }: {
2+
prefix?: string;
3+
suffix: string;
4+
}): string;
5+
export declare function removeFile(filename: string): void;

dist-test/helpers.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"use strict";
2+
var __importDefault = (this && this.__importDefault) || function (mod) {
3+
return (mod && mod.__esModule) ? mod : { "default": mod };
4+
};
5+
Object.defineProperty(exports, "__esModule", { value: true });
6+
exports.removeFile = exports.getTemporaryFilePath = void 0;
7+
const node_fs_1 = __importDefault(require("node:fs"));
8+
const node_os_1 = __importDefault(require("node:os"));
9+
const node_crypto_1 = __importDefault(require("node:crypto"));
10+
const node_path_1 = __importDefault(require("node:path"));
11+
function getTemporaryFilePath({ prefix, suffix }) {
12+
const name = node_crypto_1.default.randomUUID();
13+
return node_path_1.default.join(node_os_1.default.tmpdir(), (prefix || "") + name + suffix);
14+
}
15+
exports.getTemporaryFilePath = getTemporaryFilePath;
16+
;
17+
function removeFile(filename) {
18+
if (node_fs_1.default.existsSync(filename)) {
19+
node_fs_1.default.unlinkSync(filename);
20+
}
21+
else {
22+
//Show in red
23+
console.log("File " + filename + " not found, so not deleting.");
24+
}
25+
}
26+
exports.removeFile = removeFile;

dist-test/test_BREP.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {};

dist-test/test_BREP.js

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
"use strict";
2+
var __importDefault = (this && this.__importDefault) || function (mod) {
3+
return (mod && mod.__esModule) ? mod : { "default": mod };
4+
};
5+
Object.defineProperty(exports, "__esModule", { value: true });
6+
const should_1 = __importDefault(require("should"));
7+
const __1 = require("..");
8+
const __2 = require("..");
9+
const helpers_1 = require("./helpers");
10+
const assert_1 = __importDefault(require("assert"));
11+
describe("testing BREP input output ", function () {
12+
let b1_brep;
13+
let b2_brep;
14+
let b3_brep;
15+
let b1_volume = 0;
16+
let b1_area = 0;
17+
before(() => {
18+
b1_brep = (0, helpers_1.getTemporaryFilePath)({ prefix: "b1_", suffix: ".brep" });
19+
b2_brep = (0, helpers_1.getTemporaryFilePath)({ prefix: "b2_", suffix: ".brep" });
20+
b3_brep = (0, helpers_1.getTemporaryFilePath)({ prefix: "b3_", suffix: ".brep" });
21+
create_shapes();
22+
});
23+
after((done) => {
24+
(0, helpers_1.removeFile)(b1_brep);
25+
(0, helpers_1.removeFile)(b2_brep);
26+
(0, helpers_1.removeFile)(b3_brep);
27+
done();
28+
});
29+
function create_shapes() {
30+
let box = __2.occ.makeBox([0, 0, 0], [100, 200, 300]);
31+
let b1_result = __2.occ.writeBREP(b1_brep, box);
32+
b1_volume = box.volume;
33+
b1_area = box.area;
34+
let cyl = __2.occ.makeCylinder([0, 0, 0], [0, 0, 10], 5);
35+
let b2_result = __2.occ.writeBREP(b2_brep, cyl);
36+
let b3_result = __2.occ.writeBREP(b3_brep, [box, cyl]);
37+
b1_result.should.eql(true);
38+
b2_result.should.eql(true);
39+
b3_result.should.eql(true);
40+
}
41+
it("should write a simple shape", function () {
42+
create_shapes();
43+
});
44+
describe(" readBREP ", function () {
45+
it("ZZ1 - should throw an error if used with no argument", function () {
46+
(0, should_1.default)(function () {
47+
__2.occ.readBREP(null, (err) => {
48+
err.message.should.match(/expecting a filename/);
49+
});
50+
}).throwError();
51+
});
52+
it("ZZ2 - should call the callback method with an error if used with an invalid arguments", (done) => {
53+
__2.occ.readBREP("||this is a invalid filename||", (err, _shapes) => {
54+
err.message.should.match(/cannot read/);
55+
done();
56+
});
57+
});
58+
it("ZZ3 - should call the callback with an error if the file doesn't exist", (done) => {
59+
__2.occ.readBREP("invalid file name", (err, _shapes) => {
60+
console.log(" intercepting error ", err);
61+
(0, assert_1.default)(err !== undefined);
62+
done();
63+
});
64+
});
65+
it("ZZ4 - should read the shape back", (done) => {
66+
__2.occ.readBREP(b1_brep, (err, shapes) => {
67+
(0, should_1.default)(err).eql(null);
68+
if (!err) {
69+
shapes.length.should.equal(1);
70+
shapes[0].numFaces.should.equal(6);
71+
shapes[0].volume.should.equal(b1_volume);
72+
shapes[0].area.should.equal(b1_area);
73+
}
74+
done(err);
75+
});
76+
});
77+
it("ZZ5 - should read the shape back", (done) => {
78+
__2.occ.readBREP(b2_brep, (err, shapes) => {
79+
if (!err) {
80+
shapes.length.should.equal(1);
81+
shapes[0].numFaces.should.equal(3);
82+
}
83+
done(err);
84+
});
85+
});
86+
it("ZZ6 - should read the shape back", (done) => {
87+
__2.occ.readBREP(b3_brep, function (err, shapes) {
88+
if (!err) {
89+
shapes.length.should.equal(2);
90+
shapes[0].numFaces.should.equal(6);
91+
shapes[1].numFaces.should.equal(3);
92+
}
93+
done();
94+
});
95+
});
96+
});
97+
});
98+
function build_large_part() {
99+
let lego_filename = (0, helpers_1.getTemporaryFilePath)({ prefix: "legoPlate3x2_2x2", suffix: "" });
100+
let legoPlate = (0, __1.makeLegoBrick)(__2.occ, 3, 2, "thin");
101+
let solids = [];
102+
for (let x = 0; x < 100; x += 50) {
103+
for (let y = 0; y < 100; y += 50) {
104+
solids.push(legoPlate.translate([x, y, 0]));
105+
}
106+
}
107+
__2.occ.writeBREP(lego_filename + ".brep", solids);
108+
/*
109+
occ.writeSTL(lego_filename + ".stl", solids);
110+
111+
let obj = {solids: []};
112+
let counter = 0;
113+
solids.forEach(function (solid) {
114+
solid.name = "S" + counter;
115+
counter++;
116+
obj.solids.push(occ.buildSolidMesh(solid));
117+
});
118+
fs.writeFile(lego_filename + ".3js", JSON.stringify(obj, null, ""), function (err) {
119+
console.log("OK");
120+
});
121+
122+
*/
123+
return lego_filename;
124+
}
125+
describe("it should write and read a large brep file", function () {
126+
this.timeout(15000);
127+
let filename = build_large_part();
128+
it("should read a large BREP file quickly", (done) => {
129+
console.log(" lego file ", filename);
130+
__2.occ.readBREP(filename + ".brep", (err, solids) => {
131+
console.log(" read !!!");
132+
if (!err) {
133+
console.log(" num Faces = ", solids[0].numFaces);
134+
}
135+
done(err);
136+
});
137+
});
138+
});

dist-test/test_BoundingBox.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "should";

dist-test/test_BoundingBox.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const __1 = require("..");
4+
require("should");
5+
describe("testing bounding box", function () {
6+
describe("an empty BoundingBox", function () {
7+
let bbox;
8+
before(function () {
9+
bbox = new __1.BoundingBox();
10+
});
11+
it("should be void", function () {
12+
bbox.isVoid.should.equal(true);
13+
});
14+
});
15+
describe("an BoundingBox built with a single point in constructor", function () {
16+
let bbox;
17+
before(function () {
18+
bbox = new __1.BoundingBox([10, 20, 30]);
19+
});
20+
it("should not be void", function () {
21+
bbox.isVoid.should.equal(false);
22+
});
23+
it("should have nearPt to be correct", function () {
24+
bbox.nearPt.x.should.equal(10);
25+
bbox.nearPt.y.should.equal(20);
26+
bbox.nearPt.z.should.equal(30);
27+
});
28+
it("should have farPt to be correct", function () {
29+
bbox.farPt.x.should.equal(10);
30+
bbox.farPt.y.should.equal(20);
31+
bbox.farPt.z.should.equal(30);
32+
});
33+
});
34+
describe("adding a single point to an empty bounding box", function () {
35+
let bbox;
36+
before(function () {
37+
bbox = new __1.BoundingBox();
38+
bbox.addPoint([10, 20, 30]);
39+
});
40+
it("should not be void", function () {
41+
bbox.isVoid.should.equal(false);
42+
});
43+
it("should have nearPt to be correct", function () {
44+
bbox.nearPt.x.should.equal(10);
45+
bbox.nearPt.y.should.equal(20);
46+
bbox.nearPt.z.should.equal(30);
47+
});
48+
it("should have farPt to be correct", function () {
49+
bbox.farPt.x.should.equal(10);
50+
bbox.farPt.y.should.equal(20);
51+
bbox.farPt.z.should.equal(30);
52+
});
53+
});
54+
describe("checking calling isOut on a empty box", function () {
55+
it("should return isOut = true for any point ", function () {
56+
let bbox = new __1.BoundingBox();
57+
bbox.isOut([10, 20, 30]).should.equal(true);
58+
});
59+
});
60+
describe("checking calling isOut this box [-10,-10,-10],[5,5,5]", function () {
61+
let bbox = new __1.BoundingBox([-10, -10, -10], [5, 5, 5]);
62+
it("should return isOut = true for [10,20,30] ", function () {
63+
bbox.isOut([10, 20, 30]).should.equal(true);
64+
});
65+
it("should return isOut = false for [1,2,3] ", function () {
66+
bbox.isOut([1, 2, 3]).should.equal(false);
67+
});
68+
});
69+
});

dist-test/test_ReadWriteSTEP.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "should";

dist-test/test_ReadWriteSTEP.js

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
"use strict";
2+
var __importDefault = (this && this.__importDefault) || function (mod) {
3+
return (mod && mod.__esModule) ? mod : { "default": mod };
4+
};
5+
Object.defineProperty(exports, "__esModule", { value: true });
6+
/*eslint-env node mocha*/
7+
/*global require*/
8+
// test_STEP
9+
const assert_1 = __importDefault(require("assert"));
10+
require("should");
11+
const __1 = require("..");
12+
const helpers_1 = require("./helpers");
13+
describe("testing STEP input output ", function () {
14+
let b1_step;
15+
let b2_step;
16+
let b3_step;
17+
before(function () {
18+
b1_step = (0, helpers_1.getTemporaryFilePath)({ prefix: "b1_", suffix: ".step" });
19+
b2_step = (0, helpers_1.getTemporaryFilePath)({ prefix: "b2_", suffix: ".step" });
20+
b3_step = (0, helpers_1.getTemporaryFilePath)({ prefix: "b3_", suffix: ".step" });
21+
let box = __1.occ.makeBox([0, 0, 0], [100, 200, 300]);
22+
let b1 = __1.occ.writeSTEP(b1_step, box);
23+
let cyl = __1.occ.makeCylinder([0, 0, 0], [0, 0, 10], 5);
24+
let b2 = __1.occ.writeSTEP(b2_step, cyl);
25+
let b3 = __1.occ.writeSTEP(b3_step, [box, cyl]);
26+
b1.should.eql(true);
27+
b2.should.eql(true);
28+
b3.should.eql(true);
29+
});
30+
after(function () {
31+
(0, helpers_1.removeFile)(b1_step);
32+
(0, helpers_1.removeFile)(b2_step);
33+
(0, helpers_1.removeFile)(b3_step);
34+
});
35+
it("AZ0 - should write a simple shape", function (done) {
36+
let box = __1.occ.makeBox([0, 0, 0], [100, 200, 300]);
37+
let b1 = __1.occ.writeSTEP(b1_step, box);
38+
done();
39+
});
40+
it("AZ1 - readSTEP with callback ", function (done) {
41+
__1.occ.readSTEP(b3_step, (err, shapes) => {
42+
console.log(err, shapes);
43+
shapes.length.should.equal(2);
44+
shapes[0].numFaces.should.equal(6);
45+
shapes[1].numFaces.should.equal(3);
46+
done();
47+
});
48+
});
49+
it("AZ2 - should raise an exception with invalid arguments", function () {
50+
(function () {
51+
__1.occ.readSTEP();
52+
}).should.throwError();
53+
(function () {
54+
__1.occ.readSTEP("filename");
55+
}).should.throwError();
56+
});
57+
it("AZ3 - should call the callback with an error if the file doesn't exist", function (done) {
58+
__1.occ.readSTEP("invalid file name", function (err, shapes) {
59+
if (err) {
60+
err.message.should.match(/invalid file name/);
61+
}
62+
else {
63+
return done(new Error("Expecting Error"));
64+
}
65+
done();
66+
});
67+
});
68+
it("AZ4 - should read file one", function (done) {
69+
__1.occ.readSTEP(b1_step, function (err, shapes) {
70+
if (err) {
71+
console.log(" err = ", err, shapes);
72+
}
73+
(0, assert_1.default)(!err);
74+
shapes.length.should.equal(1);
75+
shapes[0].numFaces.should.equal(6);
76+
done();
77+
});
78+
});
79+
it("AZ5 - should read file two", function (done) {
80+
__1.occ.readSTEP(b2_step, function (err, shapes) {
81+
if (err) {
82+
console.log(" err = ", err, shapes);
83+
}
84+
(0, assert_1.default)(!err);
85+
shapes.length.should.equal(1);
86+
shapes[0].numFaces.should.equal(3);
87+
done();
88+
});
89+
});
90+
it("AZ6 - should read file three", function (done) {
91+
__1.occ.readSTEP(b3_step, function (err, shapes) {
92+
if (err) {
93+
console.log(" err = ", err, shapes);
94+
}
95+
(0, assert_1.default)(!err);
96+
shapes.length.should.equal(2);
97+
shapes[0].numFaces.should.equal(6);
98+
shapes[1].numFaces.should.equal(3);
99+
done();
100+
});
101+
});
102+
});

dist-test/test_applyTransform.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "should";

dist-test/test_applyTransform.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const __1 = require("..");
4+
require("should");
5+
describe("testing various transformation", function () {
6+
function getVerticeData(box) {
7+
let vert = box.getVertices();
8+
const triplets = vert.map((v) => [v.x, v.y, v.z]);
9+
triplets.length.should.eql(8);
10+
return triplets;
11+
}
12+
function add(v1, v2) {
13+
return [v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]];
14+
}
15+
it("#applyTransform", function () {
16+
let box = __1.occ.makeBox([0, 0, 0], [100, 200, 300]);
17+
let trsf = new __1.Transformation();
18+
trsf.makeTranslation([10, 20, 30]);
19+
let vert = getVerticeData(box);
20+
vert[1].should.eql([0, 0, 0]);
21+
box.applyTransform(trsf);
22+
let vert_after = getVerticeData(box);
23+
// translate vertex
24+
vert = vert.map((v) => add(v, [10, 20, 30]));
25+
vert_after.should.eql(vert);
26+
});
27+
});

0 commit comments

Comments
 (0)