Skip to content

Instantly share code, notes, and snippets.

@stephensmitchell
Created October 14, 2025 11:44
Show Gist options
  • Save stephensmitchell/5ee69c3f4e697a3b23cf075a9aedcce0 to your computer and use it in GitHub Desktop.
Save stephensmitchell/5ee69c3f4e697a3b23cf075a9aedcce0 to your computer and use it in GitHub Desktop.
Import and Export
# Part exporter script
# Demonstrates exporting a set of parts
# For use with Alibre Design
import fnmatch
import os
from os.path import expanduser
Win = Windows()
ScriptName = 'Part Exporter'
ExportTypes = ['STEP203', 'STEP214', 'STL', 'IGES', 'SAT', 'BMP']
Options = []
Options.append(['Folder containing parts', WindowsInputTypes.Folder, expanduser('~'), 'Choose a folder containing the parts.'])
Options.append(['Output folder', WindowsInputTypes.Folder, expanduser('~'), 'Choose a folder to put the exported files'])
Options.append(['Export type', WindowsInputTypes.StringList, ExportTypes, ExportTypes[0]])
Values = Win.OptionsDialog(ScriptName, Options, 300)
if Values == None:
sys.exit()
# get the inputs
PartsFolder = Values[0]
OutputFolder = Values[1]
ExportType = ExportTypes[Values[2]]
# validate
if not PartsFolder:
Win.ErrorDialog('No part folder selected', ScriptName)
sys.exit();
if os.path.isdir(PartsFolder) == False:
Win.ErrorDialog('Folder containing parts does not exist', ScriptName)
sys.exit();
if not OutputFolder:
Win.ErrorDialog('No output folder selected', ScriptName)
sys.exit();
if os.path.isdir(OutputFolder) == False:
Win.ErrorDialog('Output folder does not exist', ScriptName)
sys.exit();
print "Searching for parts..."
# create empty lists
Parts = []
# perform the search
for Root, Dirnames, Filenames in os.walk(PartsFolder):
for Filename in fnmatch.filter(Filenames, '*.AD_PRT'):
Parts.append(os.path.join(Root, Filename))
# if no parts found...
if len(Parts) == 0:
Win.ErrorDialog('No parts found', ScriptName)
sys.exit();
# export each part
for PartFileName in Parts:
print "Exporting {0}...".format(PartFileName)
Folder = os.path.dirname(os.path.abspath(PartFileName))
FileName = os.path.basename(PartFileName)
FileNameNoExt, Ext = os.path.splitext(FileName)
OutputFileName = OutputFolder + '\\' + FileNameNoExt
# open, export, close
P = Part(Folder, FileName)
if ExportType == 'STEP203':
P.ExportSTEP203(OutputFileName + '.stp')
print "Created {0} (203)".format(OutputFileName + '.stp')
elif ExportType == 'STEP214':
P.ExportSTEP214(OutputFileName + ".stp")
print "Created {0} (214)".format(OutputFileName + '.stp')
elif ExportType == 'STL':
P.ExportSTL(OutputFileName + ".stl")
print "Created {0}".format(OutputFileName + '.stl')
elif ExportType == 'IGES':
P.ExportIGES(OutputFileName + ".igs")
print "Created {0}".format(OutputFileName + '.igs')
elif ExportType == 'SAT':
P.ExportSAT(OutputFileName + ".sat", 18, True)
print "Created {0}".format(OutputFileName + '.sat')
elif ExportType == 'BMP':
P.SaveSnapshot(OutputFileName + '.bmp', 800, 600, True, False)
print "Created {0}".format(OutputFileName + '.bmp')
P.Close()
Win.InfoDialog('Exported {0} parts'.format(len(Parts)), ScriptName)
# exports rotated STLs with a specific face on the bottom
ScriptName = 'STL Exporter'
Win = Windows()
# called when an input changes in the dialog window
def InputChanged(Index, Value):
# use custom settings changed
if Index == Index_UseCustom:
UpdateUserInterface()
# 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)
# get current settings, if any
CurrentSettings = CurrentPart().GetUserData('alibre.stlexporter.settings')
if CurrentSettings == None:
CurrentSettings = {}
# define options to show in dialog window
Options = []
Options.append(['File Name', WindowsInputTypes.SaveFile, CurrentSettings['FileName'] if 'FileName' in CurrentSettings else None])
Index_FileName = 0
Options.append(['Bottom Face', WindowsInputTypes.Face, CurrentPart().GetFace(CurrentSettings['BottomFace']) if 'BottomFace' in CurrentSettings else None])
Index_BottomFace = 1
Options.append(['Force STL units to millimeters', WindowsInputTypes.Boolean, CurrentSettings['ForceMM'] if 'ForceMM' in CurrentSettings else True])
Index_ForceMM = 2
Options.append(['Use Custom Settings', WindowsInputTypes.Boolean, CurrentSettings['UseCustom'] if 'UseCustom' in CurrentSettings else False])
Index_UseCustom = 3
Options.append(['Custom Normal Deviation', WindowsInputTypes.Real, CurrentSettings['NormalDev'] if 'NormalDev' in CurrentSettings else 10])
Index_NormalDeviation = 4
Options.append([None, WindowsInputTypes.Image, 'NormalDeviation.jpg', 170])
Options.append(['Custom Surface Deviation', WindowsInputTypes.Real, CurrentSettings['SurfaceDev'] if 'SurfaceDev' in CurrentSettings else 0])
Index_SurfaceDeviation = 6
Options.append([None, WindowsInputTypes.Image, 'SurfaceDeviation.jpg', 170])
Options.append(['Custom Max Cell Size', WindowsInputTypes.Real, CurrentSettings['MaxCellSize'] if 'MaxCellSize' in CurrentSettings else 0])
Index_MaxCellSize = 8
Options.append([None, WindowsInputTypes.Image, 'MaxCellSize.jpg', 170])
# show dialog to user, get inputs
Values = Win.OptionsDialog(ScriptName, Options, 170, InputChanged, UpdateUserInterface)
if Values == None:
sys.exit()
# get the inputs
BottomFace = Values[Index_BottomFace]
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
CurrentPart().SetUserData('alibre.stlexporter.settings', CurrentSettings)
if CurrentSettings['FileName'] == "":
Win.ErrorDialog('No filename entered', ScriptName)
sys.exit()
if BottomFace == None:
Win.ErrorDialog('No bottom face selected', ScriptName)
sys.exit()
# export rotated stl
MyPart = CurrentPart()
MyPart.ExportRotatedSTL(CurrentSettings['FileName'], BottomFace,
CurrentSettings['ForceMM'], CurrentSettings['UseCustom'], CurrentSettings['MaxCellSize'],
CurrentSettings['NormalDev'], CurrentSettings['SurfaceDev'])
Win.InfoDialog('Export completed', ScriptName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment