-
Notifications
You must be signed in to change notification settings - Fork 0
/
catiacom.py
106 lines (86 loc) · 3.22 KB
/
catiacom.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""
Requirements:
1) Open CATIAV5 and a product file in .CATProduct or .3dxml format.
2) Run the code.
"""
import win32com.client
import numpy as np
import openpyxl
import os
import time
start_time = time.time()
catia = win32com.client.Dispatch("CATIA.Application")
current_directory = os.getcwd()
active_document = catia.activedocument.name
print("Active Document is: ", active_document)
if catia.ActiveDocument is not None:
active_document = catia.ActiveDocument.name
# Now you can work with 'active_document'
else:
print("No active document in CATIA.")
document_path = current_directory + "\\" + active_document
print("Assembly path: " "r'" + document_path)
product_document = catia.Documents.Open(document_path)
product = product_document.Product
prod_name = product.Name
print("Product Name: ", prod_name)
excel_path = current_directory + "\\" + "weights.xlsx"
try:
wb = openpyxl.load_workbook(excel_path)
ws = wb['Sheet1']
start_cell = 'A2'
start_row, start_col = openpyxl.utils.cell.coordinate_from_string(start_cell)
except:
wb = openpyxl.Workbook()
ws = wb.active
print("'weights.xlsx' file is not in the same directory with .CATProduct file.\n"
"Instead of a new 'weights.xlsx' file has been created.")
start_cell = 'A2'
start_row, start_col = openpyxl.utils.cell.coordinate_from_string(start_cell)
inertia_product = product.GetTechnologicalObject("Inertia")
mass_product = inertia_product.Mass
print(f"Total virtual mass of {prod_name} is {mass_product}.It might not indicates the true weight. \n "
f"Check the assigned material to the parts.")
# Feature Vectors
weight_matrix = []
name_matrix = []
# Function to recursively analyze subparts and access their properties
def analyze_subparts(component):
global weight_matrix
global name_matrix
for child in component.Products:
name = child.Name
weight = None
inertia_child = child.GetTechnologicalObject("Inertia")
# Attempt to get the "Mass" property if it exists
try:
weight = inertia_child.Mass
name_matrix = np.append(name, name_matrix)
weight_matrix = np.append(weight, weight_matrix)
print(len(name_matrix))
except Exception as e:
print(f"Error while retrieving weight for '{name}': {e}")
np.append("None", weight_matrix)
# Check if the component has sub-components
if hasattr(child, "Products"):
analyze_subparts(child) # Recursively analyze sub-subparts
for idx, value in enumerate(name_matrix, start=1):
cell = ws.cell(row=idx + 1, column=1)
cell.value = value
for idx, value in enumerate(weight_matrix, start=1):
cell = ws.cell(row=idx + 1, column=2)
cell.value = value
wb.save(excel_path)
# Start the analysis
analyze_subparts(product)
end_time = time.time()
pass_time = end_time - start_time
pass_time = int(pass_time)
# End Message
print(f"Process has been finished. \n"
f"Now open {excel_path} and copy A and B columns than paste it to the 'weights_2.xlsx' file.")
if pass_time >= 60:
print(f"Duration was: {pass_time/60} minutes.")
else:
print(f"Duration was: {pass_time} seconds.")
input("Press Enter to close...")