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

Can't get object oriented polygon to work #83

Closed The3rdIcon closed 9 years ago

The3rdIcon commented 9 years ago

The example for the polygon is unclear as it isn't put into context. Below is my attempt at figuring it out but I don't understand why it. The error message I get is "Error in line 17: Uncaught ReferenceError: CSG is not defined"

If you aren't going to take the time with my code can you please at least respond with an example that works that I can copy and paste into the site so I can manipulate that and get something working. The polygons example only used the openscad method which seems more time consuming as I need to figure out how to connect the faces.

a = 10;
b =2.5;
c = -a;
d = -b;
e = 0;

 var polygons = [];
 polygons.push(new CSG.Polygon([
      new CSG.Vertex(new CSG.Vector3D(b,a,e)),
      new CSG.Vertex(new CSG.Vector3D(c,a,e)),
      new CSG.Vertex(new CSG.Vector3D(b,d,e)),
      new CSG.Vertex(new CSG.Vector3D(c,d,e)),
      new CSG.Vertex(new CSG.Vector3D(a,b,e)),
      new CSG.Vertex(new CSG.Vector3D(a,c,e)),
      new CSG.Vertex(new CSG.Vector3D(d,b,e)),
      new CSG.Vertex(new CSG.Vector3D(d,c,e)),
      new CSG.Vertex(new CSG.Vector3D(b,b,e)),
      new CSG.Vertex(new CSG.Vector3D(d,b,e)),
      new CSG.Vertex(new CSG.Vector3D(b,d,e)),
      new CSG.Vertex(new CSG.Vector3D(d,d,e)),
      new CSG.Vertex(new CSG.Vector3D(e,e,a))
   ])
);

var solid = CSG.fromPolygons(polygons);
function main() {
   return solid;
}
Spiritdude commented 9 years ago

Read again the OpenJSCAD documentation: put all functions calls of OpenJSCAD in a function which is called by main(), your polygon.push() is outside of main(), put it into main() or another function which is called by main() eventually. It's a design decision of the original OpenJsCad which I kept for OpenJSCAD.org. I'm aware for starters it's confusing, this is why I prominently describe it: https://github.com/Spiritdude/OpenJSCAD.org/wiki/User-Guide#anatomy-of-a-jscad-file