Parts cut away with difference() still remains in some situations

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

Parts cut away with difference() still remains in some situations

Troberg
Try this minimalistic code sample:

union(){
    color([0.5,0.5,0.5])
    cube([50,100,100]);
    difference(){
        cube([100,100,100]);
        translate([-1,-1,-1])
        cube([51,102,102]);
    }
}

It looks like a mess, with typical "in the same plane hatching".

I've seen this in other situations as well, so I don't think it's a bug in
my code. You can try to comment out the first cube, just to see that the cut
done by difference() is correct.

Shouldn't the cut away parts be gone?



--
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: Parts cut away with difference() still remains in some situations

nophead
You need to avoid coincident faces (even removed ones) if you want the OpenCSG preview to look OK, or use render() to calculate the geometry before preview.

union(){
    color([0.5,0.5,0.5])
        cube([50,100,100]);
   
    render() difference(){
        cube([100,100,100]);
       
        translate([-1,-1,-1])
            cube([52,102,102]);
    }
}



On 9 November 2017 at 08:51, Troberg <[hidden email]> wrote:
Try this minimalistic code sample:

union(){
    color([0.5,0.5,0.5])
    cube([50,100,100]);
    difference(){
        cube([100,100,100]);
        translate([-1,-1,-1])
        cube([51,102,102]);
    }
}

It looks like a mess, with typical "in the same plane hatching".

I've seen this in other situations as well, so I don't think it's a bug in
my code. You can try to comment out the first cube, just to see that the cut
done by difference() is correct.

Shouldn't the cut away parts be gone?



--
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: Parts cut away with difference() still remains in some situations

Troberg
Thanks! Fixed it by moving the surfaces a tiny fraction of a millimeter.

Here's the code I was trying to make, a protractor for on-screen measuring
within a model, a companion to the ruler I've posted earlier. Maybe someone
finds it useful.

module protractor(radius, innerradius){
    w=radius-innerradius;
    protractorscale(90, innerradius+w/2, innerradius);
    protractorscale(45, innerradius+w/2+w/4, innerradius+w/2);
    protractorscale(10, innerradius+w/2+w/4+w/8, innerradius+w/2+w/4);
    protractorscale(5, innerradius+w/2+w/4+w/8+w/16,
innerradius+w/2+w/4+w/8);
    protractorscale(1, radius, innerradius+w/2+w/4+w/8+w/16);

    color([0,0,0])
    translate([-innerradius,-1/2,0])
    cube([innerradius*2,1,1]);
    color([0,0,0])
    translate([-1/2,-innerradius,0])
    cube([1,innerradius*2,1]);
}

module protractorscale(step, radius, innerradius){
    difference(){
        for(n=[0:step:360]){
            union(){
                color([(n/step)%2,(n/step)%2,(n/step)%2])
                translate([0,0,step/400])
                linear_extrude(height=1+step/200)
               
polygon([[0,0],[sin(n)*radius*2,cos(n)*radius*2],[sin(n+step)*radius*2,cos(n+step)*radius*2]]);
            }
        }
        difference(){
            color([0,0,0])
            translate([0,0,-1])
            cylinder(r=radius*3,3,$fn=360);
            difference(){
                color([0,0,0])
                translate([0,0,-2])
                cylinder(r=radius,5,$fn=360);
           
                color([0,0,0])
                translate([0,0,-3])
                cylinder(r=innerradius,7,$fn=360);
            }
        }
    }
}

//Test
protractor(200,150);



--
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: Parts cut away with difference() still remains in some situations

nophead
Nice but very slow to pan and zoom on my system due to the number of objects.

To make it smooth I would put all the put some renders() in at the top level and then apply colour. That will be slow once to calculate but it is then cached and drawn quickly. That is how I make big assemblies like 3D printers with hundreds of parts.

On 9 November 2017 at 10:03, Troberg <[hidden email]> wrote:
Thanks! Fixed it by moving the surfaces a tiny fraction of a millimeter.

Here's the code I was trying to make, a protractor for on-screen measuring
within a model, a companion to the ruler I've posted earlier. Maybe someone
finds it useful.

