discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Exact parameters of a module in openscad source code

GS
Guenther Sohler
Thu, Dec 22, 2022 5:50 PM

Hi

I am trying to evaluate the option to code more useful OPENSCAD functions
for myself and I look into:
src/core/UserModule.cc, void UserModule::print(std::ostream& stream, const
std::string& indent)

It appears that variable 'parameters'  hold the override parameters which
are used to call a module.
Is it possible to access the effective parameters(defaultr+override)
parameters of a module
(or even better: access local calculated values within the module)

In case there is a quick solution, I am happy to know.

Thanks!

Hi I am trying to evaluate the option to code more useful OPENSCAD functions for myself and I look into: src/core/UserModule.cc, void UserModule::print(std::ostream& stream, const std::string& indent) It appears that variable 'parameters' hold the override parameters which are used to call a module. Is it possible to access the effective parameters(defaultr+override) parameters of a module (or even better: access local calculated values within the module) In case there is a quick solution, I am happy to know. Thanks!
TP
Torsten Paul
Thu, Dec 22, 2022 6:14 PM

That's not nearly enough information to answer the question. It might
help to first outline the feature you are going for.

Starting the discussion from the internal code is usually not going
to work as the much more important part is the user visible side and
how it behaves (also in respect to backward compatibility).

You can also find a developer or two in the IRC channel if you want
to discuss such things :-).

ciao,
Torsten.

That's not nearly enough information to answer the question. It might help to first outline the feature you are going for. Starting the discussion from the internal code is usually not going to work as the much more important part is the user visible side and how it behaves (also in respect to backward compatibility). You can also find a developer or two in the IRC channel if you want to discuss such things :-). ciao, Torsten.
GS
Guenther Sohler
Fri, Dec 23, 2022 3:24 PM

Hi Torsten,

Thank you for your valuable feedback.

I am trying to write  a function, which can access the local variables of
the current module(index 0) , the father module(index 1) or the
grandfather module(index 2) etc.

I named it module_variable(index, variable_name) . The Code can be found in
the end of the email

My test code looks like:

module mymod(dim,name="none")
{
var=dim;
echo(name);
echo(module_variable(0,"var"));
echo(module_variable(1,"var"));
echo(module_variable(2,"var"));

if($children > 0)
{
     for(i=[0:$children-1])
     {
        children(i);
     }
 }

}

mymod([10,9,8],name="parent")
mymod([5,4,3],name="child");

And the output is:

ECHO: "parent"

ECHO: [10, 9, 8]

WARNING: iter is 0x34da0bb4a40 -> 0x34d9e70f500

ECHO: [10, 9, 8]

WARNING: iter is 0x34da0bb3cc0 -> 0x34d9e70f500

WARNING: iter is 0x34d9e70f500 -> 0x34da0bb4440

ECHO: undef

ECHO: "child"

ECHO: [5, 4, 3]

WARNING: iter is 0x34da0bb1a40 -> 0x34d9e703e20

ECHO: [5, 4, 3]

WARNING: iter is 0x34da0bb1200 -> 0x34d9e703e20

WARNING: iter is 0x34d9e703e20 -> 0x34da0bb4440

ECHO: undef

I expect the output of "module_variable" different in each generation, but
they always the same in each generation

Isn't the function try_lookup_variable appropriate for my purpose or are
the local variables of the parent modules being overwritten and nobody
cared so far ?

Thank you ...

