Automatic exporting a series of .STL files

I have a bunch of polisurfaces to be 3d printed. Is there a plugin or any solution which can export the selected objects in STL one by one into a folder automatically?

Hello - this is a perfect operation to script - I don think I have anything around that does that but I’m pretty sure one or more has been posted here over the years,

-Pascal

I have one here by @phcreates that exports by layer, not by object. It might work in the meantime?

also, this is old and it does not work with sublayer, copy what’s below and paste on command line:

-runscript (
’ Written by Peter Harris, 2003
’ Version 1

option explicit

ChooseLayers

Sub ChooseLayers
Dim arrLayers, arrStates(), arrResults, intCount, strLayer, i, arrLayersToExport()
Dim strUnlockCode, blnRedraw, strEmptyLayerTest, blnEmptyOrNot
intCount = 0
arrLayers = Rhino.LayerNames
If IsArray(arrLayers ) Then
For Each strLayer In arrLayers
ReDim Preserve arrStates(intCount)
arrStates(intCount) = Rhino.IsLayerOn(strLayer)
intCount = intCount + 1
Next
arrResults = Rhino.CheckListBox (arrLayers, arrStates, “Choose layers to export”, “Export Layers”)
If IsArray(arrResults) Then
intCount = 0
For i = 0 To UBound(arrLayers)
If arrResults(i) = vbTrue Then
strEmptyLayerTest = arrLayers(i)
blnEmptyOrNot = Rhino.IsLayerEmpty(strEmptyLayerTest)
If blnEmptyOrNot <> True Then
ReDim Preserve arrLayersToExport(intCount)
arrLayersToExport(intCount) = arrLayers(i)
Rhino.LayerMode arrLayers(i), 0
blnRedraw = Rhino.EnableRedraw(vbFalse)
strUnlockCode = “_UnlockSelected -SelLayer " & CHR(34) & arrLayersToExport(intCount) & CHR(34) & " enter”
Rhino.Command strUnlockCode, vbfalse
Rhino.Command “_selnone”, vbFalse
intCount = intCount + 1
End If
End If
Next
blnRedraw = Rhino.EnableRedraw(vbTrue)
If IsArray(arrLayersToExport) Then
GetFormat arrLayersToExport
End If
End If
End If

End Sub

Sub GetFormat (arrLayersToExport)
Dim arrFormatOptions(3), strFormatResult, strExportFormat
Dim strFlavorString, strExportPath
arrFormatOptions(0) = “IGES”
arrFormatOptions(1) = “STEP”
arrFormatOptions(2) = “STL”
arrFormatOptions(3) = “3DM”
strFlavorString = “”
strFormatResult = Rhino.ListBox(arrFormatOptions, “Pick export format:”, “FP Batch Export”)
If Not IsNull(strFormatResult) Then
Select Case strFormatResult
Case “IGES”
strExportFormat = “igs”
strFlavorString = strIGESFlavorStringFunction
Case “STEP”
strExportFormat = “stp”
strFlavorString = strSTEPFlavorStringFunction
Case “STL”
strExportFormat = “stl”
strFlavorString = strStlFlavorStringFunction
Case “3DM”
strExportFormat = “3dm”
strFlavorString = str3dmFlavorStringFunction
End Select
If strFlavorString <> “” Then
strExportPath = strPath
If strExportPath <> “” Then
ExportFiles strExportFormat, strFlavorString, strExportPath, arrLayersToExport

  End If
End If

End if
End Sub

'----------STL-------------
Function strStlFlavorStringFunction
Dim strMeshWarning
strMeshWarning = Rhino.MessageBox (“Please note - only mesh objects will be exported!”, 1)
If strMeshWarning <> 2 then
Rhino.Command “_point 0,0,0 _SelMesh _Invert _Lock”, vbfalse
strStlFlavorStringFunction = " ExportFileAs=Binary ExportUnfinishedObjects=Yes _enter _enter"
End If
End Function

'----------3DM-------------
Function str3dmFlavorStringFunction
str3dmFlavorStringFunction = " "
End Function

'----------STEP-------------
Function strSTEPFlavorStringFunction
Dim arrStepFlavors(2), strExportResult, RunTheScript, strStepFlavor, strStepLetterCode
arrStepFlavors(0) = “AP203ConfigControlDesign”
arrStepFlavors(1) = “AP214AutomotiveDesign”
arrStepFlavors(2) = “AP214AutomotiveDesignCC2”
strStepFlavor = Rhino.ListBox(arrStepFlavors, “Pick STEP flavor:”, “Batch Export”)
If Not IsNull(strStepFlavor) Then
Select Case strStepFlavor
Case “AP203ConfigControlDesign”
strStepLetterCode = “A”
Case “AP214AutomotiveDesign”
strStepLetterCode = “P”
Case “AP214AutomotiveDesignCC2”
strStepLetterCode = “U”
End Select
strSTEPFlavorStringFunction = " s " & strStepLetterCode & " _enter"
End if
End Function

'----------IGES-------------
Function strIGESFlavorStringFunction
Dim arrIgesFlavors(8), strExportResult, RunTheScript, strIgesFlavor
arrIgesFlavors(0) = “Default”
arrIgesFlavors(1) = “Pro/E Windows solids”
arrIgesFlavors(2) = “Pro/E Windows surfaces”
arrIgesFlavors(3) = “SolidWorks Solids”
arrIgesFlavors(4) = “SolidWorks Surfaces”
arrIgesFlavors(5) = “Alias V8.x”
arrIgesFlavors(6) = “Maya”
arrIgesFlavors(7) = “Mastercam”
arrIgesFlavors(8) = “Ashlar Vellum”
strIgesFlavor = Rhino.ListBox(arrIgesFlavors, “Pick IGES flavor:”, “Batch Export”)
If Not IsNull(strIgesFlavor) Then
strIGESFlavorStringFunction = " " & CHR(34) & strIgesFlavor & CHR(34) & " _enter"
End if
End Function

'----------Get the path-------------
Function strPath
Dim strFilename
strFilename = Rhino.SaveFileName (“Pick a folder and create a name prefix”)
If Not IsNull(strFilename) Then
strPath = strFilename
End If
End Function

'----------Export-------------
Sub ExportFiles (strExportFormat, strFlavorString, strExportPath, arrLayersToExport)
Dim strLayerX, strNumOfFiles
strNumOfFiles = 0

For each strLayerX in arrLayersToExport

strNumOfFiles = strNumOfFiles + 1

Rhino.Command “selnone", vbFalse
Rhino.command "-sellayer " & CHR(34) & strLayerX & CHR(34)’, vbfalse
Rhino.command "-export " & CHR(34) & strExportPath & "
” & strLayerX & “.” & strExportFormat & CHR(34) & strFlavorString’, vbfalse
Next
Rhino.command “_selnone”, vbfalse
Rhino.Print “Use Undo to get back to where you were before the export, if needed. Export info has also been sent to clipboard.”

WriteShopInfoToClipboard strNumOfFiles, strExportPath
End Sub

’ ---------------------------- write info to clipboard --------------------------------------
Sub WriteShopInfoToClipboard (strNumOfFiles, strExportPath)
Dim FindSlash, strFileNameForClipboard, strCropped, LengthOfString, CounterX, strRunPath

strRunPath = strExportPath
CounterX = 0
While CounterX < 9
FindSlash = InStr(strExportPath,"")
LengthOfString = Len(strExportPath) - FindSlash
strCropped = Right(strExportPath,LengthOfString)
strExportPath = strCropped
If FindSlash = 0 then
CounterX = 9
End If
Wend

Rhino.ClipboardText ("All " & strNumOfFiles & " files with the prefix " & CHR(34) & strExportPath & CHR(34))
'Rhino.Command "_-Run " & CHR(34) & strRunPath & CHR(34)
End Sub
)

