Using OpenSCAD to produce drawings for 3D Printing

A place where discussions are about 3D printing.
User avatar
SimonWood
Trainee Driver
Trainee Driver
Posts: 655
Joined: Tue Apr 07, 2015 9:46 pm
Location: West Wales
Contact:

Re: Using OpenSCAD to produce drawings for 3D Printing

Post by SimonWood » Wed Jan 12, 2022 9:00 pm

Finally came back to the shell problem... It's a really interesting one - very tough, very instructive. I've a feeling I'm going to be coming back to it for years, chipping away at improving it with each new trick I learn in OpenSCAD. I certainly haven't cracked it yet, although managed to model each shell separately and pivot them all from a point some way from the origin around which they were extruded...

Image

Code: Select all

// The width of the largest shell
maxWidth = 7; // [0:10]
// The side length of the longest shell
maxSideLength = 4; // [0:0.5:5]
// Angle described by each shell
shellAngle = 37; // [30:40]
// How far from the origin of the shells to the pivot point
pivotOffset = 2; // [0:0.5:4]
module ShellShape(width, straightlen, angle){
    rotate([90,90+angle/2,0])
        rotate_extrude(angle=angle)
            translate([-straightlen,-0,0])
                union(){
                    translate([0,-width/2,0])
                        square([straightlen,width]);
                    difference(){
                        circle(d=width);
                        translate([0,-width/2,0])
                            square([width/2,width]);
                    }
                }
};
baseThickness = 0.5;
shellOverlap = 1/3; //proportion of each shell to overlap
widthStepDown = 0.25; // how much to reduce the width by for each successive shell
sideLengthStepDown = 0.1; // how much to reduce the side length by for each successive shell
translate([0,0,pivotOffset/2+baseThickness])
    for(i=[0:1]) 
        mirror( [i, 0, 0] )
            difference() {
            union() {
                for (i=[0:3]) {
                    rotate([0,i*(1-shellOverlap)*shellAngle,0])
                        translate([0,0,-pivotOffset])
                            ShellShape(maxWidth-i*widthStepDown,maxSideLength-i*sideLengthStepDown,shellAngle);
                }
                translate([0,0,-pivotOffset])
                    cylinder(h=pivotOffset+2,d=maxWidth-4*widthStepDown);
                translate([0,0,-pivotOffset])
                    cylinder(h=1,d=maxWidth+3);
            }
            translate([-(maxWidth+maxSideLength+1),-(maxWidth+maxSideLength+1)/2,-(maxWidth+maxSideLength+1)/2])
                cube(maxWidth+maxSideLength+1);
            translate([-0.5,-(maxWidth+maxSideLength+1)/2,-(maxWidth+maxSideLength+1)-pivotOffset/2-baseThickness])
                cube(maxWidth+maxSideLength+1);
            }
$fn=100;
No idea how it will print, of course. Potentially some large overhangs, and the base I've added may make it difficult to remove the supports...

User avatar
philipy
Moderator
Moderator
Posts: 5033
Joined: Sun Jan 30, 2011 3:00 pm
Location: South Northants

Re: Using OpenSCAD to produce drawings for 3D Printing

Post by philipy » Wed Jan 12, 2022 9:51 pm

SimonWood wrote: Wed Jan 12, 2022 9:00 pm
No idea how it will print, of course. Potentially some large overhangs, and the base I've added may make it difficult to remove the supports...
I think it would print ok in resin but FDM would be difficult I suspect.
Philip

User avatar
-steves-
Administrator
Administrator
Posts: 2412
Joined: Thu Jul 28, 2011 1:50 pm
Location: Cambridge & Peterborough

Re: Using OpenSCAD to produce drawings for 3D Printing

Post by -steves- » Thu Jan 13, 2022 9:18 am

What I can say is they print brilliantly in resin as I have already done some.

The picture shows the vents printed and supports broken off, but no cleaning up has been done. If anyone wants the torpedo vent stl file, just let me know. The shell vents were done in openscad using the code provided and tweaked for size, hence the 2 sizes. :thumbup:

I also printed a dome, it came out very well on the top circle it, but the flare was very bitty, but workable.




IMG_20220113_092242.jpg
IMG_20220113_092242.jpg (1.49 MiB) Viewed 5748 times
The buck stops here .......

