Welcome! Log In Create A New Profile

Advanced

Rounding the edge of a hole?

Posted by David J 
Rounding the edge of a hole?
September 25, 2019 04:58PM
This is a simplified extract from something I'm designing:

bodyWidth = 25;    
bodyHeight = 15;  
slotWidth = 6; 
slotHeight = 10;

$fn = 50;

module slot()
{
    rotate( [-90, -90, 0] )
    union()
    {
        cylinder(d = slotWidth, h = bodyWidth + 2);
        translate( [0, -slotWidth/2, 0] )
            cube( [bodyHeight - slotHeight, slotWidth, bodyWidth + 2] );
        translate( [bodyHeight - slotHeight, 0, 0] )
            cylinder(d = slotWidth, h = bodyWidth + 2);
    }
}

difference()
{
    cube(bodyWidth);
    translate( [bodyWidth/2, -1, bodyWidth/2] )
        #slot();
}

This results in a cube with an oval hole through it (see attachment)

What I would like to do is to put a nice rounded edge around the hole at both ends. The trouble is, I don't know where to start! I would appreciate it if someone could give me some suggested techniques, helpful hints, etc. I'm not expecting anyone to actually do the work - just give me a clue about how to do it!

Note - this is something I've often wanted to do, but shied away from due to a lack of knowledge.

TIA,
David

Edited 2 time(s). Last edit at 09/25/2019 05:02PM by David J.
Attachments:
open | download - hole-in-cube.png (8.7 KB)
Re: Rounding the edge of a hole?
September 25, 2019 10:53PM
I presume you want this chamfer removed from the cube not the slot.

That is a rounded chamfer, and is very annoying do in openscad. (This is one of openscads biggest issues in my view)

You need to apply the chamfer to each component part, ie two cylinders and the square.

What I do is a 45 degree chamfer. Ie create some 90 degree cones at each end and remove how much you want
Do the same on the straight parts with a 45 degree rotated cube.
This is much easier to print, you don't get overhangs over 45 degrees.

Edited 4 time(s). Last edit at 09/26/2019 05:14AM by Dust.
Re: Rounding the edge of a hole?
September 26, 2019 12:29AM
But if you insist on rounded chamfers. Here is a cylinder for cut out with round chamfer (I just wrote this)

module Chamfered_Cylinder(CylinderDiameter,CylinderHeight,ChamferRadius,ChamferFacits) {
    difference () {
        union() {
            //Central Cyclinder
            cylinder(d=CylinderDiameter,h=CylinderHeight);
            //Bottom Chamfer cylinder
            translate ([0,0,0]) cylinder(d=CylinderDiameter+ChamferRadius,h=ChamferRadius/2);
            //Top Chamfer cylinder
            translate ([0,0,CylinderHeight-ChamferRadius/2]) cylinder(d=CylinderDiameter+ChamferRadius,h=ChamferRadius/2);
        }

        //Generate Bottom chamfer 
        for (i = [0:1:ChamferFacits-1]) {
            rotate([90,0,i*360/ChamferFacits+90]) translate([CylinderDiameter/2+ChamferRadius/2,ChamferRadius/2,-ChamferRadius/2]) cylinder(d=ChamferRadius, h=ChamferRadius);
        }

        //generate Top chamfer
        for (i = [0:1:ChamferFacits-1]) {
            rotate([90,0,i*360/ChamferFacits+90]) translate([CylinderDiameter/2+ChamferRadius/2,CylinderHeight-ChamferRadius/2,-ChamferRadius/2]) cylinder(d=ChamferRadius, h=ChamferRadius);
        }
    }
}


CylinderDiameter = 15;
CylinderHeight = 40;
ChamferRadius = 5;
ChamferFacits = 50;
//Its best to have the cylinder facits and the Chamfer facits the same value.
$fn=ChamferFacits;  

