jscad / OpenJSCAD.org

JSCAD is an open source set of modular, browser and command line tools for creating parametric 2D and 3D designs with JavaScript code. It provides a quick, precise and reproducible method for generating 3D models, and is especially useful for 3D printing applications.
https://openjscad.xyz/
MIT License
2.61k stars 511 forks source link

Command line utility does not allow outputting individual .stl's #117

Open falldeaf opened 8 years ago

falldeaf commented 8 years ago

When the 'render multiple objects' functionality is used: http://joostn.github.io/OpenJsCad/#renderingmultipleobjects

It would be really helpful to be able to output each object as a stand-alone .stl from the command line. Possibly with a flag to designate which one?

For example, given the code:

function main()
{
  var mycube = CSG.cube({radius: 10});
  var mycircle = CAG.circle({radius: 10});

  // we could just do:
  // return [mycube, mycircle];

  // give each element a description and filename:
  return [
    {
      name: "cube",              // optional, will be used as a prefix for the downloaded stl file
      caption: "A small cube",   // will be shown in the dropdown box
      data: mycube,
    },
    {
      name: "circle",            // optional, will be used as a prefix for the downloaded dxf file
      caption: "Circle",
      data: mycircle,
    },
  ];
}

Accessing each object might happen with a (m)ultiple flag:

z3dev commented 8 years ago

I like the idea, and hopefully we can re-implement this functionality in the browser. I've made some recent changes that will make this easier.

As for the CLI script, I like the proposal that the 'm' flag can be used to select an object. But the script should also output appropriately if no selection is made. Does this mean all objects are combined (union) or files are created for each object?

falldeaf commented 8 years ago

Great point about the implication of not having an M flag if multiple objects are returned. I think union'ing all objects would cover most use cases.

This may be overkill but to be really comprehensive, maybe the 'M' flag could have a few reserved keywords? (union, all) and also accept a csv of names? -m union (does the default of giving you a union of all objects) -m all (creates multiple .stl files, using numbered file collision in case multiple objects have same name [ignores -o flag]) -m cube,circle (creates .stl files for only the objects listed [ignores -o flag]) -m cube (selects the cube object only [needs -o flag to name file]

z3dev commented 8 years ago

Interesting...

Maybe we can follow one of the standard UNIX command lines, like CP. If the target is a file then "union" is implied. If the target is a directory then individual files is implied. And the -m flag serves to select which of the objects to output.

FYI, the whole file system API is available in NODEJS.

SYNOPSIS cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file target_file cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file ... target_directory

DESCRIPTION In the first synopsis form, the cp utility copies the contents of the source_file to the target_file. In the second synopsis form, the contents of each named source_file is copied to the destination target_directory. The names of the files themselves are not changed. If cp detects an attempt to copy a file to itself, the copy will fail.

hbp commented 8 years ago

You could provide both union and separate file as follows :

If the return is an array, each element can be separately output using -m 0, -m 1 etc. Without -m the union of all the elements is generated.

For a returned object, -m will output an individual item, without -m output all items are separate files.

falldeaf commented 8 years ago

z3dev, I have to say, I really like that idea! It accounts for all scenarios without cluttering the -m flag with reserved keywords.

z3dev commented 8 years ago

Cool!

I've started a new script. It only processes a single JSCAD file into STL, but reuses the code found in openjscad.js, including running the script and converting to another format. I had to make a few tweeks to OpenJsCad.Processor functions in order to make error and status messages display correctly (HTML vs CLI), etc. The BIG improvement is that include() works perfectly!!!

runnodejs.txt