discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

intersection-or-diff.

RW
Rogier Wolff
Sat, Dec 7, 2024 10:44 AM

Hi,

I want to cut objects into two (or more) puzzle-pieces by defining the
shape of the cut (in various ways).

So in normal operation I have
difference () {
theobject ();
thecutshape ();
}

but for viewing, it is useful to view the cut-shape, but only inside
the object. So then that becomes:
intersection () {
theobject ();
thecutshape ();
}

now I want to change the module intersection for difference depending
on a variable. While writing this I thought: obviously
if (test)
intersection ()
else
difference () {
theobject ();
thecutshape ();
}

won't work.... waitaminute... So I tested it and it doesn't.

Is there a reasonably elegant way of doing this?

While writing this I've come up with

module diff_or_intersect ()
{
if (test) intersection () children ();
else      difference  () children ();
}

maybe separate out the first child, but this can probably
be made to work. Anybody know of a more elegant way of doing this?

Roger. 

--
** R.E.Wolff@BitWizard.nl ** https://www.BitWizard.nl/ ** +31-15-2049110 **
**    Delftechpark 11 2628 XJ  Delft, The Netherlands.  KVK: 27239233    **
f equals m times a. When your f is steady, and your m is going down
your a** is going up.  -- Chris Hadfield about flying up the space shuttle.
**  'a' for accelleration.

Hi, I want to cut objects into two (or more) puzzle-pieces by defining the shape of the cut (in various ways). So in normal operation I have difference () { theobject (); thecutshape (); } but for viewing, it is useful to view the cut-shape, but only inside the object. So then that becomes: intersection () { theobject (); thecutshape (); } now I want to change the module intersection for difference depending on a variable. While writing this I thought: obviously if (test) intersection () else difference () { theobject (); thecutshape (); } won't work.... waitaminute... So I tested it and it doesn't. Is there a reasonably elegant way of doing this? While writing this I've come up with module diff_or_intersect () { if (test) intersection () children (); else difference () children (); } maybe separate out the first child, but this can probably be made to work. Anybody know of a more elegant way of doing this? Roger. -- ** R.E.Wolff@BitWizard.nl ** https://www.BitWizard.nl/ ** +31-15-2049110 ** ** Delftechpark 11 2628 XJ Delft, The Netherlands. KVK: 27239233 ** f equals m times a. When your f is steady, and your m is going down your a** is going up. -- Chris Hadfield about flying up the space shuttle. ** 'a' for accelleration.
JB
Jordan Brown
Sat, Dec 7, 2024 1:21 PM

On 12/7/2024 2:44 AM, Rogier Wolff via Discuss wrote:

module diff_or_intersect ()
{
if (test) intersection () children ();
else      difference  () children ();
}

I think this will work, but don't have time to test it right now:

module diff_or_intersect ()
{
if (test) {
intersection_for (i=[0:1:$children-1])
children(i);
else {
difference  () {
children(0);
children([1:1:$children-1]);
}
}
}

On 12/7/2024 2:44 AM, Rogier Wolff via Discuss wrote: > module diff_or_intersect () > { > if (test) intersection () children (); > else difference () children (); > } I think this will work, but don't have time to test it right now: module diff_or_intersect () { if (test) { intersection_for (i=[0:1:$children-1]) children(i); else { difference () { children(0); children([1:1:$children-1]); } } }
JB
Jordan Brown
Sat, Dec 7, 2024 1:32 PM

On 12/7/2024 5:21 AM, Jordan Brown wrote:

On 12/7/2024 2:44 AM, Rogier Wolff via Discuss wrote:

module diff_or_intersect ()
{
if (test) intersection () children ();
else      difference  () children ();
}

It always happens - I remember something the moment after hitting Send.

This won't work because children() yields the union of all of the
children of the current module, and for intersection() and difference()
you need them separated.  For intersection() you need each one separate;
for difference() you only need the first separated.

On 12/7/2024 5:21 AM, Jordan Brown wrote: > On 12/7/2024 2:44 AM, Rogier Wolff via Discuss wrote: >> module diff_or_intersect () >> { >> if (test) intersection () children (); >> else difference () children (); >> } It always happens - I remember something the moment after hitting Send. This won't work because children() yields the union of all of the children of the current module, and for intersection() and difference() you need them separated.  For intersection() you need each one separate; for difference() you only need the first separated.
SP
Sanjeev Prabhakar
Sat, Dec 7, 2024 1:41 PM

This works on m computer
a=3;
if (a<2) // test
{
difference(){
sphere(20);
cylinder(h=40,r=8);}
}
else {
intersection(){
sphere(20);
cylinder(h=40,r=8);
}
}

On Sat, 7 Dec 2024 at 16:53, Rogier Wolff via Discuss <
discuss@lists.openscad.org> wrote:

Hi,

I want to cut objects into two (or more) puzzle-pieces by defining the
shape of the cut (in various ways).

So in normal operation I have
difference () {
theobject ();
thecutshape ();
}

but for viewing, it is useful to view the cut-shape, but only inside
the object. So then that becomes:
intersection () {
theobject ();
thecutshape ();
}

now I want to change the module intersection for difference depending
on a variable. While writing this I thought: obviously
if (test)
intersection ()
else
difference () {
theobject ();
thecutshape ();
}

won't work.... waitaminute... So I tested it and it doesn't.

Is there a reasonably elegant way of doing this?

While writing this I've come up with

module diff_or_intersect ()
{
if (test) intersection () children ();
else      difference  () children ();
}

maybe separate out the first child, but this can probably
be made to work. Anybody know of a more elegant way of doing this?

     Roger.

--
** R.E.Wolff@BitWizard.nl ** https://www.BitWizard.nl/ ** +31-15-2049110
**
**    Delftechpark 11 2628 XJ  Delft, The Netherlands.  KVK: 27239233    **
f equals m times a. When your f is steady, and your m is going down
your a** is going up.  -- Chris Hadfield about flying up the space shuttle.
**  'a' for accelleration.


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

This works on m computer a=3; if (a<2) // test { difference(){ sphere(20); cylinder(h=40,r=8);} } else { intersection(){ sphere(20); cylinder(h=40,r=8); } } On Sat, 7 Dec 2024 at 16:53, Rogier Wolff via Discuss < discuss@lists.openscad.org> wrote: > Hi, > > I want to cut objects into two (or more) puzzle-pieces by defining the > shape of the cut (in various ways). > > So in normal operation I have > difference () { > theobject (); > thecutshape (); > } > > but for viewing, it is useful to view the cut-shape, but only inside > the object. So then that becomes: > intersection () { > theobject (); > thecutshape (); > } > > now I want to change the module intersection for difference depending > on a variable. While writing this I thought: obviously > if (test) > intersection () > else > difference () { > theobject (); > thecutshape (); > } > > won't work.... waitaminute... So I tested it and it doesn't. > > Is there a reasonably elegant way of doing this? > > While writing this I've come up with > > module diff_or_intersect () > { > if (test) intersection () children (); > else difference () children (); > } > > maybe separate out the first child, but this can probably > be made to work. Anybody know of a more elegant way of doing this? > > > Roger. > > -- > ** R.E.Wolff@BitWizard.nl ** https://www.BitWizard.nl/ ** +31-15-2049110 > ** > ** Delftechpark 11 2628 XJ Delft, The Netherlands. KVK: 27239233 ** > f equals m times a. When your f is steady, and your m is going down > your a** is going up. -- Chris Hadfield about flying up the space shuttle. > ** 'a' for accelleration. > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >