Hi,

Guest

TO

Trevor Orr

Tue, May 14, 2024 6:20 PM

Just wondering if there is a better way of doing something like this? It would be nice to not have to replicate the code. It works but for maintainability it would be nice to be able to condense the code.

if (position == "raised") { union() { difference() { module_a(); module_b(); }

module_c(); }} else if (position == "lowered") { difference() { difference() { module_a(); module_b();

}

module_c(); }}

Just wondering if there is a better way of doing something like this? It would be nice to not have to replicate the code. It works but for maintainability it would be nice to be able to condense the code.
if (position == "raised") { union() { difference() { module_a(); module_b(); }
module_c(); }} else if (position == "lowered") { difference() { difference() { module_a(); module_b();
}
module_c(); }}

JB

Jordan Brown

Wed, May 15, 2024 3:49 AM

On 5/14/2024 11:20 AM, Trevor Orr via Discuss wrote:

Just wondering if there is a better way of doing something like this?

It would be nice to not have to replicate the code. It works but for

maintainability it would be nice to be able to condense the code.

if (position == "raised") {

union() {

difference() {

module_a();

module_b();

}

module_c();

}

} else if (position == "lowered") {

difference() {

difference() {

module_a();

module_b();

}

module_c();

}

}

An obvious answer is to factor out the A-B difference:

```
module ab() {
difference() {
module_a();
module_b();
}
}
if (position == "raised") {
// Note implied union here.
module_ab();
module_c();
} else if (position == "lowered") {
difference() {
module_ab();
module_c();
}
}
```

A less obvious answer is to factor out the union-or-difference decision:

```
module difference_or_union() {
if (position == "raised") {
// Note implied union here.
children();
} else if (position == "lowered") {
difference() {
// More than two children left as an
// exercise for the reader.
children(0);
children(1);
}
}
}
difference_or_union() {
difference() {
module_a();
module_b();
}
module_c();
}
```

On 5/14/2024 11:20 AM, Trevor Orr via Discuss wrote:
> Just wondering if there is a better way of doing something like this?
> It would be nice to not have to replicate the code. It works but for
> maintainability it would be nice to be able to condense the code.
>
>
> if (position == "raised") {
> union() {
> difference() {
> module_a();
> module_b();
> }
>
> module_c();
> }
> } else if (position == "lowered") {
> difference() {
> difference() {
> module_a();
> module_b();
> }
>
> module_c();
> }
> }
>
An obvious answer is to factor out the A-B difference:
module ab() {
difference() {
module_a();
module_b();
}
}
if (position == "raised") {
// Note implied union here.
module_ab();
module_c();
} else if (position == "lowered") {
difference() {
module_ab();
module_c();
}
}
A less obvious answer is to factor out the union-or-difference decision:
module difference_or_union() {
if (position == "raised") {
// Note implied union here.
children();
} else if (position == "lowered") {
difference() {
// More than two children left as an
// exercise for the reader.
children(0);
children(1);
}
}
}
difference_or_union() {
difference() {
module_a();
module_b();
}
module_c();
}

DP

David Phillip Oster

Wed, May 15, 2024 4:47 AM

Thanks! I think this should work:

difference(){

// More than two children left as an

// exercise for the reader.

children(0);

for (i = [1:$children-1])children(i);

}

Thanks! I think this should work:
difference(){
// More than two children left as an
// exercise for the reader.
children(0);
for (i = [1:$children-1])children(i);
}
>

JB

Jordan Brown

Wed, May 15, 2024 4:59 AM

On 5/14/2024 9:47 PM, David Phillip Oster via Discuss wrote:

Thanks! I think this should work:

difference(){

// More than two children left as an

// exercise for the reader.

children(0);

for (i = [1:$children-1]) children(i);

}

Yes, it should.

On 5/14/2024 9:47 PM, David Phillip Oster via Discuss wrote:
> Thanks! I think this should work:
>
> difference(){
> // More than two children left as an
> // exercise for the reader.
> children(0);
> for (i = [1:$children-1]) children(i);
> }
Yes, it should.

JB

Jordan Brown

Wed, May 15, 2024 5:05 AM

On 5/14/2024 9:59 PM, Jordan Brown via Discuss wrote:

On 5/14/2024 9:47 PM, David Phillip Oster via Discuss wrote:

Thanks! I think this should work:

difference(){

// More than two children left as an

// exercise for the reader.

children(0);

for (i = [1:$children-1]) children(i);

}

Yes, it should.

But note that the equivalent-looking

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

will *not* work, because the "for" loop generates a single child that is

the union of all of its iterations, and difference() needs to see the

"positive" part as a separate child. It's OK to union all of the

"negative" parts together because A-B-C-D is equivalent to A-(B+C+D).

On 5/14/2024 9:59 PM, Jordan Brown via Discuss wrote:
> On 5/14/2024 9:47 PM, David Phillip Oster via Discuss wrote:
>> Thanks! I think this should work:
>>
>> difference(){
>> // More than two children left as an
>> // exercise for the reader.
>> children(0);
>> for (i = [1:$children-1]) children(i);
>> }
>
> Yes, it should.
But note that the equivalent-looking
difference(){
for (i = [0:$children-1]) children(i);
}
will *not* work, because the "for" loop generates a single child that is
the union of all of its iterations, and difference() needs to see the
"positive" part as a separate child. It's OK to union all of the
"negative" parts together because A-B-C-D is equivalent to A-(B+C+D).

TO

Trevor Orr

Wed, May 15, 2024 3:56 PM

Thanks for the tips, I will play around with it. I do that same thing in several places in my code handling different parameters that call different functions so I will see come up with.

On Tuesday, May 14, 2024 at 10:06:22 PM PDT, Jordan Brown via Discuss discuss@lists.openscad.org wrote:

On 5/14/2024 9:59 PM, Jordan Brown via Discuss wrote:

On 5/14/2024 9:47 PM, David Phillip Oster via Discuss wrote:

Thanks! I think this should work:

difference(){

// More than two children left as an

// exercise for the reader.

children(0);

for (i = [1:$children-1]) children(i);

}

Yes, it should.

But note that the equivalent-looking

difference(){

for (i = [0:$children-1]) children(i);

}

will *not* work, because the "for" loop generates a single child that is the union of all of its iterations, and difference() needs to see the "positive" part as a separate child. It's OK to union all of the "negative" parts together because A-B-C-D is equivalent to A-(B+C+D).

OpenSCAD mailing list

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

Thanks for the tips, I will play around with it. I do that same thing in several places in my code handling different parameters that call different functions so I will see come up with.
On Tuesday, May 14, 2024 at 10:06:22 PM PDT, Jordan Brown via Discuss <discuss@lists.openscad.org> wrote:
On 5/14/2024 9:59 PM, Jordan Brown via Discuss wrote:
On 5/14/2024 9:47 PM, David Phillip Oster via Discuss wrote:
Thanks! I think this should work:
difference(){
// More than two children left as an
// exercise for the reader.
children(0);
for (i = [1:$children-1]) children(i);
}
Yes, it should.
But note that the equivalent-looking
difference(){
for (i = [0:$children-1]) children(i);
}
will *not* work, because the "for" loop generates a single child that is the union of all of its iterations, and difference() needs to see the "positive" part as a separate child. It's OK to union all of the "negative" parts together because A-B-C-D is equivalent to A-(B+C+D).
_______________________________________________
OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Replying to:

Empathy v1.0
2024 ©Harmonylists.com