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:ย 

Export sketches from all components

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
meister_mischler
275 Views, 6 Replies

Export sketches from all components

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. ๐Ÿ™‚

6 REPLIES 6
Message 2 of 7

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

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?

Message 4 of 7

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

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. ๐Ÿ™‚

Message 6 of 7

Nice! I am glad it worked for you

Cheers 





Rohit Bapat
Product Owner
Message 7 of 7

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

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

Post to forums  

Technology Administrators