From 281d112beade944e144cf19f3cadbf5342769b59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20=C3=89corchard?= Date: Sat, 28 Sep 2024 21:36:34 +0200 Subject: [PATCH] BIM: Use labels in DAE export MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Gaël Écorchard --- src/Mod/BIM/importers/importDAE.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Mod/BIM/importers/importDAE.py b/src/Mod/BIM/importers/importDAE.py index 6ab0e76da3..0e383b7d04 100644 --- a/src/Mod/BIM/importers/importDAE.py +++ b/src/Mod/BIM/importers/importDAE.py @@ -34,6 +34,7 @@ __url__ = "https://www.freecad.org" import os from typing import Optional +from xml.sax.saxutils import escape as sax_escape import numpy as np @@ -56,6 +57,17 @@ else: DEBUG = True +def xml_escape(text: str, entities: dict[str, str] = None) -> str: + """Escape text for XML. + + This is a wrapper around xml.sax.saxutils.escape that replaces also + `"` with `"` by default. + """ + if entities is None: + entities = {'"': """} + return sax_escape(text, entities=entities) + + def check_collada_import() -> bool: """Return True if the `collada` module is available. @@ -207,12 +219,13 @@ def export( author = FreeCAD.ActiveDocument.CreatedBy except UnicodeEncodeError: author = FreeCAD.ActiveDocument.CreatedBy.encode("utf8") - author = author.replace("<", "") - author = author.replace(">", "") + author = xml_escape(author) col_contributor.author = author ver = FreeCAD.Version() appli = f"FreeCAD v{ver[0]}.{ver[1]} build {ver[2]}" col_contributor.authoring_tool = appli + # Bug in collada from 0.4 to 0.9, contributors are not written to file. + # Set it anyway for future versions. col_mesh.assetInfo.contributors.append(col_contributor) col_mesh.assetInfo.unitname = "meter" col_mesh.assetInfo.unitmeter = 1.0 @@ -269,7 +282,7 @@ def export( geom = collada.geometry.Geometry( collada=col_mesh, id=f"geometry{obj_ind}", - name=obj.Name, + name=xml_escape(obj.Label), sourcebyid=[vert_src, normal_src], ) input_list = collada.source.InputList()