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.
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Sketches to one DXF

Message 1 of 4
151 Views, 3 Replies

Sketches to one DXF

I have been learning and searching for days now and I will admit I have to be missing something and it could be basic.  There are wonderful examples how to make a DXF file from an existing active model, but from the python code I am just not able to get the body and the wanted sketches.  I can each sketch exported as its own dxf fine.  I can see each sketch and its name, but when I go and project the sketches I want, I am only getting the body.  Here is the code:


                            design : adsk.fusion.Design = app.activeProduct

                            rootComp = design.rootComponent
                            sketches = rootComp.sketches
                            folder = "W:\\SharedData\\Fusion_360\\DXF\\"
                            fullPath = os.path.join(folder, order)
                            i1 = 0
                            for sk in sketches:
                                sketch = sketches.item(i1)
                                # Get sketch health state
                                health = sketch.healthState
                                if health == adsk.fusion.FeatureHealthStates.ErrorFeatureHealthState or health == adsk.fusion.FeatureHealthStates.WarningFeatureHealthState:        

                                if ("Pen_Line" in or "Block" in😞
                                    sketch.saveAsDXF(fullPath + + '.dxf')
                                i1 = i1 + 1
                            #=================WORKS GREAT TO HERE========================
                            #Creates a new sketch on the specified planar entity
                            master: adsk.fusion.Sketch = rootComp.sketches.add(rootComp.xYConstructionPlane)
                            targetBody: adsk.fusion.BRepBody = rootComp.bRepBodies[0]
                            #Projects the specified entity or entities onto
                            # the x-y plane of the sketch and returns the created sketch entity(s)
                            project_result = master.project(targetBody)
                            if project_result == False:
                                print('Failure in DXF processing')

                            sketch_names = []  
                            sk_cnt = 0
                            for rtskcnt in rootComp.sketches:
                                ohmyname = rootComp.sketches.item(sk_cnt).name
                                sk_cnt = sk_cnt + 1
                            project_result = master.project(rootComp.sketches)
                            if project_result == False:
                                print('Failure in DXF processing')
                            # write the merged file
                            folder = "W:\\SharedData\\Fusion_360\\DXF\\"
                            fullPath = os.path.join(folder, order)
                            master.saveAsDXF(fullPath + '.dxf')
                            msg = '!!  DXF file has been written !!'
I have also attempted projecting the ...item but I can't get anything to work.  I DO get all the individual dxf files with the sketches and one for the body, I am so close hoping someone can see what I can't.  Thank you in advance!!!
PS. Master is the new sketch made to contain all the sketches and body.
Message 2 of 4

I believe its my understanding of the documentation of entity.  I am thinking there is one entity associated with one sketch but I can't make the connection.  Any help is pointers (ie look here) are welcomed to!

Message 3 of 4
in reply to: marylou.forsyth

Hi @marylou.forsyth .


There were a number of unclear parts.
It was a bit confusing to me, but how about a process like this?

# Fusion360API Python script

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

def run(context):
    ui = adsk.core.UserInterface.cast(None)
        app: adsk.core.Application = adsk.core.Application.get()
        ui = app.userInterface
        design : adsk.fusion.Design = app.activeProduct
        rootComp: adsk.fusion.Component = design.rootComponent

        exportFolder = pathlib.Path(r'C:\temp')

        healthStatesErrorOrWarning = [

        targetSketchNames = [

        sketches: adsk.fusion.Sketches = rootComp.sketches
        sketch: adsk.fusion.Sketch = None
        for sketch in sketches:
            health = sketch.healthState
            if health in healthStatesErrorOrWarning:

            if in targetSketchNames:
                path = exportFolder / f'{}.dxf'

        #=================WORKS GREAT TO HERE========================

        # Get all sketch elements of the root component
        sketchEntities: adsk.core.ObjectCollection = adsk.core.ObjectCollection.create()
        for sketch in rootComp.sketches:
            [sketchEntities.add(ent) for ent in sketch.sketchCurves]
            [sketchEntities.add(ent) for ent in sketch.sketchPoints]

        # Create Sketch
        master: adsk.fusion.Sketch = rootComp.sketches.add(rootComp.xYConstructionPlane) = 'Master'

        # Project Body
        targetBody: adsk.fusion.BRepBody = rootComp.bRepBodies[0]
        project_result: adsk.core.ObjectCollection = master.project(targetBody)
        if project_result.count < 1:
            print('Failure of the body project')

        # Include sketchEntities

        # Project sketchEntities

        # Export DXF
        path = exportFolder / f'{}.dxf'

        if ui:
Message 4 of 4

Wow you are fantastic!  I am sorry parts were unclear for you, thank you for plowing through it to understand my issue(s) you have totally saved me hours of work!  The entity handling is SO different than my concept thank you for the education and sharing.  You get a gold star from me and thats not easy!!

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