DSchroer / openscad-gear-configurator

20 stars 4 forks source link

Replacing gears scad with any other scad causes worker to hang #1

Closed CullenJWebb closed 1 year ago

CullenJWebb commented 1 year ago

Hello,

I've successfully replicated this configurator on my server and can customize the gear output.

However, once I change the gear.scad to any other scad (my scad file is placed in the same directory and I replace the references to gear.scad) the spinner starts and never stops.

The console wasn't giving me errors so I added console logs throughout and it hangs as soon as the main Openscad function is called.

Are there more changes that need to be made in order to use a different scad file?

Thank you for your time and this tool!

DSchroer commented 1 year ago

Hey.

Happy to hear that you are using this project.

I have a write up here on everything needed to adjust the file: https://schroer.ca/2022/03/20/openscad-configurator/

If it still does not work can you post a minimal example of the *.scad file that is broken so I can test it?

CullenJWebb commented 1 year ago

@DSchroer Hello again!

Sorry about the delay in my response.

I used that write-up and have installed everything on my machine but swapping out the .scad file still results in various errors depending on how I attempt things. Here is the .scad files which I am trying to implement: Gridfinity Rebuilt

Here is the URL where you can test my implementation (URL is temporary): https://www.gridfinity.cullenjwebb.com/fork/

Here is the code for the current attempt on that URL:

async function generateGear(pitch, teeth, thickness, boreDiameter) {
  globalThis.OpenSCAD = {
    noInitialRun: true,
  }

  importScripts("./openscad.wasm.js");

  await new Promise(resolve => {
    globalThis.OpenSCAD.onRuntimeInitialized = () => resolve();
  });

  const inst = globalThis.OpenSCAD;

  addMCAD(inst);

  // Original source file settings
  const sourceRes = await fetch("./models/gridfinity-rebuilt-bins.scad");
  const source = await sourceRes.text();
  inst.FS.writeFile("/gridfinity-rebuilt-bins.scad", source);

  // Additional files to be included
  const includedRes = await fetch("./models/gridfinity-rebuilt-utility.scad");
  const includedSource = await includedRes.text();
  inst.FS.writeFile("/gridfinity-rebuilt-utility.scad", includedSource);

  // Additional files to be included
  const includedRes2 = await fetch("./models/standard.scad");
  const includedSource2 = await includedRes2.text();
  inst.FS.writeFile("/standard.scad", includedSource2);

  inst.callMain([
    "/gridfinity-rebuilt-bins.scad",
    "-o",
    "out.stl",
  ]);
  const output = inst.FS.readFile("/out.stl");

  return output;
}

Being unfamiliar with the WASM filesystem I also attempted to merge the 3 .scad files into one. This works in openSCAD but not in the browser.

Something to note is that I am building with a Windows device and some changes were needed in the Makefile, and even then I needed to download a few libraries manually. Here's my Makefile:

ifeq ($(OS),Windows_NT)
    RM = del /Q
    MKDIR = mkdir
    CP_CMD = copy /Y
    PATH_DELIM = \\
else
    RM = rm -rf
    MKDIR = mkdir -p
    CP_CMD = cp
    PATH_DELIM = /
endif

build: configurator/configurator.js configurator/configurator.worker.js configurator/openscad.wasm configurator/openscad.wasm.js

configurator/openscad.wasm: lib
    $(CP_CMD) lib$(PATH_DELIM)openscad.wasm configurator$(PATH_DELIM)

configurator/openscad.wasm.js: lib
    $(CP_CMD) lib$(PATH_DELIM)openscad.wasm.js configurator$(PATH_DELIM)

configurator/configurator.worker.js: src/* lib node_modules
    npx rollup src/configurator.worker.js --file $@ --format iife

configurator/configurator.js: src/* lib node_modules
    npx rollup src/configurator.js --file $@ --format iife

run: lib node_modules
    npx http-server configurator

node_modules:
    npm ci

lib: scripts/pull-openscad.sh
    $(RM) lib
    $(MKDIR) lib
    cd lib && $(CURDIR)/$?

Thank you so much for your time! I'll keep poking at this and will let you know if I figure anything out.

CullenJWebb commented 1 year ago

I've figured it out! Thanks for the tool. I need to improve performance somehow but this is very cool.