123 lines
5.6 KiB
Python
123 lines
5.6 KiB
Python
# pip install pywin32
|
||
import pythoncom
|
||
from win32com.client import Dispatch, gencache
|
||
import os
|
||
import math
|
||
|
||
def project_support():
|
||
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)
|
||
saving_path = "../cdw/"
|
||
|
||
application.Visible = True
|
||
result = []
|
||
|
||
for i in range(application.Documents.Count):
|
||
try:
|
||
doc = application.Documents.Open(i)
|
||
doc_type = doc.DocumentType
|
||
if doc_type in [k_constants.ksDocumentAssembly, k_constants.ksDocumentPart]:
|
||
doc.Active = True
|
||
doc_path = doc.Path
|
||
doc_name = "-".join(doc.Name.split(".")[:-1])
|
||
result.append(f"Анализируем документ: {doc_name}")
|
||
|
||
doc_3d = module.IKompasDocument3D(doc)
|
||
doc_components = []
|
||
|
||
def all_elements(part):
|
||
doc_parts = module.IParts7(part.Parts)
|
||
for i in range(doc_parts.Count):
|
||
element = doc_parts.Part(i)
|
||
if element.Parts.Count == 0:
|
||
if not element.Standard and not any(
|
||
[el.Name == element.Name for el in doc_components]
|
||
):
|
||
doc_components.append(element)
|
||
else:
|
||
all_elements(element)
|
||
|
||
if doc_type == k_constants.ksDocumentAssembly:
|
||
all_elements(doc_3d.TopPart)
|
||
else:
|
||
doc_components.append(doc_3d.TopPart)
|
||
|
||
for component in doc_components:
|
||
component_ipart = module.IPart7(component)
|
||
if component_ipart.Standard:
|
||
continue
|
||
result.append(f"Создаем чертеж для {component.Name}")
|
||
|
||
c_doc = application.Documents.Add(k_constants.ksDocumentDrawing)
|
||
# Делаем лист А3 альбомный
|
||
c_layout = c_doc.LayoutSheets
|
||
c_sheet = c_layout.Item(0)
|
||
c_sheet.Format.Format = k_constants.ksFormatA3
|
||
c_sheet.Format.VerticalOrientation = False
|
||
c_sheet.Update()
|
||
|
||
c_size = [1, 1, 1]
|
||
if component_ipart.Owner.ResultBodies:
|
||
gabarit = [0, 0, 0, 0, 0, 0]
|
||
gabarit = component_ipart.Owner.ResultBodies.GetGabarit(*gabarit)
|
||
g1 = gabarit[1:4]
|
||
g2 = gabarit[4:]
|
||
c_size = [abs(g1[i] - g2[i]) for i in range(len(g1))]
|
||
g = max(c_size)
|
||
|
||
c_filename = component_ipart.FileName
|
||
c_name = component_ipart.Name
|
||
c_doc_2d = module.IKompasDocument2D(c_doc)
|
||
c_views = c_doc_2d.ViewsAndLayersManager.Views
|
||
c_scale = c_sheet.Format.FormatHeight / sum(c_size)
|
||
c_views.AddStandartViews(
|
||
c_filename,
|
||
c_name,
|
||
[1, 3, 5, 7],
|
||
c_size[1] * c_scale,
|
||
c_sheet.Format.FormatHeight - 25,
|
||
c_scale,
|
||
20,
|
||
20,
|
||
)
|
||
|
||
if not os.path.exists(f"{doc_path}{saving_path}"):
|
||
os.makedirs(f"{doc_path}{saving_path}")
|
||
filename = "_".join(filter(None, [component_ipart.Marking, c_name[:20]])).replace(" ", "-")
|
||
result.append(f"Сохраняем чертеж: {doc_path}{saving_path}{filename}.cdw")
|
||
c_doc.SaveAs(f"{doc_path}{saving_path}{filename}.cdw")
|
||
|
||
if doc_type == k_constants.ksDocumentAssembly:
|
||
spec = application.Documents.Add(k_constants.ksDocumentSpecification)
|
||
spec_doc = module.ISpecificationDocument(spec)
|
||
spec_doc.AttachedDocuments.Add(doc.PathName, True)
|
||
filename = "Список_деталей"
|
||
result.append(f"Сохраняем спецификацию: {doc_path}{saving_path}{filename}.spw")
|
||
spec.SaveAs(f"{doc_path}{saving_path}{filename}.spw")
|
||
|
||
except Exception as e:
|
||
result.append(f"Ошибка при обработке документа {i}: {e}")
|
||
|
||
return "\n".join(result)
|
||
|
||
except Exception as e:
|
||
return f"Произошла ошибка: {e}" |