-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
102 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,49 @@ | ||
export function fromHex(hex: string): Uint8Array { | ||
import { FS } from "./openscad"; | ||
|
||
export function writeFile( | ||
fs: FS, | ||
filePath: string, | ||
contents: string, | ||
) { | ||
ensureDirectoryExists(fs, filePath); | ||
fs.writeFile(filePath, fromHex(contents)); | ||
} | ||
|
||
export function writeFolder( | ||
fs: FS, | ||
base: string, | ||
contents: Record<string, string>, | ||
) { | ||
for (const [file, data] of Object.entries(contents)) { | ||
const fullPath = base + file; | ||
ensureDirectoryExists(fs, fullPath); | ||
fs.writeFile(fullPath, fromHex(data)); | ||
} | ||
} | ||
|
||
function fromHex(hex: string): Uint8Array { | ||
if (hex.length == 0) { | ||
return new Uint8Array(0); | ||
} | ||
return new Uint8Array(hex.match(/../g)!.map((h) => parseInt(h, 16))); | ||
} | ||
|
||
function ensureDirectoryExists(fs: FS, filePath: string) { | ||
const dirIndex = filePath.lastIndexOf("/"); | ||
if (dirIndex != -1) { | ||
const dirname = filePath.substring(0, dirIndex); | ||
ensureDirectoryExists(fs, dirname); | ||
if (dirname != "" && !exists(fs, dirname)) { | ||
fs.mkdir(dirname); | ||
} | ||
} | ||
} | ||
|
||
function exists(fs: FS, path: string) { | ||
try { | ||
fs.stat(path); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
declare module '../../res/*.conf' { | ||
const data: string; | ||
export default data; | ||
} | ||
|
||
declare module '../../res/*' { | ||
const data: Record<string, string> | string; | ||
const data: Record<string, string>; | ||
export default data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
import { OpenSCAD } from "./openscad"; | ||
import { fromHex } from "./files"; | ||
import { writeFile, writeFolder } from "./files"; | ||
|
||
import config from "../../res/fonts/fonts.conf"; | ||
import fonts from "../../res/liberation"; | ||
|
||
export function addFonts(openscad: OpenSCAD) { | ||
openscad.FS.mkdir("/fonts"); | ||
openscad.FS.writeFile("/fonts/fonts.conf", fromHex(config as string)); | ||
for (const [file, data] of Object.entries(fonts as Record<string, string>)) { | ||
openscad.FS.writeFile("/fonts/" + file, fromHex(data)); | ||
} | ||
writeFile(openscad.FS, "/fonts/fonts.conf", config as string); | ||
writeFolder(openscad.FS, "/fonts/", fonts as Record<string, string>); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,8 @@ | ||
import { OpenSCAD } from "./openscad"; | ||
import { fromHex } from "./files"; | ||
import { writeFolder } from "./files"; | ||
|
||
import mcad from "../../res/MCAD"; | ||
|
||
export function addMCAD(openscad: OpenSCAD) { | ||
openscad.FS.mkdir("/libraries"); | ||
openscad.FS.mkdir("/libraries/MCAD"); | ||
|
||
for (const [file, data] of Object.entries(mcad as Record<string, string>)) { | ||
openscad.FS.writeFile("/libraries/MCAD/" + file, fromHex(data)); | ||
} | ||
writeFolder(openscad.FS, "/libraries/MCAD/", mcad as Record<string, string>); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,51 @@ | ||
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts"; | ||
import OpenScad from "../build/openscad.js"; | ||
import { addFonts } from "../build/openscad.fonts.js"; | ||
import { join } from "https://deno.land/std/path/mod.ts"; | ||
import { loadTestFiles } from "./testing.ts"; | ||
|
||
for await (const entry of Deno.readDir(".")) { | ||
if (!entry.isDirectory) { | ||
continue; | ||
} | ||
import OpenScad, { OpenSCAD } from "../build/openscad.js"; | ||
import { addFonts } from "../build/openscad.fonts.js"; | ||
import { addMCAD } from "../build/openscad.mcad.js"; | ||
|
||
Deno.test({ | ||
name: entry.name, | ||
fn: () => runTest(entry.name), | ||
}); | ||
} | ||
Deno.test("csg", async () => { | ||
const instance = await OpenScad({ noInitialRun: true }); | ||
await runTest(instance, "./csg"); | ||
}); | ||
|
||
Deno.test("cube", async () => { | ||
const instance = await OpenScad({ noInitialRun: true }); | ||
await runTest(instance, "./cube"); | ||
}); | ||
|
||
async function runTest(directory: string) { | ||
Deno.test("cylinder", async () => { | ||
const instance = await OpenScad({ noInitialRun: true }); | ||
await runTest(instance, "./cylinder"); | ||
}); | ||
|
||
Deno.test("lib", async () => { | ||
const instance = await OpenScad({ noInitialRun: true }); | ||
await runTest(instance, "./lib"); | ||
}); | ||
|
||
Deno.test("mcad", async () => { | ||
const instance = await OpenScad({ noInitialRun: true }); | ||
addMCAD(instance); | ||
await runTest(instance, "./mcad"); | ||
}); | ||
|
||
Deno.test("text", async () => { | ||
const instance = await OpenScad({ noInitialRun: true }); | ||
addFonts(instance); | ||
await runTest(instance, "./text"); | ||
}); | ||
|
||
async function runTest(instance: OpenSCAD, directory: string) { | ||
const __dirname = new URL('.', import.meta.url).pathname; | ||
|
||
await loadTestFiles(instance, directory); | ||
await loadTestFiles(instance, join(__dirname, directory)); | ||
|
||
const code = instance.callMain([`/test.scad`, "-o", "out.stl"]); | ||
assertEquals(0, code); | ||
|
||
const output = instance.FS.readFile("out.stl", { encoding: "binary" }); | ||
await Deno.writeFile(`${directory}/out.stl`, output); | ||
await Deno.writeFile(join(__dirname, directory, "out.stl"), output); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
use <MCAD/involute_gears.scad>; | ||
|
||
bevel_gear(); |