kompas_window/save_to_iges.py

65 lines
2.5 KiB
Python

# pip install pywin32
import pythoncom
from win32com.client import Dispatch, gencache
import os
def save_opened_to_iges():
try:
# Получи API интерфейсов версии 5
api5_module = gencache.EnsureModule("{0422828C-F174-495E-AC5D-D31014DBBE87}", 0, 1, 0)
api5_api = api5_module.KompasObject(
Dispatch("Kompas.Application.5")._oleobj_.QueryInterface(
api5_module.KompasObject.CLSID, pythoncom.IID_IDispatch
)
)
module = gencache.EnsureModule("{69AC2981-37C0-4379-84FD-5DD2F3C0A520}", 0, 1, 0)
api = module.IKompasAPIObject(
Dispatch("Kompas.Application.7")._oleobj_.QueryInterface(
module.IKompasAPIObject.CLSID, pythoncom.IID_IDispatch
)
)
k_constants = gencache.EnsureModule(
"{75C9F5D0-B5B8-4526-8681-9903C567D2ED}", 0, 1, 0
).constants
application = module.IApplication(api)
application.Visible = True
for i in range(application.Documents.Count):
try:
doc = application.Documents.Open(i)
doc_type = doc.DocumentType
if doc_type in [k_constants.ksDocumentPart, k_constants.ksDocumentAssembly]:
doc.Active = True
doc_path = doc.Path
doc_name = "-".join(doc.Name.split(".")[:-1])
print(f"Попытка сохранить {doc_name}")
doc_3d = module.IKompasDocument3D(doc)
doc_api5 = api5_api.ActiveDocument3D()
save_params = doc_api5.AdditionFormatParam()
save_params.Init()
save_params.format = k_constants.ksConverterToIGES
ext = "igs"
path = f"{doc_path}{ext}/"
filename = f"{doc_name}.{ext}"
full_path = "".join([path, filename])
if not os.path.exists(path):
os.makedirs(path)
doc_api5.SaveAsToAdditionFormat(full_path, save_params)
print(f"Файл {doc_name} сохранен как IGES: {full_path}")
except Exception as e:
print(f"Ошибка при обработке документа {i}: {e}")
return "Сохранение в IGES завершено."
except Exception as e:
return f"Произошла ошибка: {e}"