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: 

Sketches to one DXF

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
marylou.forsyth
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:        
                                    ui.messageBox(sketch.errorOrWarningMessage)

                                if ("Pen_Line" in sketch.name or "Block" in sketch.name😞
                                    sketch.saveAsDXF(fullPath + sketch.name + '.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
                                sketch_names.append(rootComp.sketches.item(sk_cnt).name)
                                master.include(rootComp.sketches.item(sk_cnt))
                                sk_cnt = sk_cnt + 1
                            project_result = master.project(rootComp.sketches)
                            if project_result == False:
                                print('Failure in DXF processing')
                            app.log(str(sketch_names))
       
                            # 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 !!'
                            print(msg)
                            futil.log(msg)
 
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.
3 REPLIES 3
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
kandennti
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)
    try:
        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 = [
            adsk.fusion.FeatureHealthStates.ErrorFeatureHealthState,
            adsk.fusion.FeatureHealthStates.WarningFeatureHealthState,
        ]

        targetSketchNames = [
            'Pen_Line',
            'Block'
        ]

        sketches: adsk.fusion.Sketches = rootComp.sketches
        sketch: adsk.fusion.Sketch = None
        for sketch in sketches:
            health = sketch.healthState
            if health in healthStatesErrorOrWarning:
                ui.messageBox(sketch.errorOrWarningMessage)

            if sketch.name in targetSketchNames:
                path = exportFolder / f'{sketch.name}.dxf'
                sketch.saveAsDXF(str(path))

        #=================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.name = '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
        master.include(sketchEntities)

        # Project sketchEntities
        master.project(sketchEntities)

        # Export DXF
        path = exportFolder / f'{master.name}.dxf'
        sketch.saveAsDXF(str(path))

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
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