Modify STL CAD files for CNC or 3D Print using Python Programming

in #making6 years ago (edited)


modify stl files with python code

We are pretty familiar at this point with the 3D design creation workflow. Drawing, dragging and dropping CAD files using a mouse and a package such as Fusion 360, or a browser-based application, such as Onshape.

What, though, if you have to create a whole bunch of personalised objects? Opening, editing, exporting doesn't take long per item, but if you have 100, that soon adds up to quite a chore!

A member of a Facebook group had just that issue. They needed to add consecutive serial numbers to a vape design he had created. Initial order was 100 items.

People suggested printing the vapes then lasering the serial numbers. Or maybe hand engraving.

What if you could generate the files with code?

Generating and modifying STL CAD files with Python

View the full code for this project in this Gist.

Openscad allows us to generate CAD files in code, but if I am going to create a workflow then I like to use my swiss army knife which is Python. Well it turns out generating CAD in Python is a solved problem too!

Enter Solid Python.

Solid Python is a Python wrapper around Openscad. All the power of CAD, with all the flexibility and libraries of Python.

from solid import * 
d = difference()( cube(10), sphere(15) )
print(scad_render(d))

If you are familiar with SCAD and Python syntax then that will make more sense than if not, obviously. Essentially here we are simply using the Solid library to create an object which is a sphere-shaped hole in a cube.



Rendering outputs the CAD code, and it can be printed to the screen or saved to a file.

Modifying STL files

In OpenSCAD we can modify an STL using the following import syntax:

import("base.stl");

It wasn't documented, but I found in the code, that Solid has an equivalent feature:

a = import_stl("base.stl")

So having our base STL loaded, all we need to do is add our appropriate text.

(In my case I am adding text to an edge-lit acrylic sign base that I had designed quickly in Tinkercad, you will need your own STL file and coordinates for the text)

a = import_stl("base.stl") + translate([5, -37, 10])(
linear_extrude(height=2, convexity=4)(
text(annotation,
size=8,
font="helvetica",
halign="center",
valign="center")))

Using the text object we can add arbitrary text (here using a string called annotation), choosing
font family, size, and so-on, but we need that extrude to add depth.

We position this text using translate and the appropriate coordinates.

Generating the new STL CAD file

We could copy and paste the rendered code, but that would still be a chore for 100, so we need to automate OpenSCAD to generate STL files.

I got a little fancy and also generated a preview image and displayed it so I could see what it was doing, but you could obviously skip that aspect!


scad_render_to_file(a, outfile)<br/><br/>os.system("openscad {} -o {}".format(outfile, stlfile))
os.system("openscad --preview --imgsize=512,512 {} -o {}".format(outfile, outimage))
picture = Image.open(outimage).show()</pre>



Bottom Line

I am sure now you can extrapolate how using this approach, and supplying an appropriate string for the annotation and an output filename, you could generate hundreds of custom 3D objects ready to CNC or 3D print.

Go try it and let me know how it works out for you!


makerhacks.png



Posted from my blog with SteemPress : https://makerhacks.com/modify-stl-cad-files-for-cnc-or-3d-print-using-python-programming/

Sort:  

Hey that's really cool. It took me a moment until I understood that you use Python to generate more complex/individual OpenSCAD models.
The last function with the preview is a really cool idea as well. As I have never worked with Python before. Would this part also work under Windows or only under Linux?

It would work under Windows, you would just need to check the command line part works the same or needs the full path I think? :)

Congratulations This post has been upvoted by SteemMakers. We are a community-based project that aims to support makers and DIYers on the blockchain in every way possible.

Join our Discord Channel to connect with us and nominate your own or somebody else's posts in our review channel.

Help us to reward you for making it ! Join our voting trail or delegate steem power to the community account.

Your post is also presented on the community website www.steemmakers.com where you can find other selected content.

If you like our work, please consider upvoting this comment to support the growth of our community. Thank you.

Coin Marketplace

STEEM 0.37
TRX 0.12
JST 0.040
BTC 70162.45
ETH 3540.43
USDT 1.00
SBD 4.79