introspection

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

introspection

DrRob
Hello,

Please excuse a "newbie" question (I only started using OpenSCAD today):

Is there no way for a module to query the dimensions (or other attributes) of its children?  For example, I'd like to create a module that distributes a set of child components along a line with a fixed gap between them, but it can only do that if it knows how big each child is.

Thanks,
Rob.
Reply | Threaded
Open this post in threaded view
|

Re: introspection

iamwil
As far as I can tell, no, there isn't.

I've wanted this feature as well, especially when I'm trying to write modules that takes the hassle out of calculating everything, such as a connectTo() or flush(). However, sometimes, you find that you don't need to with something like union()--it automatically connects the two objects and calculates where the seam is for you. But sometimes, you'd like something like:

stack() {
  cube(10);
  cube(12);
}

which as far as I can tell, it's hard to write stack to support this sort of thing, because you can't query a primitive on its dimensions--you need to keep them all in global variables (or at least variables in local scope).

Wil

On Sat, May 19, 2012 at 8:55 AM, DrRob <[hidden email]> wrote:
Hello,

Please excuse a "newbie" question (I only started using OpenSCAD today):

Is there no way for a module to query the dimensions (or other attributes) of its children?  For example, I'd like to create a module that distributes a set of child components along a line with a fixed gap between them, but it can only do that if it knows how big each child is.

Thanks,
Rob.

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad




--
Wil Chung
Blog: http://iamwil.posterous.com/
Cubehero: Work with others on 3D printable projects
https://cubehero.com
Reply | Threaded
Open this post in threaded view
|

Re: introspection

Christopher Olah
> which as far as I can tell, it's hard to write stack to support this sort of
> thing, because you can't query a primitive on its dimensions--you need to
> keep them all in global variables (or at least variables in local scope).

You guys could probably implement this in the same manner I do for
ImplicitCAD. I provide a getBox{2,3} functions on the Haskell side
that return bounding boxes. This is trivial to do without help from an
outside library: have primitives return the obvious values (eg.
getBox2 (circle 3) = [3,3]), union's take the biggest box fitting the
box of their children, etc. Example implementation:
https://github.com/colah/ImplicitCAD/blob/master/Graphics/Implicit/ObjectUtil/GetBox2.hs

