Debian Codegraph @

src/ifcbimtester/bimtester/features/steps/model_federation/en.py

raw
1 # BIMTester - OpenBIM Auditing Tool
2 # Copyright (C) 2021 Dion Moult <[email protected]>
3 #
4 # This file is part of BIMTester.
5 #
6 # BIMTester is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Lesser General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # BIMTester is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Lesser General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public License
17 # along with BIMTester. If not, see <http://www.gnu.org/licenses/>.
18
19 import ifcopenshell.util.geolocation
20 import ifcopenshell.util.placement
21
22 from behave import step
23
24 from bimtester import util
25 from bimtester.ifc import IfcStore
26 from bimtester.lang import _
27
28
29 def get_decimal_points(value):
30 try:
31 return len(value.split(".")[1])
32 except:
33 return 0
34
35
36 def get_containing_spatial_elements(element):
37 results = []
38 if element.is_a("IfcSpatialElement"):
39 results.append(element)
40 for rel in element.Decomposes:
41 if rel.is_a("IfcRelAggregates"):
42 results.append(get_containing_spatial_elements(rel.RelatingObject))
43 elif element.is_a("IfcElement"):
44 for rel in element.ContainedInStructure:
45 if rel.is_a("ifcRelContainedInSpatialStructure"):
46 results.append(get_containing_spatial_elements(rel.RelatingStructure))
47 return results
48
49
50 @step('There is a datum element "{guid}" as an "{ifc_class}"')
51 def step_impl(context, guid, ifc_class):
52 element = util.assert_guid(IfcStore.file, guid)
53 util.assert_type(element, ifc_class)
54
55
56 @step(
57 'The element "{guid}" has a global easting, northing, and elevation of "{easting}", "{northing}", and "{elevation}" respectively'
58 )
59 def step_impl(context, guid, easting, northing, elevation):
60 if IfcStore.file.schema == "IFC2X3":
61 if element.is_a("IfcSite"):
62 site = element
63 else:
64 potential_sites = [s for s in get_containing_spatial_elements(element) if s.is_a("IfcSite")]
65 if potential_sites:
66 site = potential_sites[0]
67 else:
68 assert False, _("The datum element does not belong to a geolocated site")
69 map_conversion = util.assert_pset(site, "EPset_MapConversion")
70 else:
71 map_conversion = IfcStore.file.by_type("IfcMapConversion")
72 if map_conversion:
73 map_conversion = map_conversion[0].get_info()
74 else:
75 assert False, _("No map conversion was found in the file")
76
77 element = util.assert_guid(IfcStore.file, guid)
78 if not element.ObjectPlacement:
79 assert False, _("The element does not have an object placement: {}").format(element)
80 m = ifcopenshell.util.placement.get_local_placement(element.ObjectPlacement)
81 e, n, h = ifcopenshell.util.geolocation.xyz2enh(
82 m[0][3],
83 m[1][3],
84 m[2][3],
85 float(map_conversion["Eastings"]),
86 float(map_conversion["Northings"]),
87 float(map_conversion["OrthogonalHeight"]),
88 float(map_conversion["XAxisAbscissa"]),
89 float(map_conversion["XAxisOrdinate"]),
90 float(map_conversion["Scale"]),
91 )
92 element_x = round(e, get_decimal_points(easting))
93 element_y = round(n, get_decimal_points(northing))
94 element_z = round(h, get_decimal_points(elevation))
95 expected_placement = (util.assert_number(easting), util.assert_number(northing), util.assert_number(elevation))
96 if (element_x, element_y, element_z) != expected_placement:
97 assert False, _("The element {} is meant to have a location of {} but instead we found {}").format(
98 element, expected_placement, (element_x, element_y, element_z)
99 )
100
101
102 @step('The element "{guid}" has a local X, Y, and Z coordinate of "{x}", "{y}", and "{z}" respectively')
103 def step_impl(context, guid, x, y, z):
104 element = util.assert_guid(IfcStore.file, guid)
105 if not element.ObjectPlacement:
106 assert False, _("The element does not have an object placement: {}").format(element)
107 m = ifcopenshell.util.placement.get_local_placement(element.ObjectPlacement)
108 element_x = round(m[0][3], get_decimal_points(x))
109 element_y = round(m[1][3], get_decimal_points(y))
110 element_z = round(m[2][3], get_decimal_points(z))
111 expected_placement = (util.assert_number(x), util.assert_number(y), util.assert_number(z))
112 if (element_x, element_y, element_z) != expected_placement:
113 assert False, _("The element {} is meant to have a location of {} but instead we found {}").format(
114 element, expected_placement, (element_x, element_y, element_z)
115 )