Debian Codegraph @

src/ifcbimtester/bimtester/features/steps/attributes_eleclasses/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.element as eleutils
20
21 from behave import step
22
23 from bimtester import util
24 from bimtester.ifc import IfcStore
25 from bimtester.lang import _
26
27
28 @step('There are exclusively "{ifc_classes}" elements only')
29 def step_impl(context, ifc_classes):
30 only_eleclasses(context, ifc_classes)
31
32
33 @step('There are no "{ifc_class}" elements')
34 def step_impl(context, ifc_class):
35 no_eleclass(context, ifc_class)
36
37
38 @step('There are no "{ifc_class}" elements because "{reason}"')
39 def step_impl(context, ifc_class, reason):
40 no_eleclass(context, ifc_class)
41
42
43 @step('All "{ifc_class}" elements class attributes have a value')
44 def step_impl(context, ifc_class):
45 eleclass_have_class_attributes_with_a_value(context, ifc_class)
46
47
48 @step('All "{ifc_class}" elements have a name given')
49 def step_impl(context, ifc_class):
50 eleclass_has_name_with_a_value(context, ifc_class)
51
52
53 @step('All "{ifc_class}" elements have a description given')
54 def step_impl(context, ifc_class):
55 eleclass_has_description_with_a_value(context, ifc_class)
56
57
58 @step('All "{ifc_class}" elements have a name matching the pattern "{pattern}"')
59 def step_impl(context, ifc_class, pattern):
60 import re
61
62 elements = IfcStore.file.by_type(ifc_class)
63 for element in elements:
64 if not re.search(pattern, element.Name):
65 assert False
66
67
68 @step('There is an "{ifc_class}" element with a "{attribute_name}" attribute with a value of "{attribute_value}"')
69 def step_impl(context, ifc_class, attribute_name, attribute_value):
70 elements = IfcStore.file.by_type(ifc_class)
71 for element in elements:
72 if hasattr(element, attribute_name) and getattr(element, attribute_name) == attribute_value:
73 return
74 assert False
75
76
77 # ************************************************************************************************
78 # helper
79 def only_eleclasses(context, ifc_classes):
80
81 context.falseelems = []
82 context.falseguids = []
83
84 # get the list of ifc_classes
85 target_ifc_classes = ifc_classes.replace(" ", "").split(",")
86 # ToDo test if they exist in ifc standard, should be possible with ifcos
87
88 all_elements = IfcStore.file.by_type("IfcBuildingElement")
89 context.elemcount = len(all_elements)
90
91 false_elements = []
92 for elem in all_elements:
93 if elem.is_a() not in target_ifc_classes:
94 context.falseelems.append(str(elem))
95 context.falseguids.append(elem.GlobalId)
96 context.falsecount = len(context.falseelems)
97
98 # use ifc_classes in method parameter but ifc_class in string parameter
99 # be careful somehow the opposite of most other tests is tested
100 util.assert_elements(
101 ifc_classes,
102 context.elemcount,
103 context.falsecount,
104 context.falseelems,
105 message_all_falseelems=_("All {elemcount} elements in the file are not {ifc_class} elements."),
106 message_some_falseelems=_(
107 "{falsecount} of {elemcount} false_elements are not {ifc_class} elements: {falseelems}"
108 ),
109 )
110
111
112 def no_eleclass(context, ifc_class):
113
114 context.falseelems = []
115 context.falseguids = []
116
117 elements = IfcStore.file.by_type(ifc_class)
118 context.elemcount = len(IfcStore.file.by_type("IfcBuildingElement"))
119 for elem in elements:
120 context.falseelems.append(str(elem))
121 context.falseguids.append(elem.GlobalId)
122 context.falsecount = len(context.falseelems)
123
124 # be careful somehow the opposite of most other tests is tested
125 util.assert_elements(
126 ifc_class,
127 context.elemcount,
128 context.falsecount,
129 context.falseelems,
130 message_all_falseelems=_("All {elemcount} elements in the file are {ifc_class} elements."),
131 message_some_falseelems=_("{falsecount} of {elemcount} false_elements are {ifc_class} elements: {falseelems}"),
132 )
133
134
135 def eleclass_have_class_attributes_with_a_value(context, ifc_class):
136
137 from ifcopenshell.ifcopenshell_wrapper import schema_by_name
138
139 # schema = schema_by_name("IFC2X3")
140 schema = schema_by_name(IfcStore.file.schema)
141 class_attributes = []
142 for cl_attrib in schema.declaration_by_name(ifc_class).all_attributes():
143 class_attributes.append(cl_attrib.name())
144 # print(class_attributes)
145
146 context.falseelems = []
147 context.falseguids = []
148 context.falseprops = {}
149
150 elements = IfcStore.file.by_type(ifc_class)
151 failed_attribs = []
152 for elem in elements:
153 elem_failed = False
154 for cl_attrib in class_attributes:
155 attrib_value = getattr(elem, cl_attrib)
156 if not attrib_value:
157 elem_failed = True
158 failed_attribs.append(cl_attrib)
159 # print(attrib_value)
160 if elem_failed is True:
161 context.falseelems.append(str(elem))
162 context.falseguids.append(elem.GlobalId)
163 context.falseprops[elem.id()] = failed_attribs
164
165 context.elemcount = len(elements)
166 context.falsecount = len(context.falseelems)
167 util.assert_elements(
168 ifc_class,
169 context.elemcount,
170 context.falsecount,
171 context.falseelems,
172 message_all_falseelems=_(
173 "For all {elemcount} {ifc_class} elements at least one of these class attributes {parameter} has no value."
174 ),
175 message_some_falseelems=_(
176 "For the following {falsecount} out of {elemcount} {ifc_class} elements at least one of these class attributes {parameter} has no value: {falseelems}"
177 ),
178 message_no_elems=_("There are no {ifc_class} elements in the IFC file."),
179 parameter=failed_attribs,
180 )
181
182
183 def eleclass_has_name_with_a_value(context, ifc_class):
184
185 context.falseelems = []
186 context.falseguids = []
187
188 elements = IfcStore.file.by_type(ifc_class)
189 for elem in elements:
190 # print(elem.Name)
191 if not elem.Name:
192 context.falseelems.append(str(elem))
193 context.falseguids.append(elem.GlobalId)
194
195 context.elemcount = len(elements)
196 context.falsecount = len(context.falseelems)
197 util.assert_elements(
198 ifc_class,
199 context.elemcount,
200 context.falsecount,
201 context.falseelems,
202 message_all_falseelems=_("The name of all {elemcount} elements is not set."),
203 message_some_falseelems=_(
204 "The name of {falsecount} out of {elemcount} {ifc_class} elements is not set: {falseelems}"
205 ),
206 message_no_elems=_("There are no {ifc_class} elements in the IFC file."),
207 )
208
209
210 def eleclass_has_description_with_a_value(context, ifc_class):
211
212 context.falseelems = []
213 context.falseguids = []
214
215 elements = IfcStore.file.by_type(ifc_class)
216 for elem in elements:
217 # print(elem.Description)
218 if not elem.Description:
219 context.falseelems.append(str(elem))
220 context.falseguids.append(elem.GlobalId)
221
222 context.elemcount = len(elements)
223 context.falsecount = len(context.falseelems)
224 util.assert_elements(
225 ifc_class,
226 context.elemcount,
227 context.falsecount,
228 context.falseelems,
229 message_all_falseelems=_("The description of all {elemcount} elements is not set."),
230 message_some_falseelems=_(
231 "The description of {falsecount} out of {elemcount} {ifc_class} elements is not set: {falseelems}"
232 ),
233 message_no_elems=_("There are no {ifc_class} elements in the IFC file."),
234 )