-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
ifc_selftest.py
387 lines (359 loc) · 14 KB
/
ifc_selftest.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# ***************************************************************************
# * *
# * Copyright (c) 2023 Yorik van Havre <[email protected]> *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU General Public License (GPL) *
# * as published by the Free Software Foundation; either version 3 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * This program is distributed in the hope that it will be useful, *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
# * GNU General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
"""Unit test for the Native IFC module"""
import os
import time
import tempfile
import FreeCAD
import Draft
import Arch
import unittest
import requests
import ifc_import
import ifc_tools
import ifc_geometry
import ifc_materials
import ifc_layers
import ifc_psets
import ifc_objects
import ifc_generator
import ifcopenshell
import difflib
IFCOPENHOUSE_IFC4 = (
"https://github.com/aothms/IfcOpenHouse/raw/master/IfcOpenHouse_IFC4.ifc"
)
IFC_FILE_PATH = None # downloaded IFC file path
FCSTD_FILE_PATH = None # saved FreeCAD file
PARAMS = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/NativeIFC")
SINGLEDOC = False # This allows to force singledoc mode for all tests
SDU = int(SINGLEDOC) # number of objects is different in singledoc
"""
unit tests for the NativeIFC functionality. To run the tests, either:
- in terminal mode: FreeCAD -t ifc_selftest
- in the FreeCAD UI: Switch to Test Framework workbench, press "Self test" and
choose ifc_selftest in the list
"""
def getIfcFilePath():
global IFC_FILE_PATH
if not IFC_FILE_PATH:
path = tempfile.mkstemp(suffix=".ifc")[1]
results = requests.get(IFCOPENHOUSE_IFC4)
with open(path, "wb") as f:
f.write(results.content)
IFC_FILE_PATH = path
return IFC_FILE_PATH
def clearObjects():
names = [o.Name for o in FreeCAD.getDocument("IfcTest").Objects]
for n in names:
FreeCAD.getDocument("IfcTest").removeObject(n)
def compare(file1, file2):
with open(file1) as f1:
f1_text = f1.readlines()
with open(file2) as f2:
f2_text = f2.readlines()
res = [
l
for l in difflib.unified_diff(
f1_text, f2_text, fromfile=file1, tofile=file2, lineterm=""
)
]
res = [l for l in res if l.startswith("+") or l.startswith("-")]
res = [l for l in res if not l.startswith("+++") and not l.startswith("---")]
return res
class NativeIFCTest(unittest.TestCase):
def setUp(self):
# setting a new document to hold the tests
if FreeCAD.ActiveDocument:
if FreeCAD.ActiveDocument.Name != "IfcTest":
FreeCAD.newDocument("IfcTest")
else:
FreeCAD.newDocument("IfcTest")
FreeCAD.setActiveDocument("IfcTest")
def tearDown(self):
FreeCAD.closeDocument("IfcTest")
pass
def test01_ImportCoinSingle(self):
FreeCAD.Console.PrintMessage(
"1. NativeIFC import: Single object, coin mode..."
)
clearObjects()
fp = getIfcFilePath()
ifc_import.insert(
fp,
"IfcTest",
strategy=0,
shapemode=1,
switchwb=0,
silent=True,
singledoc=SINGLEDOC,
)
fco = len(FreeCAD.getDocument("IfcTest").Objects)
self.failUnless(fco == 1 - SDU, "ImportCoinSingle failed")
def test02_ImportCoinStructure(self):
FreeCAD.Console.PrintMessage(
"2. NativeIFC import: Model structure, coin mode..."
)
clearObjects()
fp = getIfcFilePath()
ifc_import.insert(
fp,
"IfcTest",
strategy=1,
shapemode=1,
switchwb=0,
silent=True,
singledoc=SINGLEDOC,
)
fco = len(FreeCAD.getDocument("IfcTest").Objects)
self.failUnless(fco == 4 - SDU, "ImportCoinStructure failed")
def test03_ImportCoinFull(self):
global FCSTD_FILE_PATH
FreeCAD.Console.PrintMessage("3. NativeIFC import: Full model, coin mode...")
clearObjects()
fp = getIfcFilePath()
d = ifc_import.insert(
fp,
"IfcTest",
strategy=2,
shapemode=1,
switchwb=0,
silent=True,
singledoc=SINGLEDOC,
)
path = tempfile.mkstemp(suffix=".FCStd")[1]
d.saveAs(path)
FCSTD_FILE_PATH = path
fco = len(FreeCAD.getDocument("IfcTest").Objects)
self.failUnless(fco > 4 - SDU, "ImportCoinFull failed")
def test04_ImportShapeFull(self):
FreeCAD.Console.PrintMessage("4. NativeIFC import: Full model, shape mode...")
clearObjects()
fp = getIfcFilePath()
d = ifc_import.insert(
fp,
"IfcTest",
strategy=2,
shapemode=0,
switchwb=0,
silent=True,
singledoc=SINGLEDOC,
)
fco = len(FreeCAD.getDocument("IfcTest").Objects)
self.failUnless(fco > 4 - SDU, "ImportShapeFull failed")
def test05_ImportFreeCAD(self):
FreeCAD.Console.PrintMessage(
"5. NativeIFC FreeCAD import: NativeIFC coin file..."
)
clearObjects()
doc = FreeCAD.open(FCSTD_FILE_PATH)
obj = doc.Objects[-1]
proj = ifc_tools.get_project(obj)
ifcfile = ifc_tools.get_ifcfile(proj)
print(ifcfile)
self.failUnless(ifcfile, "ImportFreeCAD failed")
def test06_ModifyObjects(self):
FreeCAD.Console.PrintMessage("6. NativeIFC Modifying IFC document...")
doc = FreeCAD.open(FCSTD_FILE_PATH)
obj = doc.Objects[-1]
obj.Label = "Modified name"
proj = ifc_tools.get_project(obj)
proj.IfcFilePath = proj.IfcFilePath[:-4] + "_modified.ifc"
ifc_tools.save_ifc(proj)
ifc_diff = compare(IFC_FILE_PATH, proj.IfcFilePath)
obj.ShapeMode = 0
obj.Proxy.execute(obj)
self.failUnless(
obj.Shape.Volume > 2 and len(ifc_diff) == 3, "ModifyObjects failed"
)
def test07_CreateDocument(self):
FreeCAD.Console.PrintMessage("7. NativeIFC Creating new IFC document...")
doc = FreeCAD.ActiveDocument
ifc_tools.create_document(doc, silent=True)
fco = len(FreeCAD.getDocument("IfcTest").Objects)
print(FreeCAD.getDocument("IfcTest").Objects)
self.failUnless(fco == 1 - SDU, "CreateDocument failed")
def test08_ChangeIFCSchema(self):
FreeCAD.Console.PrintMessage("8. NativeIFC Changing IFC schema...")
clearObjects()
fp = getIfcFilePath()
ifc_import.insert(
fp,
"IfcTest",
strategy=2,
shapemode=1,
switchwb=0,
silent=True,
singledoc=SINGLEDOC,
)
obj = FreeCAD.getDocument("IfcTest").Objects[-1]
proj = ifc_tools.get_project(obj)
oldid = obj.StepId
proj.Proxy.silent = True
proj.Schema = "IFC2X3"
FreeCAD.getDocument("IfcTest").recompute()
self.failUnless(obj.StepId != oldid, "ChangeIFCSchema failed")
def test09_CreateBIMObjects(self):
FreeCAD.Console.PrintMessage("9. NativeIFC Creating BIM objects...")
doc = FreeCAD.ActiveDocument
proj = ifc_tools.create_document(doc, silent=True)
site = Arch.makeSite()
site = ifc_tools.aggregate(site, proj)
bldg = Arch.makeBuilding()
bldg = ifc_tools.aggregate(bldg, site)
storey = Arch.makeFloor()
storey = ifc_tools.aggregate(storey, bldg)
wall = Arch.makeWall(None, 200, 400, 20)
wall = ifc_tools.aggregate(wall, storey)
column = Arch.makeStructure(None, 20, 20, 200)
column.IfcType = "Column"
column = ifc_tools.aggregate(column, storey)
beam = Arch.makeStructure(None, 20, 200, 20)
beam.IfcType = "Beam"
beam = ifc_tools.aggregate(beam, storey)
rect = Draft.makeRectangle(200, 200)
slab = Arch.makeStructure(rect, height=20)
slab.IfcType = "Slab"
slab = ifc_tools.aggregate(slab, storey)
# TODO create door, window
fco = len(FreeCAD.getDocument("IfcTest").Objects)
ifco = len(proj.Proxy.ifcfile.by_type("IfcRoot"))
print(ifco, "IFC objects created")
self.failUnless(fco == 8 - SDU and ifco == 12, "CreateDocument failed")
def test10_ChangePlacement(self):
FreeCAD.Console.PrintMessage("10. NativeIFC Changing Placement...")
clearObjects()
fp = getIfcFilePath()
ifc_import.insert(
fp,
"IfcTest",
strategy=2,
shapemode=1,
switchwb=0,
silent=True,
singledoc=SINGLEDOC,
)
obj = FreeCAD.getDocument("IfcTest").getObject("IfcObject00" + str(4 - SDU))
elem = ifc_tools.get_ifc_element(obj)
obj.Placement.move(FreeCAD.Vector(100, 200, 300))
new_plac = ifcopenshell.util.placement.get_local_placement(elem.ObjectPlacement)
new_plac = str(new_plac).replace(" ", "").replace("\n", "")
target = "[[1.0.0.100.][0.1.0.200.][0.0.1.300.][0.0.0.1.]]"
self.failUnless(new_plac == target, "ChangePlacement failed")
def test11_ChangeGeometry(self):
FreeCAD.Console.PrintMessage("11. NativeIFC Changing Geometry...")
clearObjects()
fp = getIfcFilePath()
ifc_import.insert(
fp,
"IfcTest",
strategy=2,
shapemode=0,
switchwb=0,
silent=True,
singledoc=SINGLEDOC,
)
obj = FreeCAD.getDocument("IfcTest").getObject("IfcObject004")
ifc_geometry.add_geom_properties(obj)
obj.ExtrusionDepth = "6000 mm"
FreeCAD.getDocument("IfcTest").recompute()
self.failUnless(obj.Shape.Volume > 1500000, "ChangeGeometry failed")
def test12_RemoveObject(self):
FreeCAD.Console.PrintMessage("12. NativeIFC Remove object...")
clearObjects()
fp = getIfcFilePath()
ifc_import.insert(
fp,
"IfcTest",
strategy=2,
shapemode=0,
switchwb=0,
silent=True,
singledoc=SINGLEDOC,
)
ifcfile = ifc_tools.get_ifcfile(FreeCAD.getDocument("IfcTest").Objects[-1])
count1 = len(ifcfile.by_type("IfcProduct"))
FreeCAD.getDocument("IfcTest").removeObject("IfcObject004")
count2 = len(ifcfile.by_type("IfcProduct"))
self.failUnless(count2 < count1, "RemoveObject failed")
def test13_Materials(self):
FreeCAD.Console.PrintMessage("13. NativeIFC Materials...")
clearObjects()
fp = getIfcFilePath()
ifc_import.insert(
fp,
"IfcTest",
strategy=2,
shapemode=0,
switchwb=0,
silent=True,
singledoc=SINGLEDOC,
)
proj = FreeCAD.getDocument("IfcTest").Objects[0]
ifc_materials.load_materials(proj)
prod = FreeCAD.getDocument("IfcTest").getObject("IfcObject006")
ifcfile = ifc_tools.get_ifcfile(prod)
mats_before = ifcfile.by_type("IfcMaterialDefinition")
mat = Arch.makeMaterial("Red")
ifc_materials.set_material(mat, prod)
elem = ifc_tools.get_ifc_element(prod)
res = ifcopenshell.util.element.get_material(elem)
mats_after = ifcfile.by_type("IfcMaterialDefinition")
self.failUnless(len(mats_after) == len(mats_before) + 1, "Materials failed")
def test14_Layers(self):
FreeCAD.Console.PrintMessage("14. NativeIFC Layers...")
clearObjects()
fp = getIfcFilePath()
ifc_import.insert(
fp,
"IfcTest",
strategy=2,
shapemode=0,
switchwb=0,
silent=True,
singledoc=SINGLEDOC,
)
proj = FreeCAD.getDocument("IfcTest").Objects[0]
ifcfile = ifc_tools.get_ifcfile(proj)
lays_before = ifcfile.by_type("IfcPresentationLayerAssignment")
layer = ifc_layers.create_layer("My Layer", proj)
prod = FreeCAD.getDocument("IfcTest").getObject("IfcObject006")
ifc_layers.add_to_layer(prod, layer)
lays_after = ifcfile.by_type("IfcPresentationLayerAssignment")
self.failUnless(len(lays_after) == len(lays_before) + 1, "Layers failed")
def test15_Psets(self):
FreeCAD.Console.PrintMessage("15. NativeIFC Psets...")
clearObjects()
fp = getIfcFilePath()
ifc_import.insert(
fp,
"IfcTest",
strategy=2,
shapemode=0,
switchwb=0,
silent=True,
singledoc=SINGLEDOC,
)
obj = FreeCAD.getDocument("IfcTest").getObject("IfcObject004")
ifcfile = ifc_tools.get_ifcfile(obj)
pset = ifc_psets.add_pset(obj, "Pset_Custom")
ifc_psets.add_property(ifcfile, pset, "MyMessageToTheWorld", "Hello, World!")
self.failUnless(ifc_psets.has_psets(obj), "Psets failed")