|
54 | 54 | """----------------------FUNCTION----------------------------""" |
55 | 55 |
|
56 | 56 |
|
57 | | -def FilterElementExistant(document, lstEleId): |
58 | | - """Filter List Element Existent or List Element non-Existent""" |
59 | | - lstEle = [] |
60 | | - lstNoEle = [] |
61 | | - |
62 | | - for id in lstEleId: |
63 | | - |
64 | | - ele = document.GetElement(id) |
65 | | - if ele: |
66 | | - lstEle.append(ele) |
67 | | - else: |
68 | | - lstNoEle.append(id) |
69 | | - |
70 | | - lstName = [e.Name for e in lstEle] |
71 | | - |
72 | | - return lstEle, lstNoEle, lstName |
73 | | - |
74 | | - |
75 | | -def ZoomAllElement(document, lstEleId, linkSelected): |
76 | | - """Zoom all Element. I will not use it for now""" |
77 | | - |
78 | | - validElements = [] |
79 | | - for elemId in lstEleId: |
80 | | - elem = document.GetElement(elemId) # document here could be "doc" or "doclink" |
81 | | - if elem and hasattr(elem, "get_BoundingBox") and elem.get_BoundingBox(None): |
82 | | - validElements.append(elem) |
83 | | - |
84 | | - if not validElements: |
85 | | - ShowNotification("Warning", "No valid elements found with BoundingBox.") |
86 | | - |
87 | | - # Gộp BoundingBox |
88 | | - firstBox = validElements[0].get_BoundingBox(None) |
89 | | - bboxMin = firstBox.Min |
90 | | - bboxMax = firstBox.Max |
91 | | - |
92 | | - for elem in validElements[1:]: |
93 | | - bbox = elem.get_BoundingBox(None) |
94 | | - if bbox: |
95 | | - bboxMin = XYZ( |
96 | | - min(bboxMin.X, bbox.Min.X), |
97 | | - min(bboxMin.Y, bbox.Min.Y), |
98 | | - min(bboxMin.Z, bbox.Min.Z), |
99 | | - ) |
100 | | - bboxMax = XYZ( |
101 | | - max(bboxMax.X, bbox.Max.X), |
102 | | - max(bboxMax.Y, bbox.Max.Y), |
103 | | - max(bboxMax.Z, bbox.Max.Z), |
104 | | - ) |
105 | | - |
106 | | - if not bboxMin or not bboxMax: |
107 | | - ShowNotification("Warning", "No valid BoundingBox found for selected elements.") |
108 | | - |
109 | | - transform = linkSelected.GetTotalTransform() |
110 | | - try: |
111 | | - globalPoint1 = transform.OfPoint(bboxMin) |
112 | | - globalPoint2 = transform.OfPoint(bboxMax) |
113 | | - |
114 | | - # Sử dụng hàm isFinite để kiểm tra tính hợp lệ |
115 | | - if not (isFinite(globalPoint1.X) and isFinite(globalPoint1.Y) and isFinite( |
116 | | - globalPoint1.Z) and |
117 | | - isFinite(globalPoint2.X) and isFinite(globalPoint2.Y) and isFinite(globalPoint2.Z)): |
118 | | - raise ValueError("Invalid BoundingBox values detected.") |
119 | | - |
120 | | - uiview.ZoomAndCenterRectangle(globalPoint1, globalPoint2) |
121 | | - except Exception as ex: |
122 | | - ShowNotification("Error", "Zoom failed due to invalid BoundingBox: {}".format(ex)) |
123 | | - |
124 | | - |
125 | | -# Define utility function |
126 | | -def isFinite(value): |
127 | | - """Kiểm tra xem giá trị có phải là số hợp lệ (finite) hay không.""" |
128 | | - if value is None: |
129 | | - return False |
130 | | - if isinstance(value, (float, int)): |
131 | | - return not (math.isnan(value) or math.isinf(value)) |
132 | | - return False |
133 | | - |
134 | 57 |
|
135 | 58 | """----------------------MAIN CODE----------------------------""" |
136 | 59 |
|
137 | 60 | try: |
138 | 61 |
|
139 | | - collectModelLink = FilteredElementCollector(doc).OfClass(RevitLinkInstance).ToElements() |
140 | | - |
141 | | - # Get linked model elements and their names |
142 | | - modelLink = [link for link in collectModelLink] |
143 | | - nameModelLink = [link.Name for link in modelLink] if modelLink else ["There is no Link Model"] |
144 | | - |
145 | | - """------------RUN FORM----------""" |
146 | | - openForm = True |
147 | | - while openForm: |
148 | | - f = MainForm(nameModelLink) |
149 | | - f.ShowDialog() |
150 | | - |
151 | | - # User cancel |
152 | | - if f.DialogResult != System.Windows.Forms.DialogResult.OK: |
153 | | - break |
154 | | - |
155 | | - elif f.DialogResult == System.Windows.Forms.DialogResult.OK: |
156 | | - |
157 | | - radioLinkModel = f._linkBtn.Checked |
158 | | - radioInModel = f._modelBtn.Checked |
159 | | - |
160 | | - lstId = f._textBox1.Text.strip().split(",") |
161 | | - lstId = list(set(lstId)) |
162 | | - |
163 | | - lstEleId = [ElementId(int(id.strip())) for id in lstId] |
164 | | - lstEle = [doc.GetElement(id) for id in lstEleId] |
165 | | - lstICollection = List[ElementId](lstEleId) |
166 | | - |
167 | | - linkModel = f._comboBox1.Text |
168 | | - |
169 | | - if radioInModel: |
170 | | - lstEle, lstNoEle, lstName = FilterElementExistant(doc, lstEleId) |
171 | | - |
172 | | - if not lstEle: |
173 | | - ShowNotification("Warning", "No elements found with the given IDs.") |
174 | | - continue |
175 | | - |
176 | | - |
177 | | - else: |
178 | | - ShowDataForm(doc, lstEleId) |
179 | | - selection.SetElementIds(lstICollection) |
180 | | - openForm = False |
181 | | - |
182 | | - |
183 | | - |
184 | | - elif radioLinkModel: |
185 | | - |
186 | | - if linkModel == "There is no Link Model": |
187 | | - ShowNotification("Warning", "There is no Link Model, please select a valid option.") |
188 | | - continue |
189 | | - else: |
190 | | - # Tìm liên kết được chọn |
191 | | - linkSelected = next((link for link in modelLink if link.Name == linkModel), None) |
192 | | - |
193 | | - # Lấy Document của file liên kết |
194 | | - docLink = linkSelected.GetLinkDocument() |
195 | | - |
196 | | - lstEle, lstNoEle, lstName = FilterElementExistant(docLink, lstEleId) |
197 | | - |
198 | | - # Hiển thị thông báo nếu không tìm thấy phần tử nào |
199 | | - if not lstEle: |
200 | | - ShowNotification("Warning", "No elements found with the given IDs.") |
201 | | - continue |
202 | | - |
203 | | - |
204 | | - else: |
205 | | - ShowDataForm(docLink, lstEleId) |
206 | | - openForm = False |
207 | | - |
208 | 62 |
|
209 | 63 |
|
210 | 64 | # Handle the case when the user cancels the operation |
|
0 commit comments