Reading Autodesk Inventor dialogs with Python

Reading Autodesk Inventor dialogs with Python

jonzavialov
Participant Participant
1,126 Views
5 Replies
Message 1 of 6

Reading Autodesk Inventor dialogs with Python

jonzavialov
Participant
Participant

I am working on a project with a lot of .iam files. Sometimes these files are missing, causing the following dialog to show up when I try to open files that use this assembly:

 

image.png

 

I am trying to create a python program that will create a COM object with Autodesk Inventor, open the file in question, and check that all the assemblies it uses are present. I want to do this by detecting if Inventor throws an error after trying to open the file.

 

Here is my code so far:

 

 

import win32com.client
from win32com.client import gencache
import time
import threading
import pythoncom
from tqdm import tqdm

WAITTIME = 5


def check_file(id, result, verbose, done):
    pythoncom.CoInitialize()
    oApp = win32com.client.Dispatch(
        pythoncom.CoGetInterfaceAndReleaseStream(id, pythoncom.IID_IDispatch)
    )

    manager = oApp.ErrorManager

    if verbose:
        with tqdm(total=WAITTIME, desc="Waiting") as pbar:
            for i in range(WAITTIME):
                if done[0]:
                    result[0] = False
                    oApp.Quit()
                    return
                pbar.update(1)
                time.sleep(1)
    else:
        for i in range(WAITTIME):
            if done[0]:
                result[0] = False
                oApp.Quit()
                return
            time.sleep(1)

    try:
        result[0] = manager.IsMessageSectionActive
        oApp.Quit()
    except:
        pass


def file_is_broken(link, verbose=False):
    oApp = win32com.client.Dispatch('Inventor.Application')  # get COM object
    oApp_id = pythoncom.CoMarshalInterThreadInterfaceInStream(pythoncom.IID_IDispatch, oApp)
    mod = gencache.EnsureModule('{D98A091D-3A0F-4C3E-B36E-61F62068D488}', 0, 1, 0)  # Loads python support
    oApp = mod.Application(oApp)

    result = [None]
    done = [False]
    thread = threading.Thread(target=check_file, args=(oApp_id, result, verbose, done,))
    thread.start()

    try:
        oApp.Documents.Open(link)
        done[0] = True
        thread.join()
    except:
        pass

    return result[0]


if __name__ == "__main__":
    print(file_is_broken("<FILE PATH>", True))

 

 

 

What I am doing is creating the COM object, opening the file from its path, waiting a couple seconds, and then checking to see if there is an error present in another thread while the document is opening. This works, however it is not exact because there can be different errors and I only want to check if the "Resolve Link" error is present so I can determine that the file has broken links.

 

Is there a way for me to read the contents of the dialog through the Inventor API COM object, so I can get more information about the error that is present?

0 Likes
Accepted solutions (1)
1,127 Views
5 Replies
Replies (5)
Message 2 of 6

A.Acheson
Mentor
Mentor

Hi @jonzavialov 

Not relating to reading the inventor dialogues but you can check the assembly for missing file references. See this post.

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 6

jonzavialov
Participant
Participant

Unfortunately the project constraints say that the language has to be Python, is there any way to achieve the same thing in Python?

0 Likes
Message 4 of 6

A.Acheson
Mentor
Mentor

Hi @jonzavialov  it will require translation and I have no expieriece with python but if you have already opened the document in pyton using inventor app then you have 60% done. It may be the easiest to stick with the referenced document than the occurrences.  

 

VB.NET Example from this thread

 

Sub Main
     Dim MissingRefFound As Boolean = False
     CheckMissingDocDescriptors(ThisApplication.ActiveDocument)
        If MissingRefFound Then
            MessageBox.Show("Missing ref found")
        End If
End Sub

Sub CheckMissingDocDescriptors(oAssyDoc As Inventor.Document)
        Dim oRefDoc As Inventor.DocumentDescriptor
        For Each oRefDoc In oAssyDoc.ReferencedDocumentDescriptors
            If MissingRefFound Then
                Exit Sub
            End If
            If Not oRefDoc.ReferenceSuppressed Then
                If oRefDoc.ReferenceMissing Then
                    MissingRefFound = True
                Else
                    CheckMissingDocDescriptors(oRefDoc.ReferencedDocument)
                End If
            End If
        Next
    End Sub

 

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 5 of 6

JelteDeJong
Mentor
Mentor
Accepted solution
doc = ThisApplication.ActiveDocument

for refDocDescriptor in doc.ReferencedFileDescriptors:
    if refDocDescriptor.DocumentFound == False:
        print("Un resolved reference found")

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 6 of 6

Frederick_Law
Mentor
Mentor

That dialog is not missing files.

That's duplicated files in different folders.

 

For example, someone created an assembly with Part1 to Part10 in one folder.

Another day created another assembly with Part1 to Part10 in another folder.

Someone open both assemblies at the same time and files got mixed up.

0 Likes