module protractor(radius, innerradius){
    w=radius-innerradius;
    protractorscale(90, innerradius+w/2, innerradius);
    protractorscale(45, innerradius+w/2+w/4, innerradius+w/2);
    protractorscale(10, innerradius+w/2+w/4+w/8, innerradius+w/2+w/4);
    protractorscale(5, innerradius+w/2+w/4+w/8+w/16,
innerradius+w/2+w/4+w/8);
    protractorscale(1, radius, innerradius+w/2+w/4+w/8+w/16);

    color([0,0,0])
    translate([-innerradius,-1/2,0])
    cube([innerradius*2,1,1]);
    color([0,0,0])
    translate([-1/2,-innerradius,0])
    cube([1,innerradius*2,1]);
}

module protractorscale(step, radius, innerradius){
    difference(){
        for(n=[0:step:360]){
            union(){
                color([(n/step)%2,(n/step)%2,(n/step)%2])
                translate([0,0,step/400])
                linear_extrude(height=1+step/200)

polygon([[0,0],[sin(n)*radius*2,cos(n)*radius*2],[sin(n+step)*radius*2,cos(n+step)*radius*2]]);
            }
        }
        difference(){
            color([0,0,0])
            translate([0,0,-1])
            cylinder(r=radius*3,3,$fn=360);
            difference(){
                color([0,0,0])
                translate([0,0,-2])
                cylinder(r=radius,5,$fn=360);

                color([0,0,0])
                translate([0,0,-3])
                cylinder(r=innerradius,7,$fn=360);
            }
        }
    }
}

//Test
protractor(200,150);



--
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: Parts cut away with difference() still remains in some situations

Ronaldo
As another nice touch, avoid the proctator to be rendered by an accidentally hit F6. Just insert a % between the color and render() suggested by nophead. That is what I do in my ruler code. 

Em 9 de nov de 2017 08:22, "nop head" <[hidden email]> escreveu:
Nice but very slow to pan and zoom on my system due to the number of objects.

To make it smooth I would put all the put some renders() in at the top level and then apply colour. That will be slow once to calculate but it is then cached and drawn quickly. That is how I make big assemblies like 3D printers with hundreds of parts.

On 9 November 2017 at 10:03, Troberg <[hidden email]> wrote:
Thanks! Fixed it by moving the surfaces a tiny fraction of a millimeter.

Here's the code I was trying to make, a protractor for on-screen measuring
within a model, a companion to the ruler I've posted earlier. Maybe someone
finds it useful.

module protractor(radius, innerradius){
    w=radius-innerradius;
    protractorscale(90, innerradius+w/2, innerradius);
    protractorscale(45, innerradius+w/2+w/4, innerradius+w/2);
    protractorscale(10, innerradius+w/2+w/4+w/8, innerradius+w/2+w/4);
    protractorscale(5, innerradius+w/2+w/4+w/8+w/16,
innerradius+w/2+w/4+w/8);
    protractorscale(1, radius, innerradius+w/2+w/4+w/8+w/16);

    color([0,0,0])
    translate([-innerradius,-1/2,0])
    cube([innerradius*2,1,1]);
    color([0,0,0])
    translate([-1/2,-innerradius,0])
    cube([1,innerradius*2,1]);
}

module protractorscale(step, radius, innerradius){
    difference(){
        for(n=[0:step:360]){
            union(){
                color([(n/step)%2,(n/step)%2,(n/step)%2])
                translate([0,0,step/400])
                linear_extrude(height=1+step/200)

polygon([[0,0],[sin(n)*radius*2,cos(n)*radius*2],[sin(n+step)*radius*2,cos(n+step)*radius*2]]);
            }
        }
        difference(){
            color([0,0,0])
            translate([0,0,-1])
            cylinder(r=radius*3,3,$fn=360);
            difference(){
                color([0,0,0])
                translate([0,0,-2])
                cylinder(r=radius,5,$fn=360);

                color([0,0,0])
                translate([0,0,-3])
                cylinder(r=innerradius,7,$fn=360);
            }
        }
    }
}

//Test
protractor(200,150);



--
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: Parts cut away with difference() still remains in some situations

Troberg
In reply to this post by nophead
That would mean that I would first have to make all the white objects, then
all the black. Too much work for an occasional measurement...



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

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