# exports rotated STLs with a specific face on the bottom
import os
import re
ScriptName = 'STL Exporter'
Win = Windows()
# get current settings, if any
CurrentSettings = CurrentAssembly().GetUserData('alibre.stlexporter.settings')
if CurrentSettings == None:
CurrentSettings = {}
# define options to show in dialog window
Options = []
Options.append(['Folder Name', WindowsInputTypes.Folder, CurrentSettings['FolderName'] if 'FolderName' in CurrentSettings else None])
Options.append([None, WindowsInputTypes.Label, "Note: This will loop thru EVERY part in the assembly. Each part will open in another window for selecting the face. If you don't select a face it will output without reorienting or using any options."])
Index_FolderName = 0
# show dialog to user, get inputs
Values = Win.OptionsDialog(ScriptName, Options, 300)
if Values == None:
sys.exit()
# get the inputs
CurrentSettings['FolderName'] = Values[Index_FolderName]
if CurrentSettings['FolderName'] == "":
Win.ErrorDialog('No FolderName entered', ScriptName)
sys.exit()
def ExportWithOptions(MyPart):
# called when an input changes in the dialog window
def InputChanged(Index, Value):
# use custom settings changed
if Index == Index_UseCustom:
UpdateUserInterface()
return
# updates the user interface based on the current selections made
def UpdateUserInterface():
UseCustom = Win.GetInputValue(Index_UseCustom)
if UseCustom == True:
Win.EnableInput(Index_MaxCellSize)
Win.EnableInput(Index_NormalDeviation)
Win.EnableInput(Index_SurfaceDeviation)
else:
Win.DisableInput(Index_MaxCellSize)
Win.DisableInput(Index_NormalDeviation)
Win.DisableInput(Index_SurfaceDeviation)
return
MyPartName = MyPart.Name
MyPartNameFirstPass = re.sub(r'<.*>', '', MyPartName)
MyPartNameClean = re.sub(r'[\\/\:*"<>\|\.%\$\^&£]', '', MyPartNameFirstPass)
MyFullPath = os.path.join(CurrentSettings['FolderName'], str(MyPartNameClean) + ".stl")
print('\n')
print(MyPartName)
TempPart = Part(MyPart.FileName, 0)
imgPath = os.path.join(CurrentSettings['FolderName'],"Temp.bmp")
TempPart.SaveSnapshot(imgPath,800,600,1,0)
# define options to show in dialog window
Options = []
Options.append(['File Name', WindowsInputTypes.SaveFile, MyFullPath])
Index_FileName = 0
Options.append(["Note:\nEach part will open in\nanother window for\nselecting the face.\nIf you don't select a\nface it will output\nwithout reorienting or\nusing any options.", WindowsInputTypes.Image, imgPath, 170])
Options.append(['Bottom Face', WindowsInputTypes.Face, None])
Index_BottomFace = 2
Options.append(['Force STL units to millimeters', WindowsInputTypes.Boolean, CurrentSettings['ForceMM'] if 'ForceMM' in CurrentSettings else False])
Index_ForceMM = 3
Options.append(['Use Custom Settings', WindowsInputTypes.Boolean, CurrentSettings['UseCustom'] if 'UseCustom' in CurrentSettings else False])
Index_UseCustom = 4
Options.append(['Custom Normal Deviation', WindowsInputTypes.Real, CurrentSettings['NormalDev'] if 'NormalDev' in CurrentSettings else 10])
Index_NormalDeviation = 5
Options.append([None, WindowsInputTypes.Image, 'NormalDeviation.jpg', 100])
Options.append(['Custom Surface Deviation', WindowsInputTypes.Real, CurrentSettings['SurfaceDev'] if 'SurfaceDev' in CurrentSettings else 0])
Index_SurfaceDeviation = 7
Options.append([None, WindowsInputTypes.Image, 'SurfaceDeviation.jpg', 100])
Options.append(['Custom Max Cell Size', WindowsInputTypes.Real, CurrentSettings['MaxCellSize'] if 'MaxCellSize' in CurrentSettings else 0])
Index_MaxCellSize = 9
Options.append([None, WindowsInputTypes.Image, 'MaxCellSize.jpg', 100])
#Options.append([None, WindowsInputTypes.Label, "Note:\nEach part will open in another window for selecting the face. If you don't select a face it will output without reorienting or using any options."])
# show dialog to user, get inputs
Values = Win.OptionsDialog(str(ScriptName) + ': ' + str(MyPartName), Options, 170, InputChanged, UpdateUserInterface)
if Values == None:
print('^ Skipped')
TempPart.Close()
return
# get the inputs
BottomFace = Values[Index_BottomFace]
if BottomFace == None:
print('^ No bottom face selected - Exported without reorienting or using any options.')
TempPart.ExportSTL(MyFullPath)
print('Saved: ' + str(CurrentSettings['FileName']))
TempPart.Close()
return
CurrentSettings['BottomFace'] = BottomFace.Name
CurrentSettings['FileName'] = Values[Index_FileName]
CurrentSettings['ForceMM'] = Values[Index_ForceMM]
CurrentSettings['UseCustom'] = Values[Index_UseCustom]
CurrentSettings['MaxCellSize'] = Values[Index_MaxCellSize]
CurrentSettings['NormalDev'] = Values[Index_NormalDeviation]
CurrentSettings['SurfaceDev'] = Values[Index_SurfaceDeviation]
# update settings on part
CurrentAssembly().SetUserData('alibre.stlexporter.settings', CurrentSettings)
if CurrentSettings['FileName'] == "":
print('^ No filename entered')
Win.ErrorDialog('No filename entered', ScriptName)
TempPart.Close()
return
# export rotated stl
TempPart.ExportRotatedSTL(CurrentSettings['FileName'], BottomFace,
CurrentSettings['ForceMM'], CurrentSettings['UseCustom'], CurrentSettings['MaxCellSize'],
CurrentSettings['NormalDev'], CurrentSettings['SurfaceDev'])
print('Saved: ' + str(CurrentSettings['FileName']))
TempPart.Close()
return
# export rotated stl
Assem = CurrentAssembly()
print('Exporting:')
for p in Assem.Parts:
ExportWithOptions(p)
Win.InfoDialog('Export completed', ScriptName)