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.66k stars 515 forks source link

[sugestion] Import stl #170

Closed paddelkraft closed 7 years ago

paddelkraft commented 8 years ago

Thanks for a great tool OpenJSCAD is my tool of choice for 3D modeling and i use it a lot. One feature that i miss is the ability to import STL files and interact with them. I believe that this feature would lift the tool to a new level.

Im A curious developer with experience in JS development so I would be willing to help out in the effort to add this feature if someone who is more familiar with the project can give me some pointers on where to start.

z3dev commented 8 years ago

@paddelkraft Cool! We would love any contribution to code or documents.

STL file can be imported (and converted) via drag and drop. What else did you have in mind?

paddelkraft commented 8 years ago

i did not know that i could not find that info in the user guide. Then this turns into a question. how do i get a handle to the CSG object of the model imported from an STL so that i can perform operations on it?

is there a way to do something like this

//dummy example
var stlObject = importedSTL("file.stl");
stlObject.translate([10,10,10]).subtract(otherObject)

or do i need to copy paste the generated polyhedron?

z3dev commented 8 years ago

@paddelkraft First, drag and drop any STL file to the page. The STL will be converted to CSG function calls. You can rename "function main()" to something like "function importedSTL()". And create...

function main() { var obj = importedSTL(); // now you can do anything to the obj obj = obj.scale([5,5,5]}.rotateZ(45); return obj; }

z3dev commented 8 years ago

Here's my take on the "direct" import...

First and foremost, "direct" import should create a native object. That means that the 'direct' import should create CSG or CAG object(s). This is for performance reasons. So, "conversions" from another format should be performed outside of the "direct" import.

Thoughts?

kaosat-dev commented 7 years ago

By 'direct' import do you mean the drag & drop version or the one using the importSTL( or even better importGeometry ?) The problem is I am not sure you can have the same API whether you are working in the 'online' version, or a locally hosted one, or cli etc

perhaps drag & droping stls could add them to the current 'workspace' and convert to native format as you say path resolution from within the script could be something like this: in browser, once you dragged and dropped it: local path is 'workspace': advantage, can also work the same way in command line / node version if the files are in the same folder const foo = importGeometry("./file.stl")

paddelkraft commented 7 years ago

i do not really care exactly how its implemented but being able to import an stl via code or if you work with a folder and the .stl objects being available as objects named after the file name or something similar.

the point is to not have to create a .jscad file of it to use it and have it auto update on change.

z3dev commented 7 years ago

@paddelkraft Yup! That's what I want...

kaosat-dev commented 7 years ago

I have started to look into implementing this but there is a crucial question : are you ok with it to support only 'local' files ? (in the 'same folder' / set of drag & dropped files)

kaosat-dev commented 7 years ago

Actually never mind on second thought it could be done in the same way 'include' works

z3dev commented 7 years ago

We should differentiate import (conversion) to "source" versus import of "geometry". Somethings cannot be imported as pure "geometry".

kaosat-dev commented 7 years ago

@z3dev do you mean something like 3mf / amf files with the extra structure & metadata etc ?

z3dev commented 7 years ago

Let me clarify... Geometry means vertexes and points; fixed and calculated.

For an example, SVG supports Bezer curves, which should be imported as functions calls to those routines. If imported as pure "geometry" (2D vertexes and points) then there design loss.

gilboonet commented 7 years ago

I'm not experienced enough to make reliable changes directly to OpenJSCAD code but I'm using a mechanism that works on a web application where user is asked to select a STL file, and then a .jscad script runs using it. What i did is :

To access the STL model the .jscad script just need to call it function name. It would really be great if such a mechanism could be directly accessible from OpenJSCAD, directly from the code or even better from an input parameter, and why not for other sources than STL.

(it can be seen here : http://gilboo.carton.free.fr/OJSC5/edit_mat8b.html )

z3dev commented 7 years ago

This issue was moved to jscad/io#8