You can now see and edit WCS in the Blender UI (new API for it too)

This commit is contained in:
Dion Moult
2024-06-23 22:23:39 +10:00
parent 03ee826e67
commit 95d2d0af83
11 changed files with 314 additions and 17 deletions
@@ -26,6 +26,7 @@ map coordinates and project local engineering coordinates.
from .. import wrap_usecases
from .add_georeferencing import add_georeferencing
from .edit_georeferencing import edit_georeferencing
from .edit_wcs import edit_wcs
from .remove_georeferencing import remove_georeferencing
wrap_usecases(__path__, __name__)
@@ -33,5 +34,6 @@ wrap_usecases(__path__, __name__)
__all__ = [
"add_georeferencing",
"edit_georeferencing",
"edit_wcs",
"remove_georeferencing",
]
@@ -0,0 +1,85 @@
# IfcOpenShell - IFC toolkit and geometry engine
# Copyright (C) 2021 Dion Moult <dion@thinkmoult.com>
#
# This file is part of IfcOpenShell.
#
# IfcOpenShell is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# IfcOpenShell 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
import ifcopenshell
import ifcopenshell.util.element
import ifcopenshell.util.geolocation
import numpy as np
from math import sin, cos, radians
def edit_wcs(
file: ifcopenshell.file,
x: float = 0.0,
y: float = 0.0,
z: float = 0.0,
rotation: float = 0.0,
is_si: bool = True,
) -> None:
"""Edits the WCS for all geometric contexts to a translation and rotation
It's recommended to leave the WCS at 0,0,0. You should generally not be
setting it to any value. Instead, your project's origin should use a
coordinate operation to convert to the projected CRS.
:param x: The X translation of the WCS
:param y: The Y translation of the WCS
:param z: The Z translation of the WCS
:param rotation: The rotation around the Z axis (i.e. top down plan view)
in decimal degrees of the WCS. Anticlockwise is positive.
Example:
.. code:: python
# This is the simplest scenario, resetting the WCS to 0,0,0 with no rotation (recommended)
ifcopenshell.api.run("georeference.edit_wcs", model)
"""
unit_scale = ifcopenshell.util.unit.calculate_unit_scale(file)
if np.isclose(rotation, 0):
xaxis_x = 1.0
xaxis_y = 0.0
else:
xaxis_x = cos(radians(rotation))
xaxis_y = sin(radians(rotation))
if np.allclose((x, y, z), (0, 0, 0)):
x = y = z = 0.0
for context in file.by_type("IfcGeometricRepresentationContext", include_subtypes=False):
old_wcs = context.WorldCoordinateSystem
if context.CoordinateSpaceDimension == 3:
if is_si:
point = file.createIfcCartesianPoint((x / unit_scale, y / unit_scale, z / unit_scale))
else:
point = file.createIfcCartesianPoint((x, y, z))
placement = file.createIfcAxis2Placement3D(
point,
file.createIfcDirection((0.0, 0.0, 1.0)),
file.createIfcDirection((xaxis_x, xaxis_y, 0.0)),
)
elif context.CoordinateSpaceDimension == 2:
if is_si:
point = file.createIfcCartesianPoint((x / unit_scale, y / unit_scale))
else:
point = file.createIfcCartesianPoint((x, y))
placement = file.createIfcAxis2Placement2D(
point,
file.createIfcDirection((xaxis_x, xaxis_y)),
)
context.WorldCoordinateSystem = placement
if file.get_total_inverses(old_wcs) == 0:
ifcopenshell.util.element.remove_deep2(file, old_wcs)
@@ -0,0 +1,47 @@
# IfcOpenShell - IFC toolkit and geometry engine
# Copyright (C) 2022 Dion Moult <dion@thinkmoult.com>
#
# This file is part of IfcOpenShell.
#
# IfcOpenShell is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# IfcOpenShell 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
import numpy as np
import test.bootstrap
import ifcopenshell.api.root
import ifcopenshell.api.context
import ifcopenshell.api.georeference
import ifcopenshell.util.geolocation
class TestEditWCS(test.bootstrap.IFC4):
def test_editing_wcs(self):
ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcProject")
ifcopenshell.api.context.add_context(self.file, "Model")
ifcopenshell.api.context.add_context(self.file, "Plan")
ifcopenshell.api.georeference.edit_wcs(self.file)
wcs = ifcopenshell.util.geolocation.get_wcs(self.file)
m = np.eye(4)
assert np.allclose(wcs, m)
ifcopenshell.api.georeference.edit_wcs(self.file, x=1, y=2, z=3)
m[:, 3] = [1, 2, 3, 1]
wcs = ifcopenshell.util.geolocation.get_wcs(self.file)
assert np.allclose(wcs, m)
ifcopenshell.api.georeference.edit_wcs(self.file, x=1, y=2, z=3, rotation=90)
m[:, 0] = [0, 1, 0, 0]
m[:, 1] = [-1, 0, 0, 0]
wcs = ifcopenshell.util.geolocation.get_wcs(self.file)
assert np.allclose(wcs, m)