|
2 | 2 | import click |
3 | 3 | import sys |
4 | 4 | import logging |
| 5 | +import io |
| 6 | +import base64 |
5 | 7 |
|
6 | 8 | from ladybug.commandutil import process_content_to_output |
7 | 9 | from dragonfly.model import Model |
8 | 10 | from dragonfly_trace.writer import model_to_trace700_csv as model_to_csv |
| 11 | +from dragonfly_trace.writer import model_to_trace700_workbook as model_to_workbook |
9 | 12 |
|
10 | 13 |
|
11 | 14 | _logger = logging.getLogger(__name__) |
@@ -147,3 +150,157 @@ def model_to_trace700_csv( |
147 | 150 | ) |
148 | 151 |
|
149 | 152 | return process_content_to_output(csv_str, output_file) |
| 153 | + |
| 154 | + |
| 155 | +@translate.command('model-to-trace700-xlsx') |
| 156 | +@click.argument('model-file', type=click.Path( |
| 157 | + exists=True, file_okay=True, dir_okay=False, resolve_path=True)) |
| 158 | +@click.option('--multiplier/--full-geometry', ' /-fg', help='Flag to note if the ' |
| 159 | + 'multipliers on each Building story will be passed along to the ' |
| 160 | + 'generated Honeybee Room objects or if full geometry objects should be ' |
| 161 | + 'written for each story in the building.', default=True, show_default=True) |
| 162 | +@click.option('--plenum/--separate-plenum', '-p/-sp', help='Flag to indicate whether ' |
| 163 | + 'ceiling/floor plenum depths assigned to Room2Ds should simply be ' |
| 164 | + 'reported as plenum depths in the tables or they should be used to generate ' |
| 165 | + 'distinct separated plenum rooms in the translation.', |
| 166 | + default=True, show_default=True) |
| 167 | +@click.option('--merge-method', '-m', help='Text to describe how the Room2Ds should ' |
| 168 | + 'be merged into individual Rooms during the translation. Specifying a ' |
| 169 | + 'value here can be an effective way to reduce the number of Room ' |
| 170 | + 'volumes in the resulting Model and, ultimately, yield a faster simulation ' |
| 171 | + 'time with less results to manage. Choose from: None, Zones, PlenumZones, ' |
| 172 | + 'Stories, PlenumStories.', type=str, default='None', show_default=True) |
| 173 | +@click.option('--imperial/--metric', '-ip/-si', help='Flag to note whether imperial ' |
| 174 | + 'or metric units should be used for values in the output XLSX.', |
| 175 | + default=True, show_default=True) |
| 176 | +@click.option('--geometry-ids/--geometry-names', ' /-gn', help='Flag to note whether a ' |
| 177 | + 'cleaned version of all geometry display names should be used instead ' |
| 178 | + 'of identifiers when translating the Model.', |
| 179 | + default=True, show_default=True) |
| 180 | +@click.option('--resource-ids/--resource-names', ' /-rn', help='Flag to note whether a ' |
| 181 | + 'cleaned version of all resource display names should be used instead ' |
| 182 | + 'of identifiers when translating the Model.', |
| 183 | + default=True, show_default=True) |
| 184 | +@click.option('--output-file', '-f', help='Optional XLSX file to output the content ' |
| 185 | + 'of the translation. By default it printed out to stdout.', |
| 186 | + type=click.File('wb'), default='-', show_default=True) |
| 187 | +def model_to_trace700_xlsx_cli( |
| 188 | + model_file, multiplier, plenum, merge_method, imperial, |
| 189 | + geometry_ids, resource_ids, output_file |
| 190 | +): |
| 191 | + """Translate a Dragonfly Model to an Excel file with tables for TRACE 700 attributes. |
| 192 | +
|
| 193 | + The resulting Excel tables can be copied into the tables that appear in the |
| 194 | + Component Tree view of TRACE 700. The order and organization of rooms in |
| 195 | + the resulting matrix should match that of the gbXML produced from the same model. |
| 196 | +
|
| 197 | + \b |
| 198 | + Args: |
| 199 | + model_file: Full path to a Dragonfly Model file (DFJSON or DFpkl). |
| 200 | + """ |
| 201 | + try: |
| 202 | + full_geometry = not multiplier |
| 203 | + separate_plenum = not plenum |
| 204 | + metric = not imperial |
| 205 | + geo_names = not geometry_ids |
| 206 | + res_names = not resource_ids |
| 207 | + model_to_trace700_xlsx( |
| 208 | + model_file, full_geometry, separate_plenum, merge_method, |
| 209 | + metric, geo_names, res_names, output_file |
| 210 | + ) |
| 211 | + except Exception as e: |
| 212 | + _logger.exception('System translation failed.\n{}'.format(e)) |
| 213 | + sys.exit(1) |
| 214 | + else: |
| 215 | + sys.exit(0) |
| 216 | + |
| 217 | + |
| 218 | +def model_to_trace700_xlsx( |
| 219 | + model_file, full_geometry=False, separate_plenum=False, merge_method='None', |
| 220 | + metric=False, geometry_names=False, resource_names=False, output_file=None, |
| 221 | + multiplier=True, plenum=True, imperial=True, geometry_ids=True, resource_ids=True |
| 222 | +): |
| 223 | + """Translate a Dragonfly Model to an Excel file with tables for TRACE 700 attributes. |
| 224 | +
|
| 225 | + The resulting Excel tables can be copied into the tables that appear in the |
| 226 | + Component Tree view of TRACE 700. The order and organization of rooms in |
| 227 | + the resulting matrix should match that of the gbXML produced from the same model. |
| 228 | +
|
| 229 | + Args: |
| 230 | + model: A dragonfly Model for which a TRACE 700 XLSX file will be returned. |
| 231 | + multiplier: If True, the multipliers on this Model's Stories will be |
| 232 | + passed along to the XLSX. If False, full geometry objects will be written |
| 233 | + for each and every floor in the building that are represented through |
| 234 | + multipliers and all resulting multipliers will be 1. (Default: True). |
| 235 | + separate_plenum: Boolean to indicate whether ceiling/floor plenum depths |
| 236 | + assigned to Room2Ds should simply be reported as plenum depths in the |
| 237 | + tables or they should be used to generate distinct separated plenum |
| 238 | + rooms in the translation. (Default: False). |
| 239 | + merge_method: An optional text string to describe how the Room2Ds should |
| 240 | + be merged into individual Rooms during the translation. Specifying a |
| 241 | + value here can be an effective way to reduce the number of Room |
| 242 | + volumes in the resulting model and, ultimately, yield a faster |
| 243 | + simulation time in the destination engine with fewer results |
| 244 | + to manage. Note that Room2Ds will only be merged if they form a |
| 245 | + continuous volume. Otherwise, there will be multiple Rooms per |
| 246 | + zone or story, each with an integer added at the end of their |
| 247 | + identifiers. Choose from the following options: |
| 248 | +
|
| 249 | + * None - No merging of Room2Ds will occur |
| 250 | + * Zones - Room2Ds in the same zone will be merged |
| 251 | + * PlenumZones - Only plenums in the same zone will be merged |
| 252 | + * Stories - Rooms in the same story will be merged |
| 253 | + * PlenumStories - Only plenums in the same story will be merged |
| 254 | +
|
| 255 | + metric: Boolean to note whether the units of the values in the resulting |
| 256 | + matrix are in SI (True) instead of IP (False). (Default: False). |
| 257 | + geometry_names: Boolean to note whether a cleaned version of all geometry |
| 258 | + display names should be used instead of identifiers when translating |
| 259 | + the Model to OSM and IDF. Using this flag will affect all Rooms, Faces, |
| 260 | + Apertures, Doors, and Shades. It will generally result in more read-able |
| 261 | + names in the OSM and IDF but this means that it will not be easy to map |
| 262 | + the EnergyPlus results back to the original Honeybee Model. Cases |
| 263 | + of duplicate IDs resulting from non-unique names will be resolved |
| 264 | + by adding integers to the ends of the new IDs that are derived from |
| 265 | + the name. (Default: False). |
| 266 | + resource_names: Boolean to note whether a cleaned version of all resource |
| 267 | + display names should be used instead of identifiers when translating |
| 268 | + the Model to OSM and IDF. Using this flag will affect all Materials, |
| 269 | + Constructions, ConstructionSets, Schedules, Loads, and ProgramTypes. |
| 270 | + It will generally result in more read-able names for the resources |
| 271 | + in the OSM and IDF. Cases of duplicate IDs resulting from non-unique |
| 272 | + names will be resolved by adding integers to the ends of the new IDs |
| 273 | + that are derived from the name. (Default: False). |
| 274 | + output_file: Optional XLSX file to output the XLSX content of the translation. |
| 275 | + By default this content will be returned from this method as a |
| 276 | + base64 string. |
| 277 | + """ |
| 278 | + # load the model and translate it to a Workbook |
| 279 | + model = Model.from_file(model_file) |
| 280 | + exclude_plenums = not separate_plenum |
| 281 | + workbook = model_to_workbook( |
| 282 | + model, multiplier, exclude_plenums, merge_method, |
| 283 | + metric, geometry_names, resource_names |
| 284 | + ) |
| 285 | + if isinstance(output_file, str): |
| 286 | + workbook.save(output_file) |
| 287 | + else: |
| 288 | + # save workbook to a byte stream |
| 289 | + out = io.BytesIO() |
| 290 | + workbook.save(out) |
| 291 | + # retrieve bytes and write them directly to file objects with wb encoding |
| 292 | + out.seek(0) # reset stream position to the beginning |
| 293 | + excel_bytes = out.read() |
| 294 | + if output_file is not None and output_file.name != '<stdout>' and \ |
| 295 | + output_file.mode == 'wb': |
| 296 | + output_file.write(excel_bytes) |
| 297 | + else: # convert the bytes to a base64 string |
| 298 | + b = base64.b64encode(excel_bytes) |
| 299 | + if output_file is None: |
| 300 | + f_contents = b.decode('utf-8') |
| 301 | + return f_contents |
| 302 | + elif output_file.mode == 'w': |
| 303 | + f_contents = b.decode('utf-8') |
| 304 | + output_file.write(f_contents) |
| 305 | + else: |
| 306 | + output_file.write(b) |
0 commit comments