Export sketches from all components

Export sketches from all components

meister_mischler
Enthusiast Enthusiast
752 Views
6 Replies
Message 1 of 7

Export sketches from all components

meister_mischler
Enthusiast
Enthusiast

A few weeks ago I found this handy script: https://forums.autodesk.com/t5/forums/forumtopicprintpage/board-id/22/message-id/15949/print-single-...

 

Sometimes it worked and sometimes it did not (or so it seemed to me). Today I talked to a guy with programming skills and we found out that the script only exports DXF sketches in active components. As my DXF are nested in components I would like to change this.


How can I change that? Is there something like "allComponents" instead of "activeComponent"?

 design = adsk.fusion.Design.cast(app.activeProduct)
 active_component = design.activeComponent
 sketches = active_component.sketches

Thanks for all your hints. 🙂

0 Likes
Accepted solutions (1)
753 Views
6 Replies
Replies (6)
Message 2 of 7

rohit.bapat
Autodesk
Autodesk

Hello @meister_mischler 

 

I have a suggestion here, instead of fetching Sketches only from the active components you can loop through the components within the document and fetch sketches from each. For example

allComponentsInDesign = design.allComponents
for component in allComponentsInDesign:
  sketches = component.sketches

 

Please let me know if this satisfies the requirement. 





Rohit Bapat
Product Owner
Message 3 of 7

meister_mischler
Enthusiast
Enthusiast

Thank you for your reply 🙂

If I replace this:

design = adsk.fusion.Design.cast(app.activeProduct)
        active_component = design.activeComponent
        sketches = active_component.sketches

 

with this:

design = adsk.fusion.Design.cast(app.activeProduct)
allComponentsInDesign = design.allComponents
for component in allComponentsInDesign:
  sketches = component.sketches

 

and leave all the other lines untouched I only get the "DXF_files" from the first component in the tree. What do I have to add to loop over all components?

0 Likes
Message 4 of 7

rohit.bapat
Autodesk
Autodesk
Accepted solution

Hello @meister_mischler 

 

For that you will have go loop in this order
Components

   -> Loop to fetch a Component

   -> Fetch Sketches from this component

        -> Loop Sketches to access each Sketch

        -> Export DXF

 

I haven't tested following code but it should work


# Online Python - IDE, Editor, Compiler, Interpreter

import adsk.core, adsk.fusion, adsk.cam, traceback, os

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

        folder_dialogue = ui.createFolderDialog()
        folder_dialogue.title = 'Select folder to export all DXFs'
        result = folder_dialogue.showDialog()

        if result == adsk.core.DialogResults.DialogOK: # If the user clicks on the "Select Folder" button
            export_folder = folder_dialogue.folder
            allComponentsInDesign = design.allComponents
            for component in allComponentsInDesign:
                sketches = component.sketches
                for sketch in sketches:
                    if is_dxf(sketch):
                        full_path = os.path.join(export_folder, sketch.name) + '.dxf'
                        sketch.saveAsDXF(full_path)
        else: # If the user clicks on the "Cancel" button 
            ui.messageBox(f'No folder selected')

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

Unless there is any compilation issue, above code should work. (I got this code from the forum post you linked in original post)

Thank you!





Rohit Bapat
Product Owner
Message 5 of 7

meister_mischler
Enthusiast
Enthusiast

Perfect, thanks 🙂

I added these lines and then it worked:

def is_dxf(sketch):
  if 'DXF' in sketch.name:
    return True


Hopefully I can extend the script so it lists the exported dxfs. But that's something for the weekend. 🙂

0 Likes
Message 6 of 7

rohit.bapat
Autodesk
Autodesk

Nice! I am glad it worked for you

Cheers 





Rohit Bapat
Product Owner
Message 7 of 7

meister_mischler
Enthusiast
Enthusiast

Its not yet weekend but the job is done. 😉
https://github.com/3kmch/fusion-scripts/tree/main/Export_DXF-Sketches

 

Screenshot_Export_DXF-Sketches.png

0 Likes