From 5ec86067cabf7d4c43a732debe330d483b9402d5 Mon Sep 17 00:00:00 2001 From: ochafik Date: Mon, 27 Mar 2023 22:38:22 +0100 Subject: [PATCH] Bundle OpenSCAD examples + update default model --- .gitignore | 2 ++ Makefile | 30 ++++++++++++------- public/index.html | 2 +- src/components/FilePicker.tsx | 5 +++- src/fs/zip-archives.ts | 8 +++++ src/state/default-scad.ts | 56 +++++++++++++++++++++++++---------- 6 files changed, 75 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index 3dd3fa0..086a9b3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,7 @@ node_modules libs dist public/libraries +public/openscad.js +public/openscad.wasm src/wasm package-lock.json \ No newline at end of file diff --git a/Makefile b/Makefile index fe85e78..651f569 100644 --- a/Makefile +++ b/Makefile @@ -8,10 +8,11 @@ all: public .PHONY: public public: \ - libs/openscad \ + libs/openscad-wasm \ public/openscad.js \ public/openscad.wasm \ public/libraries/fonts.zip \ + public/libraries/openscad.zip \ public/libraries/NopSCADlib.zip \ public/libraries/BOSL.zip \ public/libraries/BOSL2.zip \ @@ -40,19 +41,19 @@ dist/index.js: public dist/openscad-worker.js: src/openscad-worker.ts npx rollup -c -libs/openscad: - mkdir -p libs/openscad - wget ${WASM_BUILD_URL} -O libs/openscad.zip - ( cd libs/openscad && unzip ../openscad.zip ) - rm libs/openscad.zip +libs/openscad-wasm: + mkdir -p libs/openscad-wasm + wget ${WASM_BUILD_URL} -O libs/openscad-wasm.zip + ( cd libs/openscad-wasm && unzip ../openscad-wasm.zip ) + rm libs/openscad-wasm.zip rm -f src/wasm - ln -sf $(shell pwd)/libs/openscad src/wasm + ln -sf $(shell pwd)/libs/openscad-wasm src/wasm -public/openscad.js: libs/openscad libs/openscad/openscad.js - cp libs/openscad/openscad.{js,wasm} public +public/openscad.js: libs/openscad-wasm + cp libs/openscad-wasm/openscad.{js,wasm} public -public/openscad.wasm: libs/openscad libs/openscad/openscad.wasm - cp libs/openscad/openscad.wasm public +public/openscad.wasm: libs/openscad-wasm + cp libs/openscad-wasm/openscad.wasm public public/libraries/fonts.zip: libs/liberation mkdir -p public/libraries @@ -62,6 +63,13 @@ public/libraries/fonts.zip: libs/liberation libs/liberation: git clone --recurse https://github.com/shantigilbert/liberation-fonts-ttf.git ${SHALLOW} ${SINGLE_BRANCH} $@ +libs/openscad: + git clone https://github.com/openscad/openscad.git ${SHALLOW} ${SINGLE_BRANCH} $@ + +public/libraries/openscad.zip: libs/openscad + mkdir -p public/libraries + ( cd libs/openscad ; zip -r ../../public/libraries/openscad.zip `find examples -name '*.scad' | grep -v tests` ) + libs/BOSL2: git clone --recurse https://github.com/revarbat/BOSL2.git ${SHALLOW} ${SINGLE_BRANCH} $@ diff --git a/public/index.html b/public/index.html index 3f4ddf9..ae36c03 100644 --- a/public/index.html +++ b/public/index.html @@ -2,7 +2,7 @@ - + diff --git a/src/components/FilePicker.tsx b/src/components/FilePicker.tsx index f627ba6..800fec0 100644 --- a/src/components/FilePicker.tsx +++ b/src/components/FilePicker.tsx @@ -9,6 +9,9 @@ import { join } from '../fs/filesystem'; import { defaultSourcePath } from '../state/initial-state'; import { zipArchives } from '../fs/zip-archives'; +const biasedCompare = (a: string, b: string) => + a === 'openscad' ? -1 : b === 'openscad' ? 1 : a.localeCompare(b); + function listFilesAsNodes(fs: FS, path: string, accept?: (path: string) => boolean): TreeNode[] { const files: [string, string][] = [] const dirs: [string, string][] = [] @@ -27,7 +30,7 @@ function listFilesAsNodes(fs: FS, path: string, accept?: (path: string) => boole } (isDirectory ? dirs : files).push([name, childPath]); } - [files, dirs].forEach(arr => arr.sort(([a], [b]) => a.localeCompare(b))); + [files, dirs].forEach(arr => arr.sort(([a], [b]) => biasedCompare(a, b))); const nodes: TreeNode[] = [] for (const [arr, isDirectory] of [[files, false], [dirs, true]] as [[string, string][], boolean][]) { diff --git a/src/fs/zip-archives.ts b/src/fs/zip-archives.ts index a7a2bea..64f99c9 100644 --- a/src/fs/zip-archives.ts +++ b/src/fs/zip-archives.ts @@ -22,6 +22,14 @@ export type ZipArchives = { export const zipArchives: ZipArchives = { 'fonts': {}, + 'openscad': { + description: 'OpenSCAD', + gitOrigin: { + branch: 'master', + repoUrl: 'https://github.com/openscad/openscad', + include: [{glob: ['examples/*.scad', 'LICENSE']}], + }, + }, 'MCAD': { description: 'OpenSCAD Parametric CAD Library', gitOrigin: { diff --git a/src/state/default-scad.ts b/src/state/default-scad.ts index 07af119..a9cc16b 100644 --- a/src/state/default-scad.ts +++ b/src/state/default-scad.ts @@ -1,19 +1,45 @@ // Portions of this file are Copyright 2021 Google LLC, and licensed under GPL2+. See COPYING. -export default ` -rotate([90, 0, 0]) { - translate([-50, 0, 50]) - linear_extrude(10) - text("hello world!"); - -} -cube(40, center=true); -translate([10, 10, 10]) - cube(40, center=true); +export default `/* + Hello there! -// This demo includes many libraries: -// -// include -// translate([-100, 0, 0]) -// spheroid(d=100, style="icosa", $fn=20); + If you're new to OpenSCAD, please learn the basics here: + https://openscad.org/documentation.html + + There are lots of amazing libraries in the OpenSCAD ecosystem + (see this list: https://openscad.org/libraries.html). + + Some of these libraries are bundled with this playground + (search for "demo" or "example" in the file explorer above) + and can be included directly from your models. + + Any bugs (this is an Alpha!) or ideas of features? + https://github.com/openscad/openscad-playground/issues/new +*/ + +// Click on Render or hit F6 to do a fine-grained rendering. +$fn=$preview ? 20 : 100; + +translate([-24,0,0]) { + union() { + cube(15, center=true); + sphere(10); + } +} + +intersection() { + cube(15, center=true); + sphere(10); +} + +translate([24,0,0]) { + difference() { + cube(15, center=true); + sphere(10); + } +} + +translate([0, -30, -12]) + linear_extrude(1) + text("OpenSCAD Playground", halign="center", valign="center"); ` \ No newline at end of file