Skip to content

Commit

Permalink
fix(writer): fix writing shade objects and add support for ShadeMeshes
Browse files Browse the repository at this point in the history
  • Loading branch information
mostaphaRoudsari committed Nov 2, 2024
1 parent a137cf7 commit 6f32b68
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 22 deletions.
70 changes: 54 additions & 16 deletions honeybee_idaice/shade.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List, Union

from honeybee.model import Shade
from honeybee.model import Shade, ShadeMesh
from ladybug_geometry.geometry3d import Point3D, Face3D, Polyface3D, Mesh3D


Expand All @@ -14,13 +14,14 @@ def _vertices_to_idm(vertices: List[Point3D], dec_places: int = 3) -> str:


def _shade_geometry_to_idm(
geometry: Union[Face3D, Polyface3D], name: str, decimal_places: int = 3
geometry: Union[Face3D, Polyface3D, Mesh3D],
name: str, decimal_places: int = 3
):
"""Create an IDM shade block from a Ladybug geometry.
Here is an example:
((AGGREGATE :N "shade1" :T PICT3D)
((AGGREGATE-VTK :N "shade1" :T PICT3D)
(:PAR :N FILE :V "")
(:PAR :N POS :V #(0 0 0.0))
(:PAR :N SHADOWING :V :TRUE)
Expand All @@ -37,6 +38,8 @@ def _shade_geometry_to_idm(

if isinstance(geometry, Face3D):
mesh_3d = geometry.triangulated_mesh3d
elif isinstance(geometry, Mesh3D):
mesh_3d = geometry
else:
# it is a Polyface3D
meshes = [face.triangulated_mesh3d for face in geometry.faces]
Expand All @@ -52,18 +55,19 @@ def _shade_geometry_to_idm(
joined_faces = ' '.join(' '.join(str(f) for f in ff) for ff in faces)

shd_verts = _vertices_to_idm(vertices, decimal_places)
shade = f' ((AGGREGATE :N "{name}" :T PICT3D)\n' \
' (:PAR :N FILE :V "")\n' \
' (:PAR :N SHADOWING :V :TRUE)\n' \
' ((AGGREGATE :N "geom1" :T GEOM3D)\n' \
f' (:PAR :N NPOINTS :V {vertices_count})\n' \
f' (:PAR :N POINTS :DIM ({vertices_count} 3) :V #2A({shd_verts}))\n' \
' (:PAR :N CELLTYPE :V 1)\n' \
f' (:PAR :N NCELLS :V {face_count})\n' \
f' (:PAR :N NVERTICES :DIM ({face_count}) :V #({faces_count}))\n' \
f' (:PAR :N TOTNVERTS :V {total_vertices})\n' \
f' (:PAR :N VERTICES :DIM ({total_vertices}) :V #({joined_faces}))\n' \
' (:PAR :N PROPERTY :V #(1.0 1.0 1.0 0.699999988079071 1.0 1.0 1.0 0.5 1.0 1.0 1.0 0.0 1.0 1.0 1.0 0.0 0.0))))'
shade = f' ((AGGREGATE-VTK :N "{name}" :T PICT3D)\n' \
' (:PAR :N FILE :V "")\n' \
' (:PAR :N SHADOWING :V :TRUE)\n' \
' ((AGGREGATE :N "geom1" :T GEOM3D)\n' \
f' (:PAR :N NPOINTS :V {vertices_count})\n' \
f' (:PAR :N POINTS :DIM ({vertices_count} 3) :V #2A({shd_verts}))\n' \
' (:PAR :N CELLTYPE :V 1)\n' \
f' (:PAR :N NCELLS :V {face_count})\n' \
f' (:PAR :N NVERTICES :DIM ({face_count}) :V #({faces_count}))\n' \
f' (:PAR :N TOTNVERTS :V {total_vertices})\n' \
f' (:PAR :N VERTICES :DIM ({total_vertices}) :V #({joined_faces}))\n' \
' (:PAR :N PROPERTY :V #(1.0 1.0 1.0 0.699999988079071 1.0 1.0 1.0 0.5 1.0 1.0 1.0 0.0 1.0 1.0 1.0 0.0 0.0)))\n' \
f' )'

return shade

Expand Down Expand Up @@ -141,4 +145,38 @@ def shades_to_idm(shades: List[Shade], tolerance: float, decimal_places: int = 3
for shades in filtered_groups.values()]
)

return f'((AGGREGATE :N ARCDATA)\n{single_shades}\n{group_shades})'
return f'{single_shades}\n{group_shades}\n'


def shade_meshes_to_idm(shades: List[ShadeMesh], tolerance: float, decimal_places: int = 3):
"""Convert a list of Shades to a IDM string.
Args:
shades: A list of Honeybee ShadeMeshes.
tolerance: The maximum difference between X, Y, and Z values at which point
vertices are considered distinct from one another.
decimal_places: An integer for the number of decimal places to which
coordinate values will be rounded. (Default: 3).
Returns:
A formatted string that represents this shade in IDM format.
"""
if not shades:
return ''

shade_idms = []
for shade in shades:
shade.triangulate_and_remove_degenerate_faces(tolerance)
name = '_'.join(
(
' '.join(shade.display_name.split()),
shade.identifier.replace('Shade_', '')
)
)

shade_idms.append(
_shade_geometry_to_idm(shade.geometry, name, decimal_places)
)

return '\n'.join(shade_idms)
2 changes: 0 additions & 2 deletions honeybee_idaice/templates/building.idm
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,6 @@
(:PAR :N TRADER :V "Facility")
(:PAR :N METER-ROLE :V ENVIRONMENT)
(:PAR :N COLOR :V #S(RGB RED 33 GREEN 186 BLUE 200)))
((SITE-OBJECT :N SITE)
(:PAR :N SITE-AREA :V #(-100.0 -80.0 150.0 100.0)))
((SIMULATION_DATA :N SIMULATION_DATA)
((SIMULATION_PHASE :N STARTUP-PHASE)
(:PAR :N FROM-TIME :V 3911846400)
Expand Down
22 changes: 18 additions & 4 deletions honeybee_idaice/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .archive import zip_folder_to_idm
from .bldgbody import section_to_idm, MAX_FLOOR_ELEVATION_DIFFERENCE, \
IDA_ICE_BUILDING_BODY_TOL
from .shade import shades_to_idm
from .shade import shades_to_idm, shade_meshes_to_idm
from .face import face_to_idm, opening_to_idm, face_reference_plane


Expand Down Expand Up @@ -472,6 +472,23 @@ def model_to_idm(
line = line[1:]
bldg.write(line)

# site object
site_idm = '((SITE-OBJECT :N SITE)\n' \
' (:PAR :N SITE-AREA :V #(-100.0 -80.0 150.0 100.0))\n'
bldg.write(site_idm)
has_shade = model.shade_meshes or model.shades
if has_shade:
bldg.write(' ((AGGREGATE :N ARCDATA)\n')
# add shades to building if any
shades_idm = shades_to_idm(model.shades, model.tolerance, dec_count)
bldg.write(shades_idm)
shades_idm = shade_meshes_to_idm(model.shade_meshes, model.tolerance, dec_count)
bldg.write(shades_idm)
# end of site object
if has_shade:
bldg.write('))\n')
else:
bldg.write(')\n')
# create a building sections/bodies for the building
sections = section_to_idm(
model, max_int_wall_thickness=max_int_wall_thickness,
Expand All @@ -483,9 +500,6 @@ def model_to_idm(
for room in model.rooms:
bldg.write(f'((CE-ZONE :N "{room.display_name}" :T ZONE))\n')

# add shades to building
shades_idm = shades_to_idm(model.shades, model.tolerance, dec_count)
bldg.write(shades_idm)
bldg.write(f'\n;[end of {bldg_name}.idm]\n')

# copy all the template files
Expand Down

0 comments on commit 6f32b68

Please sign in to comment.