Skip to content

Commit

Permalink
fix(cleanup_merge_faces): Add try...except... to merge-faces for erro…
Browse files Browse the repository at this point in the history
…r handling
  • Loading branch information
ed-p-may committed Dec 19, 2023
1 parent 06a4a4e commit 7c39282
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions PHX/from_HBJSON/cleanup_merge_faces.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from copy import copy
from collections import defaultdict
import math
from typing import List, Sequence, Tuple, Union, TypeVar
from typing import List, Sequence, Tuple, Union, TypeVar, Any

from honeybee.face import Face
from honeybee.shade import Shade
Expand All @@ -16,10 +16,12 @@
from ladybug_geometry.geometry3d.face import Face3D
from ladybug_geometry.geometry3d.plane import Plane
from ladybug_geometry.geometry3d.pointvector import Point3D, Vector3D
import ladybug_geometry.boolean as pb


# -----------------------------------------------------------------------------
# -- Geometry Functions

def cross_product(
a: Union[Sequence[float], Vector3D], b: Union[Sequence[float], Vector3D]
) -> Sequence[float]:
Expand Down Expand Up @@ -83,7 +85,7 @@ def angle_between_planes(plane1, plane2, _tolerance):


# -----------------------------------------------------------------------------
# Sorting
# Sorting HB-Faces

TFaceOrShade = TypeVar("TFaceOrShade", Face, Shade)

Expand Down Expand Up @@ -230,7 +232,7 @@ def sort_hb_faces(


# -----------------------------------------------------------------------------
# -- Merging Faces
# -- Merging HB-Faces


def _get_polygon2d_in_reference_space(
Expand Down Expand Up @@ -278,7 +280,7 @@ def _check_and_add_sub_face(
_apertures: List[Aperture],
_tolerance: float,
_angle_tolerance: float,
):
) -> None:
"""Check whether a HB-sub-face is valid for an HB-face and, if so, add it.
NOTE: this method is copied from honeybee's Grasshopper component "HB Add Subface"
Expand Down Expand Up @@ -392,7 +394,13 @@ def merge_hb_face_polygons(

# -------------------------------------------------------------------------
# -- Try and merge all the new Polygon2Ds together.
merged_polygons = Polygon2D.boolean_union_all(polygons_in_ref_space, _tolerance)
try:
merged_polygons = Polygon2D.boolean_union_all(polygons_in_ref_space, _tolerance)
except Exception as e:
msg = f"ERROR merging faces: {[f.display_name for f in _faces]} [Tolerance: {_tolerance}]"
merged_polygons = poly2ds
print(msg)
print(e)

if len(poly2ds) > 100:
print(f"merge_hb_face_polygons resulted in: {len(merged_polygons)} faces.")
Expand All @@ -402,7 +410,7 @@ def merge_hb_face_polygons(

def merge_hb_faces(
_faces: List[Face], _tolerance: float, _angle_tolerance_degrees: float
):
) -> List[Face]:
"""Merge a group of HB-Faces into the fewest number of faces possible."""

if not _faces:
Expand All @@ -418,7 +426,7 @@ def merge_hb_faces(
apertures.extend([ap.duplicate() for ap in getattr(f, "apertures", [])])

# -------------------------------------------------------------------------
# -- Merge the Polygons togther
# -- Merge the Polygons together
merged_polygons, ref_plane, ref_face = merge_hb_face_polygons(
_faces, _tolerance, _angle_tolerance_degrees
)
Expand Down Expand Up @@ -454,7 +462,7 @@ def merge_hb_faces(

# -------------------------------------------------------------------------
# -- Add the apertures back in
faces_with_apertures_ = []
faces_with_apertures_: List[Face] = []
for _face in faces:
_check_and_add_sub_face(_face, apertures, _tolerance, _angle_tolerance_degrees)
faces_with_apertures_.append(_face)
Expand Down

0 comments on commit 7c39282

Please sign in to comment.