Why doesn't this use statement work?

classic Classic list List threaded Threaded
21 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Why doesn't this use statement work?

jamcultur
As I mentioned in another thread, I have a long complicated piece of OpenSCAD
code that I am trying to
simplify so that I can enhance it, but I haven't found a way that works yet.
My latest attempt to modularize the code failed, and I don't understand why.
Here's a simplified example with three .scad files.

part1.scad___________

module part1() {
    echo("part1");
    part2();
}
____________________


part2.scad___________

module part2() {
    echo("part2");
}
____________________


part_test.scad_________

use <part1.scad>
use <part2.scad>

part1();
____________________

When I try to preview or render part_test.scad, I get the message, "WARNING:
Ignoring unknown module 'part2', even though there is a  "use <part2.scad>"
statement in the file, and part2.scad is in the same directory. If I move
the "use <part2.scad>" statement from part_test.scad, to part1.scad, it
works, but that structure would not allow me to modularize my code in a
useful way. Is there a way to make this work?





--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Why doesn't this use statement work?

Ronaldo
part2() is defined only after the compilation of part1.scad. Reverse the use's order in part_test.scad. 

Em ter, 27 de mar de 2018 21:46, jamcultur <[hidden email]> escreveu:
As I mentioned in another thread, I have a long complicated piece of OpenSCAD
code that I am trying to
simplify so that I can enhance it, but I haven't found a way that works yet.
My latest attempt to modularize the code failed, and I don't understand why.
Here's a simplified example with three .scad files.

part1.scad___________

module part1() {
    echo("part1");
    part2();
}
____________________


part2.scad___________

module part2() {
    echo("part2");
}
____________________


part_test.scad_________

use <part1.scad>
use <part2.scad>

part1();
____________________

When I try to preview or render part_test.scad, I get the message, "WARNING:
Ignoring unknown module 'part2', even though there is a  "use <part2.scad>"
statement in the file, and part2.scad is in the same directory. If I move
the "use <part2.scad>" statement from part_test.scad, to part1.scad, it
works, but that structure would not allow me to modularize my code in a
useful way. Is there a way to make this work?





--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Why doesn't this use statement work?

nophead
I don't think that will work.

part1.scad uses part2, so it must use part2.scad.

part_test.scad only needs to use part1.scad.

On 27 March 2018 at 21:59, Ronaldo Persiano <[hidden email]> wrote:
part2() is defined only after the compilation of part1.scad. Reverse the use's order in part_test.scad. 

Em ter, 27 de mar de 2018 21:46, jamcultur <[hidden email]> escreveu:
As I mentioned in another thread, I have a long complicated piece of OpenSCAD
code that I am trying to
simplify so that I can enhance it, but I haven't found a way that works yet.
My latest attempt to modularize the code failed, and I don't understand why.
Here's a simplified example with three .scad files.

part1.scad___________

module part1() {
    echo("part1");
    part2();
}
____________________


part2.scad___________

module part2() {
    echo("part2");
}
____________________


part_test.scad_________

use <part1.scad>
use <part2.scad>

part1();
____________________

When I try to preview or render part_test.scad, I get the message, "WARNING:
Ignoring unknown module 'part2', even though there is a  "use <part2.scad>"
statement in the file, and part2.scad is in the same directory. If I move
the "use <part2.scad>" statement from part_test.scad, to part1.scad, it
works, but that structure would not allow me to modularize my code in a
useful way. Is there a way to make this work?





--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org



_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Why doesn't this use statement work?

jamcultur
In reply to this post by Ronaldo
Ronaldo wrote
> part2() is defined only after the compilation of part1.scad. Reverse the
> use's order in part_test.scad.

I tried that, and it didn't work. I still get the message WARNING: Ignoring
unknown module 'part2'.



--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Why doesn't this use statement work?

boxcarmib
In reply to this post by jamcultur
I think your error message is not from “part_test.scad” but from part1.scad which can’t find part2.
If you add

use <part2.scad> to part.scad like this:

part1.scad _________

use <part2.scad>

module part1() {
        echo(“part1”);
        part2();
}

It should work fine.

