147 lines
6.6 KiB
Python
147 lines
6.6 KiB
Python
# pip install pywin32
|
|
import pythoncom
|
|
from win32com.client import Dispatch, gencache
|
|
import os
|
|
|
|
def get_all_sheets():
|
|
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
|
|
)
|
|
)
|
|
|
|
application = module.IApplication(api)
|
|
|
|
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 [4, 5]: # 4 — чертеж, 5 — сборка
|
|
doc.Active = True
|
|
doc_path = doc.Path
|
|
doc_name = "-".join(doc.Name.split(".")[:-1])
|
|
print(f"Анализируем документ: {doc_name}")
|
|
|
|
doc_3d = module.IKompasDocument3D(doc)
|
|
top_part = doc_3d.TopPart
|
|
elements = []
|
|
bends = []
|
|
welding = []
|
|
|
|
def look_features(element):
|
|
feature = module.IFeature7(element)
|
|
sub_features = feature.SubFeatures(1, True, False) or []
|
|
for item in sub_features:
|
|
if type(item) in (module.ISheetMetalBend, module.ISheetMetalLineBend, module.ISheetMetalBody):
|
|
sub_sheets = item.Owner.SubFeatures(1, True, False)
|
|
if sub_sheets:
|
|
for b in sub_sheets:
|
|
bend = module.ISheetMetalBend(b)
|
|
bends.append(bend)
|
|
|
|
def look_drawing(part):
|
|
drawing_context = module.IDrawingContainer(part)
|
|
macro = module.IMacroObject3D(drawing_context)
|
|
sub_features = macro.Owner.SubFeatures(1, True, False) or []
|
|
for item in sub_features:
|
|
if type(item) in (module.IUserDesignationCompObj,):
|
|
welding.append(item)
|
|
|
|
def find_elements(part):
|
|
try:
|
|
drawing_context = module.IDrawingContainer(part)
|
|
macro = module.IMacroObject3D(drawing_context)
|
|
sub_features = macro.Owner.SubFeatures(1, True, False) or []
|
|
for item in sub_features:
|
|
if type(item) in (module.IUserDesignationCompObj,):
|
|
welding.append(item)
|
|
except Exception as e:
|
|
print("Ошибка в DrawingContext")
|
|
|
|
try:
|
|
doc_parts = module.IParts7(part.Parts)
|
|
for i in range(doc_parts.Count):
|
|
element = doc_parts.Part(i)
|
|
if element.Parts.Count == 0:
|
|
elements.append(element)
|
|
look_features(element)
|
|
find_elements(element)
|
|
except Exception as e:
|
|
print("Ошибка в Parts")
|
|
|
|
if doc_type == 5:
|
|
find_elements(top_part)
|
|
else:
|
|
elements.append(top_part)
|
|
look_drawing(top_part)
|
|
look_features(top_part)
|
|
|
|
print(f"Найдено:\n Элементов: {len(elements)}\n Гибов: {len(bends)}\n")
|
|
|
|
sorted_data = {
|
|
"Name": {},
|
|
"Material": {},
|
|
"Area": {},
|
|
}
|
|
|
|
for e in elements:
|
|
for n in sorted_data.keys():
|
|
if n == "Name":
|
|
v = f"{getattr(e, n)}, масса {round(getattr(e, 'Mass'), 3)}"
|
|
res = sorted_data[n].get(v, 0) + 1
|
|
elif n == "Area":
|
|
mass_inertial_params = module.IMassInertiaParam7(e)
|
|
val = mass_inertial_params.Area * 0.0001
|
|
v = f"площадь {getattr(e, 'Material')}, м²:"
|
|
res = round(sorted_data[n].get(v, 0) + val, 6)
|
|
else:
|
|
v = getattr(e, n)
|
|
res = sorted_data[n].get(v, 0) + 1
|
|
sorted_data[n][v] = res
|
|
|
|
sorted_data["Area"]["Total"] = sum(sorted_data["Area"].values())
|
|
|
|
welding_key = "Welding"
|
|
for w in welding:
|
|
if welding_key not in sorted_data:
|
|
sorted_data[welding_key] = {}
|
|
w_name = w.Name
|
|
w_name_split = w_name.split("-")
|
|
w_len = w_name_split[-1].split("@")[0]
|
|
sorted_data[welding_key][w_name] = w_len
|
|
|
|
if welding_key in sorted_data:
|
|
def float_f(n):
|
|
try:
|
|
return float(n)
|
|
except Exception as e:
|
|
return 0
|
|
sorted_data[welding_key]["Total"] = sum([float_f(f) for f in sorted_data[welding_key].values()])
|
|
|
|
result.append(f"Документ: {doc_name}")
|
|
for s in sorted_data:
|
|
result.append(s)
|
|
for n in sorted_data[s]:
|
|
result.append(f"{n} -- {sorted_data[s][n]}")
|
|
result.append("-----")
|
|
|
|
except Exception as e:
|
|
result.append(f"Ошибка при обработке документа {i}: {e}")
|
|
|
|
return "\n".join(result)
|
|
|
|
except Exception as e:
|
|
return f"Произошла ошибка: {e}" |