by ajb · 6y ago · 0 comments
I'm sure you can get used to it eventually. But I think we could do better in terms of 'cad designed by programmers'
x = 5;
cylinder(d = x, h=10);
x = 20;
Well, this will make a cylinder with a diameter of 20 instead of the expected value of 5. Yes, I'm familiar with various programming models and the idea of immutable variables. But when you have a language that looks and feels like something else familiar, this behavior can throw you for a loop.In a declarative languages, one should not be able to update variables. In some contexts, rebiding makes sense, but not on OpenSCAD either.
When I was using OpenSCAD about 10 years ago, I immediately found it very intuitive and easy to use for simple things. But for anything complex, I ran into this sort of thing constantly. At the time the documentation of the language mentioned such issues, but failed to specify the language completely, leaving lots of ambiguous cases that I just had to try out to see what would happen. The behavior really seemed to be something that just agglomerated as the author had added features, rather than something planned with a design philosophy behind it. I found it hard to work around and plan more complex shapes as a result. I haven't been back to look at the docs in ages; perhaps it's better-specified now.
I ended up doing my more complex stuff in an OpenSCAD clone I hacked together on top of https://evanw.github.io/csg.js/ until I ran into problems with non-manifold surfaces. Simple problems I could repair, but that library's really only intended for visualizations, not generating STLs for printing, so eventually I went beyond what I could fix.
So then I wrote an implicit surface renderer [also in JS] using https://github.com/mikolalysenko/surface-nets, and that was less-precise, and a bit limited [my code, not theirs], but got the job done. A short while later, I found implicitcad.org, and saw what I could have written, had I had just one more epiphany. I've never used it, but I like the philosophy behind it, and that may be what I try when I get back to 3D printing.
OpenSCAD really should throw an error on compile if you try to redefine variables, but its intent is a declarative language. Once you think of it as such, most of its design choices start to make sense.
Have you tried OpenJSCAD? It's OpenSCAD meets Javascript.
Now, to address the things you mentioned:
>I want to be able to make a cylinder from point A to point B.
You can write a function that does it in OpenSCAD already
> I want to be able to declare a cube and then refer to its corners.
Referring to vertices of a solid in general seems to be incompatible with OpenSCAD's paradigm of modeling with boolean operations. You don't know where the vertices are until you render.
You can already write a helper function which returns you the vertices of a cube. More than that, you would need another tool. It's not about syntax, to my understanding.
> Referring to vertices of a solid in general seems to be incompatible with OpenSCAD's paradigm of modeling with boolean operations. You don't know where the vertices are until you render.
Is it? Well I suppose it is in the general case. It would be hard to refer to an edge that has been created as a result of some difference operation. But the corners of a cube are still valid reference points even if they've been chopped off, and aren't part of the final solid.
Incidentally, I ended up needing a function that places a cylinder between two points in OpenSCAD yesterday.
Here is my implementation - might come in handy!
// Transpose of matrix A (swap rows and columns)
function transpose(A) = [for (j = [0:len(A[0])-1]) [for(i = [0:len(A)-1]) A[i][j]]];
// Cylinder of radius r from P to Q
module cyl_between(P, Q, r){
v = Q - P; // vector from P to Q
L = norm(v); // height of the cylnder = dist(P, Q)
c = v / L; // unit vector: direction from P to Q
is_c_vertical = ( 1 - abs(c * [0, 0, 1]) < 1e-6); //is c parallel to z axis?
u = is_c_vertical ? [1, 0, 0] : cross([0, 0, 1], c); // normal to c and Z axis
a = u / norm(u); // unit vector normal to c and the Z axis
b = cross(c, a); // unit vector normal to a and b
// [a, b, c] is an orthonormal basis, i.e. the rotation matrix; P is the translation
MT = [a, b, c, P]; // the transformation matrix
M = transpose(MT); // OpenSCAD wants vectors in columns, so we need to transpose
multmatrix(M)
cylinder(h=L, r=r, $fn=24);
}I've created a few small things in OpenSCAD. To build up a part, I haven't found a better way than just keep adding up the width of the subcomponents to get the origin of the next piece.
It is manageable for me, and I find traditional CAD programs difficult, so I'll keep using it. But I want a better way to do all this.