Sketches to one DXF

Sketches to one DXF

marylou.forsyth
Contributor Contributor
462 Views
3 Replies
Message 1 of 4

Sketches to one DXF

marylou.forsyth
Contributor
Contributor

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.
0 Likes
Accepted solutions (1)
463 Views
3 Replies
Replies (3)
Message 2 of 4

marylou.forsyth
Contributor
Contributor

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!

0 Likes
Message 3 of 4

kandennti
Mentor
Mentor
Accepted solution

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

marylou.forsyth
Contributor
Contributor

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!!

0 Likes