Programmatically altering stl CAD files with Python

3k views Asked by At

I have many stl CAD files for 3D printing and I was wondering how I can manipulate an individual CAD file by making a simple change programatically. Like if I want to double the dimensions of everything in the CAD file, how could I accomplish this in python? I could convert it to gcode if that makes things simpler.

I have come across FreeCad but I am not sure if this is the easiest and best way of programmatically altering CAD files. I also wanted an option that allows for freedom of manipulation beyond just size, maybe also seeing how I can programmatically alter the shape of the CAD file.

2

There are 2 answers

0
sliptonic On

STL files are meshes. Their exterior is defined by triangulated surfaces. Meshes are notoriously difficult to modify after the fact. That's why you always want to save your designs in the native format and also in an open solid format like .STP or .IGES.

That said, simple scaling of a mesh is pretty easy and can be done trivially in FreeCAD. You can do it in the gui in the mesh design workbench or through python. Here's a post about that: https://forum.freecadweb.org/viewtopic.php?t=9109#p74047

FreeCAD also has some tools to convert a mesh into a solid. Results are usually less than spectacular but might be good enough depending on your needs. Search on the FreeCAD forum for posts about converting or look here: https://wiki.freecadweb.org/FreeCAD_and_Mesh_Import

Gcode doesn't say anything about the model itself. It specifies where a tool should move so it's just a series of directives that move a tool from place to place at a given speed. Going from Gcode to a model that you can edit is pretty difficult or impossible.

0
jimy byerley On

Well to be precise, STL files are even not meshes, but triangles soups. Which means that triangles are even not connected to each other (well they need to be adjacent to be used by software generatif g-code, but no obligation else)

just for clarification: g-code format is just a sequence of instructions for the machines (3d-printers, etc) so it's very good for printers, but also certainly the worst format to manipulate geometry.

one solution

you can use the python pymadcad module to deal with STL files So for simple transformation operations (such as scale, translation etc)

from madcad import *

part = read('path/to/your_file.stl')
transformed = part.transform(mat3(2))  # scale by a factor 2 (diagonal matrix of 2)
write(transformed, 'path/to/output_file.stl')

If you need more complex operations (such as booleans, chamfers, etc) you will need to make the triangle connected again before the desired operations.

from madcad import *

part = read('path/to/your_file.stl')
part.mergeclose()    # merge points at the same location

# your desired operations (example)
transformed = union(part, icosphere(vec3(1,1,1), 3.))   # add a sphere a diameter 3

write(transformed, 'path/to/output_file.stl')

note

pymadcad is internally using numpy-stl to import stl files, if you need only basic manipulations you can also only install that one