| 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 math |
| 20 |
import numpy as np |
| 21 |
import ifcopenshell |
| 22 |
import ifcopenshell.util.element |
| 23 |
import ifcopenshell.util.placement |
| 24 |
import ifcopenshell.util.geolocation |
| 25 |
|
| 26 |
from behave import step |
| 27 |
|
| 28 |
from bimtester import util |
| 29 |
from bimtester.ifc import IfcStore |
| 30 |
from bimtester.lang import _ |
| 31 |
|
| 32 |
|
| 33 |
@step('There must be at least one "{ifc_class}" element') |
| 34 |
def step_impl(context, ifc_class): |
| 35 |
assert len(IfcStore.file.by_type(ifc_class)) >= 1, _("An element of {} could not be found").format(ifc_class) |
| 36 |
|
| 37 |
|
| 38 |
def check_ifc4_geolocation(entity_name, prop_name=None, value=None, should_assert=True): |
| 39 |
if entity_name not in IfcStore.bookmarks: |
| 40 |
has_entity = False |
| 41 |
project = IfcStore.file.by_type("IfcProject")[0] |
| 42 |
for context in project.RepresentationContexts: |
| 43 |
if entity_name == "IfcMapConversion": |
| 44 |
if ( |
| 45 |
context.is_a("IfcGeometricRepresentationContext") |
| 46 |
and context.ContextType == "Model" |
| 47 |
and context.HasCoordinateOperation |
| 48 |
): |
| 49 |
IfcStore.bookmarks[entity_name] = context.HasCoordinateOperation[0] |
| 50 |
has_entity = True |
| 51 |
elif entity_name == "IfcProjectedCRS": |
| 52 |
if ( |
| 53 |
context.is_a("IfcGeometricRepresentationContext") |
| 54 |
and context.ContextType == "Model" |
| 55 |
and context.HasCoordinateOperation |
| 56 |
and context.HasCoordinateOperation[0].TargetCRS |
| 57 |
): |
| 58 |
IfcStore.bookmarks[entity_name] = context.HasCoordinateOperation[0].TargetCRS |
| 59 |
has_entity = True |
| 60 |
if not has_entity: |
| 61 |
assert False, _("No model geometric representation contexts refer to an {}").format(entity_name) |
| 62 |
if not prop_name: |
| 63 |
return |
| 64 |
actual_value = getattr(IfcStore.bookmarks[entity_name], prop_name) |
| 65 |
if should_assert: |
| 66 |
assert actual_value == value, _('We expected a value of "{}" but instead got "{}"').format(value, actual_value) |
| 67 |
else: |
| 68 |
return actual_value |
| 69 |
|
| 70 |
|
| 71 |
@step("The project must have coordinate reference system data") |
| 72 |
def step_impl(context): |
| 73 |
if IfcStore.file.schema == "IFC2X3": |
| 74 |
for site in IfcStore.file.by_type("IfcSite"): |
| 75 |
util.assert_pset(site, "EPset_ProjectedCRS") |
| 76 |
return |
| 77 |
check_ifc4_geolocation("IfcProjectedCRS") |
| 78 |
|
| 79 |
|
| 80 |
@step('The name of the CRS must be "{coordinate_reference_name}"') |
| 81 |
def step_impl(context, coordinate_reference_name): |
| 82 |
if IfcStore.file.schema == "IFC2X3": |
| 83 |
for site in IfcStore.file.by_type("IfcSite"): |
| 84 |
util.assert_pset(site, "EPset_ProjectedCRS", "Name", coordinate_reference_name) |
| 85 |
return |
| 86 |
check_ifc4_geolocation("IfcProjectedCRS", "Name", coordinate_reference_name) |
| 87 |
|
| 88 |
|
| 89 |
@step('The description of the CRS must be "{value}"') |
| 90 |
def step_impl(context, value): |
| 91 |
if IfcStore.file.schema == "IFC2X3": |
| 92 |
for site in IfcStore.file.by_type("IfcSite"): |
| 93 |
util.assert_pset(site, "EPset_ProjectedCRS", "Description", value) |
| 94 |
return |
| 95 |
check_ifc4_geolocation("IfcProjectedCRS", "Description", value) |
| 96 |
|
| 97 |
|
| 98 |
@step('The geodetic datum must be "{coordinate_reference_name}"') |
| 99 |
def step_impl(context, coordinate_reference_name): |
| 100 |
if IfcStore.file.schema == "IFC2X3": |
| 101 |
for site in IfcStore.file.by_type("IfcSite"): |
| 102 |
util.assert_pset(site, "EPset_ProjectedCRS", "GeodeticDatum", coordinate_reference_name) |
| 103 |
return |
| 104 |
check_ifc4_geolocation("IfcProjectedCRS", "GeodeticDatum", coordinate_reference_name) |
| 105 |
|
| 106 |
|
| 107 |
@step('The vertical datum must be "{coordinate_reference_name}"') |
| 108 |
def step_impl(context, coordinate_reference_name): |
| 109 |
if IfcStore.file.schema == "IFC2X3": |
| 110 |
for site in IfcStore.file.by_type("IfcSite"): |
| 111 |
util.assert_pset(site, "EPset_ProjectedCRS", "VerticalDatum", coordinate_reference_name) |
| 112 |
return |
| 113 |
check_ifc4_geolocation("IfcProjectedCRS", "VerticalDatum", coordinate_reference_name) |
| 114 |
|
| 115 |
|
| 116 |
@step('The map projection must be "{coordinate_reference_name}"') |
| 117 |
def step_impl(context, coordinate_reference_name): |
| 118 |
if IfcStore.file.schema == "IFC2X3": |
| 119 |
for site in IfcStore.file.by_type("IfcSite"): |
| 120 |
util.assert_pset(site, "EPset_ProjectedCRS", "MapProjection", coordinate_reference_name) |
| 121 |
return |
| 122 |
check_ifc4_geolocation("IfcProjectedCRS", "MapProjection", coordinate_reference_name) |
| 123 |
|
| 124 |
|
| 125 |
@step('The map zone must be "{coordinate_reference_name}"') |
| 126 |
def step_impl(context, coordinate_reference_name): |
| 127 |
if IfcStore.file.schema == "IFC2X3": |
| 128 |
for site in IfcStore.file.by_type("IfcSite"): |
| 129 |
util.assert_pset(site, "EPset_ProjectedCRS", "MapZone", coordinate_reference_name) |
| 130 |
return |
| 131 |
check_ifc4_geolocation("IfcProjectedCRS", "MapZone", coordinate_reference_name) |
| 132 |
|
| 133 |
|
| 134 |
@step('The map unit must be "{unit}"') |
| 135 |
def step_impl(context, unit): |
| 136 |
if IfcStore.file.schema == "IFC2X3": |
| 137 |
for site in IfcStore.file.by_type("IfcSite"): |
| 138 |
util.assert_pset(site, "EPset_ProjectedCRS", "MapUnit", unit) |
| 139 |
return |
| 140 |
actual_value = check_ifc4_geolocation("IfcProjectedCRS", "MapUnit", should_assert=False) |
| 141 |
if not actual_value: |
| 142 |
assert False, _("A unit was not provided in the projected CRS") |
| 143 |
if actual_value.is_a("IfcSIUnit"): |
| 144 |
prefix = actual_value.Prefix if actual_value.Prefix else "" |
| 145 |
actual_value = prefix + actual_value.Name |
| 146 |
elif actual_value.is_a("IfcConversionBasedUnit"): |
| 147 |
actual_value = actual_value.Name |
| 148 |
assert actual_value == unit, _('We expected a value of "{}" but instead got "{}"').format(unit, actual_value) |
| 149 |
|
| 150 |
|
| 151 |
@step("The project must have coordinate transformations to convert from local to global coordinates") |
| 152 |
def step_impl(context): |
| 153 |
if IfcStore.file.schema == "IFC2X3": |
| 154 |
for site in IfcStore.file.by_type("IfcSite"): |
| 155 |
util.assert_pset(site, "EPset_MapConversion") |
| 156 |
check_ifc4_geolocation("IfcMapConversion") |
| 157 |
|
| 158 |
|
| 159 |
@step('The eastings of the model must be offset by "{number}" to derive its global coordinates') |
| 160 |
def step_impl(context, number): |
| 161 |
number = util.assert_number(number) |
| 162 |
if IfcStore.file.schema == "IFC2X3": |
| 163 |
for site in IfcStore.file.by_type("IfcSite"): |
| 164 |
util.assert_pset(site, "EPset_MapConversion", "Eastings", number) |
| 165 |
return |
| 166 |
check_ifc4_geolocation("IfcMapConversion", "Eastings", number) |
| 167 |
|
| 168 |
|
| 169 |
@step('The northings of the model must be offset by "{number}" to derive its global coordinates') |
| 170 |
def step_impl(context, number): |
| 171 |
number = util.assert_number(number) |
| 172 |
if IfcStore.file.schema == "IFC2X3": |
| 173 |
for site in IfcStore.file.by_type("IfcSite"): |
| 174 |
util.assert_pset(site, "EPset_MapConversion", "Northings", number) |
| 175 |
return |
| 176 |
check_ifc4_geolocation("IfcMapConversion", "Northings", number) |
| 177 |
|
| 178 |
|
| 179 |
@step('The height of the model must be offset by "{number}" to derive its global coordinates') |
| 180 |
def step_impl(context, number): |
| 181 |
number = util.assert_number(number) |
| 182 |
if IfcStore.file.schema == "IFC2X3": |
| 183 |
for site in IfcStore.file.by_type("IfcSite"): |
| 184 |
util.assert_pset(site, "EPset_MapConversion", "OrthogonalHeight", number) |
| 185 |
return |
| 186 |
check_ifc4_geolocation("IfcMapConversion", "OrthogonalHeight", number) |
| 187 |
|
| 188 |
|
| 189 |
@step('The model must be rotated clockwise by "{number}" to derive its global coordinates') |
| 190 |
def step_impl(context, number): |
| 191 |
number = util.assert_number(number) |
| 192 |
if IfcStore.file.schema == "IFC2X3": |
| 193 |
return check_ifc2x3_geolocation("EPset_MapConversion", "Height", number) |
| 194 |
abscissa = check_ifc4_geolocation("IfcMapConversion", "XAxisAbscissa", should_assert=False) |
| 195 |
ordinate = check_ifc4_geolocation("IfcMapConversion", "XAxisOrdinate", should_assert=False) |
| 196 |
actual_value = round(ifcopenshell.util.geolocation.xaxis2angle(abscissa, ordinate), 3) |
| 197 |
value = round(number, 3) |
| 198 |
assert actual_value == value, _('We expected a value of "{}" but instead got "{}"').format(value, actual_value) |
| 199 |
|
| 200 |
|
| 201 |
@step('The model must be scaled along the horizontal axis by "{number}" to derive its global coordinates') |
| 202 |
def step_impl(context, number): |
| 203 |
number = util.assert_number(number) |
| 204 |
if IfcStore.file.schema == "IFC2X3": |
| 205 |
for site in IfcStore.file.by_type("IfcSite"): |
| 206 |
util.assert_pset(site, "EPset_MapConversion", "Scale", number) |
| 207 |
return |
| 208 |
check_ifc4_geolocation("IfcMapConversion", "Scale", number) |
| 209 |
|
| 210 |
|
| 211 |
@step('The model must be rotated clockwise by "{number}" for true north to point up') |
| 212 |
def step_impl(context, number): |
| 213 |
number = util.assert_number(number) |
| 214 |
project = IfcStore.file.by_type("IfcProject")[0] |
| 215 |
for c in project.RepresentationContexts: |
| 216 |
if c.TrueNorth: |
| 217 |
actual_value = round( |
| 218 |
ifcopenshell.util.geolocation.yaxis2angle( |
| 219 |
c.TrueNorth.DirectionRatios[0], c.TrueNorth.DirectionRatios[1] |
| 220 |
), |
| 221 |
3, |
| 222 |
) |
| 223 |
value = round(number, 3) |
| 224 |
assert actual_value == value, _('We expected a value of "{}" but instead got "{}"').format( |
| 225 |
value, actual_value |
| 226 |
) |
| 227 |
return |
| 228 |
assert False, _("True north is not defined in the file") |
| 229 |
|
| 230 |
|
| 231 |
@step('The site "{guid}" has a longitude of "{number}"') |
| 232 |
def step_impl(context, guid, number): |
| 233 |
number = util.assert_number(number) |
| 234 |
site = util.assert_guid(IfcStore.file, guid) |
| 235 |
util.assert_type(site, "IfcSite") |
| 236 |
ref = util.assert_attribute(site, "RefLongitude") |
| 237 |
number = ifcopenshell.util.geolocation.dd2dms(number, use_ms=(len(ref) == 4)) |
| 238 |
util.assert_attribute(site, "RefLongitude", number) |
| 239 |
|
| 240 |
|
| 241 |
@step('The site "{guid}" has a latitude of "{number}"') |
| 242 |
def step_impl(context, guid, number): |
| 243 |
number = util.assert_number(number) |
| 244 |
site = util.assert_guid(IfcStore.file, guid) |
| 245 |
util.assert_type(site, "IfcSite") |
| 246 |
ref = util.assert_attribute(site, "RefLatitude") |
| 247 |
number = ifcopenshell.util.geolocation.dd2dms(number, use_ms=(len(ref) == 4)) |
| 248 |
util.assert_attribute(site, "RefLatitude", number) |
| 249 |
|
| 250 |
|
| 251 |
@step('The site "{guid}" has an elevation of "{number}"') |
| 252 |
def step_impl(context, guid, number): |
| 253 |
number = util.assert_number(number) |
| 254 |
site = util.assert_guid(IfcStore.file, guid) |
| 255 |
util.assert_type(site, "IfcSite") |
| 256 |
util.assert_attribute(site, "RefElevation", number) |
| 257 |
|
| 258 |
|
| 259 |
@step('The site "{guid}" must be coincident with the project origin') |
| 260 |
def step_impl(context, guid): |
| 261 |
site = util.assert_guid(IfcStore.file, guid) |
| 262 |
util.assert_type(site, "IfcSite") |
| 263 |
if not site.ObjectPlacement: |
| 264 |
assert False, _("The site has no object placement") |
| 265 |
site_placement = ifcopenshell.util.placement.get_local_placement(site.ObjectPlacement)[:, 3][0:3] |
| 266 |
origin = np.array([0, 0, 0]) |
| 267 |
assert np.allclose(origin, site_placement), _('The site location is at "{}" instead of "{}"') |