//Extra cylinder on bottom to appease the scad gods
translate ([0,0,-ChamferRadius]) cylinder(d=CylinderDiameter+ChamferRadius,h=ChamferRadius);
//Extra cylinder on top to appease the scad gods
translate ([0,0,CylinderHeight]) cylinder(d=CylinderDiameter+ChamferRadius,h=ChamferRadius);
//The Cylinder with chamfer
Chamfered_Cylinder(CylinderDiameter,CylinderHeight,ChamferRadius,ChamferFacits);

Edited 3 time(s). Last edit at 09/26/2019 12:50AM by Dust.
Re: Rounding the edge of a hole?
September 26, 2019 12:52AM
If you can live with the outside edges of the cube being rounded, then Minkowski is the answer. However, this is very slow operation. Put minkowski() {... sphere(1); } around your main block.


minkowski() {
difference() {
    cube(bodyWidth);
    translate( [bodyWidth/2, -1, bodyWidth/2] )
        slot();
}
sphere(1);
}
Re: Rounding the edge of a hole?
September 26, 2019 04:24AM
Hmm - I need to study your answers! Especially that code example...

I hadn't thought about using minkowski when the object has the slots - in the real project I had applied it to the body prior to 'cutting' the slots. I shall have to try that out. However it means re-arranging my code as I have included screw holes in the same part as the slots (not a major crisis).

My other random thought last night was about making the slot itself a better shape - making the cube and two cylinders with 'fat ends' so that a difference with a larger object results in a rounded exit. I suspect that's what the code example is doing, but I'll confirm that later today...

Thanks for all your help - I'll see how it works out.
Re: Rounding the edge of a hole?
September 26, 2019 08:06AM
Right - I tried using minkowski... life is too short... and all of my dimensions were screwed up (as expected); it would be a PITA to make size adjustments to compensate. So, good idea, but too painful.

I've been trying to develop my own idea, and here's how far I've got:



UPDATE 1: I've just look at the code written by 'Dust' - mine follows a similar technique. I didn't copy yours, honest!
UPDATE 2: Dust's approach is better than mine...