In any case, the point I'm trying to make is that you shouldn't feel
held back if CGAL, OpenCSG, etc, won't provide this functionality for
you (though I'd be surprised if that was the case).

And this is a super useful feature to have. For example, it allows
ImplicitCAD to support things like:

pack([30,30], sep=5) {
   for (n=[0,1,2]) cube(10);
   sphere(5);
}

Which automatically packs a bunch of objects into a rectangle. Really
useful for automatically preparing print platters.

Chris

Reply | Threaded
Open this post in threaded view
|

Re: introspection

nophead
I can't see how this would work in openscad because there is no syntax for mixing modules and expressions. They are entirely different.

On 20 May 2012 18:05, Christopher Olah <[hidden email]> wrote:
> which as far as I can tell, it's hard to write stack to support this sort of
> thing, because you can't query a primitive on its dimensions--you need to
> keep them all in global variables (or at least variables in local scope).

You guys could probably implement this in the same manner I do for
ImplicitCAD. I provide a getBox{2,3} functions on the Haskell side
that return bounding boxes. This is trivial to do without help from an
outside library: have primitives return the obvious values (eg.
getBox2 (circle 3) = [3,3]), union's take the biggest box fitting the
box of their children, etc. Example implementation:
https://github.com/colah/ImplicitCAD/blob/master/Graphics/Implicit/ObjectUtil/GetBox2.hs

In any case, the point I'm trying to make is that you shouldn't feel
held back if CGAL, OpenCSG, etc, won't provide this functionality for
you (though I'd be surprised if that was the case).

And this is a super useful feature to have. For example, it allows
ImplicitCAD to support things like:

pack([30,30], sep=5) {
  for (n=[0,1,2]) cube(10);
  sphere(5);
}

Which automatically packs a bunch of objects into a rectangle. Really
useful for automatically preparing print platters.

Chris
_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad

Reply | Threaded
Open this post in threaded view
|

Re: introspection

Christopher Olah
> I can't see how this would work in openscad because there is no syntax for
> mixing modules and expressions. They are entirely different.

This is true. You could still provide primitive modules that use this
sort of functionality, like my pack() module in ImplicitCAD.

You could also work it into the module declaration stuff. Presently,
when you declare a module you have $children (the number of children)
and child (a module that, given n, gives the nth child). You could
also provide a function childBox(n) which gives the box of the nth
child. In fact, I think I'll do that with ImplicitCAD.

Alternatively, one could imagine a number of hacks. One is to have a
module getBox such that "getBox(a) foo" sets the variable a to be the
bounding box of foo. This would be very ugly, and I'm not sure how it
would play with the variable limitations of OpenSCAD, but it may be
possible.

I haven't hacked on OpenSCAD for over a year now, so I may be
forgetting obvious reasons why these aren't possible. But I thought
I'd throw in my 2 cents.

Chris

Reply | Threaded
Open this post in threaded view
|

Re: introspection

clothbot
In reply to this post by nophead
Before the Value refactoring I'd experimented with adding a PolySet Value type to import STL and DXF and image formats (via ImageMagic++) into. I hadn't quite figured out how to get render() to write into a Value, but that would be the most likely mechanism to get module data into expressions.

Once things get back to normal at work I'll look into reimplementing PolySet Values if someone doesn't beat me to it. ;)

Andrew.

Sent from my iPhone

On 2012-05-20, at 1:10 PM, nop head <[hidden email]> wrote:

I can't see how this would work in openscad because there is no syntax for mixing modules and expressions. They are entirely different.

On 20 May 2012 18:05, Christopher Olah <[hidden email]> wrote:
> which as far as I can tell, it's hard to write stack to support this sort of
> thing, because you can't query a primitive on its dimensions--you need to
> keep them all in global variables (or at least variables in local scope).

You guys could probably implement this in the same manner I do for
ImplicitCAD. I provide a getBox{2,3} functions on the Haskell side
that return bounding boxes. This is trivial to do without help from an
outside library: have primitives return the obvious values (eg.
getBox2 (circle 3) = [3,3]), union's take the biggest box fitting the
box of their children, etc. Example implementation:
https://github.com/colah/ImplicitCAD/blob/master/Graphics/Implicit/ObjectUtil/GetBox2.hs

In any case, the point I'm trying to make is that you shouldn't feel
held back if CGAL, OpenCSG, etc, won't provide this functionality for
you (though I'd be surprised if that was the case).

And this is a super useful feature to have. For example, it allows
ImplicitCAD to support things like:

pack([30,30], sep=5) {
  for (n=[0,1,2]) cube(10);
  sphere(5);
}

Which automatically packs a bunch of objects into a rectangle. Really
useful for automatically preparing print platters.

Chris
_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad

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

Re: introspection

joe M
In reply to this post by Christopher Olah
Hello,

I just checked out ImplicitCAD being a haskell guy myself. Would love
to use it instead of FreeCAD, which I am currently trying to learn.

I submitted an issue on github about the dependency with haskell98. I
am trying to use implicit with ghc7.4.1.

btw, can Implicit export in .step format? I learnt that .step has more
details than .stl and want to use the 3d shape for milling operation
(gcode generated by heekscnc).

Any thoughts, please?

Thanks
Joe

On Sun, May 20, 2012 at 2:44 PM, Christopher Olah
<[hidden email]> wrote:

>> I can't see how this would work in openscad because there is no syntax for
>> mixing modules and expressions. They are entirely different.
>
> This is true. You could still provide primitive modules that use this
> sort of functionality, like my pack() module in ImplicitCAD.
>
> You could also work it into the module declaration stuff. Presently,
> when you declare a module you have $children (the number of children)
> and child (a module that, given n, gives the nth child). You could
> also provide a function childBox(n) which gives the box of the nth
> child. In fact, I think I'll do that with ImplicitCAD.
>
> Alternatively, one could imagine a number of hacks. One is to have a
> module getBox such that "getBox(a) foo" sets the variable a to be the
> bounding box of foo. This would be very ugly, and I'm not sure how it
> would play with the variable limitations of OpenSCAD, but it may be
> possible.
>
> I haven't hacked on OpenSCAD for over a year now, so I may be
> forgetting obvious reasons why these aren't possible. But I thought
> I'd throw in my 2 cents.
>
> Chris
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://rocklinux.net/mailman/listinfo/openscad

Reply | Threaded
Open this post in threaded view
|

Re: introspection

Christopher Olah
In reply to this post by Christopher Olah
> You could also work it into the module declaration stuff. Presently,
> when you declare a module you have $children (the number of children)
> and child (a module that, given n, gives the nth child). You could
> also provide a function childBox(n) which gives the box of the nth
> child. In fact, I think I'll do that with ImplicitCAD.

I implemented this in ImplicitCAD and I think it's a fairly nice way
to interact with the bounding boxes. Here's a trivial example:

> module foo() {
>         for (n= [0 : children-1] ) echo (childBox(n));
> }
>
> foo()  for (n = [1:4])  cube(n);

Which produces the output:

> [[0.0,0.0,0.0],[1.0,1.0,1.0]]
> [[0.0,0.0,0.0],[2.0,2.0,2.0]]
> [[0.0,0.0,0.0],[3.0,3.0,3.0]]
> [[0.0,0.0,0.0],[4.0,4.0,4.0]]
> Nothing to render

For a more interesting example:

>module stack(sep) {
>        zshift = 0;
>        union() for (n= [0 : children - 1] ) {
>                zshift = zshift - childBox(n)[0][2] + sep;
>                translate ([0,0,zshift]) child(n);
>                zshift = zshift + childBox(n)[1][2];
>        }
>}
>
>stack(4)  for (n = [1:4])  cube(10*n);

Which results in http://imgur.com/TdY8s

In any case, if people are considering whether this is a good feature
or not, ImplicitCAD may be a good sand box.

You can see the relevant commit to OpenSCAD here:
https://github.com/colah/ImplicitCAD/commit/54de31b18a59ef1fbb744f8be980257fb68d1449

Joe: Email me off list and we can talk. ImplicitCAD does not support
step files at the moment and I don't know anything about them, but I'd
be happy to look into it. Also, ImplicitCAD produces its own gcode --
presently only one device, hacklab's laser cutter, is supported, but I
want to expand that -- and may be able to produce better results
because it has more information.

All the best,

Chris