Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(serialize): Check for OpenStudio Installation before gbXML Import #264

Merged
merged 1 commit into from Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file modified honeybee_grasshopper_core/icon/HB Dump gbXML.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified honeybee_grasshopper_core/icon/HB Load gbXML.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions honeybee_grasshopper_core/json/HB_Dump_gbXML.json
@@ -1,5 +1,5 @@
{
"version": "1.3.0",
"version": "1.3.1",
"nickname": "DumpGBXML",
"outputs": [
[
Expand Down Expand Up @@ -50,7 +50,7 @@
}
],
"subcategory": "3 :: Serialize",
"code": "\ntry: # import the core honeybee dependencies\n from honeybee.model import Model\n from honeybee.config import folders\nexcept ImportError as e:\n raise ImportError('\\nFailed to import honeybee:\\n\\t{}'.format(e))\n\ntry: # import the honeybee_energy dependencies\n from honeybee_energy.run import to_gbxml_osw, run_osw, add_gbxml_space_boundaries\nexcept ImportError as e:\n raise ImportError('\\nFailed to import honeybee_energy:\\n\\t{}'.format(e))\n\ntry: # import the core ladybug_{{cad}} dependencies\n from ladybug_{{cad}}.{{plugin}} import all_required_inputs\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug_{{cad}}:\\n\\t{}'.format(e))\n\nimport os\n\n\nif all_required_inputs(ghenv.Component) and _dump:\n # check the input and set the component defaults\n assert isinstance(_model, Model), \\\n 'Excpected Honeybee Model object. Got {}.'.format(type(_model))\n name = _name_ if _name_ is not None else _model.identifier\n lower_name = name.lower()\n gbxml_file = name if lower_name.endswith('.xml') or lower_name.endswith('.gbxml') \\\n else '{}.xml'.format(name)\n folder = _folder_ if _folder_ is not None else folders.default_simulation_folder\n gbxml = os.path.join(folder, gbxml_file)\n\n # write out the HBJSON and OpenStudio Workflow (OSW) that translates models to gbXML\n out_directory = os.path.join(folders.default_simulation_folder, 'temp_translate')\n if not os.path.isdir(out_directory):\n os.makedirs(out_directory)\n hb_file = _model.to_hbjson(name, out_directory, included_prop=['energy'])\n osw = to_gbxml_osw(hb_file, gbxml, out_directory)\n\n # run the measure to translate the model JSON to an openstudio measure\n osm, idf = run_osw(osw, silent=True)\n if idf is None:\n raise Exception('Running OpenStudio CLI failed.')\n\n # add in the space boundary geometry if the user has requested it\n if full_geo_:\n add_gbxml_space_boundaries(gbxml, _model)\n",
"code": "\nimport os\n\ntry: # import the core honeybee dependencies\n from honeybee.model import Model\n from honeybee.config import folders\nexcept ImportError as e:\n raise ImportError('\\nFailed to import honeybee:\\n\\t{}'.format(e))\n\ntry: # import the honeybee_energy dependencies\n from honeybee_energy.run import to_gbxml_osw, run_osw, add_gbxml_space_boundaries\nexcept ImportError as e:\n raise ImportError('\\nFailed to import honeybee_energy:\\n\\t{}'.format(e))\n\ntry:\n from lbt_recipes.version import check_openstudio_version\nexcept ImportError as e:\n raise ImportError('\\nFailed to import lbt_recipes:\\n\\t{}'.format(e))\n\ntry: # import the core ladybug_{{cad}} dependencies\n from ladybug_{{cad}}.{{plugin}} import all_required_inputs\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug_{{cad}}:\\n\\t{}'.format(e))\n\n\nif all_required_inputs(ghenv.Component) and _dump:\n # check the presence of openstudio and check that the version is compatible\n check_openstudio_version()\n\n # check the input and set the component defaults\n assert isinstance(_model, Model), \\\n 'Excpected Honeybee Model object. Got {}.'.format(type(_model))\n name = _name_ if _name_ is not None else _model.identifier\n lower_name = name.lower()\n gbxml_file = name if lower_name.endswith('.xml') or lower_name.endswith('.gbxml') \\\n else '{}.xml'.format(name)\n folder = _folder_ if _folder_ is not None else folders.default_simulation_folder\n gbxml = os.path.join(folder, gbxml_file)\n\n # write out the HBJSON and OpenStudio Workflow (OSW) that translates models to gbXML\n out_directory = os.path.join(folders.default_simulation_folder, 'temp_translate')\n if not os.path.isdir(out_directory):\n os.makedirs(out_directory)\n hb_file = _model.to_hbjson(name, out_directory, included_prop=['energy'])\n osw = to_gbxml_osw(hb_file, gbxml, out_directory)\n\n # run the measure to translate the model JSON to an openstudio measure\n osm, idf = run_osw(osw, silent=True)\n if idf is None:\n raise Exception('Running OpenStudio CLI failed.')\n\n # add in the space boundary geometry if the user has requested it\n if full_geo_:\n add_gbxml_space_boundaries(gbxml, _model)\n",
"category": "Honeybee",
"name": "HB Dump gbXML",
"description": "Dump a Honyebee Model to a gbXML file.\n_\nThe gbXML format is a common open standard used to transfer energy model geometry\nand (some) energy simulation properties from one simulation environment to another.\n_\nThe forward translators within the OpenStudio SDK are used to export all Honeybee\nmodel geometry and properties.\n-"
Expand Down
4 changes: 2 additions & 2 deletions honeybee_grasshopper_core/json/HB_Load_gbXML.json
@@ -1,5 +1,5 @@
{
"version": "1.3.0",
"version": "1.3.1",
"nickname": "LoadGBXML",
"outputs": [
[
Expand Down Expand Up @@ -29,7 +29,7 @@
}
],
"subcategory": "3 :: Serialize",
"code": "\ntry: # import the ladybug_geometry dependencies\n from ladybug_geometry.geometry3d.pointvector import Vector3D\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug_geometry:\\n\\t{}'.format(e))\n\ntry: # import the core honeybee dependencies\n from honeybee.model import Model\n from honeybee.config import folders\nexcept ImportError as e:\n raise ImportError('\\nFailed to import honeybee:\\n\\t{}'.format(e))\n\ntry: # import the core ladybug_{{cad}} dependencies\n from ladybug_{{cad}}.{{plugin}} import all_required_inputs, give_warning\n from ladybug_{{cad}}.config import conversion_to_meters, units_system, tolerance\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug_{{cad}}:\\n\\t{}'.format(e))\n\nimport os\nimport subprocess\nimport json\n\n\ndef model_units_tolerance_check(model):\n \"\"\"Convert a model to the current {{Cad}} units and check the tolerance.\n\n Args:\n model: A honeybee Model, which will have its units checked.\n \"\"\"\n # check the model units\n if model.units != units_system():\n print('Imported model units \"{}\" do not match that of the current {{Cad}} '\n 'model units \"{}\"\\nThe model is being automatically converted '\n 'to the {{Cad}} doc units.'.format(model.units, units_system()))\n model.convert_to_units(units_system())\n\n # check that the model tolerance is not too far from the {{Cad}} tolerance\n if model.tolerance / tolerance >= 100:\n msg = 'Imported Model tolerance \"{}\" is significantly coarser than the ' \\\n 'current {{Cad}} model tolerance \"{}\".\\nIt is recommended that the ' \\\n '{{Cad}} document tolerance be changed to be coarser and this ' \\\n 'component is re-run.'.format(model.tolerance, tolerance)\n print msg\n give_warning(ghenv.Component, msg)\n\n\nif all_required_inputs(ghenv.Component) and _load:\n # sense the type of file we are loading\n lower_fname = _gbxml.lower()\n if lower_fname.endswith('.xml') or lower_fname.endswith('.gbxml'):\n cmd_name = 'model-from-gbxml'\n elif lower_fname.endswith('.osm'):\n cmd_name = 'model-from-osm'\n elif lower_fname.endswith('.idf'):\n cmd_name = 'model-from-idf'\n else:\n raise ValueError('Failed to recongize the input _gbxml file type.\\n'\n 'Make sure that it has an appropriate file extension.')\n\n # Execute the honybee CLI to obtain the model JSON via CPython\n shell = True if os.name == 'nt' else False\n cmds = [folders.python_exe_path, '-m', 'honeybee_energy', 'translate',\n cmd_name, _gbxml]\n process = subprocess.Popen(cmds, stdout=subprocess.PIPE, shell=shell)\n stdout = process.communicate()\n model_dict = json.loads(stdout[0])\n\n # load the model from the JSON dictionary and convert it to {{Cad}} model units\n model = Model.from_dict(model_dict)\n model_units_tolerance_check(model)\n\n # given that most other software lets doors go to the edge, move them slightly for HB\n move_vec = Vector3D(0, 0, 0.02 / conversion_to_meters())\n for room in model.rooms:\n for face in room.faces:\n doors = face.doors\n for door in doors:\n door.move(move_vec)\n if len(doors) != 0:\n face._punched_geometry = None\n",
"code": "\nimport os\nimport subprocess\nimport json\n\ntry: # import the ladybug_geometry dependencies\n from ladybug_geometry.geometry3d.pointvector import Vector3D\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug_geometry:\\n\\t{}'.format(e))\n\ntry: # import the core honeybee dependencies\n from honeybee.model import Model\n from honeybee.config import folders\nexcept ImportError as e:\n raise ImportError('\\nFailed to import honeybee:\\n\\t{}'.format(e))\n\ntry:\n from lbt_recipes.version import check_openstudio_version\nexcept ImportError as e:\n raise ImportError('\\nFailed to import lbt_recipes:\\n\\t{}'.format(e))\n\ntry: # import the core ladybug_{{cad}} dependencies\n from ladybug_{{cad}}.{{plugin}} import all_required_inputs, give_warning\n from ladybug_{{cad}}.config import conversion_to_meters, units_system, tolerance\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug_{{cad}}:\\n\\t{}'.format(e))\n\n\ndef model_units_tolerance_check(model):\n \"\"\"Convert a model to the current {{Cad}} units and check the tolerance.\n\n Args:\n model: A honeybee Model, which will have its units checked.\n \"\"\"\n # check the model units\n if model.units != units_system():\n print('Imported model units \"{}\" do not match that of the current {{Cad}} '\n 'model units \"{}\"\\nThe model is being automatically converted '\n 'to the {{Cad}} doc units.'.format(model.units, units_system()))\n model.convert_to_units(units_system())\n\n # check that the model tolerance is not too far from the {{Cad}} tolerance\n if model.tolerance / tolerance >= 100:\n msg = 'Imported Model tolerance \"{}\" is significantly coarser than the ' \\\n 'current {{Cad}} model tolerance \"{}\".\\nIt is recommended that the ' \\\n '{{Cad}} document tolerance be changed to be coarser and this ' \\\n 'component is re-run.'.format(model.tolerance, tolerance)\n print msg\n give_warning(ghenv.Component, msg)\n\n\nif all_required_inputs(ghenv.Component) and _load:\n # check the presence of openstudio and check that the version is compatible\n check_openstudio_version()\n\n # sense the type of file we are loading\n lower_fname = _gbxml.lower()\n if lower_fname.endswith('.xml') or lower_fname.endswith('.gbxml'):\n cmd_name = 'model-from-gbxml'\n elif lower_fname.endswith('.osm'):\n cmd_name = 'model-from-osm'\n elif lower_fname.endswith('.idf'):\n cmd_name = 'model-from-idf'\n else:\n raise ValueError('Failed to recongize the input _gbxml file type.\\n'\n 'Make sure that it has an appropriate file extension.')\n\n # Execute the honybee CLI to obtain the model JSON via CPython\n shell = True if os.name == 'nt' else False\n cmds = [folders.python_exe_path, '-m', 'honeybee_energy', 'translate',\n cmd_name, _gbxml]\n process = subprocess.Popen(cmds, stdout=subprocess.PIPE, shell=shell)\n stdout = process.communicate()\n model_dict = json.loads(stdout[0])\n\n # load the model from the JSON dictionary and convert it to {{Cad}} model units\n model = Model.from_dict(model_dict)\n model_units_tolerance_check(model)\n\n # given that most other software lets doors go to the edge, move them slightly for HB\n move_vec = Vector3D(0, 0, 0.02 / conversion_to_meters())\n for room in model.rooms:\n for face in room.faces:\n doors = face.doors\n for door in doors:\n door.move(move_vec)\n if len(doors) != 0:\n face._punched_geometry = None\n",
"category": "Honeybee",
"name": "HB Load gbXML",
"description": "Load a gbXML, OSM, or IDF file as a Honeybee Model.\n_\nThe reverse translators within the OpenStudio SDK are used to import all geometry\nand boundary conditions (including adjacencies) to a Honeybee format.\n_\nNote that, while all geometry will be imported, it is possible that not all of the\nproperties assigned to this geometry will be imported, particularly if a certain\nproperty is not supported in the OpenStudio SDK. Honeybee will assign defaults\nfor missing properites and, the HBJSON format should be used whenever lossless\nfile transfer is needed.\n-"
Expand Down
14 changes: 11 additions & 3 deletions honeybee_grasshopper_core/src/HB Dump gbXML.py
Expand Up @@ -40,11 +40,13 @@

ghenv.Component.Name = 'HB Dump gbXML'
ghenv.Component.NickName = 'DumpGBXML'
ghenv.Component.Message = '1.3.0'
ghenv.Component.Message = '1.3.1'
ghenv.Component.Category = 'Honeybee'
ghenv.Component.SubCategory = '3 :: Serialize'
ghenv.Component.AdditionalHelpFromDocStrings = '4'

import os

try: # import the core honeybee dependencies
from honeybee.model import Model
from honeybee.config import folders
Expand All @@ -56,15 +58,21 @@
except ImportError as e:
raise ImportError('\nFailed to import honeybee_energy:\n\t{}'.format(e))

try:
from lbt_recipes.version import check_openstudio_version
except ImportError as e:
raise ImportError('\nFailed to import lbt_recipes:\n\t{}'.format(e))

try: # import the core ladybug_rhino dependencies
from ladybug_rhino.grasshopper import all_required_inputs
except ImportError as e:
raise ImportError('\nFailed to import ladybug_rhino:\n\t{}'.format(e))

import os


if all_required_inputs(ghenv.Component) and _dump:
# check the presence of openstudio and check that the version is compatible
check_openstudio_version()

# check the input and set the component defaults
assert isinstance(_model, Model), \
'Excpected Honeybee Model object. Got {}.'.format(type(_model))
Expand Down
18 changes: 13 additions & 5 deletions honeybee_grasshopper_core/src/HB Load gbXML.py
Expand Up @@ -31,11 +31,15 @@

ghenv.Component.Name = 'HB Load gbXML'
ghenv.Component.NickName = 'LoadGBXML'
ghenv.Component.Message = '1.3.0'
ghenv.Component.Message = '1.3.1'
ghenv.Component.Category = 'Honeybee'
ghenv.Component.SubCategory = '3 :: Serialize'
ghenv.Component.AdditionalHelpFromDocStrings = '4'

import os
import subprocess
import json

try: # import the ladybug_geometry dependencies
from ladybug_geometry.geometry3d.pointvector import Vector3D
except ImportError as e:
Expand All @@ -47,16 +51,17 @@
except ImportError as e:
raise ImportError('\nFailed to import honeybee:\n\t{}'.format(e))

try:
from lbt_recipes.version import check_openstudio_version
except ImportError as e:
raise ImportError('\nFailed to import lbt_recipes:\n\t{}'.format(e))

try: # import the core ladybug_rhino dependencies
from ladybug_rhino.grasshopper import all_required_inputs, give_warning
from ladybug_rhino.config import conversion_to_meters, units_system, tolerance
except ImportError as e:
raise ImportError('\nFailed to import ladybug_rhino:\n\t{}'.format(e))

import os
import subprocess
import json


def model_units_tolerance_check(model):
"""Convert a model to the current Rhino units and check the tolerance.
Expand All @@ -82,6 +87,9 @@ def model_units_tolerance_check(model):


if all_required_inputs(ghenv.Component) and _load:
# check the presence of openstudio and check that the version is compatible
check_openstudio_version()

# sense the type of file we are loading
lower_fname = _gbxml.lower()
if lower_fname.endswith('.xml') or lower_fname.endswith('.gbxml'):
Expand Down
Binary file modified honeybee_grasshopper_core/user_objects/HB Dump gbXML.ghuser
Binary file not shown.
Binary file modified honeybee_grasshopper_core/user_objects/HB Load gbXML.ghuser
Binary file not shown.