discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Problem with difference in 2D model

MF
mike.fraser.1945+osc@gmail.com
Mon, Dec 9, 2024 4:07 PM

The Cheat Sheet makes it clear that difference() works in the 2D world.

This model seems not to.  The Preview shows the tiny holes correctly but the Render does not, nor does a laser cut when exported as SVG.

What an I doing wrong?  Nightly Build 10.09

// Crosses

include <BOSL2/std.scad>

wd = 3 ;

bar = 6 ;

module limb(wd=3, bar=6) {

right(-wd/2) square(\[wd,bar\]) ;

}

rotate([0,0,90]) {

union() {

    difference() {

        limb() ;

        translate(\[-(bar-1),0,0\]) #circle(d=0.7, $fn=30) ;

    }

    rotate(\[0,0,90\]) limb() ;

    rotate(\[0,0,180\]) limb() ;

    rotate(\[0,0,270\]) limb(wd,2\*bar) ;

}

translate(\[1.0\*bar,1.4\*bar, 0\]) rotate(\[0,0,180\])  

    union() {

        difference() {

            limb() ;

            translate(\[-(bar-1),0,0\]) #circle(d=0.7, $fn=30) ;

        }

        rotate(\[0,0,90\]) limb() ;

        rotate(\[0,0,180\]) limb() ;

        rotate(\[0,0,270\]) limb(wd,2\*bar) ;

}

}

The Cheat Sheet makes it clear that difference() works in the 2D world. This model seems not to. The Preview shows the tiny holes correctly but the Render does not, nor does a laser cut when exported as SVG. What an I doing wrong? Nightly Build 10.09 // Crosses include <BOSL2/std.scad> wd = 3 ; bar = 6 ; module limb(wd=3, bar=6) { right(-wd/2) square(\[wd,bar\]) ; } rotate(\[0,0,90\]) { union() { difference() { limb() ; translate(\[-(bar-1),0,0\]) #circle(d=0.7, $fn=30) ; } rotate(\[0,0,90\]) limb() ; rotate(\[0,0,180\]) limb() ; rotate(\[0,0,270\]) limb(wd,2\*bar) ; } translate(\[1.0\*bar,1.4\*bar, 0\]) rotate(\[0,0,180\]) union() { difference() { limb() ; translate(\[-(bar-1),0,0\]) #circle(d=0.7, $fn=30) ; } rotate(\[0,0,90\]) limb() ; rotate(\[0,0,180\]) limb() ; rotate(\[0,0,270\]) limb(wd,2\*bar) ; } }
RW
Rogier Wolff
Mon, Dec 9, 2024 4:48 PM

On Mon, Dec 09, 2024 at 04:07:44PM +0000, mike.fraser.1945+osc--- via Discuss wrote:

The Cheat Sheet makes it clear that difference() works in the 2D world.

This model seems not to.  The Preview shows the tiny holes correctly
but the Render does not, nor does a laser cut when exported as SVG.

When you hit render on a 2D object, you'll get the object in blue with
a red "outline". You can clearly see that no hole has been defined.

On my computer, I see the shimmering when you give the # to the circle, but
then no hole when you remove it.

Thus the hole is differenced, and then filled up with something else.

difference() {
union() {
limb() ;
rotate([0,0,90]) limb() ;
rotate([0,0,180]) limb() ;
rotate([0,0,270]) limb(wd,2*bar) ;
}
translate([-(bar-1),0,0]) circle(d=0.7, $fn=30) ;
}

works. (doing the other half is left as an excercise for the reader).

(It is the second one, the rotated by 90 degrees that you want the
hole in. You might have been mistaken because of the "global"
rotate (90). )

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.

On Mon, Dec 09, 2024 at 04:07:44PM +0000, mike.fraser.1945+osc--- via Discuss wrote: > The Cheat Sheet makes it clear that difference() works in the 2D world. > This model seems not to. The Preview shows the tiny holes correctly > but the Render does not, nor does a laser cut when exported as SVG. When you hit render on a 2D object, you'll get the object in blue with a red "outline". You can clearly see that no hole has been defined. On my computer, I see the shimmering when you give the # to the circle, but then no hole when you remove it. Thus the hole is differenced, and then filled up with something else. difference() { union() { limb() ; rotate([0,0,90]) limb() ; rotate([0,0,180]) limb() ; rotate([0,0,270]) limb(wd,2*bar) ; } translate([-(bar-1),0,0]) circle(d=0.7, $fn=30) ; } works. (doing the other half is left as an excercise for the reader). (It is the second one, the rotated by 90 degrees that you want the hole in. You might have been mistaken because of the "global" rotate (90). ) 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.
DP
David Phillip Oster
Mon, Dec 9, 2024 5:12 PM

