Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Fusion 360 "Python" Script - Flat Pattern & Export To Dxf

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
isocam
233 Views, 3 Replies

Fusion 360 "Python" Script - Flat Pattern & Export To Dxf

Can anybody help?

 

Is it possible, using a Fusion 360 "Python" Script to automatically "Flat Pattern" & Export a "dxf" file from a "sheet metal" model?

 

If it is, can somebody post the script file required to do this?

 

Many thanks in advance!

 

Darren

3 REPLIES 3
Message 2 of 4
kandennti
in reply to: isocam
Message 3 of 4
kandennti
in reply to: kandennti

@isocam -San.

 

The execution condition is that the root component must have a sheet metal body.

# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.fusion as fusion
import pathlib

def run(context):
    ui: core.UserInterface = None
    try:
        app: core.Application = core.Application.get()
        ui = app.userInterface
        des: fusion.Design = app.activeProduct
        root: fusion.Component = des.rootComponent

        # get sheetmetal bodies
        sheetBodies = [b for b in root.bRepBodies if b.isSheetMetal]

        # get sheetmetal large face
        flatFaces = [f for f in sheetBodies[0].faces
            if f.geometry.objectType == core.Plane.classType()]
        targetFace: fusion.BRepFace = max(flatFaces, key=lambda f: f.area)

        # remove flatpattern
        flat: fusion.FlatPattern = root.flatPattern
        if flat:
            flatProd: fusion.FlatPatternProduct = flat.parentComponent.parentDesign
            flatProd.deleteMe()

        # create flatpattern
        flat: fusion.FlatPattern = root.createFlatPattern(targetFace)

        # export path
        exportPath = str(pathlib.Path("C:/temp") / f"{flat.name}.dxf")

        # export dxf
        export_dxf(exportPath, flat)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


def export_dxf(path: str, flat: fusion.FlatPattern) -> None:
    app: core.Application = core.Application.get()
    des: fusion.Design = app.activeProduct
    expMgr: fusion.ExportManager = des.exportManager
    dxfOpt: fusion.DXFFlatPatternExportOptions = expMgr.createDXFFlatPatternExportOptions(
        path,
        flat,
    )
    expMgr.execute(dxfOpt)
Message 4 of 4
BrianEkins
in reply to: isocam

Here's a simple script that checks the active document has a flat pattern and exports it as DXF.

def run(context):
    app = adsk.core.Application.get()
    ui = app.userInterface
    try:
        flatProduct: adsk.fusion.FlatPatternProduct = app.activeDocument.products.itemByProductType('FlatPatternProductType')
        if flatProduct is None:
            ui.messageBox('No flat pattern exists in the active document.')
            return

        flat = flatProduct.flatPattern
        if flat is None:
            ui.messageBox('No flat pattern exists in the active document.')
            return

        filename = 'C:/Temp/Test.dxf'
        flatExportOptions = flatProduct.exportManager.createDXFFlatPatternExportOptions(filename, flat)
        if flatProduct.exportManager.execute(flatExportOptions):
            ui.messageBox(f'Successfully exported flat pattern to: "{filename}"')
        else:
            ui.messageBox('Flat pattern export failed.')
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report