Ditton Meadow Light Railway (DMLR)
Member of Peterborough and District Association
http://peterborough.16mm.org.uk/

User avatar
SimonWood
Trainee Driver
Trainee Driver
Posts: 655
Joined: Tue Apr 07, 2015 9:46 pm
Location: West Wales
Contact:

Re: Using OpenSCAD to produce drawings for 3D Printing

Post by SimonWood » Thu Jan 13, 2022 9:40 am

-steves- wrote: Thu Jan 13, 2022 9:18 am What I can say is they print brilliantly in resin as I have already done some.
Excellent! That's fast work - and they look great, better than I thought they'd turn out!

User avatar
-steves-
Administrator
Administrator
Posts: 2412
Joined: Thu Jul 28, 2011 1:50 pm
Location: Cambridge & Peterborough

Re: Using OpenSCAD to produce drawings for 3D Printing

Post by -steves- » Thu Jan 13, 2022 9:41 am

I don't know if anyone has tried it, but Tinkercad also has a code type way of doing things called codeblocks.

This is a screenshot of an example one that I was playing around with.



delete.JPG
delete.JPG (241.83 KiB) Viewed 5749 times
The buck stops here .......

Ditton Meadow Light Railway (DMLR)
Member of Peterborough and District Association
http://peterborough.16mm.org.uk/

User avatar
SimonWood
Trainee Driver
Trainee Driver
Posts: 655
Joined: Tue Apr 07, 2015 9:46 pm
Location: West Wales
Contact:

Re: Using OpenSCAD to produce drawings for 3D Printing

Post by SimonWood » Thu Jan 13, 2022 9:46 am

-steves- wrote: Thu Jan 13, 2022 9:41 am I don't know if anyone has tried it, but Tinkercad also has a code type way of doing things called blocks.
Didn't even know about that - that looks really useful, especially given how easy it is to quickly get something together in TinkerCAD, and it looks like this could enable progression to quite precise refinements.

User avatar
philipy
Moderator
Moderator
Posts: 5033
Joined: Sun Jan 30, 2011 3:00 pm
Location: South Northants

Re: Using OpenSCAD to produce drawings for 3D Printing

Post by philipy » Thu Jan 13, 2022 10:00 am

Well done to both of you as a collaborative effort. The prints have turned out pretty much as I thought they would.
Philip

User avatar
-steves-
Administrator
Administrator
Posts: 2412
Joined: Thu Jul 28, 2011 1:50 pm
Location: Cambridge & Peterborough

Re: Using OpenSCAD to produce drawings for 3D Printing

Post by -steves- » Thu Jan 13, 2022 10:01 am

This is an example of CodeBlocks working. It has differing speeds, I went from medium to fast after a couple of runs through.


The buck stops here .......

Ditton Meadow Light Railway (DMLR)
Member of Peterborough and District Association
http://peterborough.16mm.org.uk/

User avatar
SimonWood
Trainee Driver
Trainee Driver
Posts: 655
Joined: Tue Apr 07, 2015 9:46 pm
Location: West Wales
Contact:

Re: Using OpenSCAD to produce drawings for 3D Printing

Post by SimonWood » Wed Jan 18, 2023 8:14 pm

I meant to update this thread with progress on my headboard, but forgot to do so. I'd got to the point where I could generate a headboard but the letter spacing was causing me a headache:
SimonWood wrote: Sat Jan 01, 2022 9:14 am Even after fiddling the parameters you can see the letter spacing is a problem more generally - since really having each letter spaced evenly never works. An 'I' is much narrower than a 'W'. I haven't found anything in OpenSCAD to solve this, although I think from Googling around there may be libraries other people have developed which I can use - I need to look into this more...
Well, I found the library to do this, and in the process I also found a new OpenSCAD command which I think is going to be really useful for structuring projects.

The library is fontmetrics which includes the function measureText() which returns the width of a string - exactly what I needed! So I can now measure the width of each letter individually and use that to work out the angle to move through before position the next letter. Job done!

Code: Select all