The problem is with the input file, not OpenSCAD. The file subtracts off
the hole, then immediately adds a square back in to cover the hole again.
Here, I've corrected it:

// Crosses

wd = 3 ;

bar = 6 ;

module limb(wd=3, bar=6) {

translate([-wd/2,0]) square([wd,bar]) ;

}

rotate([0,0,90]) {

difference() {

union() {

  limb() ;

  rotate([0,0,90]) limb() ;

  rotate([0,0,180]) limb() ;

  rotate([0,0,270]) limb(wd,2*bar) ;

}

translate([-(bar-1),0])circle(d=0.7, $fn=30) ;

}

translate([1.0bar,1.4bar]) rotate(180)

difference() {

  union() {

    limb() ;

    rotate(90) limb() ;

    rotate(180) limb() ;

    rotate(270) limb(wd,2*bar) ;

  }

  translate([-(bar-1),0])circle(d=0.7, $fn=30) ;

}

}

On Mon, Dec 9, 2024 at 8:08 AM mike.fraser.1945+osc--- via Discuss <
discuss@lists.openscad.org> wrote:

The Cheat Sheet makes it clear that difference() works in the 2D world.

This model seems not to. The Preview shows the tiny holes correctly but
the Render does not, nor does a laser cut when exported as SVG.

What an I doing wrong? Nightly Build 10.09

// Crosses

include <BOSL2/std.scad>

wd = 3 ;

bar = 6 ;

module limb(wd=3, bar=6) {

right(-wd/2) square([wd,bar]) ;

}

rotate([0,0,90]) {

union() {

difference() {

limb() ;

translate([-(bar-1),0,0]) #circle(d=0.7, $fn=30) ;

}

rotate([0,0,90]) limb() ;

rotate([0,0,180]) limb() ;

rotate([0,0,270]) limb(wd,2*bar) ;

}

translate([1.0bar,1.4bar, 0]) rotate([0,0,180])

union() {

difference() {

limb() ;

translate([-(bar-1),0,0]) #circle(d=0.7, $fn=30) ;

}

rotate([0,0,90]) limb() ;

rotate([0,0,180]) limb() ;

rotate([0,0,270]) limb(wd,2*bar) ;

}

}


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

The problem is with the input file, not OpenSCAD. The file subtracts off the hole, then immediately adds a square back in to cover the hole again. Here, I've corrected it: // Crosses wd = 3 ; bar = 6 ; module limb(wd=3, bar=6) { translate([-wd/2,0]) square([wd,bar]) ; } rotate([0,0,90]) { difference() { union() { limb() ; rotate([0,0,90]) limb() ; rotate([0,0,180]) limb() ; rotate([0,0,270]) limb(wd,2*bar) ; } translate([-(bar-1),0])circle(d=0.7, $fn=30) ; } translate([1.0*bar,1.4*bar]) rotate(180) difference() { union() { limb() ; rotate(90) limb() ; rotate(180) limb() ; rotate(270) limb(wd,2*bar) ; } translate([-(bar-1),0])circle(d=0.7, $fn=30) ; } } On Mon, Dec 9, 2024 at 8:08 AM mike.fraser.1945+osc--- via Discuss < discuss@lists.openscad.org> wrote: > The Cheat Sheet makes it clear that difference() works in the 2D world. > > This model seems not to. The Preview shows the tiny holes correctly but > the Render does not, nor does a laser cut when exported as SVG. > > > What an I doing wrong? Nightly Build 10.09 > > // Crosses > > include <BOSL2/std.scad> > > wd = 3 ; > > bar = 6 ; > > module limb(wd=3, bar=6) { > > right(-wd/2) square([wd,bar]) ; > > } > > rotate([0,0,90]) { > > union() { > > difference() { > > limb() ; > > translate([-(bar-1),0,0]) #circle(d=0.7, $fn=30) ; > > } > > rotate([0,0,90]) limb() ; > > rotate([0,0,180]) limb() ; > > rotate([0,0,270]) limb(wd,2*bar) ; > > } > > translate([1.0*bar,1.4*bar, 0]) rotate([0,0,180]) > > union() { > > difference() { > > limb() ; > > translate([-(bar-1),0,0]) #circle(d=0.7, $fn=30) ; > > } > > rotate([0,0,90]) limb() ; > > rotate([0,0,180]) limb() ; > > rotate([0,0,270]) limb(wd,2*bar) ; > > } > > } > > > > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
JB
Jordan Brown
Mon, Dec 9, 2024 5:17 PM

