From bde8c9416e5443a78feb36eff7824b0a6be8fb6a Mon Sep 17 00:00:00 2001 From: Yorik van Havre Date: Tue, 19 Oct 2021 13:55:24 +0200 Subject: [PATCH] Draft: Allow to use QCAD pro as a converter to import/export DWG files --- src/Mod/Draft/Resources/ui/preferences-dwg.ui | 53 ++++++- src/Mod/Draft/importDWG.py | 147 +++++++++++------- 2 files changed, 145 insertions(+), 55 deletions(-) diff --git a/src/Mod/Draft/Resources/ui/preferences-dwg.ui b/src/Mod/Draft/Resources/ui/preferences-dwg.ui index ba870a8253..4d97ab91cc 100644 --- a/src/Mod/Draft/Resources/ui/preferences-dwg.ui +++ b/src/Mod/Draft/Resources/ui/preferences-dwg.ui @@ -26,12 +26,56 @@ DWG conversion + + + + + + Conversion method: + + + + + + + This is the method FreeCAD will use to convert DWG files to DXF. If "Automatic" is chosen, FreeCAD will try to find one of the following convertors in the same order as they are shown here. If FreeCAD is unable to find any, you might need to choose a specific convertor and indicate its path here under. Choose the "dwg2dxf" utility if using LibreDWG, "ODAFileConverter" if using the ODA file converter, or the "dwg2dwg" utility if using the pro version of QCAD. + + + DWGConversion + + + Mod/Draft + + + + Automatic + + + + + LibreDWG + + + + + ODA Converter + + + + + QCAD pro + + + + + + - Path to ODA file converter + Path to file converter @@ -53,7 +97,7 @@ - <html><head/><body><p><span style=" font-weight:600;">Note:</span> DXF options apply to DWG files as well.</p></body></html> + <html><head/><body><p><span style=" font-weight:600;">Note:</span> DXF options apply to DWG files as well.</p></body></html> true @@ -91,6 +135,11 @@ Gui::FileChooser
Gui/PrefWidgets.h
+ + Gui::PrefComboBox + QComboBox +
Gui/PrefWidgets.h
+
diff --git a/src/Mod/Draft/importDWG.py b/src/Mod/Draft/importDWG.py index 8c9dd1c19d..288dcb0a86 100644 --- a/src/Mod/Draft/importDWG.py +++ b/src/Mod/Draft/importDWG.py @@ -179,10 +179,6 @@ def getTeighaConverter(): if teigha: if os.path.exists(teigha): return teigha - from DraftTools import translate - _msg = ("ODA (formerly Teigha) File Converter not found, " - "DWG support is disabled") - FCC.PrintMessage(translate("draft", _msg) + "\n") return None @@ -203,41 +199,62 @@ def convertToDxf(dwgfilename): """ import os, tempfile, subprocess, sys - try: - outdir = tempfile.mkdtemp() - basename = os.path.basename(dwgfilename) - result = outdir + os.sep + os.path.splitext(basename)[0] + ".dxf" - proc = subprocess.Popen(("dwg2dxf", dwgfilename, "-o", result)) - proc.communicate() - return result - except Exception: - pass + p = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Draft") + conv = p.GetInt("DWGConversion",0) + path = p.GetString("TeighaFileConverter","") - teigha = getTeighaConverter() - if teigha: - indir = os.path.dirname(dwgfilename) - outdir = tempfile.mkdtemp() - basename = os.path.basename(dwgfilename) - cmdline = ('"%s" "%s" "%s" "ACAD2000" "DXF" "0" "1" "%s"' - % (teigha, indir, outdir, basename)) - FCC.PrintMessage(translate("ImportDWG", "Converting:") - + " " + cmdline + "\n") - if six.PY2: - if isinstance(cmdline, six.text_type): - encoding = sys.getfilesystemencoding() - cmdline = cmdline.encode(encoding) - subprocess.call(cmdline, shell=True) # os.system(cmdline) - result = outdir + os.sep + os.path.splitext(basename)[0] + ".dxf" - if os.path.exists(result): - FCC.PrintMessage(translate("ImportDWG", - "Conversion successful") + "\n") + if conv in [0,1]: # LibreDWG dxf2dwg/dwg2dxf + if not path: + path = "dwg2dxf" + if path.endswith("dxf2dwg"): + path = os.path.join(os.path.dirname,"dwg2dxf") + try: + outdir = tempfile.mkdtemp() + basename = os.path.basename(dwgfilename) + result = outdir + os.sep + os.path.splitext(basename)[0] + ".dxf" + proc = subprocess.Popen((path, dwgfilename, "-o", result)) + proc.communicate() return result + except Exception: + if conv != 0: + FCC.PrintError(translate("draft", "LibreDWG error") + "\n") + + if conv in [0,2]: # ODA + teigha = getTeighaConverter() + if teigha: + indir = os.path.dirname(dwgfilename) + outdir = tempfile.mkdtemp() + basename = os.path.basename(dwgfilename) + cmdline = ('"%s" "%s" "%s" "ACAD2000" "DXF" "0" "1" "%s"' % (teigha, indir, outdir, basename)) + FCC.PrintMessage(translate("draft", "Converting:") + " " + cmdline + "\n") + if six.PY2: + if isinstance(cmdline, six.text_type): + encoding = sys.getfilesystemencoding() + cmdline = cmdline.encode(encoding) + subprocess.call(cmdline, shell=True) # os.system(cmdline) + result = outdir + os.sep + os.path.splitext(basename)[0] + ".dxf" + if os.path.exists(result): + FCC.PrintMessage(translate("draft", "Conversion successful") + "\n") + return result + else: + FCC.PrintError(translate("draft","Error during DWG conversion. Try moving the DWG file to a directory path without spaces and non-english characters, or try saving to a lower DWG version.") + "\n") else: - _msg = ("Error during DWG to DXF conversion. " - "Try moving the DWG file to a directory path\n" - "without spaces and non-english characters, " - "or try saving to a lower DWG version.") - FCC.PrintMessage(translate("ImportDWG", _msg) + "\n") + if conv != 0: + FCC.PrintError(translate("draft", "ODA File Converter not found") + "\n") + + if conv in [0,3]: # QCAD + if not path: + path = "dwg2dwg" + try: + outdir = tempfile.mkdtemp() + basename = os.path.basename(dwgfilename) + result = outdir + os.sep + os.path.splitext(basename)[0] + ".dxf" + proc = subprocess.Popen((path, "-o", result, dwgfilename)) + proc.communicate() + return result + except Exception: + FCC.PrintError(translate("draft", "QCAD error") + "\n") + return None @@ -258,23 +275,47 @@ def convertToDwg(dxffilename, dwgfilename): str The same `dwgfilename` file path. """ - import os, subprocess + import os, subprocess, shutil - import shutil - if shutil.which("dxf2dwg"): - proc = subprocess.Popen(("dxf2dwg", dxffilename, "-y", "-o", dwgfilename)) - proc.communicate() - return dwgfilename + p = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Draft") + conv = p.GetInt("DWGConversion",0) + path = p.GetString("TeighaFileConverter","") + + if conv in [0,1]: # LibreDWG dxf2dwg/dwg2dxf + if not path: + path = "dxf2dwg" + if path.endswith("dwg2dxf"): + path = os.path.join(os.path.dirname,"dxf2dwg") + try: + proc = subprocess.Popen((path, dxffilename, "-y", "-o", dwgfilename)) + proc.communicate() + return dwgfilename + except Exception: + if conv != 0: + FCC.PrintError(translate("draft", "LibreDWG error") + "\n") + + if conv in [0,2]: # ODA + teigha = getTeighaConverter() + if teigha: + indir = os.path.dirname(dxffilename) + outdir = os.path.dirname(dwgfilename) + basename = os.path.basename(dxffilename) + cmdline = ('"%s" "%s" "%s" "ACAD2000" "DWG" "0" "1" "%s"' % (teigha, indir, outdir, basename)) + FCC.PrintMessage(translate("draft", "Converting:") + " " + cmdline + "\n") + subprocess.call(cmdline, shell=True) # os.system(cmdline) + return dwgfilename + else: + if conv != 0: + FCC.PrintError(translate("draft", "ODA File Converter not found") + "\n") + + if conv in [0,3]: # QCAD + if not path: + path = "dwg2dwg" + try: + proc = subprocess.Popen((path, "-o", dwgfilename, dxffilename)) + proc.communicate() + return dwgfilename + except Exception: + FCC.PrintError(translate("draft", "QCAD error") + "\n") - teigha = getTeighaConverter() - if teigha: - indir = os.path.dirname(dxffilename) - outdir = os.path.dirname(dwgfilename) - basename = os.path.basename(dxffilename) - cmdline = ('"%s" "%s" "%s" "ACAD2000" "DWG" "0" "1" "%s"' - % (teigha, indir, outdir, basename)) - FCC.PrintMessage(translate("ImportDWG", "Converting:") - + " " + cmdline + "\n") - subprocess.call(cmdline, shell=True) # os.system(cmdline) - return dwgfilename return None