function angle_subtended_by_arc(radius,arc_length) = arc_length*360/(2*radius*PI);
function add_elements(array, to=-1, from=0) = from < len(array) - 1 && (from < to || to == -1) ? array[from] + add_elements(array, to, from + 1) : array[from];
function angles_of_letters(the_text,radius,font_size) = [for (i=[0:len(the_text)-1]) angle_subtended_by_arc(radius,measureText(the_text[i],size=font_size))];
module text_arc(the_text,radius,font_size,center=false) {
    angles_of_letters=angles_of_letters(the_text,radius,font_size);
    center_offset = center ? add_elements(angles_of_letters,len(the_text)-1)/2 : 0;
        for (i = [0:len(the_text)-1]) {
            rotate(i == 0 ? center_offset : center_offset-add_elements(angles_of_letters,i-1))
                translate([0,radius,0])
                    text(the_text[i],halign="left",font_size); 
    }

}
But that code won't work yet, because the function measureText() isn't defined in my project, it's in fontmetrics.scad. So first of all I need to download fontmetrics (and its companion fontmetricsdata.scad) and I can then either place it in the same directory (folder) as my project, or in one of the places OpenSCAD looks for libraries - I won't cover the details of that here because it depends which operating system you're on, but it's an option if you think that's neater. Once I've done that, I have to include a line in my project to reference fontmetrics. There are actually two OpenSCAD commands that can do this: include<> and use<>. If you include<file.scad>; it basically pulls in everything in file.scad as if it were in your file. But if you use<file.scad>; it only brings in the modules and functions from file.scad so you can call them from with your project. I prefer this: it means if I define a constant in my project that happens to share the same name as a constant in the file I want to reference, it won't cause a clash.

Anyway, having added fontmetrics.scad to the same folder as headboard.scad, all I need to do now is add this line:

Code: Select all

use <fontmetrics.scad>;
Now my function above will be able to use measureText().

The benefit of this command is that I can now break down my projects into multiple files. So if I were drawing a wagon I could, say, have files axlebox.scad and solebar.scad and coupling.scad etc. and then in reference them in wagon.scad with 'use<axlebox.scad>;' etc. Not only will this make it more organised to work on and easier to find the bits of code I'm looking for, but potentially I can reuse bits (for example I might want to use 'axlebox.scad' in 'van.scad'). Admittedly this also increases the potential for complexity... and this has made me think about versioning. I'm experimenting with using GitHub for this - so you can now find the latest working headboard files on GitHub.

I've also finally got round to putting the headboard project on Thingiverse.

So even if you don't want to play with the OpenSCAD code you can now download it and type your own headboard text in as a parameter to generate an STL file for any headboard you like.

User avatar
FWLR
Driver
Driver
Posts: 4262
Joined: Sat Aug 05, 2017 9:45 am
Location: Preston, Lancashire, UK

Re: Using OpenSCAD to produce drawings for 3D Printing

Post by FWLR » Tue May 30, 2023 9:26 am

Fascinating stuff, but how the heck do you print those domes and vents on a filament printer. I have a Flashforge AD3 and I use Tinkercad. The Codeblocks looks interesting though. Is it easy enough to use Steve.

The OpenSCAD is way beyond me though... :roll:

User avatar
-steves-
Administrator
Administrator
Posts: 2412
Joined: Thu Jul 28, 2011 1:50 pm
Location: Cambridge & Peterborough

Re: Using OpenSCAD to produce drawings for 3D Printing

Post by -steves- » Thu Jun 01, 2023 10:18 am

FWLR wrote: Tue May 30, 2023 9:26 am Fascinating stuff, but how the heck do you print those domes and vents on a filament printer. I have a Flashforge AD3 and I use Tinkercad. The Codeblocks looks interesting though. Is it easy enough to use Steve.

The OpenSCAD is way beyond me though... :roll:
I don't find codeblocks easy to use, it's about the same as SCAD, it requires some coding knowledge, of which I have zero. As for printing those bits, I doubt a filament printer would do a good job of it, they really require a resin printer to get that kind of finish on small parts like these. :thumbleft:
The buck stops here .......

Ditton Meadow Light Railway (DMLR)
Member of Peterborough and District Association
http://peterborough.16mm.org.uk/

User avatar
FWLR
Driver
Driver
Posts: 4262
Joined: Sat Aug 05, 2017 9:45 am
Location: Preston, Lancashire, UK

Re: Using OpenSCAD to produce drawings for 3D Printing

Post by FWLR » Thu Jun 01, 2023 11:42 am

Thanks Steve, that's good to know. :thumbright:

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest