最近玩了一下Python,想试试连接Inventor。找了一下,发现还真有,以后可以摸黑打码玩INventor了。
import os
import ctypes
import win32com
win32com.__gen_path__ = os.path.join(os.path.split(__file__)[0], "gen_dir")
import win32com.client
import pythoncom
#ThisApplication = win32com.client.gencache.EnsureDispatch("Inventor.Application")
try:
ThisApplication = win32com.client.GetActiveObject("Inventor.Application")
except:
ThisApplication = win32com.client.Dispatch("Inventor.Application")
#ThisApplication.Visible=Trueforeign_filename = "C:\\Inventor\\test\\零件2.ipt"
#Document = ThisApplication.ActiveDocument
# Open files
open_documents = []
if ThisApplication.Documents.Count:
for i in range(ThisApplication.Documents.Count):
open_document = ThisApplication.Documents.Item(i+1)
print(open_document.FullFileName)
open_documents.append(open_document)if foreign_filename not in open_documents:
document = ThisApplication.Documents.Open(foreign_filename)
document_opened = True
else:
document_opened = Falseif document_opened:
document.Close(True)
运行结果: