How to determine which documents are open in the GUI

kandennti
Mentor

How to determine which documents are open in the GUI

kandennti
Mentor
Mentor

Hi there.

It is possible that I am missing something. I would appreciate it if you know how to do this.

 

The following script will output the number of Application.documents and the name of the data file to be retrieved.

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core

def run(context):
    ui: adsk.core.UserInterface = None
    try:
        app: adsk.core.Application = adsk.core.Application.get()
        ui = app.userInterface

        docs: adsk.core.Documents = app.documents
        app.log(f'-- Documents Count:{docs.count} --')

        for doc in docs:
            df: adsk.core.DataFile = doc.dataFile
            app.log(f'{df.name}.{df.fileExtension}')

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

 

If the script is executed with only one drawing file open in the GUI, the related f3d documents will also be listed.
Is there any way to determine which document is actually open in the GUI from the Application.documents?

 

0 Likes
Reply
Accepted solutions (1)
537 Views
6 Replies
Replies (6)

kandennti
Mentor
Mentor
・・・
        for doc in docs:
            df: adsk.core.DataFile = doc.dataFile
            app.log(f'{df.name}.{df.fileExtension} -- isVisible:{doc.isVisible}')

 

I could not determine this using the isVisible property.
Probably because it is displayed in the browser tree.

1.png

I would like to determine what documents are open as tabs.

0 Likes

j.han97
Advocate
Advocate

Hi @kandennti ,

 

I do not work with linked documents so I cannot try it on my side. 

 

Have you tried the app.activeDocument function? Does it gives all linked documents as well?

0 Likes

kandennti
Mentor
Mentor

@j.han97 Thanks for the replies.

 

The following question came up in the Japanese forum.
"When I change a file, I want to update all affected files, is there an easy way to do this?"
(Are probably a personal license)

 

It should be enough to open and update the topmost f3d file in the Assy state, but if there are many files, it is difficult to find them, so we created a script to find and open them using the API.
The update is done manually by the user.

 

I have found that reopening a document that is already open with the API only takes longer, it's pointless.
Therefore, we want to determine if it has already been opened.

 

If I am only opening drawings (f2d), I find that it is much faster to do "Document.activate" than to do "Documents.open" for the corresponding f3d file.

0 Likes

j.han97
Advocate
Advocate

I probably misunderstood the problem previously: So you want the list of all documents opened as tabs, is that correct?

0 Likes

kandennti
Mentor
Mentor

@j.han97 .

 

I want to open documents that have not been opened, but do not want to process the Documents.open method on documents that have already been opened.

This is because it will only take away from the Fusion360 operation and will not change anything as a result.

0 Likes

kandennti
Mentor
Mentor
Accepted solution

I found the following text command.

DocumentMgt.DumpDocTxnEntity

 

Here is the result of opening and executing just the drawing.

DocumentMgt.DumpDocTxnEntity 
---------------------
Top level document: draw v2
	Number of Chains: 0
	XRef Missing: 0
	BU Computed Needed: 0

The name of the tab of the document opened in the GUI is listed.
Therefore, we were able to determine.

 

A sample script was created using this information.

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core
import re


def run(context):
    ui: adsk.core.UserInterface = None
    try:
        app: adsk.core.Application = adsk.core.Application.get()
        ui = app.userInterface
        app.log(u'TextCommandWindow.Clear')

        docs: adsk.core.Documents = app.documents
        app.log(f'-- Documents Count:{docs.count} --')

        topLevelNames = getTopLevelDocumentNames()
        doc: adsk.fusion.FusionDocument
        docLst = [d for d in docs]
        for doc in docs:
            df: adsk.core.DataFile = doc.dataFile
            topLevelInfo = '<-- Top Level !!' if doc.name in topLevelNames else ''
            app.log(f'{df.name}.{df.fileExtension} {topLevelInfo}')

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

def getTopLevelDocumentNames() -> list:
    app: adsk.core.Application = adsk.core.Application.get()
    res: str = app.executeTextCommand(u'DocumentMgt.DumpDocTxnEntity')
    topLevelNames = re.findall(r'Top level document: (.+)\r', res)

    return topLevelNames

 

The result of the execution is shown here.

 -- Documents Count:6 --
 SubAssy_NonPosition.f3d 
 B.f3d 
 A.f3d 
 SubAssy_Position.f3d 
 Assy.f3d 
 draw.f2d <-- Top Level !!

We were able to determine correctly.

2 Likes