Is there a better way of doing it?

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

Is there a better way of doing it?

eZe
This post has NOT been accepted by the mailing list yet.
Hello,

First of all i am enjoying openScad totally i just installed it yesterday, here is my first design I just wanted to know if there is better or more effecient of doing  because i see the endless possibility with OpenScad and don't want to be making the same mistake over and over. Here is my code

length=100;
width=24;
depth=40;
thickness=2;
drv_rad=18;
drv_mnt_x=width;
drv_mnt_y=18;
drv_mnt_z=length-drv_rad-10;
eps=0.5;
plate_offset=8.5;
drv_plate=2.1;
hole=width-19.6;
groove=3;
mu=0.4;
groove_thick=1.2;
module box(){
    difference(){
        cube([width,depth,length]);
        translate([thickness,thickness,thickness])
        cube([width,depth-2*thickness,length-2*thickness]);
        }
        }
       
 module box_cut(){
     difference()
     {
       box();
       translate([width-groove,mu+groove_thick,mu]) cube([groove+eps,depth-2*(mu+groove_thick),groove_thick]);
       translate([width-groove,mu,length-mu-groove_thick]) cube([groove+eps,depth-2*(mu),groove_thick]);
       translate([width-groove,mu,mu]) cube([groove+eps,groove_thick,length-(mu+groove_thick)]);
       translate([width-groove,depth-mu-groove_thick,mu]) cube([groove+eps,groove_thick,length-(mu+groove_thick)]);  
         }
     }      
       
module box_driver(){
    difference(){
    union()
    {
        box_cut();
        translate([drv_mnt_x,drv_mnt_y,drv_mnt_z]){
            rotate(90,[1,0,0]) cylinder(r=drv_rad+thickness,h=drv_mnt_y+7);
            }
        }
        translate([drv_mnt_x,drv_mnt_y+eps,drv_mnt_z]){
            rotate(90,[1,0,0]) cylinder(r=drv_rad,h=drv_mnt_y+7+2*eps);
            }
            translate([drv_mnt_x,-7-eps,drv_mnt_z-drv_rad-thickness-eps]) cube([drv_rad+thickness+eps,drv_mnt_y+7+2*eps,2*(drv_rad+thickness+eps)]);
       
    }
}

module box_driver_full(){
    difference(){
        box_driver();
        translate([hole,plate_offset,drv_mnt_z-drv_rad-thickness-eps]) cube([width+eps,drv_plate,2*(drv_rad+thickness+eps)]);
        }
   
    }
   
box_driver_full();
   
Reply | Threaded
Open this post in threaded view
|

Re: Is there a better way of doing it?

stonysmith
My own preference is to center about the origin:

module box(){
    difference(){
        cube([width,depth,length],center=true);
        //translate([thickness,thickness,thickness])  // no longer needed
        cube([width-2*thickness,depth-2*thickness,length-2*thickness],center=true);
        }
        }
Reply | Threaded
Open this post in threaded view
|

Re: Is there a better way of doing it?

MichaelAtOz
Administrator
In reply to this post by eZe
Hi eZe,

Welcome to the forum. Your post is still flagged as "This post has NOT been accepted by the mailing list yet", so nobody gets it unless they look.
You need to subscribe to the mailing list, and respond to the registration email.

I'm being a bit more expansive than you may have wanted, but early tips are better IMO. While overkill for small examples, many of these will become essential when your code gets more complex.

Firstly, if you ever see something like this in preview:




it points to what can be a problem, coincident faces, often not a problem when rendering, but when involved in other CSG they can bite you in the arse at the worst time and lead to holes in the mesh and/or degenerate faces. So best to avoid them.

I note you use eps so you may have picked up on this. Usually this is done by subtracting eps from the start and adding 2*eps at the end, like I've shown below, but in your code there is the other way shown in your code further below.


It is very common to have a pattern like this:
    i=20;
    cube([i,i,i]);
    translate([0,0,-eps])
        cylinder(r=i/4,h=i+2*eps);

