Variables are not variable

http://www.openscad.org/
enggmaug
Printmaster!
Posts: 301
Joined: Wed Nov 13, 2013 3:54 am
Location: Antony, France

Variables are not variable

Postby enggmaug » Tue Mar 01, 2016 4:57 am

Hi,

I am a programmer and in that respect wanted to give a try to Openscad.

I spent about 1 day programming different objects in order to get used to it.
I faced a problem while using it : Variables are set at compile, not at run time.
In other words, you cannot change the value of a variable within a program.

What is the point calling them variables ?

I just feel like I lost my time trying it. In the user manual, they say that this makes the beauty of Openscad. My opinion is that it makes it useless to me.

Because of that, I believe there is nothing I can do faster in openscad than in my usual CAD software.

If you expect a programming language to actually support variables, don't loose too much time with openscad.
If you are an openscad user and know a workaround, please tell me how.

User avatar
jmpreuss
Printmaster!
Posts: 181
Joined: Fri Sep 20, 2013 1:12 pm

Re: Variables are not variable

Postby jmpreuss » Tue Mar 01, 2016 8:19 am

OpenScad is a different kind of programming language than typical and I always have to re-calibrate my brain every time I need to program in it. That being said there are ways to get variable type behavior out of it, it just isn't going to be in a way that feels natural. The best way I think to see this is to look at some openscad code on thingiverse from things that can be customized. One of my latest projects has code that I kind of commented http://www.thingiverse.com/thing:1154214
PTMNBN="Printer that must not be named" - a heavily upgraded Replicator 2

User avatar
geneb
ULTIMATE 3D JEDDI
Posts: 4326
Joined: Mon Oct 15, 2012 12:47 pm
Location: Graham, WA
Contact:

Re: Variables are not variable

Postby geneb » Tue Mar 01, 2016 9:14 am

OpenSCAD is a poor programming language masquerading as an even worse design tool. :) Do yourself a favor and check out either DesignSpark Mechanical or FreeCAD.

g.
Delta Power!
Defeat the Cartesian Agenda!
http://www.f15sim.com - 80-0007, The only one of its kind.
http://geneb.simpits.org - Technical and Simulator Projects

enggmaug
Printmaster!
Posts: 301
Joined: Wed Nov 13, 2013 3:54 am
Location: Antony, France

Re: Variables are not variable

Postby enggmaug » Tue Mar 01, 2016 11:22 am

jmpreuss wrote:OpenScad is a different kind of programming language than typical and I always have to re-calibrate my brain every time I need to program in it. That being said there are ways to get variable type behavior out of it, it just isn't going to be in a way that feels natural. The best way I think to see this is to look at some openscad code on thingiverse from things that can be customized. One of my latest projects has code that I kind of commented http://www.thingiverse.com/thing:1154214


Well yes... you don't use variable variables.

Customizer just allows to force the value of constant "variables".

What I'm trying to do is something like that, within a loop, on a if condition :
variable = variable +1;

That is impossible as far I could see.

Geneb, I was not looking for a design tool, but I thought openscad could open new doors to me... it appears it cannot.
I know Freecad, and don't like the user interface. I use Wings3D with great success, and OnShape on specific occasions.

I never heard of DesignSpark Mechanical. I'll have a look at it, since onshape is very intuitive, but the free plan is also very limited in term of data you can host.

User avatar
geneb
ULTIMATE 3D JEDDI
Posts: 4326
Joined: Mon Oct 15, 2012 12:47 pm
Location: Graham, WA
Contact:

Re: Variables are not variable

Postby geneb » Tue Mar 01, 2016 1:40 pm

DSM has a workflow very similar to SketchUp.

g.
Delta Power!
Defeat the Cartesian Agenda!
http://www.f15sim.com - 80-0007, The only one of its kind.
http://geneb.simpits.org - Technical and Simulator Projects

User avatar
jmpreuss
Printmaster!
Posts: 181
Joined: Fri Sep 20, 2013 1:12 pm

Re: Variables are not variable

Postby jmpreuss » Tue Mar 01, 2016 8:10 pm

enggmaug wrote:
What I'm trying to do is something like that, within a loop, on a if condition :
variable = variable +1;


