Hi, I am new to Openscad and very enthusiastic about it. For resolving my problem below I already searched the internet, the site of Openscad and the community of openscad, with no result. So:
If I define a module as following,
module move(size = [0, 0, 0], protrusion_x = 0, protrusion_y = 0, protrusion_z = 0)
how am I capable to evaluate the vector size? I tried the following without success:
size.x = size.x + protrusion_x;
And
size[0] = size[0] + protrusion_x;
What is the right way to achieve this?
The module should do the following:
module move(size, protrusion_x = 0, protrusion_y = 0, protrusion_z = 0)
{
size.x = size.x + protrusion_x;
size.y = size.y + protrusion_y;
size.z = size.z + protrusion_z;
move(size);
}
move(size, x = 0.1) cube(cube_size);
Any help is appreciated
Best wishes
Christian
size[0] = size[0] + protrusion_x;
You can't reassign identifiers like that, see
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/The_OpenSCAD_Language#Variables
size[0] size[1] etc are how to reference the value.
size.x .y .z is a shortcut that can be used for the first three elements,
but not to assign them
Try
module move(size=[0, 0, 0], protrusion_x = 0, protrusion_y = 0, protrusion_z
= 0)
{
vector = [ size.x + protrusion_x, size.y + protrusion_y, size.z +
protrusion_z];
translate(vector)
DoSomeThing();
}
Admin - email* me if you need anything, or if I've done something stupid...
Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.
--
Sent from: http://forum.openscad.org/
On 3/5/2020 10:05 PM, MichaelAtOz wrote:
size[0] = size[0] + protrusion_x;
[...]
vector = [ size.x + protrusion_x, size.y + protrusion_y, size.z +
protrusion_z];
Or if you want an intermediate variable, that's OK too:
tmp_x = size.x + protrusion_x;
tmp_y = size.y + protrusion_y;
tmp_z = size.z + protrusion_z;
or with tmp as an array:
tmp = [
size.x + protrusion_x,
size.y + protrusion_y,
size.z + protrusion_z
];
or you might consider having protrusion_* be an array itself, which
would make it even simpler:
tmp = size + protrusion;
You just can't change the value of an existing variable (or array element).
There are many things to clarify in the code you show:
module move(size = [0, 0, 0], protrusion_x = 0, protrusion_y = 0,
protrusion_z = 0)
new_size = size + [protrusion_x, protrusion_y, protrusion_z];
But you can't set single components of a variable meant to contain a vector
(=list) or matrix value (=list of lists). However OpenSCAD lets you write:
module move(size = [0, 0, 0], protrusion_x = 0, protrusion_y = 0,
protrusion_z = 0)
size = size + [protrusion_x, protrusion_y, protrusion_z];
This is because the local "variable" size shadows the parameter size
after it is defined. Note that the parameter size can be used in the
R-value of that definition, where it is not yet shadowed. Similarily, the
parameter size would shadow a global variable named size. Thus you get
three different entities (two being shadowed) by writing:
size= [0,0,0];
module move(size = size, protrusion_x = 0, protrusion_y = 0, protrusion_z =
0)
size = size + [protrusion_x, protrusion_y, protrusion_z];
Don't do this without using a logic to that stops the recursion.
move([10, 10, 10], 1, 2, 13) cube($size);
module move(size = [0, 0, 0], protrusion_x = 0, protrusion_y = 0,
protrusion_z = 0)
{
$size = size + [protrusion_x, protrusion_y, protrusion_z];
children();
}
--
Sent from: http://forum.openscad.org/