> On Mar 27, 2018, at 1:46 PM, jamcultur <[hidden email]> wrote:
>
> As I mentioned in another thread, I have a long complicated piece of OpenSCAD
> code that I am trying to
> simplify so that I can enhance it, but I haven't found a way that works yet.
> My latest attempt to modularize the code failed, and I don't understand why.
> Here's a simplified example with three .scad files.
>
> part1.scad___________
>
> module part1() {
>    echo("part1");
>    part2();
> }
> ____________________
>
>
> part2.scad___________
>
> module part2() {
>    echo("part2");
> }
> ____________________
>
>
> part_test.scad_________
>
> use <part1.scad>
> use <part2.scad>
>
> part1();
> ____________________
>
> When I try to preview or render part_test.scad, I get the message, "WARNING:
> Ignoring unknown module 'part2', even though there is a  "use <part2.scad>"
> statement in the file, and part2.scad is in the same directory. If I move
> the "use <part2.scad>" statement from part_test.scad, to part1.scad, it
> works, but that structure would not allow me to modularize my code in a
> useful way. Is there a way to make this work?
>
>
>
>
>
> --
> Sent from: http://forum.openscad.org/
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Why doesn't this use statement work?

boxcarmib
In reply to this post by jamcultur
sorry… duh… I see thats you know that.


> On Mar 27, 2018, at 2:13 PM, jamcultur <[hidden email]> wrote:
>
> Ronaldo wrote
>> part2() is defined only after the compilation of part1.scad. Reverse the
>> use's order in part_test.scad.
>
> I tried that, and it didn't work. I still get the message WARNING: Ignoring
> unknown module 'part2'.
>
>
>
> --
> Sent from: http://forum.openscad.org/
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Why doesn't this use statement work?

JordanBrown
In reply to this post by jamcultur
On 3/27/2018 1:46 PM, jamcultur wrote:
If I move the "use <part2.scad>" statement from part_test.scad, to part1.scad, it works, but that structure would not allow me to modularize my code in a useful way.

Actually, it's *requiring* you to modularize your program.

The main program invokes only part1.  It doesn't know about part2.  It is only part1 that knows that it needs part2, so it is the one that needs the "use".


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Why doesn't this use statement work?

jamcultur
JordanBrown wrote

> On 3/27/2018 1:46 PM, jamcultur wrote:
>> If I move the "use
> <part2.scad>
> " statement from part_test.scad, to part1.scad, it works, but that
> structure would not allow me to modularize my code in a useful way.
>
> Actually, it's *requiring* you to modularize your program.
>
> The main program invokes only part1.  It doesn't know about part2.  It
> is only part1 that knows that it needs part2, so it is the one that
> needs the "use".

I need part1 and part2 to be independent modules without any information
about each other. That is so that I can code different versions of part_test
that use different versions of part1 and part2. I want any version of part1
to work with any version of part2. That won't work if put the use for a
particular version of part2 in part1.



--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Why doesn't this use statement work?

doug.moen
Then I guess you need `include` instead of `use`.

On 27 March 2018 at 19:35, jamcultur <[hidden email]> wrote:
JordanBrown wrote
> On 3/27/2018 1:46 PM, jamcultur wrote:
>> If I move the "use
> <part2.scad>
> " statement from part_test.scad, to part1.scad, it works, but that
> structure would not allow me to modularize my code in a useful way.
>
> Actually, it's *requiring* you to modularize your program.
>
> The main program invokes only part1.  It doesn't know about part2.  It
> is only part1 that knows that it needs part2, so it is the one that
> needs the "use".

I need part1 and part2 to be independent modules without any information
about each other. That is so that I can code different versions of part_test
that use different versions of part1 and part2. I want any version of part1
to work with any version of part2. That won't work if put the use for a
particular version of part2 in part1.



--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Why doesn't this use statement work?

nophead
Or pass part2 to part1 as a child.

On 28 March 2018 at 01:10, doug moen <[hidden email]> wrote:
Then I guess you need `include` instead of `use`.

On 27 March 2018 at 19:35, jamcultur <[hidden email]> wrote:
JordanBrown wrote
> On 3/27/2018 1:46 PM, jamcultur wrote:
>> If I move the "use
> <part2.scad>
> " statement from part_test.scad, to part1.scad, it works, but that
> structure would not allow me to modularize my code in a useful way.
>
> Actually, it's *requiring* you to modularize your program.
>
> The main program invokes only part1.  It doesn't know about part2.  It
> is only part1 that knows that it needs part2, so it is the one that
> needs the "use".

I need part1 and part2 to be independent modules without any information
about each other. That is so that I can code different versions of part_test
that use different versions of part1 and part2. I want any version of part1
to work with any version of part2. That won't work if put the use for a
particular version of part2 in part1.



--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org



_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Why doesn't this use statement work?

jamcultur
In reply to this post by doug.moen
doug.moen wrote
> Then I guess you need `include` instead of `use`.

Include doesn't work for the same reason as use. It needs to be placed in
part1.scad instead of part_test.scad.