Value builtin_module_variable(const std::shared_ptr<const Context>&
context, const FunctionCall * func_call)
{
Arguments arguments(func_call->arguments, context);
Location loc = Location::NONE;
double d;
std::string parname;
if (arguments.size() == 0) {
d = 1;
} else if (!check_arguments("module_variable", arguments, loc, {
Value::Type::NUMBER, Value::Type::STRING })) {
return Value::undefined.clone();
} else {
d = arguments[0]->toDouble();
parname = arguments[1]->toString();
}

int n = trunc(d);
if (n < 0) {
LOG(message_group::Warning, loc, arguments.documentRoot(), "Negative
depth index (%1$d) not allowed", n);
return Value::undefined.clone();
}

std::shared_ptr<const Context> cont_iter= context, parent;

for(int i=0;i<n;i++) { // traverse n parents
parent  =  cont_iter->getParent() ;
if(parent == NULL) {
LOG(message_group::Warning, loc, arguments.documentRoot(), "Parent
Number of out Range");
}
LOG(message_group::Warning, loc, arguments.documentRoot(), "iter  is
%1$p -> %2$p", cont_iter,parent);
cont_iter = parent;
}
boost::optional<const Value&> result =
cont_iter->try_lookup_variable(parname);
if (!result) {
return Value::undefined.clone();
}
return result->clone();

}

On Thu, Dec 22, 2022 at 7:14 PM Torsten Paul Torsten.Paul@gmx.de wrote:

That's not nearly enough information to answer the question. It might
help to first outline the feature you are going for.

Starting the discussion from the internal code is usually not going
to work as the much more important part is the user visible side and
how it behaves (also in respect to backward compatibility).

You can also find a developer or two in the IRC channel if you want
to discuss such things :-).

ciao,
Torsten.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Hi Torsten, Thank you for your valuable feedback. I am trying to write a function, which can access the local variables of the current module(index 0) , the father module(index 1) or the grandfather module(index 2) etc. I named it module_variable(index, variable_name) . The Code can be found in the end of the email My test code looks like: module mymod(dim,name="none") { var=dim; echo(name); echo(module_variable(0,"var")); echo(module_variable(1,"var")); echo(module_variable(2,"var")); if($children > 0) { for(i=[0:$children-1]) { children(i); } } } mymod([10,9,8],name="parent") mymod([5,4,3],name="child"); And the output is: ECHO: "parent" ECHO: [10, 9, 8] WARNING: iter is 0x34da0bb4a40 -> 0x34d9e70f500 ECHO: [10, 9, 8] WARNING: iter is 0x34da0bb3cc0 -> 0x34d9e70f500 WARNING: iter is 0x34d9e70f500 -> 0x34da0bb4440 ECHO: undef ECHO: "child" ECHO: [5, 4, 3] WARNING: iter is 0x34da0bb1a40 -> 0x34d9e703e20 ECHO: [5, 4, 3] WARNING: iter is 0x34da0bb1200 -> 0x34d9e703e20 WARNING: iter is 0x34d9e703e20 -> 0x34da0bb4440 ECHO: undef I expect the output of "module_variable" different in each generation, but they always the same in each generation Isn't the function try_lookup_variable appropriate for my purpose or are the local variables of the parent modules being overwritten and nobody cared so far ? Thank you ... =================== Value builtin_module_variable(const std::shared_ptr<const Context>& context, const FunctionCall * func_call) { Arguments arguments(func_call->arguments, context); Location loc = Location::NONE; double d; std::string parname; if (arguments.size() == 0) { d = 1; } else if (!check_arguments("module_variable", arguments, loc, { Value::Type::NUMBER, Value::Type::STRING })) { return Value::undefined.clone(); } else { d = arguments[0]->toDouble(); parname = arguments[1]->toString(); } int n = trunc(d); if (n < 0) { LOG(message_group::Warning, loc, arguments.documentRoot(), "Negative depth index (%1$d) not allowed", n); return Value::undefined.clone(); } std::shared_ptr<const Context> cont_iter= context, parent; for(int i=0;i<n;i++) { // traverse n parents parent = cont_iter->getParent() ; if(parent == NULL) { LOG(message_group::Warning, loc, arguments.documentRoot(), "Parent Number of out Range"); } LOG(message_group::Warning, loc, arguments.documentRoot(), "iter is %1$p -> %2$p", cont_iter,parent); cont_iter = parent; } boost::optional<const Value&> result = cont_iter->try_lookup_variable(parname); if (!result) { return Value::undefined.clone(); } return result->clone(); } ========== On Thu, Dec 22, 2022 at 7:14 PM Torsten Paul <Torsten.Paul@gmx.de> wrote: > That's not nearly enough information to answer the question. It might > help to first outline the feature you are going for. > > Starting the discussion from the internal code is usually not going > to work as the much more important part is the user visible side and > how it behaves (also in respect to backward compatibility). > > You can also find a developer or two in the IRC channel if you want > to discuss such things :-). > > ciao, > Torsten. > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
TP
Torsten Paul
Fri, Dec 23, 2022 5:07 PM

On 23.12.22 16:24, Guenther Sohler wrote:

I am trying to write  a function, which can access the local variables of
the current module(index 0) , the father module(index 1) or the grandfather
 module(index 2) etc.

Why? What is the actual use case?

It seems $ variables should already cover this for the most part.

module c() {
color($c) cube();
}

module m(col) {
$c = col; children();
}

m("red") c();
translate([2, 0, 0]) m("blue") c();

ciao,
Torsten.

On 23.12.22 16:24, Guenther Sohler wrote: > I am trying to write  a function, which can access the local variables of > the current module(index 0) , the father module(index 1) or the grandfather > module(index 2) etc. Why? What is the actual use case? It seems $ variables should already cover this for the most part. module c() { color($c) cube(); } module m(col) { $c = col; children(); } m("red") c(); translate([2, 0, 0]) m("blue") c(); ciao, Torsten.
GS
Guenther Sohler
Sat, Dec 24, 2022 9:30 AM

Hi Torsten,
Thank you for your immediate response.

Why? What is the actual use case?

Sorry, I forgot to mention.
I appreciate the relationship between parent and child very much and I want
to use it for my purposes.
My Plan is to  implement a relational placement between child and parent
like this:

mymod([10,9,8],name="parent")
mymod([5,4,3],name="child",ref="bot",dest="top");

whereas bot could mean the lower left corner of the cube and top could mean
the upper right corner of the cube.

I was aware of the $ variables in openscad, but I could not yet find the
documentation which makes me completely understand their functionality.
It appears i have yet kind-of-solution (they have the parent access
feature) but the code still is not very nice :

refoff=
    (ref == "top")?$top :
    (ref == "bot")?$bot :
    [0,0,0];

I plan not only to have  two "handles" but many many and my code will
become ugly very fast.
Is there another "hidden" feature in openscad which evaluates the content
of a variable and looks up a variable's content in the current scope ?

Thank you very much for your attention and Merry Xmas!

It seems $ variables should already cover this for the most part.

module c() {
color($c) cube();
}

module m(col) {
$c = col; children();
}

m("red") c();
translate([2, 0, 0]) m("blue") c();

ciao,
Torsten.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Hi Torsten, Thank you for your immediate response. > > > Why? What is the actual use case? > Sorry, I forgot to mention. I appreciate the relationship between parent and child very much and I want to use it for my purposes. My Plan is to implement a relational placement between child and parent like this: mymod([10,9,8],name="parent") mymod([5,4,3],name="child",ref="bot",dest="top"); whereas bot could mean the lower left corner of the cube and top could mean the upper right corner of the cube. I was aware of the $ variables in openscad, but I could not yet find the documentation which makes me completely understand their functionality. It appears i have yet kind-of-solution (they have the parent access feature) but the code still is not very nice : refoff= (ref == "top")?$top : (ref == "bot")?$bot : [0,0,0]; I plan not only to have two "handles" but many many and my code will become ugly very fast. Is there another "hidden" feature in openscad which evaluates the content of a variable and looks up a variable's content in the current scope ? Thank you very much for your attention and Merry Xmas! > It seems $ variables should already cover this for the most part. > > module c() { > color($c) cube(); > } > > module m(col) { > $c = col; children(); > } > > m("red") c(); > translate([2, 0, 0]) m("blue") c(); > > ciao, > Torsten. > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
MM
Michael Möller
Sat, Dec 24, 2022 11:31 AM

Arrays are lookuptables ... yes, they need a numeric index, but you can
make an include file which defines some variablenames with appropiate
constants.

So (writing on the mobile, ie no guarantees)
K_top=1 ; K_bot=2 ;
K_table=[[1,0,0],[0,1,0]] ;
yourmod(ref) { translate(K_table[ref]) ; children() ;}

yourmod(K_top) sphere(r=2) ;
yourmod(K_bot) sphere(r=1) ;

Season's Greetings
Msquare

lør. 24. dec. 2022 10.32 skrev Guenther Sohler guenther.sohler@gmail.com:

Hi Torsten,
Thank you for your immediate response.

Why? What is the actual use case?

Sorry, I forgot to mention.
I appreciate the relationship between parent and child very much and I
want to use it for my purposes.
My Plan is to  implement a relational placement between child and parent
like this:

mymod([10,9,8],name="parent")
mymod([5,4,3],name="child",ref="bot",dest="top");

whereas bot could mean the lower left corner of the cube and top could
mean the upper right corner of the cube.

I was aware of the $ variables in openscad, but I could not yet find the
documentation which makes me completely understand their functionality.
It appears i have yet kind-of-solution (they have the parent access
feature) but the code still is not very nice :

 refoff=
     (ref == "top")?$top :
     (ref == "bot")?$bot :
     [0,0,0];

I plan not only to have  two "handles" but many many and my code will
become ugly very fast.
Is there another "hidden" feature in openscad which evaluates the content
of a variable and looks up a variable's content in the current scope ?

Thank you very much for your attention and Merry Xmas!

It seems $ variables should already cover this for the most part.

module c() {
color($c) cube();
}

module m(col) {
$c = col; children();
}

m("red") c();
translate([2, 0, 0]) m("blue") c();

ciao,
Torsten.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Arrays are lookuptables ... yes, they need a numeric index, but you can make an include file which defines some variablenames with appropiate constants. So (writing on the mobile, ie no guarantees) K_top=1 ; K_bot=2 ; K_table=[[1,0,0],[0,1,0]] ; yourmod(ref) { translate(K_table[ref]) ; children() ;} yourmod(K_top) sphere(r=2) ; yourmod(K_bot) sphere(r=1) ; Season's Greetings Msquare lør. 24. dec. 2022 10.32 skrev Guenther Sohler <guenther.sohler@gmail.com>: > Hi Torsten, > Thank you for your immediate response. > >> >> > Why? What is the actual use case? >> > Sorry, I forgot to mention. > I appreciate the relationship between parent and child very much and I > want to use it for my purposes. > My Plan is to implement a relational placement between child and parent > like this: > > mymod([10,9,8],name="parent") > mymod([5,4,3],name="child",ref="bot",dest="top"); > > whereas bot could mean the lower left corner of the cube and top could > mean the upper right corner of the cube. > > I was aware of the $ variables in openscad, but I could not yet find the > documentation which makes me completely understand their functionality. > It appears i have yet kind-of-solution (they have the parent access > feature) but the code still is not very nice : > > refoff= > (ref == "top")?$top : > (ref == "bot")?$bot : > [0,0,0]; > > I plan not only to have two "handles" but many many and my code will > become ugly very fast. > Is there another "hidden" feature in openscad which evaluates the content > of a variable and looks up a variable's content in the current scope ? > > Thank you very much for your attention and Merry Xmas! > > > > > > > > >> It seems $ variables should already cover this for the most part. >> >> module c() { >> color($c) cube(); >> } >> >> module m(col) { >> $c = col; children(); >> } >> >> m("red") c(); >> translate([2, 0, 0]) m("blue") c(); >> >> ciao, >> Torsten. >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
AM
Adrian Mariano
Sat, Dec 24, 2022 1:38 PM

Before you reinvent the wheel you may want to take a look at the BOSL2
library and its attachment features, or perhaps the OpenSCAD General
Library of Relativity.  Both of these libraries implement relative
positioning, and also the ability to apply operations like difference on
relatively positioned objects.  The feature you want to create can be done
with $ variables.  A $ variable is simply a variable that is available to
all children.  If you still feel driven to write your own version of this
functionality, you may get some insight into how to do it by looking at the
code of those libraries.

https://github.com/revarbat/BOSL2/wiki/attachments.scad
https://github.com/revarbat/BOSL2/wiki/Tutorial-Attachments

General Library of Relativity:
https://github.com/davidson16807/relativity.scad

On Sat, Dec 24, 2022 at 4:32 AM Guenther Sohler guenther.sohler@gmail.com
wrote:

Hi Torsten,
Thank you for your immediate response.

Why? What is the actual use case?

Sorry, I forgot to mention.
I appreciate the relationship between parent and child very much and I
want to use it for my purposes.
My Plan is to  implement a relational placement between child and parent
like this:

mymod([10,9,8],name="parent")
mymod([5,4,3],name="child",ref="bot",dest="top");

whereas bot could mean the lower left corner of the cube and top could
mean the upper right corner of the cube.

I was aware of the $ variables in openscad, but I could not yet find the
documentation which makes me completely understand their functionality.
It appears i have yet kind-of-solution (they have the parent access
feature) but the code still is not very nice :

 refoff=
     (ref == "top")?$top :
     (ref == "bot")?$bot :
     [0,0,0];

I plan not only to have  two "handles" but many many and my code will
become ugly very fast.
Is there another "hidden" feature in openscad which evaluates the content
of a variable and looks up a variable's content in the current scope ?

Thank you very much for your attention and Merry Xmas!

It seems $ variables should already cover this for the most part.

module c() {
color($c) cube();
}

module m(col) {
$c = col; children();
}

m("red") c();
translate([2, 0, 0]) m("blue") c();

ciao,
Torsten.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Before you reinvent the wheel you may want to take a look at the BOSL2 library and its attachment features, or perhaps the OpenSCAD General Library of Relativity. Both of these libraries implement relative positioning, and also the ability to apply operations like difference on relatively positioned objects. The feature you want to create can be done with $ variables. A $ variable is simply a variable that is available to all children. If you still feel driven to write your own version of this functionality, you may get some insight into how to do it by looking at the code of those libraries. https://github.com/revarbat/BOSL2/wiki/attachments.scad https://github.com/revarbat/BOSL2/wiki/Tutorial-Attachments General Library of Relativity: https://github.com/davidson16807/relativity.scad On Sat, Dec 24, 2022 at 4:32 AM Guenther Sohler <guenther.sohler@gmail.com> wrote: > Hi Torsten, > Thank you for your immediate response. > >> >> > Why? What is the actual use case? >> > Sorry, I forgot to mention. > I appreciate the relationship between parent and child very much and I > want to use it for my purposes. > My Plan is to implement a relational placement between child and parent > like this: > > mymod([10,9,8],name="parent") > mymod([5,4,3],name="child",ref="bot",dest="top"); > > whereas bot could mean the lower left corner of the cube and top could > mean the upper right corner of the cube. > > I was aware of the $ variables in openscad, but I could not yet find the > documentation which makes me completely understand their functionality. > It appears i have yet kind-of-solution (they have the parent access > feature) but the code still is not very nice : > > refoff= > (ref == "top")?$top : > (ref == "bot")?$bot : > [0,0,0]; > > I plan not only to have two "handles" but many many and my code will > become ugly very fast. > Is there another "hidden" feature in openscad which evaluates the content > of a variable and looks up a variable's content in the current scope ? > > Thank you very much for your attention and Merry Xmas! > > > > > > > > >> It seems $ variables should already cover this for the most part. >> >> module c() { >> color($c) cube(); >> } >> >> module m(col) { >> $c = col; children(); >> } >> >> m("red") c(); >> translate([2, 0, 0]) m("blue") c(); >> >> ciao, >> Torsten. >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >