Advertisement
IBNobody

Save Visible as STP-STL-3MF

Oct 29th, 2023 (edited)
863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | Source Code | 0 0
  1. #Author-
  2. #Description-
  3.  
  4. # To install/use:
  5. # 1. Open Fusion 360.
  6. # 2. Hit Shift-S to bring up the "Scrips and Add-Ins" window.
  7. # 3. Click "Create".
  8. # 4. Select Python.
  9. # 5. Name the script "Save Visible as STP-STL-3MF" and click "Create".
  10. # 6. Click "Edit". Your PY IDE / text editor should appear.
  11. # 7. Paste this script in and save.
  12.  
  13. # 1. Hit Shift-S to bring up the "Scrips and Add-Ins" window.
  14. # 2. Select "Save Visible as STP-STL-3MF".
  15. # 3. Click "Run".
  16. # 4. Look for your output in this folder: "%appdata%\Autodesk\Autodesk Fusion 360\API\Scripts\Save Visible as STP-STL-3MF\out"
  17.  
  18.  
  19. import adsk.core, adsk.fusion, traceback
  20. import os.path, sys
  21.  
  22. def run(context):
  23.     ui = None
  24.     try:
  25.         app = adsk.core.Application.get()
  26.         ui  = app.userInterface
  27.  
  28.         # get active design
  29.         product = app.activeProduct
  30.         design = adsk.fusion.Design.cast(product)
  31.  
  32.         # get root component in this design
  33.         rootComp = design.rootComponent
  34.  
  35.         # create a single exportManager instance
  36.         exportMgr = design.exportManager
  37.  
  38.         # get the script location
  39.         scriptDir = os.path.dirname(os.path.realpath(__file__))
  40.  
  41.         # Create out dir if it does not exist
  42.         outDir = scriptDir + '/' + 'out'
  43.         if not os.path.exists(outDir):
  44.             os.mkdir(outDir)
  45.  
  46.  
  47.         # Create design dir if it does not exist
  48.         designDir = outDir + '/' + rootComp.name
  49.         if not os.path.exists(designDir):
  50.             os.mkdir(designDir)
  51.  
  52.         # export the root component in 3MF format
  53.         fileName = designDir + '/' + rootComp.name
  54.         stpOptions = exportMgr.createC3MFExportOptions(rootComp, fileName)
  55.         exportMgr.execute(stpOptions)
  56.  
  57.         # export the occurrence one by one in the root component to a specified file
  58.         allOcc = rootComp.allOccurrences
  59.  
  60.         for occ in allOcc:
  61.             if occ.isLightBulbOn:
  62.                 fileName = designDir + '/' + occ.component.name
  63.  
  64.                 # export the occurrence in STL format
  65.                 stlExportOptions = exportMgr.createSTLExportOptions(occ, fileName)
  66.                 stlExportOptions.sendToPrintUtility = False
  67.                 exportMgr.execute(stlExportOptions)
  68.  
  69.                 # export the occurrence in STP format
  70.                 stpOptions = exportMgr.createSTEPExportOptions(fileName, occ.component)
  71.                 exportMgr.execute(stpOptions)
  72.  
  73.     except:
  74.         if ui:
  75.             ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement