Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] ImportLibrary() and Proper Typescript Support #15

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ CascadeStudio/
node_modules/opencascade.js/dist/opencascade.js
# Add the above line for now, can take it out if necessary

# TypeScript Compiler Bin
!node_modules/typescript/
!node_modules/typescript/*/
!node_modules/typescript/bin/
!node_modules/typescript/bin/*
!node_modules/typescript/lib/
!node_modules/typescript/lib/*

# potpack
!node_modules/potpack/
!node_modules/potpack/index.js
Expand Down
21 changes: 21 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<meta name="theme-color" content="#1e1e1e">

<!-- Include these early and directly so they happen first -->
<link rel="prefetch" href="./js/CADWorker/CascadeStudioMainWorker.js">
<script>
// Install Cascade Studio as a Progressive Web App for Offline Access
// This needs to be put before ANY HTTP Requests are made, so it can cache them.
Expand Down Expand Up @@ -61,6 +62,26 @@
<script type="text/javascript" src="./node_modules/rawflate/rawinflate.js"></script>
<script type="text/javascript" src="./js/MainPage/CascadeViewHandles.js"></script>
<script type="text/javascript" src="./js/MainPage/CascadeView.js"></script>

<!-- Speed up loading by prefetching URLs here -->
<link rel="prefetch" href="./js/CADWorker/CascadeStudioShapeToMesh.js">
<link rel="prefetch" href="./node_modules/opencascade.js/dist/oc.d.ts">
<link rel="prefetch" href="./node_modules/three/build/three.d.ts">
<link rel="prefetch" href="./node_modules/typescript/lib/lib.es5.d.ts">
<link rel="prefetch" href="./node_modules/typescript/lib/lib.webworker.d.ts">
<link rel="prefetch" href="./node_modules/opencascade.js/dist/opencascade.wasm.js">
<link rel="prefetch" href="./node_modules/monaco-editor/min/vs/language/typescript/tsMode.js">
<link rel="prefetch" href="./node_modules/typescript/bin/typescript.min.js">
<link rel="prefetch" href="./node_modules/monaco-editor/min/vs/basic-languages/typescript/typescript.js">
<link rel="prefetch" href="./node_modules/opentype.js/dist/opentype.min.js">
<link rel="prefetch" href="./textures/dullFrontLitMetal.png">
<link rel="prefetch" href="./node_modules/monaco-editor/min/vs/base/worker/workerMain.js">
<link rel="prefetch" href="./manifest.webmanifest">
<link rel="prefetch" href="./js/Libraries/CascadeStudioStandardLibrary.ts">
<link rel="prefetch" href="./node_modules/monaco-editor/min/vs/language/typescript/tsWorker.js">
<link rel="prefetch" href="./js/Libraries/CascadeStudioStandardUtils.ts">
<link rel="prefetch" href="./js/CADWorker/CascadeStudioFileUtils.js">

</head>

<body onload="initialize();" style="margin:0px; background-color:rgb(34, 34, 34); "><!--overflow:hidden; position:fixed;-->
Expand Down
36 changes: 33 additions & 3 deletions js/CADWorker/CascadeStudioMainWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,40 @@ console.error = function (err, url, line, colno, errorObj) {

// Import the set of scripts we'll need to perform all the CAD operations
importScripts(
'../../node_modules/three/build/three.min.js',
'./CascadeStudioStandardLibrary.js',
//'../../node_modules/three/build/three.min.js',
'./CascadeStudioShapeToMesh.js',
'../../node_modules/opencascade.js/dist/opencascade.wasm.js',
'../../node_modules/opentype.js/dist/opentype.min.js',
'../../node_modules/typescript/bin/typescript.min.js',
'../../node_modules/potpack/index.js');

let importedLibraries = {};
/** This function imports a typescript file to the current workspace.
* Note, urls are not imported multiple times unless forceReload is true. */
function ImportLibrary(urls, forceReload) {
urls.forEach((url) => {
if (!importedLibraries[url] || forceReload) {
let oReq = new XMLHttpRequest();
oReq.addEventListener("load", (response) => {
if (response.target.status === 200 && response.target.responseText) {
importedLibraries[url] = ts.transpileModule(response.target.responseText,
{ compilerOptions: { module: ts.ModuleKind.CommonJS } }).outputText;
eval.call(null, importedLibraries[url]);
postMessage({ "type": "addLibrary", payload: { url: url, contents: response.target.responseText } });
} else {
console.error("Could not find library at this URL! URL: "+ response.target.responseURL +", Status Code: "+response.target.status);
console.error(response);
}
});
oReq.open("GET", url, false); oReq.send();
} else {
// Already Imported this URL, no need to do so again...
}
});
}

ImportLibrary(['../Libraries/CascadeStudioStandardLibrary.ts']);

// Preload the Various Fonts that are available via Text3D
var preloadedFonts = ['../../fonts/Roboto.ttf',
'../../fonts/Papyrus.ttf', '../../fonts/Consolas.ttf'];
Expand Down Expand Up @@ -71,7 +98,10 @@ function Evaluate(payload) {
opNumber = 0; // This keeps track of the progress of the evaluation
GUIState = payload.GUIState;
try {
eval(payload.code);
let transpiled = ts.transpileModule(payload.code,
{ compilerOptions: { module: ts.ModuleKind.CommonJS } });

eval(transpiled.outputText);
} catch (e) {
setTimeout(() => {
e.message = "Line " + currentLineNumber + ": " + currentOp + "() encountered " + e.message;
Expand Down
12 changes: 7 additions & 5 deletions js/CADWorker/CascadeStudioShapeToMesh.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
function LengthOfCurve(geomAdaptor, UMin, UMax, segments = 5) {
let point1 = new THREE.Vector3(), point2 = new THREE.Vector3(), arcLength = 0, gpPnt = new oc.gp_Pnt();
let point1 = [0.0, 0.0, 0.0], point2 = [0.0, 0.0, 0.0],
arcLength = 0, gpPnt = new oc.gp_Pnt();
for (let s = UMin; s <= UMax; s += (UMax - UMin) / segments){
geomAdaptor.D0(s, gpPnt);
point1.set(gpPnt.X(), gpPnt.Y(), gpPnt.Z());
point1[0] = gpPnt.X(); point1[1] = gpPnt.Y(); point1[2] = gpPnt.Z();
if (s == UMin) {
point2.copy(point1);
point2[0] = point1[0]; point2[1] = point1[1]; point2[2] = point1[2];
} else {
arcLength += point1.distanceTo(point2);
let xDiff = point2[0] - point1[0], yDiff = point2[1] - point1[1], zDiff = point2[2] - point1[2];
arcLength += Math.sqrt((xDiff * xDiff) + (yDiff * yDiff) + (zDiff * zDiff));//point1.distanceTo(point2);
}
point2.copy(point1);
point2[0] = point1[0]; point2[1] = point1[1]; point2[2] = point1[2];
}
return arcLength;
}
Expand Down
Loading