I’m working on a subprogram of a large project that’s fetching parametrical data from 3D files and CAM files for data analytics. I’ve been trying to walk through a large article database containing 3D files from Inventor (.ipt files) from which I want to compute the volume.
I’m doing this by using the pywin32 module to interact with Inventor 2022 API. It works fine for most files but I have some problems handling corrupt and/or unresolved file links. These files make Inventor freeze and stop working which halts the python program:
import time
import win32com.client
import psutil
from pathlib import Path
def close_inventor():
for proc in psutil.process_iter():
try:
if "Inventor.exe" in proc.name():
proc.kill()
print("Autodesk Inventor process forcefully terminated.")
except Exception as e:
print("Error:", e)
def IptScraper(ipt_filename, ipt_file_path, timeout=10):
start_time = time.time()
try:
InventorApp = win32com.client.Dispatch("Inventor.Application")
InventorApp.Visible = True
Document = InventorApp.Documents.Open(ipt_file_path)
part_doc = Document.ComponentDefinition
Volume = part_doc.MassProperties.Volume
Document.Close(False)
Ipt_Information = {"FileName" : ipt_filename,
"Volume" : Volume}
except Exception as e:
if time.time() - start_time > timeout:
print("Autodesk Inventor has not responded for 8 seconds. Attempting to close it.")
close_inventor()
Ipt_Information = {"FileName" : "N/A",
"Volume" : "N/A"
}
print("Error" + str(e))
return Ipt_Information
# Example usage
ipt_filename = str(Path(r"690561.A.105_0"))
ipt_file_path = str(Path(r"D:\Test\690561.A.105_0 (Spanmiddelen) (FLAGGED).ipt"))
ipt_file_path2 = str(Path(r"D:\Test\632.006.442_003.ipt"))
print(IptScraper(ipt_filename, ipt_file_path))
I’ve tried using a timeout to forcefully close Inventor but the program gets stuck while opening the document:
Document = InventorApp.Documents.Open(ipt_file_path)
I need to manually force close Inventor before the program proceeds and calls the Exception.
I have filtered out a normal file and corrupt one to test. I have tried to use a context manager before but the problem solemnly lies in opening the document. I want to try and keep this program as simple as it can be. Inventor just gets unresponsive because it cant find the unresolved filelink to build the 3D model and keeps looking for it in the Workspace. After manually closing Inventor with task manager the program continues:
Autodesk Inventor has not responded for 10 seconds. Attempting to close it.
Error(-2147023170, 'The remote procedure call failed.', None, None)
{'FileName': 'N/A', 'Volume': 'N/A'}
I want to try and skip the iteration if it takes longer than 10 seconds, forcefully close Inventor and then go to the next .ipt file without having to interact manually.
Gerben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.