When you're asking for help, always please try to reduce your example to
its simplest form - remove things until there's nothing left to remove
without triggering the problem.

Often, in that process you will find your problem.

Here, for instance, you have two crosses, and the problem is
demonstrated on both.  Get rid of one of them, so that we have half as
much to look at.  There's also a rotate() and a couple of union()s that
are not needed.  (Note, incidentally, that it's actually somewhat rare
that you need an explicit union(), because almost all operations
implicitly union their children.)  Remove libraries if practical - here,
the only thing you're using from BOSL2 is right(), so turn that into the
corresponding translate().

That leaves us with:

wd = 3 ;
bar = 6 ;

module limb(wd=3, bar=6) {
    translate([-wd/2,0]) square([wd,bar]) ;
}

difference() {
    limb() ;
    translate([-(bar-1),0,0]) #circle(d=0.7, $fn=30) ;
}
rotate([0,0,90]) limb() ;
rotate([0,0,180]) limb() ;
rotate([0,0,270]) limb(wd,2*bar) ;

Next, just to see what's what, I started putting # in front of each call
to limb() to show where that call is (without having to do the rotates
and translates in my head).

That pretty rapidly led to the answer.  Putting a # in front of the
first call to limb(), the one in the difference(), showed:

Oops!  You're subtracting the circle from the wrong limb()!  You want to
be subtracting it from the 90° limb().

But also, now that I'm looking at it more closely, you say that it
worked for preview... but really, it didn't.  Yes, you can see a red
circle there, but that's because of the # highlight.  (And,
incidentally, Z-fights with the surface, not that there's really much to
do about that.)  If you turn off the highlight on the circle, it disappears.

Also, even highlighted, a circular hole won't quite look like that.  It
would look like so:

and if not highlighted would look like so:

So that minimal case would look like:

wd = 3 ;
bar = 6 ;

module limb(wd=3, bar=6) {
    translate([-wd/2,0]) square([wd,bar]) ;
}

difference() {
    rotate([0,0,90]) limb() ;
    translate([-(bar-1),0,0]) circle(d=0.7, $fn=30) ;
}
limb() ;
rotate([0,0,180]) limb() ;
rotate([0,0,270]) limb(wd,2*bar) ;

Since you want to create two identical crosses, you might want to take
that, wrap it in a module, and call it twice.

When you're asking for help, always please try to reduce your example to its simplest form - remove things until there's nothing left to remove without triggering the problem. Often, in that process you will find your problem. Here, for instance, you have two crosses, and the problem is demonstrated on both.  Get rid of one of them, so that we have half as much to look at.  There's also a rotate() and a couple of union()s that are not needed.  (Note, incidentally, that it's actually somewhat rare that you need an explicit union(), because almost all operations implicitly union their children.)  Remove libraries if practical - here, the only thing you're using from BOSL2 is right(), so turn that into the corresponding translate(). That leaves us with: wd = 3 ; bar = 6 ; module limb(wd=3, bar=6) { translate([-wd/2,0]) square([wd,bar]) ; } difference() { limb() ; translate([-(bar-1),0,0]) #circle(d=0.7, $fn=30) ; } rotate([0,0,90]) limb() ; rotate([0,0,180]) limb() ; rotate([0,0,270]) limb(wd,2*bar) ; Next, just to see what's what, I started putting # in front of each call to limb() to show where that call is (without having to do the rotates and translates in my head). That pretty rapidly led to the answer.  Putting a # in front of the first call to limb(), the one in the difference(), showed: Oops!  You're subtracting the circle from the wrong limb()!  You want to be subtracting it from the 90° limb(). But also, now that I'm looking at it more closely, you say that it worked for preview... but really, it didn't.  Yes, you can see a red circle there, but that's because of the # highlight.  (And, incidentally, Z-fights with the surface, not that there's really much to do about that.)  If you turn off the highlight on the circle, it disappears. Also, even highlighted, a circular hole won't quite look like that.  It would look like so: and if not highlighted would look like so: So that minimal case would look like: wd = 3 ; bar = 6 ; module limb(wd=3, bar=6) { translate([-wd/2,0]) square([wd,bar]) ; } difference() { rotate([0,0,90]) limb() ; translate([-(bar-1),0,0]) circle(d=0.7, $fn=30) ; } limb() ; rotate([0,0,180]) limb() ; rotate([0,0,270]) limb(wd,2*bar) ; Since you want to create two identical crosses, you might want to take that, wrap it in a module, and call it twice.