-
Notifications
You must be signed in to change notification settings - Fork 2
/
gbxmlMerge.py
199 lines (153 loc) · 5.75 KB
/
gbxmlMerge.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# -*- coding: utf-8 -*-
"""
Spyder Editor
Merges two gbXML files exported from Revit. First gbXML based on Revit masses does not include openings.
Second gbXML based on Revit spaces includes openings. Openings within variable 'dist' parameter are
take from gbXML based on Revit spaces and merged with gbXML based on Revit masses.
"""
# Topologic on PyPi: https://test.pypi.org/project/topologicpy/
# import: modules
from lxml import etree
from xgbxml import get_parser
import matplotlib.pyplot as plt
from copy import copy
# import topologicpy.bin.topologic.topologic as tp
from topologicpy import topologic as tp # proven to be the most reliable method of importing 'topologic'
# print(dir(tp.FaceUtility)) # troubleshooting of topologic module path(s)
# import: gui modules
from tkinter import ttk
from tkinter import *
from tkinter.ttk import *
from tkinter import filedialog
Tk().withdraw() # hides command line window
# # define: file variables: sandbox project
# fpa = "./topologic/Input/01_Simplified/Wall Interior/Simple Building_Separation Lines_Wall Interior_Mass.xml"
# fpb = "./topologic/Input/01_Simplified/Wall Interior/Simple Building_Separation Lines_Wall Interior_Space.xml"
# fpo = "./topologic/Output/01_Simplified/Simple Building_Separation Lines_Wall Interior.xml"
# define: file variables with gui
fpa = filedialog.askopenfilename(title="gbXML Without Openings", filetypes=[("xml","*.xml")])
fpb = filedialog.askopenfilename(title="gbXML With Openings", filetypes=[("xml","*.xml")])
fpo = filedialog.asksaveasfilename(defaultextension='.xml', filetypes=[("xml","*.xml")])
# set: distance tolerance of opening from surface in gbXML length units (typically the thickness of the roof or wall)
dist = 1.0
# use: xgbxml to generate a lxml parser / read: gbXML version from input file
tree_parser=etree.parse(fpa)
gbxml=tree_parser.getroot()
parser=get_parser(version=gbxml.attrib['version'])
# parser=get_parser(version='0.37')
# open: the file using the lxml parser
tree_A = etree.parse(fpa,parser)
gbxml_A = tree_A.getroot()
# # render: the gbXML etree
# ax = gbxml_A.Campus.render()
# ax.figure.set_size_inches(8, 8)
# ax.set_title('gbXML A_Geometry.xml')
# plt.show()
# open: the file using the lxml parser
tree_B = etree.parse(fpb,parser)
gbxml_B = tree_B.getroot()
# # render: the gbXML etree
# ax = gbxml_B.Campus.render()
# ax.figure.set_size_inches(8, 8)
# ax.set_title('gbXML B_Openings.xml')
# plt.show()
# make: a copy of gbxml_A named gbxml_C
gbxml_C = copy(gbxml_A)
etree_C = etree.ElementTree(gbxml_C)
# define: topologicpy faceByVertices (Wassim Jabi) - 27MAY22
def faceByVertices(vertices):
vertices
edges = []
for i in range(len(vertices)-1):
v1 = vertices[i]
v2 = vertices[i+1]
try:
e = tp.Edge.ByStartVertexEndVertex(v1, v2)
if e:
edges.append(e)
except:
continue
v1 = vertices[-1]
v2 = vertices[0]
# print("V1:", v1.X(), v1.Y(), v1.Z())
# print("V2:", v2.X(), v2.Y(), v2.Z())
try:
e = tp.Edge.ByStartVertexEndVertex(v1, v2)
if e:
edges.append(e)
except:
print("Edge creation failed!")
pass
# print("I managed to create",len(edges),"edges")
if len(edges) >= 3:
c = tp.Cluster.ByTopologies(edges, False)
w = c.SelfMerge()
if w.Type() == tp.Wire.Type() and w.IsClosed():
f = tp.Face.ByExternalBoundary(w)
else:
raise Exception("Error: Could not get a valid wire")
else:
raise Exception("Error: could not get a valid number of edges")
return f
# get: gbxml_B openings (ops)
# make: Topologic opening centroids from gbxml_B openings (ocs)
ops = []
ocs = []
for op in gbxml_B.Campus.Surfaces.Openings:
ops.append(op)
o = []
for c in op.PlanarGeometry.get_coordinates():
o.append(tp.Vertex.ByCoordinates(c[0],c[1],c[2]))
ocs.append(tp.Topology.Centroid(faceByVertices(o)))
# get: gbxml_C exterior surfaces (exsu)
# make: Topologic suface faces (sfs) from gbxml_C surfaces
exsu = []
sfs = []
for su in gbxml_C.Campus.Surfaces:
if su.get_attribute('surfaceType') in ['ExteriorWall', 'Roof']:
# if su.get_attribute('surfaceType') in ['Roof']:
exsu.append(su)
s = []
for c in su.PlanarGeometry.get_coordinates():
s.append(tp.Vertex.ByCoordinates(c[0],c[1],c[2]))
sfs.append(faceByVertices(s))
# test: gbxml_B Topologic opening centroid (ocs) IsInside(face,point,tolerance) of gbxml_C Topologic surface face (sfs) (vin)
vin = []
for oc in ocs:
r = []
for sf in sfs:
r.append(tp.FaceUtility.IsInside(sf,oc,dist))
vin.append(r)
# # qa: count number of true responses per opening vertex
# count = []
# for v in vin:
# count.append(v.count(True))
# get: indices of gbxml_C surfaces with gbxml_B opening centroid isinside (sfoc)
sfoc = []
for v in vin:
if True in v:
sfoc.append(v.index(True))
else:
sfoc.append(False)
# insert: gbxml_B opening into gbxml_C surface object if opening within variable 'dist' parameter
i = 0
for sf in sfoc:
if sf==False:
i+=1
else:
try:
exsu[sf].insert(3, exsu[sf].copy_opening(ops[i],tolerance=dist)) # copy_opening is xgbxml method
i+=1
except ValueError:
print('Caught ValueError. Check opening: ' + ops[i].Name.text + '.')
i+=1
except Exception:
print('Caught Exception. Check opening: ' + ops[i].Name.text + '.')
i+=1
# render: the gbXML etree
ax = gbxml_C.Campus.render()
ax.figure.set_size_inches(8, 8)
ax.set_title('gbXML C_Composite.xml')
plt.show()
# write: the gbXML_C etree to a local file
etree_C.write(fpo, pretty_print=True)