Skip to content

Commit

Permalink
[Techdraw] Import SVG files with UTF-8 encoding (FreeCAD#18816)
Browse files Browse the repository at this point in the history
* [Techdraw] Fix importing SVG files with UTF-8 encoding

* [Techdraw] fix Lint feedback
  • Loading branch information
Syres916 authored Jan 6, 2025
1 parent 5236541 commit b86e1ad
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/Mod/TechDraw/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ SET(TDTestFile_SRCS
TDTest/TestHatch.svg
TDTest/TestImage.png
TDTest/TestSymbol.svg
TDTest/TestNonAsciiSymbol.svg
TDTest/TestTemplate.svg
)

Expand Down
24 changes: 17 additions & 7 deletions src/Mod/TechDraw/Gui/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,16 +456,25 @@ void CmdTechDrawView::activated(int iMsg)
filename = Base::Tools::escapeEncodeFilename(filename);
auto filespec = DU::cleanFilespecBackslash(filename.toStdString());
openCommand(QT_TRANSLATE_NOOP("Command", "Create Symbol"));
doCommand(Doc, "f = open(\"%s\", 'r')", filespec.c_str());
doCommand(Doc, "import codecs");
doCommand(Doc,
"f = codecs.open(\"%s\", 'r', encoding=\"utf-8\")",
filespec.c_str());
doCommand(Doc, "svg = f.read()");
doCommand(Doc, "f.close()");
doCommand(Doc, "App.activeDocument().addObject('TechDraw::DrawViewSymbol', '%s')",
doCommand(Doc,
"App.activeDocument().addObject('TechDraw::DrawViewSymbol', '%s')",
FeatName.c_str());
doCommand(
Doc,
"App.activeDocument().%s.translateLabel('DrawViewSymbol', 'Symbol', '%s')",
FeatName.c_str(),
FeatName.c_str());
doCommand(Doc, "App.activeDocument().%s.translateLabel('DrawViewSymbol', 'Symbol', '%s')",
FeatName.c_str(), FeatName.c_str());
doCommand(Doc, "App.activeDocument().%s.Symbol = svg", FeatName.c_str());
doCommand(Doc, "App.activeDocument().%s.addView(App.activeDocument().%s)", PageName.c_str(),
FeatName.c_str());
doCommand(Doc,
"App.activeDocument().%s.addView(App.activeDocument().%s)",
PageName.c_str(),
FeatName.c_str());
}
else {
std::string FeatName = getUniqueObjectName("Image");
Expand Down Expand Up @@ -1551,7 +1560,8 @@ void CmdTechDrawSymbol::activated(int iMsg)
filename = Base::Tools::escapeEncodeFilename(filename);
auto filespec = DU::cleanFilespecBackslash(filename.toStdString());
openCommand(QT_TRANSLATE_NOOP("Command", "Create Symbol"));
doCommand(Doc, "f = open(\"%s\", 'r')", (const char*)filespec.c_str());
doCommand(Doc, "import codecs");
doCommand(Doc, "f = codecs.open(\"%s\", 'r', encoding=\"utf-8\")", filespec.c_str());
doCommand(Doc, "svg = f.read()");
doCommand(Doc, "f.close()");
doCommand(Doc, "App.activeDocument().addObject('TechDraw::DrawViewSymbol', '%s')",
Expand Down
23 changes: 22 additions & 1 deletion src/Mod/TechDraw/TDTest/DrawViewSymbolTest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import FreeCAD
import codecs
import os
import unittest
from .TechDrawTestUtilities import createPageWithSVGTemplate
Expand All @@ -21,7 +22,7 @@ def testMakeSymbol(self):
sym = FreeCAD.ActiveDocument.addObject("TechDraw::DrawViewSymbol", "TestSymbol")
path = os.path.dirname(os.path.abspath(__file__))
symbolFileSpec = path + "/TestSymbol.svg"
f = open(symbolFileSpec, "r")
f = codecs.open(symbolFileSpec, "r", encoding="utf-8")
svg = f.read()
f.close()
sym.Symbol = svg
Expand All @@ -33,6 +34,26 @@ def testMakeSymbol(self):

self.assertTrue("Up-to-date" in sym.State)

def testNonAsciiSymbol(self):
"""Tests if a Non-Ascii symbol can be added to page"""
sym = FreeCAD.ActiveDocument.addObject(
"TechDraw::DrawViewSymbol", "NonAsciiSymbol"
)
path = os.path.dirname(os.path.abspath(__file__))
symbolFileSpec = path + "/TestNonAsciiSymbol.svg"
f = codecs.open(symbolFileSpec, "r", encoding="utf-8")
svg = f.read()
f.close()
sym.Symbol = svg
self.page.addView(sym)
sym.X = 220.0
sym.Y = 150.0

FreeCAD.ActiveDocument.recompute()

self.assertTrue("Up-to-date" in sym.State)



if __name__ == "__main__":
unittest.main()
23 changes: 23 additions & 0 deletions src/Mod/TechDraw/TDTest/TestNonAsciiSymbol.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b86e1ad

Please sign in to comment.