--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Why doesn't this use statement work?

jamcultur
In reply to this post by nophead
nophead wrote
> Or pass part2 to part1 as a child.

Could you explain how to pass part2 to part1 as a child? I've already tried
to use children() to do what I want to do, and it didn't work, as I
described in this post:
children() doesn't work with objects generated in a for loop
<http://forum.openscad.org/How-to-apply-changes-to-all-shapes-generated-in-a-for-loop-td23685.html>  



--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Why doesn't this use statement work?

doug.moen
In reply to this post by jamcultur
I tested the following and it works for me.

part1.scad___________


module part1() {
    echo("part1");
    part2();
}
____________________


part2.scad___________

module part2() {
    echo("part2");
}
____________________


part_test.scad_________

include <part1.scad>
include <part2.scad>

part1();
____________________


On 28 March 2018 at 12:09, jamcultur <[hidden email]> wrote:
doug.moen wrote
> Then I guess you need `include` instead of `use`.

Include doesn't work for the same reason as use. It needs to be placed in
part1.scad instead of part_test.scad.




--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Why doesn't this use statement work?

nophead
>children() doesn't work with objects generated in a for loop

Well yes and no. A for loop produces a union of all its iterations, so only produces one child. I think modules do the same. Nothing can return multiple children, you can only pass them explicitly as a list.

But for this case it would work to decouple part1 and part 2.

part1.scad___________

module part1() {
    echo("part1");
    children();
}
____________________


part2.scad___________

module part2() {
    echo("part2");
}
____________________


part_test.scad_________

use <part1.scad>
use <part2.scad>

part1() part2();

On 28 March 2018 at 17:26, doug moen <[hidden email]> wrote:
I tested the following and it works for me.

part1.scad___________


module part1() {
    echo("part1");
    part2();
}
____________________


part2.scad___________

module part2() {
    echo("part2");
}
____________________


part_test.scad_________

include <part1.scad>
include <part2.scad>

part1();
____________________


On 28 March 2018 at 12:09, jamcultur <[hidden email]> wrote:
doug.moen wrote
> Then I guess you need `include` instead of `use`.

Include doesn't work for the same reason as use. It needs to be placed in
part1.scad instead of part_test.scad.




--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org



_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Why doesn't this use statement work?

jamcultur
In reply to this post by doug.moen
doug.moen wrote

> I tested the following and it works for me.
>
> part_test.scad_________
>
> include
> <part1.scad>
> include
> <part2.scad>
> part1();
> ____________________

It didn't work when I first tested it because I had
use <part1.scad>
include <part2.scad>

I did that because I had some code in part1.scad that I didn't want to be
executed when the file was included. When I took that code out and included
both files, it worked. I think this might be the best solution for me and I
will continue testing it. Thank you!



--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Why doesn't this use statement work?

jamcultur
In reply to this post by nophead
nophead wrote

>>children() doesn't work with objects generated in a for loop
>
> Well yes and no. A for loop produces a union of all its iterations, so
> only
> produces one child. I think modules do the same. Nothing can return
> multiple children, you can only pass them explicitly as a list.
>
> But for this case it would work to decouple part1 and part 2.
>
> part1.scad___________
>
> module part1() {
>     echo("part1");
>     children();
> }
> ____________________
>
>
> part2.scad___________
>
> module part2() {
>     echo("part2");
> }
> ____________________
>
>
> part_test.scad_________
>
> use
> <part1.scad>
> use
> <part2.scad>
> part1() part2();

I haven't tested this yet to see if it will work with my code that generates
objects in a loop, but I will. Thanks.




--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Why doesn't this use statement work?

nophead
I don't think you can generate objects in a loop. It is always a single object because the results are implicitly unioned. The only way I know to get multiple children is to list them explicitly. E.g.

part1() {
    part2();
    cube();
   sphere();
}

On 30 March 2018 at 16:08, jamcultur <[hidden email]> wrote:
nophead wrote
>>children() doesn't work with objects generated in a for loop
>
> Well yes and no. A for loop produces a union of all its iterations, so
> only
> produces one child. I think modules do the same. Nothing can return
> multiple children, you can only pass them explicitly as a list.
>
> But for this case it would work to decouple part1 and part 2.
>
> part1.scad___________
>
> module part1() {
>     echo("part1");
>     children();
> }
> ____________________
>
>
> part2.scad___________
>
> module part2() {
>     echo("part2");
> }
> ____________________
>
>
> part_test.scad_________
>
> use
> <part1.scad>
> use
> <part2.scad>
> part1() part2();

