discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

How passing vectors to modules and use them within the module?

CE
Christian Eugster
Fri, Mar 6, 2020 5:49 AM

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

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
M
MichaelAtOz
Fri, Mar 6, 2020 6:05 AM

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...

  • click on my MichaelAtOz label, there is a link to email me.

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/

> 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... * click on my MichaelAtOz label, there is a link to email me. 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/
JB
Jordan Brown
Fri, Mar 6, 2020 6:52 AM

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).

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).
P
Parkinbot
Fri, Mar 6, 2020 12:20 PM

There are many things to clarify in the code you show:

  1. You can evaluate an expression into a new! local variable (note: a
    variable in OpenSCAD is always a constant in the relevant scope):

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];

  1. You define a recursion by
    module move(size = [0, 0, 0], protrusion_x = 0, protrusion_y = 0,
    protrusion_z = 0)
    move(size + [protrusion_x, protrusion_y, protrusion_z]);

Don't do this without using a logic to that stops the recursion.

  1. You seem to intend to define move() as an operator (like translate()
    rotate() and so on) that modifies the construction parameters. To be honest,
    I would postpone this enterprise until you feel OK with the basics. A
    solution for this would involve a special purpose variable characterized by
    $ that is handed to and used by operands of such an expression. E.g.

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/

There are many things to clarify in the code you show: 1. You can evaluate an expression into a new! local variable (note: a variable in OpenSCAD is always a constant in the relevant scope): 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]; 2. You define a recursion by module move(size = [0, 0, 0], protrusion_x = 0, protrusion_y = 0, protrusion_z = 0) move(size + [protrusion_x, protrusion_y, protrusion_z]); Don't do this without using a logic to that stops the recursion. 3. You seem to intend to define move() as an operator (like translate() rotate() and so on) that modifies the construction parameters. To be honest, I would postpone this enterprise until you feel OK with the basics. A solution for this would involve a special purpose variable characterized by $ that is handed to and used by operands of such an expression. E.g. 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/