sheeter - use more strict data access where it's implied

This commit is contained in:
Andrej730
2025-06-25 18:22:54 +05:00
parent 73b5c2f1ae
commit 7440c321b6
+26 -19
View File
@@ -61,8 +61,8 @@ class SheetBuilder:
shutil.copy(ootb_titleblock_path, titleblock_path)
view_root = ET.parse(titleblock_path).getroot()
view_width = self.convert_to_mm(view_root.attrib.get("width"))
view_height = self.convert_to_mm(view_root.attrib.get("height"))
view_width = self.convert_to_mm(view_root.attrib["width"])
view_height = self.convert_to_mm(view_root.attrib["height"])
view = ET.SubElement(root, "g")
view.attrib["data-type"] = "titleblock"
titleblock = ET.SubElement(view, "image")
@@ -87,6 +87,7 @@ class SheetBuilder:
) -> None:
filename = drawing.Name
layout_path = tool.Drawing.get_document_uri(sheet, "LAYOUT")
assert layout_path
layout_dir = os.path.dirname(layout_path)
drawing_path = tool.Drawing.get_document_uri(tool.Drawing.get_drawing_reference(drawing))
@@ -110,8 +111,8 @@ class SheetBuilder:
view.attrib["data-type"] = "drawing"
view.attrib["data-id"] = str(reference.id())
view.attrib["data-drawing"] = drawing.GlobalId
view_width = self.convert_to_mm(view_root.attrib.get("width"))
view_height = self.convert_to_mm(view_root.attrib.get("height"))
view_width = self.convert_to_mm(view_root.attrib["width"])
view_height = self.convert_to_mm(view_root.attrib["height"])
x, y = self.next_drawing_location(layout_root, view_width)
@@ -134,16 +135,16 @@ class SheetBuilder:
# how wide is the title block frame
try:
titleblock_width = self.convert_to_mm(titleblocks[0][0].attrib.get("width"))
titleblock_width = self.convert_to_mm(titleblocks[0][0].attrib["width"])
except (IndexError, AttributeError):
titleblock_width = 840.0
# where does the last drawing finish
try:
last = drawings[-1][0]
last_width = self.convert_to_mm(last.attrib.get("width"))
last_x = self.convert_to_mm(last.attrib.get("x"))
last_y = self.convert_to_mm(last.attrib.get("y"))
last_width = self.convert_to_mm(last.attrib["width"])
last_x = self.convert_to_mm(last.attrib["x"])
last_y = self.convert_to_mm(last.attrib["y"])
except (IndexError, AttributeError):
return [DEFAULT_POSITION.x, DEFAULT_POSITION.y]
@@ -155,8 +156,8 @@ class SheetBuilder:
for drawing in drawings:
for image in drawing:
try:
image_y = self.convert_to_mm(image.attrib.get("y"))
image_height = self.convert_to_mm(image.attrib.get("height"))
image_y = self.convert_to_mm(image.attrib["y"])
image_height = self.convert_to_mm(image.attrib["height"])
except AttributeError:
return [DEFAULT_POSITION.x, DEFAULT_POSITION.y]
if image_y + image_height + DRAWING_PADDING > last_y:
@@ -167,6 +168,7 @@ class SheetBuilder:
ET.register_namespace("", "http://www.w3.org/2000/svg")
layout_path = tool.Drawing.get_document_uri(sheet, "LAYOUT")
assert layout_path
layout_tree = ET.parse(layout_path)
layout_root = layout_tree.getroot()
ifc_file = tool.Ifc.get()
@@ -210,6 +212,7 @@ class SheetBuilder:
ET.register_namespace("", "http://www.w3.org/2000/svg")
layout_path = tool.Drawing.get_document_uri(sheet, "LAYOUT")
assert layout_path
if not os.path.exists(layout_path):
return
layout_tree = ET.parse(layout_path)
@@ -233,6 +236,7 @@ class SheetBuilder:
tool.Drawing.create_svg_document(document)
document_name = os.path.splitext(os.path.basename(view_path))[0]
layout_path = tool.Drawing.get_document_uri(sheet, "LAYOUT")
assert layout_path
layout_dir = os.path.dirname(layout_path)
ET.register_namespace("", "http://www.w3.org/2000/svg")
@@ -278,13 +282,14 @@ class SheetBuilder:
title.attrib["xlink:href"] = os.path.relpath(title_path, layout_dir)
title.attrib["x"] = str(x)
title.attrib["y"] = str(y)
title.attrib["width"] = str(self.convert_to_mm(title_root.attrib.get("width")))
title.attrib["height"] = str(self.convert_to_mm(title_root.attrib.get("height")))
title.attrib["width"] = str(self.convert_to_mm(title_root.attrib["width"]))
title.attrib["height"] = str(self.convert_to_mm(title_root.attrib["height"]))
def build(self, sheet: ifcopenshell.entity_instance) -> dict:
self.references = {"SHEET": None, "RASTER": []}
layout_path = tool.Drawing.get_document_uri(sheet, "LAYOUT")
assert layout_path
self.layout_dir = os.path.dirname(layout_path)
sheet_path = tool.Ifc.resolve_uri(tool.Drawing.get_default_sheet_path(sheet[0], sheet.Name))
@@ -333,7 +338,9 @@ class SheetBuilder:
# add .prefix class to all css selectors
style = svg.find(f"{SVG}defs/{SVG}style")
assert style
style_data = style.text
assert style_data is not None
text = ""
brackets_level = 0
for l in style_data:
@@ -412,6 +419,7 @@ class SheetBuilder:
view.append(svg)
if view_title is not None:
assert foreground
foreground_path = self.get_href(foreground)
data = reference.get_info()
data.update({"Sheet" + k: v for k, v in sheet.get_info().items()})
@@ -473,13 +481,12 @@ class SheetBuilder:
view.remove(image)
def get_href(self, element: ET.Element) -> str:
return urllib.parse.unquote(element.attrib.get(f"{XLINK}href")).replace("\\", "/")
return urllib.parse.unquote(element.attrib[f"{XLINK}href"]).replace("\\", "/")
def parse_embedded_svg(self, image: ET.Element, data: dict) -> ET.Element:
group = ET.Element("g")
group.attrib["transform"] = "translate({},{})".format(
self.convert_to_mm(image.attrib.get("x")), self.convert_to_mm(image.attrib.get("y"))
)
x, y = self.convert_to_mm(image.attrib["x"]), self.convert_to_mm(image.attrib["y"])
group.attrib["transform"] = f"translate({x},{y})"
# Convert viewBox into a clip path
clip_id = str(uuid.uuid4())
@@ -489,8 +496,8 @@ class SheetBuilder:
rect = ET.Element("rect")
rect.attrib["x"] = "0"
rect.attrib["y"] = "0"
rect.attrib["width"] = str(self.convert_to_mm(image.attrib.get("width")))
rect.attrib["height"] = str(self.convert_to_mm(image.attrib.get("height")))
rect.attrib["width"] = str(self.convert_to_mm(image.attrib["width"]))
rect.attrib["height"] = str(self.convert_to_mm(image.attrib["height"]))
clip_path.append(rect)
self.defs.append(clip_path)
@@ -503,7 +510,7 @@ class SheetBuilder:
self.scale = embedded.attrib.get("data-scale")
images = embedded.findall(f"{SVG}image")
for image in images:
old_href = Path(image.attrib.get(f"{XLINK}href"))
old_href = Path(image.attrib[f"{XLINK}href"])
if not os.path.isabs(old_href):
template_dir = Path(os.path.join(self.layout_dir, svg_path)).resolve().parent
old_href = Path(os.path.join(template_dir, old_href))