Creating New Document from Add-In

Creating New Document from Add-In

alden_360
Contributor Contributor
213 Views
4 Replies
Message 1 of 5

Creating New Document from Add-In

alden_360
Contributor
Contributor

Hello, I have been doing some testing on my end with regards to creating files and deriving parts to them, which seems to be working well. I just noticed however that the tool tips for both the Documents.add and Document.saveAs methods have this in their descriptions:

"Saving a document is not supported within any of the Command related events. When a command is running, a transaction is open, and saving a document cannot be transacted and, as a result, cannot be contained within a command transaction."

 

Is this still true in the API and if it is is there any workaround that anyone has found? Code that was working on my end is below. Thanks!

 

def command_execute(args: adsk.core.CommandEventArgs):
    # General logging for debug.
    futil.log(f'{CMD_NAME} Command Execute Event')
    des = adsk.fusion.Design.cast(app.activeProduct)

    ...

    target_body: adsk.fusion.BRepBody = selection.body  # Store body
    active_doc = des.parentDocument
    folder = active_doc.dataFile.parentFolder

    new_doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType, True)
    new_design: adsk.fusion.Design = new_doc.products.itemByProductType('DesignProductType')
    new_comp = new_design.rootComponent
    der_feats = new_comp.features.deriveFeatures
    der_input = der_feats.createInput(des)
    der_input.sourceEntities = [target_body]
    der_feat = der_feats.add(der_input)
    new_doc.saveAs('new_file', folder, '', '')

 

0 Likes
Accepted solutions (1)
214 Views
4 Replies
Replies (4)
Message 2 of 5

Jorge_Jaramillo
Advisor
Advisor
Accepted solution

Hi,

 

When I need to do something that is not allowed inside a Command Event, I use a Custom Event.

This way you can make any operation under the Fusion's main thread but outside the boundary of the commands events.

 

You can find how to deal with a Custom Event in the following sample:

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-85edd118-c2a4-11e6-b401-3417ebc87622

 

I hope this can help you.

 

Regards,

Jorge Jaramillo

 

Message 3 of 5

alden_360
Contributor
Contributor

Hi Jorge, thanks for the tip. I'll give that a try!

0 Likes
Message 4 of 5

navya_gelli
Autodesk
Autodesk

Hello @alden_360,

 

Just circling back: Were you able to figure this out, or do you still need a hand? Let us know by giving us an update on where you are at so the community can help guide you with next steps if possible.

Navya | Community Manager
0 Likes
Message 5 of 5

alden_360
Contributor
Contributor

This seemed to work on my end, though the functionality working within the command as well makes me think that it only really effects whether the creation can be undone. Some of the recent changes to the versioning/part numbering system have caused me to take a bit of a step back from utilizing the API for this intent so I have not spent much time validating whether this is the case or not.

0 Likes