multiple DXF export

JYZMT
Advocate

multiple DXF export

JYZMT
Advocate
Advocate

Hello,

 

I found this post,

 

https://forums.autodesk.com/t5/fusion-360-design-validate/export-multiple-sketches-into-one-dxf/td-p...

 

and while this is exactly what I need, I'd settle for just exporting multiple sketch as multiple dxfs, but doing it in one go, rather than individually. So simply selecting all the sketches I need to export (maybe 10?) then scripting the export, and ideally using the sketch name as the file name (if only right clicking on them would allow this!!!)

 

I can't find anything out there already?  I'm guessing this is the kind of thing that could be scripted? If I know it can be done, I can invest some time in learning how, but please do direct me to anything that might help!

 

Or is it not much more difficult to do the original request? only with sketch names as layer names?

 

Thanks so much for any help.

3 Likes
Reply
Accepted solutions (1)
5,063 Views
18 Replies
Replies (18)

marshaltu
Autodesk
Autodesk
Accepted solution

Hello,

 

It should be doable. Please refer to the following sample how to export all selected sketch as separate dxf files.

 

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

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface
        
        sketches = []
        for seln in ui.activeSelections:
            sketch = adsk.fusion.Sketch.cast(seln.entity)
            if sketch:
                sketches.append(sketch)

        folderdlg = ui.createFolderDialog()
        folderdlg.title = 'Please select a folder to save dxf files:' 
        res = folderdlg.showDialog()
        if res == adsk.core.DialogResults.DialogOK:
            folder = folderdlg.folder
            for sketch in sketches:
                fullpath = os.path.join(folder, sketch.name)
                sketch.saveAsDXF(fullpath + '.dxf')
                    
        else:
            ui.messageBox('No folder is selected.')

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

Thanks,

Marshal



Marshal Tu
Fusion Developer
>
3 Likes

JYZMT
Advocate
Advocate

Thank you, Marshall!

This looks very interesting. I'll work my way through it and report back.

Thanks again. 

0 Likes

JYZMT
Advocate
Advocate

Great, it works of course, Thanks. This is so helpful.

 

When I first copied it in, I grabbed it from the "solution!" dialogue box in this forum, and it came into spyder with no indentation? So I VERY slowly worked through it, adding the indentation (This is my first exploration into Python - I'm not a developer). After some trial and error, I got it working - only when I came back here to thank you, I can clearly see all the indentation is in your original post!!!

 

But, A few hours introduction to python and trying to understand how this script works is probably time well spent.

 

Thanks again

Rob

0 Likes

Anonymous
Not applicable

I've jumped through the hoops to take multiple sketches and output a single DXF with each sketch on its own layer (ok, so it's 2 fixed sketches but it could be easily modified to an arbitrary number of sketches and layers). Let me know if you'd like to see the code... or maybe I'll feel inspired enough to just publish a new add-in this weekend to do this 😉

1 Like

JYZMT
Advocate
Advocate

Hello,

 

Thanks, I'd be very interested to see the code. Currently I use Vectorworks to import multiple dxfs into a file, (it allocates file names to layer names in this process), then export again... but automating as much as possible helps to stream line work flow. I'm somewhat out my comfort zone with python API scripting... but the more I experiment the better I'll get. Its just all a bit daunting at the start. 

 

Thanks again

0 Likes

Anonymous
Not applicable

@JYZMT wrote:

Hello,

 

Thanks, I'd be very interested to see the code. Currently I use Vectorworks to import multiple dxfs into a file, (it allocates file names to layer names in this process), then export again... but automating as much as possible helps to stream line work flow. I'm somewhat out my comfort zone with python API scripting... but the more I experiment the better I'll get. Its just all a bit daunting at the start. 

 

Thanks again


Ya, I was less than impressed with the API design myself - so I abstracted it away 😉 I need to get around to publishing my framework on GitHub - feel free to use my "fission" library in your own projects.

 

Anyway, see line 95 in the attached source - that's a good place to start hacking from

2 Likes

Anonymous
Not applicable

By the way - in case you havn't "manually installed" an add-in before here's the process:

 

Manual Install:
Extract the attached zip file wherever you want.
In Fusion open the Add-ins dialog, on the Add-Ins tab click the small green plus icon near the text "My Add-Ins" (in the list of add-ins).
Then locate and select the add-in folder (previously extracted from the zip) and say OK.
Check the "Run on startup" checkbox, click RUN and you will find the addin under the sketch menu.

 

 

If you do end up using my fission library in your own projects do yourself a favor and avoid copy-pasting folders of code. On Windows you can create "links" with this command...

 

Execute from directory you wish to create the link within (run this from the "cmd" prompt)

mklink /J fission X:\path\to\fission

1 Like

JYZMT
Advocate
Advocate

Thanks, I'll take a look over the weekend. I can really see the power of scripting, but its quite a learning curve. Lets see how far I get.....

0 Likes

JYZMT
Advocate
Advocate

errrrrrr yikes. This is way beyond me at the moment!

 

How come there are 2 .py files in there? does the main script talk to the merging script? 

 

I think I will have to get my head around some of this, but it is going to take me a while.

 

I have 2 questions before I even get going if that's ok? 

 

Firstly, using the merge dxf code, any idea what might happen when 2 sources have the same name? So in the example here, we are selecting multiple sketches and asking it to export multiple dxfs.... if 2 have the same name, it simply overrides one of them, and you only get one. Logical I guess.

 

In the merge dxf code, would it do the same with layers. I.e. if you had 2 of "Sketch A", you would only get one layer (sketch A) with the geometry from only one sketch.... or would it merge the geometry from both sketches into a single layer (called Sketch A)? 

 