Hi Gustavo,
Thank you very much sharing his script with me. Unfortunately I am using standard Grasshopper widgets, and I am not familiar with programming. How can use this?

Here are two somewhat more recent scripts from my library.

This one has 4 pre-made settings choices: Coarse, Medium, Fine and Extra Fine… choose one.

BatchExportSTLByObjWSett.py (3.9 KB)

This one just has one setting, you can open the script file in any text editor and change the settings as you like and then re-save the file. You will recognize the settings from the Rhino mesh detailed options.

BatchExportSTLByObjWFixedSettings.py (1.8 KB)

image

In both cases the export is by object - one file per object exported - and will export directly to the folder with the original Rhino 3dm file, or if the file has not been saved it will ask you to choose a folder.

To run the script, save it somewhere on your hard drive and then use

! _-RunPythonScript "full path to script file"

or just open it in the script editor (EditPythonScript) and run it from there.

–Mitch

Hi Mitc,
Is it a pithyon widget?

No widget. This runs in Rhino and does not need Grasshopper.

thank you it works perfectly!

Is it possibele to decide from which number starts the numbering? I am processing several group of obejects.
If something goes wrong it would be much esaiser to figure out which part should be produced again…

It’s certainly possible to add that…

Wouldn’t it be better in that case to name/number the objects themselves and have the filename correspond with the name/number of the object?

