def _format(self, context, element, my_res, children_res) -> "str or None": """Format to FDS notation. On error raise BFException.""" # Expected output: # ! Me: Message < my_res.labels # ! Me: Children1: Message < body < children_res values # Children1 body # ! Me: Children2: Message # Children2 body # My value < my_res.value return "".join(( fds_format.to_comment(my_res.labels), "".join(child_res.value or str() for child_res in children_res or tuple()), # child_res.value could be None my_res.value or str(), # my_res.value could be None ))
def _format(self, context, element, my_res, children_res) -> "str or None": """Format to FDS notation. On error raise BFException.""" # Expected output: # ! OBST: Message < my_res.labels # ! OBST: ID: Message + children labels (from self.bf_props) # ! OBST: XB: Message # &OBST ID='example' XB=... /\n < body < c_multivalue[0] + c_value # &OBST ID='example' XB=... /\n c_multivalue[1] + c_value # &XXXX P1=... /\n < my_res.value # Append children messages to my messages (children are self.bf_props) my_res.msgs.extend(label for child_res in children_res for label in child_res.labels) # Search and extract the first (and should be only) multivalue from children values child_multivalues = None for child_res in children_res: if isiterable(child_res.value): # Set the multivalue and remove multivalue child_res from single value children_res child_multivalues = child_res.value children_res.remove(child_res) # Each multivalue contains its multi ID, then remove ordinary single ID sent by "bf_id" children_res.remove(children_res["bf_id"]) break # Set fds_label: use self.fds_label or last children_res value, that should be bf_free # When using last children value, remove child from BFList if self.fds_label: fds_label = self.fds_label else: fds_label = children_res.pop().value # Join children values children_value = " ".join(child_res.value for child_res in children_res if child_res.value) # Build body: When multivalue exists, ID is embedded into each child_multivalue; # else ID is embedded into children_value if child_multivalues: body = "".join("&{} {} {} /\n".format( fds_label, child_multivalue, children_value, ) for child_multivalue in child_multivalues ) else: body = "&{} {} /\n".format(fds_label, children_value) # Return return "".join(( fds_format.to_comment(my_res.labels), body, my_res.value or str(), # my_res.value could be None ))
def scene_to_fds(operator, context, filepath=""): """Export current Blender Scene to an FDS case file""" # Init t0 = time.time() w = context.window_manager.windows[0] w.cursor_modal_set("WAIT") to_fds_error = False to_ge1_error = False if not filepath.lower().endswith('.fds'): filepath += '.fds' filepath = bpy.path.abspath(filepath) sc = context.scene # Prepare FDS filepath print( "BFDS: io.scene_to_fds: Exporting current scene to FDS case file: {}". format(sc.name)) if not utilities.is_writable(filepath): w.cursor_modal_restore() operator.report({"ERROR"}, "FDS file not writable, cannot export") return {'CANCELLED'} # Prepare FDS file fds_file = fds_format.to_comment(( "Generated by BlenderFDS {} on Blender {}".format( version.blenderfds_version_string, version.blender_version_string, ), "Date: {}".format( time.strftime("%a, %d %b %Y, %H:%M:%S", time.localtime()), ), "File: {}".format(bpy.data.filepath, ), " ", )) try: fds_file += sc.to_fds(context=context) except BFException as err: fds_file += "".join(("ERROR: {}\n".format(msg) for msg in err.labels)) to_fds_error = True # Write FDS file fds_file += fds_format.to_comment( ("Generated in {0:.0f} s.".format(time.time() - t0), )) if not utilities.write_to_file(filepath, fds_file): w.cursor_modal_restore() operator.report({"ERROR"}, "FDS file not writable, cannot export") return {'CANCELLED'} # Prepare GE1 filepath (always export!) print( "BFDS: io.scene_to_fds: Exporting current scene to GE1 render file: {}" .format(sc.name)) filepath = filepath[:-4] + '.GE1' if not utilities.is_writable(filepath): w.cursor_modal_restore() operator.report({"ERROR"}, "GE1 file not writable, cannot export") return {'CANCELLED'} # Prepare GE1 file try: ge1_file = sc.to_ge1(context=context) except BFException as err: ge1_file = "".join(("ERROR: {}\n".format(msg) for msg in err.labels)) to_ge1_error = True # Write GE1 file if not utilities.write_to_file(filepath, ge1_file): w.cursor_modal_restore() operator.report({"ERROR"}, "GE1 file not writable, cannot export") return {'CANCELLED'} # Check errors if to_fds_error: w.cursor_modal_restore() operator.report({"ERROR"}, "Errors reported, check exported FDS file") return {'CANCELLED'} if to_ge1_error: w.cursor_modal_restore() operator.report({"ERROR"}, "Errors reported, check exported GE1 file") return {'CANCELLED'} # End w.cursor_modal_restore() print("BFDS: io.scene_to_fds: End.") operator.report({"INFO"}, "FDS File exported") return {'FINISHED'}
def scene_to_fds(operator, context, filepath=""): """Export current Blender Scene to an FDS case file""" # Init t0 = time.time() w = context.window_manager.windows[0] w.cursor_modal_set("WAIT") to_fds_error = False to_ge1_error = False if not filepath.lower().endswith('.fds'): filepath += '.fds' filepath = bpy.path.abspath(filepath) sc = context.scene # Prepare FDS filepath print("BFDS: io.scene_to_fds: Exporting current scene to FDS case file: {}".format(sc.name)) if not utilities.is_writable(filepath): w.cursor_modal_restore() operator.report({"ERROR"}, "FDS file not writable, cannot export") return {'CANCELLED'} # Prepare FDS file fds_file = fds_format.to_comment(( "Generated by BlenderFDS {} on Blender {}".format( version.blenderfds_version_string, version.blender_version_string, ), "Date: {}".format( time.strftime("%a, %d %b %Y, %H:%M:%S", time.localtime()), ), "File: {}".format( bpy.data.filepath, ), " ", )) try: fds_file += sc.to_fds(context=context) except BFException as err: fds_file += "".join(("ERROR: {}\n".format(msg) for msg in err.labels)) to_fds_error = True # Write FDS file fds_file += fds_format.to_comment(("Generated in {0:.0f} s.".format(time.time()-t0),)) if not utilities.write_to_file(filepath, fds_file): w.cursor_modal_restore() operator.report({"ERROR"}, "FDS file not writable, cannot export") return {'CANCELLED'} # Prepare GE1 filepath (always export!) print("BFDS: io.scene_to_fds: Exporting current scene to GE1 render file: {}".format(sc.name)) filepath = filepath[:-4] + '.GE1' if not utilities.is_writable(filepath): w.cursor_modal_restore() operator.report({"ERROR"}, "GE1 file not writable, cannot export") return {'CANCELLED'} # Prepare GE1 file try: ge1_file = sc.to_ge1(context=context) except BFException as err: ge1_file = "".join(("ERROR: {}\n".format(msg) for msg in err.labels)) to_ge1_error = True # Write GE1 file if not utilities.write_to_file(filepath, ge1_file): w.cursor_modal_restore() operator.report({"ERROR"}, "GE1 file not writable, cannot export") return {'CANCELLED'} # Check errors if to_fds_error: w.cursor_modal_restore() operator.report({"ERROR"}, "Errors reported, check exported FDS file") return {'CANCELLED'} if to_ge1_error: w.cursor_modal_restore() operator.report({"ERROR"}, "Errors reported, check exported GE1 file") return {'CANCELLED'} # End w.cursor_modal_restore() print("BFDS: io.scene_to_fds: End.") operator.report({"INFO"}, "FDS File exported") return {'FINISHED'}