I haven't tested this yet to see if it will work with my code that generates
objects in a loop, but I will. Thanks.




--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Why doesn't this use statement work?

JordanBrown
On 3/30/2018 10:19 AM, nop head wrote:
I don't think you can generate objects in a loop. It is always a single object because the results are implicitly unioned. The only way I know to get multiple children is to list them explicitly. E.g.

part1() {
    part2();
    cube();
   sphere();
}

[ Just thinking out loud.  This seems like an interesting idea; I'm not pushing hard for it. ]

I wonder how reasonable it would be to have a construct that would take a CSG tree and crack it apart into individual subtrees.

That is, "explode() union() { x; y; }" would be equivalent to "x; y;".  (For operations other than "union" maybe it would be an error, or maybe the operation would be discarded.)

From a tree manipulation standpoint it seems plausible, and it seems like it might be useful, e.g. allowing a wrapper module to pass its children to an inner module as individual objects:
module inner() {
	// do something with children
	echo($children);
}

module outer() {
	// do stuff, then invoke inner on the children
	inner() explode() children();
}

outer() {
	sphere();
	cube();
}

would echo 2, not 1.

From a syntactic perspective, I'm bothered by having something that looks like a module but yields more than one object.  It seems like some kind of different syntax would be desirable.



_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Why doesn't this use statement work?

jamcultur
I tried nophead's solution in my code, and it worked. In case anyone is
interested, my code is a jewelry design program. I've made about 60 designs
with the previous version. Here is a pendant I designed with it that I cast
in sterling silver.
<http://forum.openscad.org/file/t1635/AlienArthropod3.jpg>

Although the previous version could be used to design a huge range of
geometric and fractal designs, there were things I wanted to do with it that
it couldn't do. The reorganized version is much more powerful, flexible, and
extendable. Here's a design I made to test the reorganized code. This design
would have been impossible with the previous version, but was easy with the
reorganized version.
<http://forum.openscad.org/file/t1635/1a.png>
<http://forum.openscad.org/file/t1635/1b.png>

I probably won't make this one in metal. It's not really my style. But I'm
excited about what I can now design.



--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
rew
Reply | Threaded
Open this post in threaded view
|

Re: Why doesn't this use statement work?

rew
In reply to this post by nophead
On Fri, Mar 30, 2018 at 06:19:27PM +0100, nop head wrote:
> I don't think you can generate objects in a loop. It is always a single
> object because the results are implicitly unioned.

In that case, that's a hint we should think about possibly chaiging
things in the language.

The C language was designed so that the basic functions do things that
are useful, but also that they don't do too much. For example, printf
does not automatically add a newline. writing a function to add that
is easy, building something that removes the newline is not.

So.... a list of objects is easily unioned by adding union in
front, while a unioned list is not easily un-unioned.

for (i=[0:10:50])
   translate ([i,i,0]) cube (15);

should give me the list of 6 cubes,  while
union () {
  for (i=[0:10:50])
    translate ([i,i,0]) cube (15);
  }

gives the union of those objects.

Roger.

>
> On 30 March 2018 at 16:08, jamcultur <[hidden email]> wrote:
>
> > nophead wrote
> > >>children() doesn't work with objects generated in a for loop
> > >
> > > Well yes and no. A for loop produces a union of all its iterations, so
> > > only
> > > produces one child. I think modules do the same. Nothing can return
> > > multiple children, you can only pass them explicitly as a list.
> > >
> > > But for this case it would work to decouple part1 and part 2.
> > >
> > > part1.scad___________
> > >
> > > module part1() {
> > >     echo("part1");
> > >     children();
> > > }
> > > ____________________
> > >
> > >
> > > part2.scad___________
> > >
> > > module part2() {
> > >     echo("part2");
> > > }
> > > ____________________
> > >
> > >
> > > part_test.scad_________
> > >
> > > use
> > > <part1.scad>
> > > use
> > > <part2.scad>
> > > part1() part2();
> >
> > I haven't tested this yet to see if it will work with my code that
> > generates
> > objects in a loop, but I will. Thanks.
> >
> >
> >
> >
> > --
> > Sent from: http://forum.openscad.org/
> >
> > _______________________________________________
> > OpenSCAD mailing list
> > [hidden email]
> > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
> >

> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


--
** [hidden email] ** http://www.BitWizard.nl/ ** +31-15-2600998 **
**    Delftechpark 26 2628 XH  Delft, The Netherlands. KVK: 27239233    **
*-- BitWizard writes Linux device drivers for any device you may have! --*
The plan was simple, like my brother-in-law Phil. But unlike
Phil, this plan just might work.

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
12