What's new

Exporting all Models from Assembly for 3D printing

sacherjj

Senior Member
Couldn't find a good way to get each of the individual model STEP or STL out from an Assembly for printing, except opening each manually to export. This exports all, and deduplicates multiple used parts by filename.
 

Attachments

  • export_all_for_3d_print.py
    1.2 KB · Views: 25

bolsover

Alibre Super User
I had a go at writing a recursive version that will walk through all the subassemblies...
It works but I don't write much Python so I'm sure other more experienced users can improve - it certainly needs some exception handling since stp/stl export 'can' fail quite dramatically.

Python:
# create windows object

Win = Windows()

assem = CurrentAssembly()
parts = assem.Parts
subs = assem.SubAssemblies
print assem.FileName


#default_folder = 'c\\Users\\Joe Sacher\\OneDrive\\Alibre\\Spice Rack\\'
default_folder = ''


# construct list of items for the window
Options = []
# ask user for text
Options.append(['Export Folder', WindowsInputTypes.Folder, default_folder])
# filename suffix
Options.append(['Filename Suffix', WindowsInputTypes.String, ''])
# export types
export_types = ["STEP", "STL"]
Options.append(['Export type', WindowsInputTypes.StringList, export_types])
 
# show window and output results
# if user closes window or clicks on Cancel button then Values will be set to 'None'
Values = Win.OptionsDialog('Export All Models', Options)
if Values is None:
  exit()
target_folder, suffix, export_type_index = Values

used_files = set()

# export function
def export(parts):
  for part in parts:
    if part.FileName not in used_files:
      root_filename = part.FileName.split('/')[-1].rsplit('.', 1)[0]
      filepath = target_folder + '\\' + root_filename + suffix
      print filepath
      if export_type_index == 0:
        part.ExportSTEP203(filepath + '.stp')
      elif export_type_index == 1:
        part.ExportSTL(filepath + '.stl')
      used_files.add(part.FileName)

# recursive function to walk tree of subassemblies
def recurse(subassembly):
  parts = subassembly.Parts
  if len(parts) > 0:
   export(parts)
  subs =  subassembly.SubAssemblies
  if len(subs) > 0:
   for sub in subs:
   # recursion here
    recurse(sub)


export(parts)

if len(subs) > 0:
 for sub in subs:
  recurse(sub)

exit()
 

HKOCH

Member
I had a go at writing a recursive version that will walk through all the subassemblies...
It works but I don't write much Python so I'm sure other more experienced users can improve - it certainly needs some exception handling since stp/stl export 'can' fail quite dramatically.

Python:
# create windows object

Win = Windows()

assem = CurrentAssembly()
parts = assem.Parts
subs = assem.SubAssemblies
print assem.FileName


#default_folder = 'c\\Users\\Joe Sacher\\OneDrive\\Alibre\\Spice Rack\\'
default_folder = ''


# construct list of items for the window
Options = []
# ask user for text
Options.append(['Export Folder', WindowsInputTypes.Folder, default_folder])
# filename suffix
Options.append(['Filename Suffix', WindowsInputTypes.String, ''])
# export types
export_types = ["STEP", "STL"]
Options.append(['Export type', WindowsInputTypes.StringList, export_types])
 
# show window and output results
# if user closes window or clicks on Cancel button then Values will be set to 'None'
Values = Win.OptionsDialog('Export All Models', Options)
if Values is None:
  exit()
target_folder, suffix, export_type_index = Values

used_files = set()

# export function
def export(parts):
  for part in parts:
    if part.FileName not in used_files:
      root_filename = part.FileName.split('/')[-1].rsplit('.', 1)[0]
      filepath = target_folder + '\\' + root_filename + suffix
      print filepath
      if export_type_index == 0:
        part.ExportSTEP203(filepath + '.stp')
      elif export_type_index == 1:
        part.ExportSTL(filepath + '.stl')
      used_files.add(part.FileName)

# recursive function to walk tree of subassemblies
def recurse(subassembly):
  parts = subassembly.Parts
  if len(parts) > 0:
   export(parts)
  subs =  subassembly.SubAssemblies
  if len(subs) > 0:
   for sub in subs:
   # recursion here
    recurse(sub)


export(parts)

if len(subs) > 0:
 for sub in subs:
  recurse(sub)

exit()
Thanks a lot for your input here - great help to see and learn on these scripts. Unfortunately it doesn't work for me. Shoud it work when using PDM together with the Assmbly for file-storage?
 

bolsover

Alibre Super User
Thanks a lot for your input here - great help to see and learn on these scripts. Unfortunately it doesn't work for me. Shoud it work when using PDM together with the Assmbly for file-storage?
I should have noted that it does not work with PDM. Getting it to work with PDM is, I think, quite a bit more difficult since there doesn't seem to be a direct way of obtaining the paths to the components. In testing, I exported a large assembly from PDM and then ran the script against the top level assembly. It worked OK but for one part which threw an exception whilst generating the stp file.
 
Top