# Problems making a negative space Classic List Threaded 4 messages Open this post in threaded view
|

## Problems making a negative space

 Hello, I'm having issues making a negative space with difference. I'm trying to make a lattice, and have made all of what will be negative spaces, but when I try to invert them the difference function removed my whole model. I've tried offsetting the shape I'm subtracting, and that hasn't made a difference. I also tried increasing the number of facets in case it was a resolution issue, and also changing the order in which I use the difference function, but that also has made no difference.Please could someone help? Apologies for the unelegant code, I'm a newbie.  Matt  \$fn=20; R=10; n=12; C=sqrt((pow((2*PI*R)/n,2))/2); S=0.625*C; difference(){ inverse_lattice_proper();     cyl(); } module inverse_lattice_proper(){ intersection(){   cyl();     inverse_lattice(); }} module inverse_lattice(){ layer_1(); layer_2(); layer_3(); layer_4(); layer_5(); layer_6(); } module layer_6(){     scale([(R-(sqrt(pow(C,2)*2)/2))/R,(R-(sqrt(pow(C,2)*2)/2))/R,1]) layer_5(); } module layer_5(){     scale([(R-(sqrt(pow(C,2)*2)/2))/R,(R-(sqrt(pow(C,2)*2)/2))/R,1]) layer_4(); } module layer_4(){     scale([(R-(sqrt(pow(C,2)*2)/2))/R,(R-(sqrt(pow(C,2)*2)/2))/R,1]) layer_3(); } module layer_3(){     scale([(R-(sqrt(pow(C,2)*2)/2))/R,(R-(sqrt(pow(C,2)*2)/2))/R,1]) layer_2(); } module layer_2(){     scale([(R-(sqrt(pow(C,2)*2)/2))/R,(R-(sqrt(pow(C,2)*2)/2))/R,1]) layer_1(); } module lattice(){     rotate([45,0,0]){ intersection(){ cube(C, center=true); sphere(S, center=true); } } } module cyl(){ translate([0,0,10]){ cylinder (h=40,r=R,center=true); } }module layer_1(){ layer(); translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();     translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();         translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();         translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();         translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();         translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();         translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();         translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();         translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();     }     }}}}}}}}}}}}}}}}}}}}}}}}}}     module layer(){ union(){ translate([R,0,0.5*C]){lattice(); translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice(); translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice(); translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice(); }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}; _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: Problems making a negative space

 Hoo boy, there's a lot going on here.1) Your main issue looks like you have the modules swapped around in your difference operation.  You want the (cylinder minus the "inverse_lattice") right?    A - B is not the same as B - A, so the module you list first matters.2) Your "module_inverse_proper" is unnecessary.  There's no reason to get the intersection of your negative object with the same positive object that its about to be subtracted from.3) Learn to use "for" loops, instead of nesting dozens of calls, you could probably shorten your code by 10x or so.4) This is a really complex model and going to take quite a while to render.  Read this https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/FAQ#Why_is_the_preview_so_slow?You can get around the issue of exponentially exploding number of CSG elements somewhat, by wrapping your lattice with a call to render() ( https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/CSG_Modelling#render )  like so:module lattice() {  render() rotate([45, 0, 0]) intersection() {    cube(C, center = true);    sphere(S);  }}5) sphere doesn't have a "center" parameter.  Its always centered, which is why those warnings are showing.6) You have these weird patterns of expression all around your code:  sqrt(pow(X,2) * Y)  OR   sqrt(pow(X,2) / Y)sqrt(pow(X,2) * Y)  ==  ( X * sqrt(Y) ) since your X expression is always positive anyways.  So they can be simplified for better readability.- HansOn Mon, Oct 21, 2019 at 2:55 PM matt meek via Discuss <[hidden email]> wrote: Hello, I'm having issues making a negative space with difference. I'm trying to make a lattice, and have made all of what will be negative spaces, but when I try to invert them the difference function removed my whole model. I've tried offsetting the shape I'm subtracting, and that hasn't made a difference. I also tried increasing the number of facets in case it was a resolution issue, and also changing the order in which I use the difference function, but that also has made no difference.Please could someone help? Apologies for the unelegant code, I'm a newbie.  Matt  \$fn=20; R=10; n=12; C=sqrt((pow((2*PI*R)/n,2))/2); S=0.625*C; difference(){ inverse_lattice_proper();     cyl(); } module inverse_lattice_proper(){ intersection(){   cyl();     inverse_lattice(); }} module inverse_lattice(){ layer_1(); layer_2(); layer_3(); layer_4(); layer_5(); layer_6(); } module layer_6(){     scale([(R-(sqrt(pow(C,2)*2)/2))/R,(R-(sqrt(pow(C,2)*2)/2))/R,1]) layer_5(); } module layer_5(){     scale([(R-(sqrt(pow(C,2)*2)/2))/R,(R-(sqrt(pow(C,2)*2)/2))/R,1]) layer_4(); } module layer_4(){     scale([(R-(sqrt(pow(C,2)*2)/2))/R,(R-(sqrt(pow(C,2)*2)/2))/R,1]) layer_3(); } module layer_3(){     scale([(R-(sqrt(pow(C,2)*2)/2))/R,(R-(sqrt(pow(C,2)*2)/2))/R,1]) layer_2(); } module layer_2(){     scale([(R-(sqrt(pow(C,2)*2)/2))/R,(R-(sqrt(pow(C,2)*2)/2))/R,1]) layer_1(); } module lattice(){     rotate([45,0,0]){ intersection(){ cube(C, center=true); sphere(S, center=true); } } } module cyl(){ translate([0,0,10]){ cylinder (h=40,r=R,center=true); } }module layer_1(){ layer(); translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();     translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();         translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();         translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();         translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();         translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();         translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();         translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();         translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();     }     }}}}}}}}}}}}}}}}}}}}}}}}}}     module layer(){ union(){ translate([R,0,0.5*C]){lattice(); translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice(); translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice(); translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice(); }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}; _______________________________________________ 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
 In reply to this post by OpenSCAD mailing list Thanks for all the help everyone - there's a lot to learn here, so thanks for the hints re loops and render() especially. I'll definitely spend some time getting to grips with those. sqrt(pow(C,2)*2) is the length of the hypotenuse of the cube - making it a constant would definitely clean up the maths a bit, thank you.   For some reason even reversing the order of my modules in the difference operation didn't work, however I did manage to get the model working- I had to perform the difference operator on my original basic unit (module lattice), rather than on the compiled module , and that produced what I wanted. Thanks again everyone, as I say, I'm only a beginner, so really appreciate the patient advice! Matt From: Discuss <[hidden email]> on behalf of Hans L via Discuss <[hidden email]> Sent: 21 October 2019 23:08 To: OpenSCAD general discussion <[hidden email]> Cc: Hans L <[hidden email]> Subject: Re: [OpenSCAD] Problems making a negative space   Hoo boy, there's a lot going on here. 1) Your main issue looks like you have the modules swapped around in your difference operation.  You want the (cylinder minus the "inverse_lattice") right?    A - B is not the same as B - A, so the module you list first matters. 2) Your "module_inverse_proper" is unnecessary.  There's no reason to get the intersection of your negative object with the same positive object that its about to be subtracted from. 3) Learn to use "for" loops, instead of nesting dozens of calls, you could probably shorten your code by 10x or so. 4) This is a really complex model and going to take quite a while to render.  Read this https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/FAQ#Why_is_the_preview_so_slow? You can get around the issue of exponentially exploding number of CSG elements somewhat, by wrapping your lattice with a call to render() ( https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/CSG_Modelling#render )  like so: module lattice() {   render() rotate([45, 0, 0]) intersection() {     cube(C, center = true);     sphere(S);   } } 5) sphere doesn't have a "center" parameter.  Its always centered, which is why those warnings are showing. 6) You have these weird patterns of expression all around your code:  sqrt(pow(X,2) * Y)  OR   sqrt(pow(X,2) / Y) sqrt(pow(X,2) * Y)  ==  ( X * sqrt(Y) ) since your X expression is always positive anyways.  So they can be simplified for better readability. - Hans On Mon, Oct 21, 2019 at 2:55 PM matt meek via Discuss <[hidden email]> wrote: Hello, I'm having issues making a negative space with difference. I'm trying to make a lattice, and have made all of what will be negative spaces, but when I try to invert them the difference function removed my whole model. I've tried offsetting the shape I'm subtracting, and that hasn't made a difference. I also tried increasing the number of facets in case it was a resolution issue, and also changing the order in which I use the difference function, but that also has made no difference.Please could someone help? Apologies for the unelegant code, I'm a newbie.  Matt  \$fn=20; R=10; n=12; C=sqrt((pow((2*PI*R)/n,2))/2); S=0.625*C; difference(){ inverse_lattice_proper();     cyl(); } module inverse_lattice_proper(){ intersection(){   cyl();     inverse_lattice(); }} module inverse_lattice(){ layer_1(); layer_2(); layer_3(); layer_4(); layer_5(); layer_6(); } module layer_6(){     scale([(R-(sqrt(pow(C,2)*2)/2))/R,(R-(sqrt(pow(C,2)*2)/2))/R,1]) layer_5(); } module layer_5(){     scale([(R-(sqrt(pow(C,2)*2)/2))/R,(R-(sqrt(pow(C,2)*2)/2))/R,1]) layer_4(); } module layer_4(){     scale([(R-(sqrt(pow(C,2)*2)/2))/R,(R-(sqrt(pow(C,2)*2)/2))/R,1]) layer_3(); } module layer_3(){     scale([(R-(sqrt(pow(C,2)*2)/2))/R,(R-(sqrt(pow(C,2)*2)/2))/R,1]) layer_2(); } module layer_2(){     scale([(R-(sqrt(pow(C,2)*2)/2))/R,(R-(sqrt(pow(C,2)*2)/2))/R,1]) layer_1(); } module lattice(){     rotate([45,0,0]){ intersection(){ cube(C, center=true); sphere(S, center=true); } } } module cyl(){ translate([0,0,10]){ cylinder (h=40,r=R,center=true); } }module layer_1(){ layer(); translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();     translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();         translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();         translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();         translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();         translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();         translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();         translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();         translate([0,0,sqrt(pow(C,2)*2)/2]){     rotate ([0,0,180/n]){         layer();     }     }}}}}}}}}}}}}}}}}}}}}}}}}}     module layer(){ union(){ translate([R,0,0.5*C]){lattice(); translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice(); translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice(); translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice();         translate([R*cos(360/n)-R,R*sin(360/n),0]){     rotate ([0,0,360/n]){lattice(); }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}; _______________________________________________ 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