# [OpenSCAD] Problems making a negative space

matt meek elephant100 at hotmail.co.uk
Tue Oct 22 04:48:20 EDT 2019

```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
[cid:beff9dab-0635-4539-b8cf-5e0222044647]

________________________________
From: Discuss <discuss-bounces at lists.openscad.org> on behalf of Hans L via Discuss <discuss at lists.openscad.org>
Sent: 21 October 2019 23:08
Cc: Hans L <thehans at gmail.com>
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 <discuss at lists.openscad.org<mailto:discuss at lists.openscad.org>> 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
[cid:16df07d76c76374261b1][cid:16df07d76c76528feba2]

\$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();
}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}};

_______________________________________________
-------------- next part --------------
An HTML attachment was scrubbed...
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 2019-10-21 (7).png
Type: image/png
Size: 93893 bytes
Desc: 2019-10-21 (7).png
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 2019-10-21 (8).png
Type: image/png
Size: 35987 bytes
Desc: 2019-10-21 (8).png