Can you give an example of what you are trying to model that you want to use this if statement? It could be it's impossible with an if statement but it may be possible another way. I have programmed some pretty difficult models in OpenSCAD and not once have I run into something that was impossible, though maybe I had to do it very differently than I first thought.
PTMNBN="Printer that must not be named" - a heavily upgraded Replicator 2

enggmaug
Printmaster!
Posts: 301
Joined: Wed Nov 13, 2013 3:54 am
Location: Antony, France

Re: Variables are not variable

Postby enggmaug » Wed Mar 02, 2016 3:58 am

jmpreuss wrote:
enggmaug wrote:
What I'm trying to do is something like that, within a loop, on a if condition :
variable = variable +1;


Can you give an example of what you are trying to model that you want to use this if statement? It could be it's impossible with an if statement but it may be possible another way. I have programmed some pretty difficult models in OpenSCAD and not once have I run into something that was impossible, though maybe I had to do it very differently than I first thought.


Sure... as a learning exercice, I was trying to build a parametric wall, in which you specify the wall dimensions, and min Max dimension of stones in both length and height.
The idea was to randomly choose integer dimensions for each stone, and build the wall up.

For example, all my stones are between 5 and 15 units in length, and between 5 and 15 units in height, only using integer values. That gives 11 different widths, 11 different heights, so 121 different stone types. Running twice the algorithm with a different seed value for the random would build 2 different walls, but with similar aspect.

The algorithm builds the wall sideways first, and then upways.

I use 2 intricated for loops for parsing the wall sideways and upways. I then use a if statement to check if a stone is present or not. If not, I create a new one.

That was the basic idea...

User avatar
Renha
Printmaster!
Posts: 276
Joined: Fri Apr 11, 2014 2:18 am
Location: Balashikha, Russian Federation
Contact:

Re: Variables are not variable

Postby Renha » Wed Mar 02, 2016 5:26 am

How algo should act if stones should be between 20 and 20 both dimensions, and wall dimension should be 45x45?
What if wall should be 19x19? 19x20?
What about third dimension?

OpenSCAD is a nice programming language masquerading as an even nicer design tool.  Do yourself a favor and don't check out either DesignSpark Mechanical or FreeCAD.
3D printing is magic!

Sorry my engrish :-/ you could PM me in Russian.

User avatar
jmpreuss
Printmaster!
Posts: 181
Joined: Fri Sep 20, 2013 1:12 pm

Re: Variables are not variable

Postby jmpreuss » Wed Mar 02, 2016 7:38 am

enggmaug wrote:
Sure... as a learning exercice, I was trying to build a parametric wall, in which you specify the wall dimensions, and min Max dimension of stones in both length and height.
The idea was to randomly choose integer dimensions for each stone, and build the wall up.

For example, all my stones are between 5 and 15 units in length, and between 5 and 15 units in height, only using integer values. That gives 11 different widths, 11 different heights, so 121 different stone types. Running twice the algorithm with a different seed value for the random would build 2 different walls, but with similar aspect.

The algorithm builds the wall sideways first, and then upways.

I use 2 intricated for loops for parsing the wall sideways and upways. I then use a if statement to check if a stone is present or not. If not, I create a new one.

That was the basic idea...


That is an interesting exercise, one I can't answer off the top of my head. I'll give it some thought though it may be a few days as I am in the middle of getting my projects completed for the MRRF.
PTMNBN="Printer that must not be named" - a heavily upgraded Replicator 2

enggmaug
Printmaster!
Posts: 301
Joined: Wed Nov 13, 2013 3:54 am
Location: Antony, France

Re: Variables are not variable

Postby enggmaug » Wed Mar 02, 2016 9:26 am

Renha wrote:How algo should act if stones should be between 20 and 20 both dimensions, and wall dimension should be 45x45?
What if wall should be 19x19? 19x20?
What about third dimension?

OpenSCAD is a nice programming language masquerading as an even nicer design tool.  Do yourself a favor and don't check out either DesignSpark Mechanical or FreeCAD.



What?

Algo should act the same with your values.
min max values given were just example. and anyway, they are parametric.