The reason I ask is I probably only need 5-10 layer definitions, but with lots of geometry from lots of sketches/bodies/components. So, if it can't merge, I'm going to try and combine multiple geometry into a single sketch inside fusion before I export. But if the merge dxf approach DOES have the ability to merge geometry from lots of sketches into a single layer, then maybe I don't do it in fusion, but through the script. 

 

Second question, when creating a dxf layer name, do you think its possible to combine the name of the sketch, and join it to a user parameter input?

So the sketch name might be "CUT_PLY_"

A specific user parameter (for Ply) might be 18

So the dxf name would be CUT_PLY_18

 

Let me know if you have any idea, but don't worry if not. Thanks for your help so far.

Rob

 

 

0 Likes

Anonymous
Not applicable

Oh, there's more than 2 .py files in there - look in the fission folder 😉

 

DXF files are messy - dxf_merge.py is largely a complicated hack doing as little DXF processing as possible. It doesn't actually generate the DXF data (but it does tweak it) the code first saves sketches to temporary DXF files (from line 86 in the main py file) then it passes that/those file name(s) to the DxfFileMerger object along with layer name and color code. Then eventually the merged DXF is saved (line 100 in the main py file).

 

The function signature is add_file(dxf_filename, layer_name, layer_color). You can call add_file as many times as you like, each call will include the DXF file on a layer in the final DXF output - i advise against reusing a layer name as the code has no guards to prevent it and i'm not really sure how other programs would handle that case - but it could be modified to do so (a simpler solution to implement would be to project all of the sketches you want merged into a new sketch then export that one).

 

layer_color is actually a color code not something standard like a hex value or string - if you look in the resources/colors folder you'll see an HTML file, if you run a simple web server serving this folder and browse that HTML file you can hover over the various color tiles and it will tell you what code it is... but to make things easier here's the table with numbers overlayed.

 

ColorTable.png

0 Likes

JYZMT
Advocate
Advocate

Thanks for this. Like I say, its going to take me a while to get my head around it.

 

I also found this, if it helps you at all

 

https://www.opendesk.cc/blog/opendesk-at-autodesk

 

the git hub files are listed in the article but here is the dxf exporter

 

https://github.com/opendesk/fusion360-dxf-export

 

Again its a dauntingly long piece of code, but this, along with yours might gives me a good point to start hacking from.

 

I'll let you know if I get anywhere. First though, its more introduction to Python you tube tutorials! 

 

0 Likes

Anonymous
Not applicable
Interesting find, thanks for sharing. It looks like the opendesk code is
built to generate dxf files directly from bodies - compleatly within python
code. The code is not written for easy reuse unfortunatly and it looks like
it can only support linear and circular curve segments (lines, arcs and
circles not splines or ellipses or fixedsplines as generated when you split
a curved body at any angle not aligned with the curve plane). My code gets
around the complexities of these other curves by letting Fusion generate
the dxf code. Just something to be aware of and it might help you
understand them both to know.

I'm sure that my main .py file will have a comment pointing to a rather
massive Autodesk pdf describing the dxf format and "standard" codes. If not
I'm sure I have it bookmarked, let me know if you can't find it and would
like the link.
0 Likes

Scottjb
Explorer
Explorer

Does anyone have any idea how to get that opendesk ADDIN running? I have tried but it doesn't seem to be working out for me,

 

Thanks,

 

Scott

0 Likes

JYZMT
Advocate
Advocate

It did not work for me either. I've been in contact with open desk. The 2 people that did have long since left the company, and they are not pursuing this as a tool. It was an experiment, but that was as far as they took it. 

Sorry

0 Likes

Scottjb
Explorer
Explorer

Strange, clearly they put a lot of time and effort into it. Would have been great to get it working and test. 

 

Thanks, Scott

0 Likes

JeromeBriot
Mentor
Mentor

Hello,

 

@JYZMT and @Scottjb, follow these instructions :

  1. Clone the repo https://github.com/opendesk/fusion360-dxf-export or download the zip file and unzip it.
  2. Open Opendesk-dxf-exporter.manifest in a text editor
  3. Change "addin" to "script" at line 3
  4. Save the file
  5. Open Opendesk-dxf-exporter.py in a text editor
  6. Change path at line 256 to a location on your computer. On Windows, you have to double the separator : C:\\Users\\FolderA\\FolderB
  7. Search for the "make_model_layer_dict" function (line 379) and move "global MODEL_LAYER_DICT" (line 381) at the beginning of the function (line 380)
  8. Save the file
  9. Open Fusion 360
  10. Open the "Script and Addins" dialog under TOOLS tab > MAKE menu
  11. Click on the green "+" next to "My Scripts" and select the folder that contains the files Opendesk-dxf-exporter.py and Opendesk-dxf-exporter.manifest
  12. Select the script in the list
  13. Click on Run

Voila!

 

I tested with the file the authors provided here: http://a360.co/1sYVDbz

 

0 Likes

Scottjb
Explorer
Explorer

Fantastic!

 

Great post, got it working now

 

Many thanks,

 

Scott

0 Likes

therealsamchaney
Advocate
Advocate

I've made a slightly different version from @marshaltu which only exports sketches that have "DXF" in their name. This is useful for me because I have designs I use for laser cutting and I only need to export a few specific sketches and not the others. Hope it's useful for someone else.

#Author-Sam Chaney
#Description-Export multiple sketches as DXF if the sketch name contains 'DXF'

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)
        active_component = design.activeComponent
        sketches = active_component.sketches

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

        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
            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()))

 

Thanks!

-Sam

1 Like