File size: 14,093 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 | # SPDX-License-Identifier: LGPL-2.1-or-later
# -*- coding: utf8 -*-
## @package importDWG
# \ingroup DRAFT
# \brief DWG file importer & exporter
"""
@package importDWG
ingroup DRAFT
\brief DWG file importer & exporter
This module provides support for importing and exporting Autodesk DWG files.
This module is only a thin layer that uses the ODA (formerly Teigha) File
Converter application to convert to/from DXF. Then the real work is done by
importDXF
Test files
https://knowledge.autodesk.com/support/autocad/downloads/
caas/downloads/content/autocad-sample-files.html
"""
# Check code quality with
# flake8 --ignore=E226,E266,E401,W503
# ***************************************************************************
# * Copyright (c) 2009 Yorik van Havre <yorik@uncreated.net> *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
# * as published by the Free Software Foundation; either version 2 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * This program is distributed in the hope that it will be useful, *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
# * GNU Library General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
import FreeCAD
from FreeCAD import Console as FCC
from draftutils import params
if FreeCAD.GuiUp:
from draftutils.translate import translate
else:
def translate(context, txt):
return txt
def open(filename):
"""Open filename and parse using importDXF.open().
Parameters
----------
filename : str
The path to the filename to be opened.
Returns
-------
App::Document
The new FreeCAD document object created, with the parsed information.
"""
dxf = convertToDxf(filename)
if dxf:
import importDXF
doc = importDXF.open(dxf)
return doc
return
def insert(filename, docname):
"""Imports a file using importDXF.insert().
If no document exist, it is created.
Parameters
----------
filename : str
The path to the filename to be opened.
docname : str
The name of the active App::Document if one exists, or
of the new one created.
Returns
-------
App::Document
The active FreeCAD document, or the document created if none exists,
with the parsed information.
"""
dxf = convertToDxf(filename)
if dxf:
import importDXF
# Warning: function doesn't return?
doc = importDXF.insert(dxf, docname)
return doc
return
def export(objectslist, filename):
"""Export the DWG file with a given list of objects.
The objects are exported with importDXF.export().
Then the result is converted to DWG.
Parameters
----------
exportList : list
List of document objects to export.
filename : str
Path to the new file.
Returns
-------
str
The same `filename` input.
"""
import importDXF
import os
import tempfile
outdir = tempfile.mkdtemp()
_basename = os.path.splitext(os.path.basename(filename))[0]
dxf = outdir + os.sep + _basename + ".dxf"
importDXF.export(objectslist, dxf)
convertToDwg(dxf, filename)
return filename
def get_libredwg_converter(typ):
"""Find the LibreDWG converter.
It searches the FreeCAD parameters database, then searches the OS search path.
There are no standard installation paths.
`typ` is required because LibreDWG uses two converters and we store only one.
Parameters
----------
typ : str
"dwg2dxf" or "dxf2dwg".
Returns
-------
str
The full path of the converter.
"""
import os
import platform
path = params.get_param("TeighaFileConverter")
if "dwg2dxf" in path or "dxf2dwg" in path: # path set manually
if typ not in path:
path = os.path.dirname(path) + "/" + typ + os.path.splitext(path)[1]
if os.path.exists(path) and os.path.isfile(path):
return path
elif platform.system() == "Windows":
for sub in os.getenv("PATH").split(os.pathsep):
path = sub.replace("\\", "/") + "/" + typ + ".exe"
if os.path.exists(path) and os.path.isfile(path):
return path
else: # for Linux and macOS
for sub in os.getenv("PATH").split(os.pathsep):
path = sub + "/" + typ
if os.path.exists(path) and os.path.isfile(path):
return path
return None
def get_oda_converter():
"""Find the ODA converter.
It searches the FreeCAD parameters database, then searches for common paths.
Parameters
----------
None
Returns
-------
str
The full path of the converter.
"""
import os
import platform
path = params.get_param("TeighaFileConverter")
if "ODAFileConverter" in path: # path set manually
if os.path.exists(path) and os.path.isfile(path):
return path
elif platform.system() == "Windows":
odadir = os.path.expandvars("%ProgramFiles%\\ODA").replace("\\", "/")
if os.path.exists(odadir):
for sub in os.listdir(odadir):
path = odadir + "/" + sub + "/" + "ODAFileConverter.exe"
if os.path.exists(path) and os.path.isfile(path):
return path
elif platform.system() == "Linux":
path = "/usr/bin/ODAFileConverter"
if os.path.exists(path) and os.path.isfile(path):
return path
else: # for macOS
path = "/Applications/ODAFileConverter.app/Contents/MacOS/ODAFileConverter"
if os.path.exists(path) and os.path.isfile(path):
return path
return None
def get_qcad_converter():
"""Find the QCAD converter.
It searches the FreeCAD parameters database, then searches for common paths.
Parameters
----------
None
Returns
-------
str
The full path of the converter.
"""
import os
import platform
path = params.get_param("TeighaFileConverter")
if "dwg2dwg" in path: # path set manually
pass
elif platform.system() == "Windows":
path = os.path.expandvars("%ProgramFiles%\\QCAD\\dwg2dwg.bat").replace("\\", "/")
elif platform.system() == "Linux":
# /home/$USER/opt/qcad-3.28.1-trial-linux-qt5.14-x86_64/dwg2dwg
path = os.path.expandvars("/home/$USER/opt")
if os.path.exists(path) and os.path.isdir(path):
for sub in os.listdir(path):
if "qcad" in sub:
path = path + "/" + sub + "/" + "dwg2dwg"
break
else: # for macOS
path = "/Applications/QCAD.app/Contents/Resources/dwg2dwg"
if os.path.exists(path) and os.path.isfile(path):
return path
return None
def convertToDxf(dwgfilename):
"""Convert a DWG file to a DXF file.
If the converter is found it is used, otherwise the conversion fails.
Parameters
----------
dwgfilename : str
The input filename.
Returns
-------
str
The new file produced.
"""
import os
import subprocess
import tempfile
dwgfilename = dwgfilename.replace("\\", "/")
conv = params.get_param("DWGConversion")
error_msg = (
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"
)
if conv in [0, 1]: # LibreDWG
libredwg = get_libredwg_converter("dwg2dxf")
if libredwg is not None:
outdir = tempfile.mkdtemp().replace("\\", "/")
basename = os.path.basename(dwgfilename)
result = outdir + "/" + os.path.splitext(basename)[0] + ".dxf"
cmdline = [libredwg, dwgfilename, "-o", result]
FCC.PrintMessage(translate("draft", "Converting:") + " " + str(cmdline) + "\n")
proc = subprocess.Popen(cmdline)
proc.communicate()
if os.path.exists(result):
FCC.PrintMessage(translate("draft", "Conversion successful") + "\n")
return result
else:
FCC.PrintError(error_msg)
elif conv != 0:
FCC.PrintError(translate("draft", "LibreDWG converter not found") + "\n")
if conv in [0, 2]: # ODA
oda = get_oda_converter()
if oda is not None:
indir = os.path.dirname(dwgfilename)
outdir = tempfile.mkdtemp().replace("\\", "/")
basename = os.path.basename(dwgfilename)
cmdline = [oda, indir, outdir, "ACAD2000", "DXF", "0", "1", basename]
FCC.PrintMessage(translate("draft", "Converting:") + " " + str(cmdline) + "\n")
proc = subprocess.Popen(cmdline)
proc.communicate()
result = outdir + "/" + os.path.splitext(basename)[0] + ".dxf"
if os.path.exists(result):
FCC.PrintMessage(translate("draft", "Conversion successful") + "\n")
return result
else:
FCC.PrintError(error_msg)
elif conv != 0:
FCC.PrintError(translate("draft", "ODA converter not found") + "\n")
if conv in [0, 3]: # QCAD
qcad = get_qcad_converter()
if qcad is not None:
outdir = tempfile.mkdtemp().replace("\\", "/")
basename = os.path.basename(dwgfilename)
result = outdir + "/" + os.path.splitext(basename)[0] + ".dxf"
cmdline = [qcad, "-f", "-o", result, dwgfilename]
FCC.PrintMessage(translate("draft", "Converting:") + " " + str(cmdline) + "\n")
proc = subprocess.Popen(cmdline, cwd=os.path.dirname(qcad)) # cwd required for Windows
proc.communicate()
if os.path.exists(result):
FCC.PrintMessage(translate("draft", "Conversion successful") + "\n")
return result
else:
FCC.PrintError(error_msg)
elif conv != 0:
FCC.PrintError(translate("draft", "QCAD converter not found") + "\n")
FCC.PrintError(
translate(
"draft",
"""No suitable external DWG converter has been found.
Please set one manually under menu Edit → Preferences → Import/Export → DWG
For more information see:
https://wiki.freecad.org/Import_Export_Preferences""",
)
+ "\n"
)
return None
def convertToDwg(dxffilename, dwgfilename):
"""Convert a DXF file to a DWG file.
If the converter is found it is used, otherwise the conversion fails.
Parameters
----------
dxffilename : str
The input DXF file
dwgfilename : str
The output DWG file
Returns
-------
str
The same `dwgfilename` file path.
"""
import os
import subprocess
dxffilename = dxffilename.replace("\\", "/")
dwgfilename = dwgfilename.replace("\\", "/")
conv = params.get_param("DWGConversion")
if conv in [0, 1]: # LibreDWG
libredwg = get_libredwg_converter("dxf2dwg")
if libredwg is not None:
cmdline = [libredwg, dxffilename, "-y", "-o", dwgfilename]
FCC.PrintMessage(translate("draft", "Converting:") + " " + str(cmdline) + "\n")
proc = subprocess.Popen(cmdline)
proc.communicate()
return dwgfilename
elif conv != 0:
FCC.PrintError(translate("draft", "LibreDWG converter not found") + "\n")
if conv in [0, 2]: # ODA
oda = get_oda_converter()
if oda is not None:
indir = os.path.dirname(dxffilename)
outdir = os.path.dirname(dwgfilename)
basename = os.path.basename(dxffilename)
cmdline = [oda, indir, outdir, "ACAD2000", "DWG", "0", "1", basename]
FCC.PrintMessage(translate("draft", "Converting:") + " " + str(cmdline) + "\n")
proc = subprocess.Popen(cmdline)
proc.communicate()
return dwgfilename
elif conv != 0:
FCC.PrintError(translate("draft", "ODA converter not found") + "\n")
if conv in [0, 3]: # QCAD
qcad = get_qcad_converter()
if qcad is not None:
cmdline = [qcad, "-f", "-o", dwgfilename, dxffilename]
FCC.PrintMessage(translate("draft", "Converting:") + " " + str(cmdline) + "\n")
proc = subprocess.Popen(cmdline, cwd=os.path.dirname(qcad)) # cwd required for Windows
proc.communicate()
return dwgfilename
elif conv != 0:
FCC.PrintError(translate("draft", "QCAD converter not found") + "\n")
FCC.PrintError(
translate(
"draft",
"""No suitable external DWG converter has been found.
Please set one manually under menu Edit → Preferences → Import/Export → DWG
For more information see:
https://wiki.freecad.org/Import_Export_Preferences""",
)
+ "\n"
)
return None
|