ChangeSheetTitleBlock operator

Added operator to change title block of currently selected sheet, it's located right after "Add sheet" button.
The way it works - you select the sheet, select the desired titleblock from dropdown menu and it overrides current titleblock from the sheet with the new one.
This commit is contained in:
Andrej730
2023-04-06 10:54:04 +05:00
parent ab23030051
commit c2320a3c1e
4 changed files with 51 additions and 1 deletions
@@ -55,6 +55,7 @@ classes = (
operator.LoadSheets,
operator.OpenSchedule,
operator.OpenSheet,
operator.ChangeSheetTitleBlock,
operator.OpenView,
operator.RemoveDrawing,
operator.RemoveDrawingFromSheet,
@@ -847,6 +847,27 @@ class CreateSheets(bpy.types.Operator):
return {"FINISHED"}
class ChangeSheetTitleBlock(bpy.types.Operator):
bl_idname = "bim.change_sheet_title_block"
bl_label = "Change Sheet Title Block"
bl_description = "Change the title block of the active sheet"
bl_options = {"REGISTER"}
def execute(self, context):
scene = context.scene
props = scene.DocProperties
active_sheet = props.sheets[props.active_sheet_index]
sheet = tool.Ifc.get().by_id(active_sheet.ifc_definition_id)
sheet_builder = sheeter.SheetBuilder()
sheet_builder.data_dir = scene.BIMProperties.data_dir
titleblock = scene.DocProperties.titleblock
sheet_builder.change_titleblock(sheet, titleblock)
self.report({"INFO"}, 'Title block changed for sheet "{}" to {}'.format(sheet.Name, titleblock))
return {"FINISHED"}
class OpenView(bpy.types.Operator):
bl_idname = "bim.open_view"
bl_label = "Open View"
@@ -48,7 +48,7 @@ class SheetBuilder:
view = ET.SubElement(root, "g")
view.attrib["data-type"] = "titleblock"
titleblock = ET.SubElement(view, "image")
titleblock.attrib["xlink:href"] = "../templates/titleblocks/" + titleblock_name + ".svg"
titleblock.attrib["xlink:href"] = f"../templates/titleblocks/{titleblock_name}.svg"
titleblock.attrib["x"] = "0"
titleblock.attrib["y"] = "0"
titleblock.attrib["width"] = str(view_width)
@@ -314,6 +314,33 @@ class SheetBuilder:
group.append(child)
return group
def change_titleblock(self, sheet, titleblock_name):
ET.register_namespace("", "http://www.w3.org/2000/svg")
ET.register_namespace("xlink", "http://www.w3.org/1999/xlink")
titleblock_svg_path = os.path.join(self.data_dir, "templates", "titleblocks", f"{titleblock_name}.svg")
view_root = ET.parse(titleblock_svg_path).getroot()
view_width = self.convert_to_mm(view_root.attrib.get("width"))
view_height = self.convert_to_mm(view_root.attrib.get("height"))
sheet_dir = os.path.join(self.data_dir, "sheets")
sheet_name = os.path.splitext(os.path.basename(tool.Drawing.get_document_uri(sheet)))[0]
sheet_path = os.path.join(sheet_dir, sheet_name + ".svg")
sheet_tree = ET.parse(sheet_path)
root = sheet_tree.getroot()
titleblock = sheet_tree.findall('{http://www.w3.org/2000/svg}g[@data-type="titleblock"]')[0]
image = titleblock.findall("{http://www.w3.org/2000/svg}image[@{http://www.w3.org/1999/xlink}href]")[0]
image.attrib["{http://www.w3.org/1999/xlink}href"] = f"../templates/titleblocks/{titleblock_name}.svg"
image.attrib["width"] = str(view_width)
image.attrib["height"] = str(view_height)
root.attrib["width"] = "{}mm".format(view_width)
root.attrib["height"] = "{}mm".format(view_height)
root.attrib["viewBox"] = "0 0 {} {}".format(view_width, view_height)
sheet_tree.write(sheet_path)
def convert_to_mm(self, value):
# CSS is what defines these possibilities
# https://www.w3.org/TR/SVG/refs.html#ref-css-values-3
@@ -274,6 +274,7 @@ class BIM_PT_sheets(Panel):
row = self.layout.row(align=True)
row.prop(self.props, "titleblock", text="")
row.operator("bim.add_sheet", text="", icon="ADD")
row.operator("bim.change_sheet_title_block", text="", icon="MODIFIER_DATA")
row.operator("bim.disable_editing_sheets", text="", icon="CANCEL")
if self.props.sheets and self.props.active_sheet_index < len(self.props.sheets):