Export stl script

HI guyand girls

I’am looking for something that can select all the 3dm file into a folder, and export each of them in stl…

I have found some answer or script on the forum, but I haven’t been able to make them work, or they wasn’t exactly doing what I was expecting.

I have try to combine a python script :

"""Exports one STL file per object selected, export folder is same as current
folder if file has been saved; otherwise, choice of file name/location.
Choice of a four "standard" settings for export, inch or metric.
Script by Mitch Heynick 04.09.15
Adapted for V6 24.08.18 (should work in both V5 and V6)"""

import rhinoscriptsyntax as rs
import scriptcontext as sc
import os

def GetSTLSettings(ang,ar,dist,grid,den,maxL=0,minL=.0001):
    #Changed in V6 - UseSimpleParameters response *inverted*
    e_str = "_ExportFileAs=_Binary "
    e_str+= "_ExportUnfinishedObjects=_Yes "
    e_str+= "_UseSimpleDialog=_No "
    e_str+= "_UseSimpleParameters=_{} ".format("Yes" if rs.ExeVersion()==6 else "No")
    e_str+= "_Enter _DetailedOptions "
    e_str+= "_JaggedSeams=_No "
    e_str+= "_PackTextures=_No "
    e_str+= "_Refine=_Yes "
    e_str+= "_SimplePlane=_Yes "
    e_str+= "_AdvancedOptions "
    e_str+= "_Angle={} ".format(ang)
    e_str+= "_AspectRatio={} ".format(ar)
    e_str+= "_Distance={} ".format(dist)
    e_str+= "_Density={} ".format(den)
    e_str+= "_Grid={} ".format(grid)
    e_str+= "_MaxEdgeLength={} ".format(maxL)
    e_str+= "_MinEdgeLength={} ".format(minL)
    e_str+= "_Enter _Enter"
    return e_str
    
def BatchExportSTLByObject():
    #get unit system
    us=rs.UnitSystem()
    if us != 2 and us!= 8:
        msg="Unsupported file units detected for STL.\n"
        msg+="Please use either inches or millimeters."
        rs.MessageBox(msg)
        return
    
    msg="Select objects to export as individual .stl files"
    objs = rs.GetObjects(msg, 8 + 16 + 32,preselect=True)
    if not objs : return
    doc_name=sc.doc.Name
    
    filt = "STL Files (*.stl)|*.stl||"
    if not doc_name:
        #document hasn't been saved
        filename=rs.SaveFileName("Main file name/folder for STL export?", filt)
        if filename==None: return
    else:
        #document has been saved, get path
        msg="Folder for export? (Enter to save in current folder)"
        folder = rs.BrowseForFolder(rs.WorkingFolder(), msg)
        if not folder: return
        filename=os.path.join(folder,doc_name)
    
    #numerical settings:(angle,aspect,dist,grid,density,max_edgelen,min_edgelen)
    coarse_mm=[30,0,0.03,16,0,0,0.01]
    coarse_in=[30,0,0.001,16,0,0,0.0005]
    medium_mm=[15,0,0.01,32,0,0,0.001]
    medium_in=[15,0,0.0005,32,0,0,0.00005]
    fine_mm=[5,0,0.005,64,0,0,0.0001]
    fine_in=[5,0,0.0003,64,0,0,0.00001]
    extrafine_mm=[2,0,0.0025,64,0,0,0.0001]
    extrafine_in=[2,0,0.0001,64,0,0,0.00001]
    
    #user select settings
    C='Coarse' ; M='Medium' ; F='Fine   ' ; XF='Extrafine'
    if us==2:
        exs=[C+' (0.03mm)',M+' (0.01mm)',F+' (0.005mm)',XF+' (0.0025mm)']
    else:
        exs=[C+' (.001")',M+' (.0005")',F+' (.0003")',XF+' (.0001")']
    
    msg="Choose export parameters"
    ex_choice=rs.ListBox(exs,msg,"STL export",exs[2])
    if ex_choice is None: return
    if ex_choice==exs[0]:
        if us==2: sett_str=GetSTLSettings(*coarse_mm)
        else: sett_str=GetSTLSettings(*coarse_in)
    elif ex_choice==exs[1]:
        if us==2: sett_str=GetSTLSettings(*medium_mm)
        else: sett_str=GetSTLSettings(*medium_in)
    elif ex_choice==exs[2]:
        if us==2: sett_str=GetSTLSettings(*fine_mm)
        else: sett_str=GetSTLSettings(*fine_in)
    elif ex_choice==exs[3]:
        if us==2: sett_str=GetSTLSettings(*extrafine_mm)
        else: sett_str=GetSTLSettings(*extrafine_in)
    
    #start the export sequence
    rs.EnableRedraw(False)
    for i, obj in enumerate(objs):
        e_file_name = "{}-{}.stl".format(filename[:-4], str(i+1))
        print e_file_name
        rs.UnselectAllObjects()
        rs.SelectObject(obj)
        rs.Command('-_Export "{}" {} _Enter'.format(e_file_name,sett_str), True)
    rs.EnableRedraw(True)

BatchExportSTLByObject()

with a “processor” that I did myself with rs command from lunchbox

but It wasn’t doing exactly what I want.

I want all the part from a 3dm file (brep/surface or whatever) to be exported in the same file.
and the file to be named the exact same name as the 3dm file they are coming from…

any one know an easy method ?

thanks !

Assigned to Scripting category

Maybe…?
BatchConvertToSTL.py (2.7 KB)

1 Like

tx !

it’s just perfect !

I know that I’am asking a lot…
but, do you think there is any way to modify it into a grashopper python box with a button ?
because I’am creating a kind of library tool on a grasshopper file, and it’s been a while since I was asking myself, is it easy to modify a python script to make it run with a button into grasshopper…

thanks

Sorry, no idea how to do that really.

Dear Helvetosaur,

I really appreciate your help ! But it seems that the code have just a little bug.
When I use, it end by an unknown command :
“SetRedrawOn”
and all the rhino windows turn black, and it is no longer possible to use the software.
So I did modify the command myself on the script for “_SetRedrawOn” and it’s know perfectly working.
The complete working script is here :
Export_STL_By_Folder.py (2.7 KB)

“_SetRedrawOn” is not an unknown command…

But in any case, it should probably work without it - I think this is a leftover from an old .rvb script.