Message 1 of 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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.
Solved! Go to Solution.