Similar issues can happen when joining two objects, always embed one just inside the other, by eps or more, never butt the faces together. This can happen haphazardly when joining curved surfaces, you'll find...

Second, format/layout. I think you will find that one statement per line and indentation will make it a lot easier to see what's what. I've rejigged your code below in the way I would do it. It allows inserting extra transformations easy and also commenting one out temporarily in testing. Also makes for shorter lines, no wrapped lines, and the preview window can be bigger.

Then, a suggestion, label your close braces '}', then you can easily see what opening operators they pair with. I use '// [a letter or two]', such as '// i' for intersection(), '// if' for if(), '// t' for translate(), f for for, etc as shown below. I also use '// -' in difference() to separate the positive from the negative (removed) parts; in more complex code it is not always apparent. Label a module() close brace with the module name.



Your code, how I do it:

length=100;
width=24;
depth=40;
thickness=2;
drv_rad=18;
drv_mnt_x=width;
drv_mnt_y=18;
drv_mnt_z=length-drv_rad-10;
eps=0.05;  // better, until you do bigger, real big models, then use 0.1
plate_offset=8.5;
drv_plate=2.1;
hole=width-19.6;
groove=3;
mu=0.4;
groove_thick=1.2;

module box(){
        difference(){
                cube([width,depth,length]);
        // -
                translate([thickness,thickness,thickness])
                        cube([width,depth-2*thickness,length-2*thickness]);
        } // d
} // box
       
module box_cut(){
        difference(){ // <-- moved { up, like you did elsewhere
                box();
        // -
                // coincident faces problem
                // use -eps, +2*eps
                *translate([width-groove,mu+groove_thick-eps,mu]) // -eps
                        cube([groove+eps,depth-2*(mu+groove_thick)+2*eps,groove_thick]); // +2*eps
                // or
                #translate([width-groove,mu,mu])
                        cube([groove+eps,depth-2*mu,groove_thick]);
                translate([width-groove,mu,length-mu-groove_thick])
                        cube([groove+eps,depth-2*(mu),groove_thick]);
                translate([width-groove,mu,mu])
                        cube([groove+eps,groove_thick,length-(mu+groove_thick)]);
                translate([width-groove,depth-mu-groove_thick,mu])
                        cube([groove+eps,groove_thick,length-(mu+groove_thick)]);  
        } // d
} // box_cut

module box_driver(){
        difference(){
                union(){ // <-- moved { up, like you did elsewhere
                        box_cut();
                        translate([drv_mnt_x,drv_mnt_y,drv_mnt_z]){ // {} not needed
                                rotate(90,[1,0,0])
                                        cylinder(r=drv_rad+thickness,h=drv_mnt_y+7);
                        } // t
                } //u
        // -
                translate([drv_mnt_x,drv_mnt_y+eps,drv_mnt_z]){ // {} not needed
                        rotate(90,[1,0,0])
                                cylinder(r=drv_rad,h=drv_mnt_y+7+2*eps);
                } // t
                translate([drv_mnt_x,-7-eps,drv_mnt_z-drv_rad-thickness-eps])
                        cube([drv_rad+thickness+eps,drv_mnt_y+7+2*eps,2*(drv_rad+thickness+eps)]);
        } // d
} // box_driver

module box_driver_full(){
        difference(){
                box_driver();
        // -
                translate([hole,plate_offset,drv_mnt_z-drv_rad-thickness-eps])
                        cube([width+eps,drv_plate,2*(drv_rad+thickness+eps)]);
        } // d
} // box_driver_full

box_driver_full();
Admin - email* me if you need anything, or if I've done something stupid...
* click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work.
Obviously inclusion of works of previous authors is not included in the above.
eZe
Reply | Threaded
Open this post in threaded view
|

Re: Is there a better way of doing it?

eZe
This post has NOT been accepted by the mailing list yet.
Thank you for the reply i will keep in mind the coincident problem and i totall agree with commenting after bracket to keep track of the functions :)