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 » 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 2 guests