kompas_window/get_opened_files.py

51 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import pythoncom
from win32com.client import Dispatch, gencache
def get_opened_files():
"""
Функция для получения списка открытых файлов в КОМПАС-3D
Возвращает форматированную строку с информацией о файлах
"""
try:
pythoncom.CoInitialize()
# Подключаемся к API КОМПАС
kompas_module = gencache.EnsureModule("{69AC2981-37C0-4379-84FD-5DD2F3C0A520}", 0, 1, 0)
kompas_api = kompas_module.IKompasAPIObject(
Dispatch("Kompas.Application.7")._oleobj_.QueryInterface(
kompas_module.IKompasAPIObject.CLSID, pythoncom.IID_IDispatch
)
)
application = kompas_module.IApplication(kompas_api)
k_constants = gencache.EnsureModule(
"{75C9F5D0-B5B8-4526-8681-9903C567D2ED}", 0, 1, 0
).constants
result = []
result.append("[b]Открытые файлы в КОМПАС:[/b]\n")
for i in range(application.Documents.Count):
doc = application.Documents.Open(i)
doc_type = "Неизвестный тип"
if doc.DocumentType == k_constants.ksDocumentDrawing:
doc_type = "Чертеж"
elif doc.DocumentType == k_constants.ksDocumentPart:
doc_type = "Деталь"
elif doc.DocumentType == k_constants.ksDocumentAssembly:
doc_type = "Сборка"
elif doc.DocumentType == k_constants.ksDocumentFragment:
doc_type = "Фрагмент"
elif doc.DocumentType == k_constants.ksDocumentSpecification:
doc_type = "Спецификация"
result.append(f"{i+1}. [b]{doc.Name}[/b] ({doc_type})")
result.append(f" Путь: {doc.Path}\n")
return "\n".join(result)
except Exception as e:
return f"[color=ff3333]Ошибка при получении списка файлов: {str(e)}[/color]"
finally:
pythoncom.CoUninitialize()