Skip to content

Commit

Permalink
feat(to_honeybee): Methods to transfer extension properties to_honeybee
Browse files Browse the repository at this point in the history
This new code will ensure that ProgramTypes and ConstructionSets assigned to Dragonfly objects will be transferred to Honeybee objects.
  • Loading branch information
chriswmackey authored and Chris Mackey committed Oct 27, 2019
1 parent 9fe6852 commit df87487
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- stage: deploy
if: branch = master AND (NOT type IN (pull_request))
before_install:
- npm i -g npm@6.6.0
- nvm install lts/* --latest-npm
python:
- "3.6"
install:
Expand Down
4 changes: 2 additions & 2 deletions dragonfly/building.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ def to_honeybee(self, use_multiplier=True, tolerance=None):
If None, no splitting will occur. Default: None.
"""
if use_multiplier:
hb_rooms = [room for story in self._unique_stories
for room in story.to_honeybee(True, tolerance)]
hb_rooms = [room.to_honeybee(story.multiplier, tolerance)
for story in self._unique_stories for room in story]
else:
hb_rooms = [room.to_honeybee(1, tolerance) for room in self.all_room_2ds]
return Model(self.display_name, hb_rooms)
Expand Down
38 changes: 37 additions & 1 deletion dragonfly/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
and dragonfly-energy. Note that these Property objects are not intended to exist
on their own but should have a host object.
"""
from honeybee.properties import RoomProperties


class _Properties(object):
"""Base class for all Properties classes."""
_do_not_duplicate = ('host', 'to_dict', 'ToString')
_do_not_duplicate = ('host', 'to_dict', 'to_honeybee', 'ToString')

def __init__(self, host):
"""Initialize properties.
Expand Down Expand Up @@ -52,6 +53,32 @@ def _duplicate_extension_attr(self, original_properties):
traceback.print_exc()
raise Exception('Failed to duplicate {}: {}'.format(var, e))

def _add_extension_attr_to_honeybee(self, host, honeybee_properties):
"""Add Dragonfly properties for extensions to Honeybee extension properties.
This method should be called within the to_honeybee method for any
dragonfly-core geometry object that maps directly to a honeybee-core object.
Args:
host: A honeybee-core object that hosts these properties.
honeybee_properties: A honeybee-core Properties object to which the
dragonfly-core extension attributes will be added.
"""
attr = [atr for atr in dir(self)
if not atr.startswith('_') and atr not in self._do_not_duplicate]

for atr in attr:
var = getattr(self, atr)
if not hasattr(var, 'to_honeybee'):
continue
try:
setattr(honeybee_properties, '_' + atr, var.to_honeybee(host))
except Exception as e:
import traceback
traceback.print_exc()
raise Exception('Failed to translate {} to_honeybee: {}'.format(var, e))
return honeybee_properties

def _add_extension_attr_to_dict(self, base, abridged, include):
"""Add attributes for extensions to the base dictionary.
Expand Down Expand Up @@ -217,6 +244,15 @@ def to_dict(self, abridged=False, include=None):
base = self._add_extension_attr_to_dict(base, abridged, include)
return base

def to_honeybee(self, host):
"""Convert this Room2D's extension properties to honeybee Room properties.
Args:
host: A honeybee-core Room object that hosts these properties.
"""
hb_prop = RoomProperties(host)
return self._add_extension_attr_to_honeybee(host, hb_prop)

def __repr__(self):
"""Properties representation."""
return 'Room2DProperties'
3 changes: 3 additions & 0 deletions dragonfly/room2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,9 @@ def to_honeybee(self, multiplier=1, tolerance=None):
if self._parent.is_top_floor:
hb_room[-1].boundary_condition = bcs.outdoors

# transfer any extension properties assigned to the Room2D
hb_room._properties = self.properties.to_honeybee(hb_room)

return hb_room

def to_dict(self, abridged=False, included_prop=None):
Expand Down
5 changes: 5 additions & 0 deletions tests/building_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ def test_to_honeybee():
assert len(hb_model.rooms[0][1].apertures) == 1
assert len(hb_model.rooms[0][2].apertures) == 0

hb_model = building.to_honeybee(True, 0.01)
assert len(hb_model.rooms) == 2
for room in hb_model.rooms:
assert room.multiplier == 4


def test_to_dict():
"""Test the Building to_dict method."""
Expand Down

0 comments on commit df87487

Please sign in to comment.