EDIT : I finally understood you question.

Algo should check if the remaining available part on the right is bigger or not than average value.
If yes, normal behavior.
If no, last stone takes all remaining space on the right.

I did not mention 3rd dimension, because this part is trivial.

Openscad is not a nice programming language to me.
As a design tool, it appears clumsy to me, but I can understand why someone would use it.

As I stated I don't know DesignSpark M.
I know Freecad a bit and I don't like the GUI. But I would never recommend anyone not to check it out. It is a nice piece of free software.
Freecad is not convenient to me, but I recognize it's very powerful.

BenTheRighteous
Printmaster!
Posts: 695
Joined: Fri Nov 07, 2014 9:38 am

Re: Variables are not variable

Postby BenTheRighteous » Thu Mar 03, 2016 12:09 am

One non-intuitive use of OpenSCAD (which I have not explored personally) is the ability to generate code for it algorithmically.

So, write some code in Java or something which has nice loops and variables, and inside the loop write out your .scad file line-by-line.

It won't be the most elegant .scad file ever, but it would get the job done.

Example (pseudo Java):

String theCode;
for (int x = 0 ; x < 10 ; x++) {
theCode += "translate(" + (x * 2) + ", 0, 0) cube(1)");
}
new File("test.scad").write(theCode).close();

Ok that code was horrible but I hope it gets the point across. You do the loop in Java which generates a file that tells OpenSCAD exactly what you want using procedural statements and constants, rather than trying to do loops and calculations and whatever else in OpenSCAD.
nitewatchman wrote:it was much cleaner and easier than killing a chicken on top of the printer.

enggmaug
Printmaster!
Posts: 301
Joined: Wed Nov 13, 2013 3:54 am
Location: Antony, France

Re: Variables are not variable

Postby enggmaug » Thu Mar 03, 2016 5:15 am

BenTheRighteous wrote:One non-intuitive use of OpenSCAD (which I have not explored personally) is the ability to generate code for it algorithmically.

So, write some code in Java or something which has nice loops and variables, and inside the loop write out your .scad file line-by-line.

It won't be the most elegant .scad file ever, but it would get the job done.

Example (pseudo Java):

String theCode;
for (int x = 0 ; x < 10 ; x++) {
theCode += "translate(" + (x * 2) + ", 0, 0) cube(1)");
}
new File("test.scad").write(theCode).close();

Ok that code was horrible but I hope it gets the point across. You do the loop in Java which generates a file that tells OpenSCAD exactly what you want using procedural statements and constants, rather than trying to do loops and calculations and whatever else in OpenSCAD.


Thanks for the workaround.

Actually, the wall was just an exercice... it's not like I need it.
But I'm glad I tried it right in the beginning, so that I could realize that openscad is just not for me.

Using any programming language, I'm sure there are ways to do the same in a more elegant and practical way by directly buiding a stl file or similar, rather than writing openscad commands.

enggmaug
Printmaster!
Posts: 301
Joined: Wed Nov 13, 2013 3:54 am
Location: Antony, France

Re: Variables are not variable

Postby enggmaug » Thu Mar 03, 2016 7:35 am

geneb wrote:(...) Do yourself a favor and check out either DesignSpark Mechanical or FreeCAD.
g.


Well, I had a closer look at Freecad, and realized that it can be entirely controlled using Python scripting.

I might give a closer look to that, because if it is what I imagine it is, then possibilities are endless.

I am now studying this page, and have great faith.

User avatar
geneb
ULTIMATE 3D JEDDI
Posts: 4326
Joined: Mon Oct 15, 2012 12:47 pm
Location: Graham, WA
Contact:

Re: Variables are not variable

Postby geneb » Thu Mar 03, 2016 8:42 am

Neat! You might want to check out the "latest" branch of FreeCAD. It's my understanding that the stable branch lags behind pretty significantly.

g.
Delta Power!
Defeat the Cartesian Agenda!
http://www.f15sim.com - 80-0007, The only one of its kind.
http://geneb.simpits.org - Technical and Simulator Projects


Return to “OpenSCAD”

Who is online

Users browsing this forum: No registered users and 1 guest