Converting .stl to .step using Python and FreeCAD

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
Post Reply
umardastgir
Posts: 32
Joined: Mon Apr 22, 2019 9:29 pm

Converting .stl to .step using Python and FreeCAD

Post by umardastgir »

I am working on a project to convert a .stl file to .step in Python. I followed https://www.youtube.com/watch?v=g_UeaFTmbRs youtube video on how to do this with the help of a GUI. In took help from the Python Console built-in FreeCAD and came up with the following script. In the Script below, I am using an file called bracket.stl and it has a mesh with the name of bracket.

Code: Select all

    Mesh.open(INPUT)
    App.setActiveDocument("Unnamed")
    App.ActiveDocument=App.getDocument("Unnamed")
    FreeCAD.getDocument("Unnamed").addObject("Part::Feature","bracket001")

    __shape__=Part.Shape()
    __shape__.makeShapeFromMesh(FreeCAD.getDocument("Unnamed").getObject("bracket").Mesh.Topology,0.100000)
    FreeCAD.getDocument("Unnamed").getObject("bracket001").Shape=__shape__
    FreeCAD.getDocument("Unnamed").getObject("bracket001").purgeTouched()
    del __shape__

    App.ActiveDocument.addObject('Part::Feature','bracket001').Shape=App.ActiveDocument.bracket001.Shape.removeSplitter()
    App.ActiveDocument.ActiveObject.Label=App.ActiveDocument.bracket001.Label
    App.ActiveDocument.recompute()

    __objs__=[]
    __objs__.append(FreeCAD.getDocument("Unnamed").getObject("bracket001001"))

    Part.export(__objs__,u"output.step")
    del __objs__
The above code works completely fine and converts a .stl file to .step

I tried to make the code more generic (to account for different files names and different Mesh numbers). Following is what I came up with

Code: Select all

    Mesh.open(INPUT)
    App.setActiveDocument("Unnamed")
    App.ActiveDocument=App.getDocument("Unnamed")

    objects = FreeCAD.getDocument("Unnamed").Objects

    FreeCAD.getDocument("Unnamed").addObject("Part::Feature","Feature_1")

    __objs__=[]
    for ob in objects:
        mesh = ob.Name
        print("MESH NAME:" + mesh)
        __shape__=Part.Shape()
        __shape__.makeShapeFromMesh(FreeCAD.getDocument("Unnamed").getObject("mesh").Mesh.Topology,0.100000)
        FreeCAD.getDocument("Unnamed").getObject("Feature_1").Shape=__shape__
        FreeCAD.getDocument("Unnamed").getObject("Feature_1").purgeTouched()
        del __shape__

        App.ActiveDocument.addObject('Part::Feature','Feature_1').Shape=App.ActiveDocument.Feature_1.Shape.removeSplitter()
        App.ActiveDocument.ActiveObject.Label=App.ActiveDocument.Feature_1.Label
        App.ActiveDocument.recompute()

#        __objs__=[]
        __objs__.append(FreeCAD.getDocument("Unnamed").getObject("Feature_2"))

    Part.export(__objs__,u"output.step")
    del __objs__
However, the script doesn't work as expected. It always creates a file with a fixed (very small) size which does not contain the required Part. Can someone point out whats wrong with my generic version?
Post Reply