Obviously it doesn't yet achieve my original objective, but it's a start. I won't show the code just yet as it's messy and horrible, but I do plan to make it into a library so I'll put it up here for review when it's in decent shape (and when I'm not too ashamed to show it in public!).

Cheers,
David

Edited 2 time(s). Last edit at 09/26/2019 08:16AM by David J.
Re: Rounding the edge of a hole?
September 26, 2019 08:51AM
My code is freeware! do with it what you wish, no strings attached.
Re: Rounding the edge of a hole?
September 26, 2019 10:36AM
Quote
Dust
My code is freeware! do with it what you wish, no strings attached.

Thank you! If I do create a library then you will be credited in the header... smileys with beer
Re: Rounding the edge of a hole?
September 26, 2019 11:49AM
Interesting problem... smiling smiley

Here my approach, which is a bit different. Instead of subtracting cylinders to get the chamfer, I construct the cylinder in slices:
// chamfercyl - create a cylinder with round chamfered ends
module chamfercyl(
   r,              // cylinder radius
   h,              // cylinder height
   b=0,            // bottom chamfer radius (=0 none, >0 outside, <0 inside)
   t=0,            // top chamfer radius (=0 none, >0 outside, <0 inside)
   offset=[[0,0]], // optional offsets in X and Y to create
                   // convex hulls at slice level
   slices=10,      // number of slices used for chamfering
   eps=0.01,       // tiny overlap of slices
){
    astep=90/slices;
    hull()for(o = offset)
       translate([o[0],o[1],abs(b)-eps])cylinder(r=r,h=h-abs(b)-abs(t)+2*eps);
    if(b)for(a=[0:astep:89.999])hull()for(o = offset)
       translate([o[0],o[1],abs(b)-abs(b)*sin(a+astep)-eps])
          cylinder(r2=r+(1-cos(a))*b,r1=r+(1-cos(a+astep))*b,h=(sin(a+astep)-sin(a))*abs(b)+2*eps);
    if(t)for(a=[0:astep:89.999])hull()for(o = offset)
       translate([o[0],o[1],h-abs(t)+abs(t)*sin(a)-eps])
          cylinder(r1=r+(1-cos(a))*t,r2=r+(1-cos(a+astep))*t,h=(sin(a+astep)-sin(a))*abs(t)+2*eps);
}

// now build David's example, the cube with the chamfered hole (viewed from below to make things easy...)
$fn=36;
difference(){
   translate([-12.5,-12.5,0])cube(25);
   chamfercyl(3,25,3,3,[[-2,0],[2,0]]);
}


The module chamfercyl() takes as parameters the radius r and height h of the cylinder and in addition the chamfer radii b for bottom and t for top, where a positive chamfer radius goes to the outside and a negative to the inside of the main cylinder. The slices parameter gives the number of slices for rounding the chamfer. The optional offset array can be used to generate a convex hull at the slice level (cannot be done at the object level because the final object is not convex if outside chamfering is used). In David's object I use the offsets to make a slot instead of a round hole. Finally, the parameter eps can be used to add a tiny overlap of the slices.

Here some more simple examples to show the basic working of the module chamfercyl():
translate([-60,0,0])chamfercyl(10,50,0,12);
translate([-30,0,0])chamfercyl(10,50,-3,-6);
translate([0,0,0])chamfercyl(10,50,7,-3);
translate([30,0,0])chamfercyl(10,50,-9,2);
translate([60,0,0])chamfercyl(10,50,4,4);
gives the following cylinders:


Re: Rounding the edge of a hole?
September 26, 2019 12:34PM
@enif

Darn that is a good way to do it!!
Re: Rounding the edge of a hole?
September 26, 2019 12:53PM
@Dust

Thanks! smiling smiley
Re: Rounding the edge of a hole?
September 26, 2019 02:58PM
Quote
David J
Right - I tried using minkowski... life is too short... and all of my dimensions were screwed up (as expected); it would be a PITA to make size adjustments to compensate. So, good idea, but too painful.

Right... I'd forgotten that Minkowski with a sphere moves the surface by the radius of the sphere. For sphere of radius 1, you need to subtract 1 from each of your width and height variables.
Re: Rounding the edge of a hole?
September 26, 2019 03:43PM
Quote
frankvdh
Quote
David J
Right - I tried using minkowski... life is too short... and all of my dimensions were screwed up (as expected); it would be a PITA to make size adjustments to compensate. So, good idea, but too painful.

Right... I'd forgotten that Minkowski with a sphere moves the surface by the radius of the sphere. For sphere of radius 1, you need to subtract 1 from each of your width and height variables.

Correct - and it's not a significant problem if you're dealing with the external measurements of a solid. However, both sides of the holes are also reduced by the radius of the sphere - the calculations just get too complicated. But really the compilation time is the major issue when you have more than 1 hole in the object.
Re: Rounding the edge of a hole?
September 26, 2019 03:48PM
Wow, that's tough to read! yawning smiley
Clever though... I think. I'll have to get studying. smiling smiley


Quote
enif
Interesting problem... smiling smiley

Here my approach, which is a bit different. Instead of subtracting cylinders to get the chamfer, I construct the cylinder in slices:

-- snip for brevity ---

Edited 2 time(s). Last edit at 09/26/2019 04:03PM by David J.
Re: Rounding the edge of a hole?
September 29, 2019 04:06AM
Well, enif's code works very nicely - here's what I did with it:

Desk cable organiser

Enif - have a look at what I did with your code, and let me know if you're happy with what I've done. Also, if you want a more detailed credit in the code and on Thingiverse then please PM me with your details.

Many thanks to all,
David
Re: Rounding the edge of a hole?
September 29, 2019 06:28AM
@David J

This is perfect - thanks for giving credit smiling smiley

Since I am also quite active on Thingiverse, you could -but only if you like!- underlay the word "enif" with a link to my thingiverse account, which is www.thingiverse.com/enif .
Re: Rounding the edge of a hole?
September 29, 2019 08:13AM
I will do! Thanks for the help.
Sorry, only registered users may post in this forum.

Click here to login