Certainly it would be better, but the parts are baked geometry. The ID is written on the side of the part too.

OK - which of the two scripts above are you using? (or both)

I have tried the one with four setting, but I need usually one extraifine setting in the future, so I think I would use the second one.

OK, have a go with this one…

BatchExportSTLByObjWFixedSettingsPlus.py (1.9 KB)

Thank you very much for your time and kindness. I am at work now. I will try it tonight.

It works great! Thank you!

Hi everyone,
I am trying to save multiple STL files in a subfolder of the current working directory, but it keeps saving the objects in the current working directory (i.e. where the 3dm file is stored) instead of the subfolder. My commands are as following:

curDir=rs.WorkingFolder()
folderDir = curDir+‘fragments/’
os.mkdir(folderDir)
for name in objNames:
fileName = os.path.join(folderDir, name+’.stl’)
rs.Command(’_-Export '+fileName)

But it keeps saving the stl files as
curDir/name+’.stl’
instead of curDir/fragments/name+’.stl’

Any suggestions how to solve this?
Thank you!
Simon

I used the code that Helvetosaur sent. However, In my command line there are no ‘DetailedOptions’ with the Export command. I can set the Distance, Grid etc, but not decide whether or not to Refine the mesh. Also manually the command line returns it is an invalid option at each step in the command.

Does anyone where the cause of my problem lies?

Thanks!

What version of Rhino are you running? How are you running the script?

Rhino 6. Run the python script in Rhino Python editor:

import rhinoscriptsyntax as rs

def GetSTLSettings():
    e_str = "_ExportFileAs=_Binary "
    e_str+= "_ExportUnfinishedObjects=_Yes "
    e_str+= "_UseSimpleDialog=_No "
    e_str+= "_UseSimpleParameters=_No "
    e_str+= "_Enter _DetailedOptions "
    e_str+= "_JaggedSeams=_Yes "
    e_str+= "_PackTextures=_Yes "
    e_str+= "_Refine=_Yes "
    e_str+= "_SimplePlane=_Yes "
    e_str+= "_AdvancedOptions "
    e_str+= "_Angle=2 "
    e_str+= "_AspectRatio=0 "
    e_str+= "_Distance=10 "
    e_str+= "_Grid=4 "
    e_str+= "_MaxEdgeLength=0 "
    e_str+= "_MinEdgeLength=0.003 "
    e_str+= "_Enter _Enter"
    return e_str
    
def ExportSTLByObject():
    a = rs.GetObject()
    rs.SelectObject(a)
    stl_sett = GetSTLSettings()
    file_name = r'C:\Users\j.vanderidder\Documents\test3.stl'
    rs.Command('-_Export "{}" {}'.format(file_name,stl_sett), False)
    rs.EnableRedraw(True)

ExportSTLByObject()