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: 

Updating out-of-date references through API

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
sharas_s
314 Views, 2 Replies

Updating out-of-date references through API

I am trying to update out-of-date references of a document and encountered few issues. 

I have the following situation:

  • Document `A` which has references to documents `B` and `C`.
  • Document `B` also has a reference to a document `B.1`

How to update all references (`B`, `C` and `B.1`) from document `A` using API?

This is my approach:

 

 

import adsk.core, adsk.fusion, traceback

def update_document_demo( document, recurse=False, save_message="Updated" 😞 
    for doc_ref in document.documentReferences:
        if recurse:
            print ("ref doc:")
            print (doc_ref.referencedDocument.name)
            # if not doc_ref.referencedDocument.isUpToDate: # can't check if referenced document is up to date, raises an error 😕 
            child_document = app.documents.open( doc_ref.referencedDocument.dataFile, True )
            adsk.doEvents()
            child_document.activate()
            update_document_demo( child_document, recurse=True, save_message=save_message )
            child_document.save( save_message )
            # child_document.close(saveChanges=False)
            print (f"updated child doc: {child_document.name}")
            adsk.doEvents()

        if doc_ref.isOutOfDate:
            print (f"updating reference to {doc_ref.referencedDocument.name} ")
            if not doc_ref.getLatestVersion():
                print ("getLastestVersion was unsuccessful")
            adsk.doEvents()
            print (f"finished updating reference to {doc_ref.referencedDocument.name}")

    if not document.isUpToDate:
        document.activate()
        raise Exception( f"Document {document.name} has references which could not be updated. Update manually and continue" )
    
    adsk.doEvents()

def run( context ):
    app = adsk.core.Application.get()
    ui  = app.userInterface
    doc = app.activeDocument
    update_document_demo(document=doc, recurse=True)

 

 

If I run this on provided dummy files (attached) when documents `B`, `C` and `B.1` are modified and are out-of-date for document `A`, I get the following output before the exception is raised:

 

 

ref doc:
B v2
ref doc:
B.1 v1
updated child doc: B.1 v1
updating reference to B.1 v1 
finished updating reference to B.1 v2
updated child doc: B v3   
updating reference to B v2  <--------- !!!!!!! 
finished updating reference to C v1   <--------- !!!!!!! 

ref doc:
B v3
ref doc:
B.1 v2
updated child doc: B.1 v1
updated child doc: B v3

 


Few observations:

  • The order `document.documentReferences` seems to be non deterministic. Running this multiple times gives a different sequence of `documentReferences`
  • Occasionally after `documentReference.getLatestVersion()` object changes its pointer to another `documentReference`. In this example, we were updating reference to `B` and afterwards it changed its reference to `C` document.

Tags (2)
2 REPLIES 2
Message 2 of 3
kandennti
in reply to: sharas_s

Hi @sharas_s .

 

There may be a better way, but this one works.

app.executeTextCommand(u'Commands.Start PLM360DeepRefreshDocumentCommand')

 

Message 3 of 3
sharas_s
in reply to: sharas_s

This command works quite well. Thanks! 

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report