Files
IfcOpenShell/src/ifcopenshell-python/ifcopenshell/api/georeference/edit_wcs.py
T
Dion Moult 19a1d88970 Fix all ty diagnostics on ifcviewer-wgpu (ci-lint ty-ios + ty-bonsai)
This branch carried v0.8.0's strict `[tool.ty.rules] all = "error"` config but
not the source fixes that were made upstream to satisfy it, so both ci-lint ty
gates were failing: `poe ty-ios` reported 256 diagnostics and `poe ty-bonsai`
258. Both are now clean.

Most fixes are ported from v0.8.0 and follow two idioms: initialise a name
before a conditional that may not bind it (plus an `assert` where the invariant
is real but not provable), and close an exhaustive `if`/`elif` chain with
`else: assert False, <discriminant>`.

The branch's own newer accessors are preserved throughout - `.file`,
`.declaration`, `file.types()`, `get_max_id()` are kept rather than reverted to
`wrapped_data.*`, and non-ty upstream changes (notably the in-progress geometry
cache removal) are deliberately not pulled in.

Notable fixes that are not straight ports:

* ifcopenshell_wrapper.pyi: `entity_instance.file` was declared as
  `def file(self) -> file`, where the property name shadows the `class file`
  below it, so the annotation resolved to `Unknown`. Every `element.file` in
  the codebase was therefore unchecked. Qualifying it to `ifcopenshell.file`
  restores `.schema` to its Literal union and surfaces no new diagnostics.

* model/wall.py: a duplicated merge fragment in the void-straddle path ran an
  always-true `if void_straddles:` that read `new_opening` from the mutually
  exclusive branch (stale value, or NameError on the first iteration), followed
  by an unreachable duplicate `elif`. Removing it makes the file match v0.8.0.

* light/operator.py: upstream's own fix unpacks three targets from two values
  and raises ValueError unconditionally; corrected to `None, None, None`.

* assign_system.py, validate.py, geom/main.py: walrus-in-genexp is valid at
  runtime (PEP 572 binds in the containing scope) but ty does not model it;
  rewritten as explicit loops, matching upstream.

Verified: poe ty-ios, poe ty-bonsai, ruff check src/ nix/, black --check .,
and compileall -W error at py3.10 (ifcopenshell-python) and py3.11 (bonsai).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 19:14:39 +10:00

98 lines
3.7 KiB
Python

# 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/>.
from math import cos, radians, sin
import numpy as np
import ifcopenshell
import ifcopenshell.util.element
import ifcopenshell.util.unit
from ifcopenshell.util.shape_builder import ShapeBuilder
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
Typically, a project's local engineering origin (0, 0, 0) has a coordinate
operation (e.g. map conversion) to a projected CRS. If a WCS is provided,
the coordinate operation is relative to the WCS, not the local engineering
origin.
For example, if I have an IfcSite with a placement at (10, 0, 0) and a map
conversion of (50, 0, 0), my IfcSite's local XYZ is at (10, 0, 0) with an
ENH (Easting, Northing, Height) of (60, 0, 0).
If I then define by WCS at (15, 0, 0), my IfcSite's local XYZ is still at
(10, 0, 0) but its ENH is now at (45, 0, 0).
It's recommended to leave the WCS at 0,0,0. Please :)
: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.georeference.edit_wcs(model)
"""
unit_scale = ifcopenshell.util.unit.calculate_unit_scale(file)
builder = ShapeBuilder(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:
xyz = (x / unit_scale, y / unit_scale, z / unit_scale)
else:
xyz = (x, y, z)
placement = builder.create_axis2_placement_3d(xyz, (0.0, 0.0, 1.0), (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)),
)
else:
assert False, context
context.WorldCoordinateSystem = placement
if file.get_total_inverses(old_wcs) == 0:
ifcopenshell.util.element.remove_deep2(file, old_wcs)