Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Add macOS paths to importDWG.py #9830

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 22 additions & 18 deletions src/Mod/Draft/importDWG.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf8 -*-

Check warning on line 1 in src/Mod/Draft/importDWG.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

would reformat src/Mod/Draft/importDWG.py
## @package importDWG
# \ingroup DRAFT
# \brief DWG file importer & exporter
Expand Down Expand Up @@ -46,7 +46,7 @@
if FreeCAD.GuiUp:
from draftutils.translate import translate
else:
def translate(context, txt):

Check warning on line 49 in src/Mod/Draft/importDWG.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Unused argument 'context' (unused-argument)
return txt

# Save the native open function to avoid collisions
Expand All @@ -54,7 +54,7 @@
pythonopen = open


def open(filename):

Check warning on line 57 in src/Mod/Draft/importDWG.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Redefining built-in 'open' (redefined-builtin)

Check warning on line 57 in src/Mod/Draft/importDWG.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Either all return statements in a function should return an expression, or none of them should. (inconsistent-return-statements)
"""Open filename and parse using importDXF.open().

Parameters
Expand All @@ -69,13 +69,13 @@
"""
dxf = convertToDxf(filename)
if dxf:
import importDXF

Check warning on line 72 in src/Mod/Draft/importDWG.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Import outside toplevel (importDXF) (import-outside-toplevel)
doc = importDXF.open(dxf)
return doc
return


def insert(filename, docname):

Check warning on line 78 in src/Mod/Draft/importDWG.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Either all return statements in a function should return an expression, or none of them should. (inconsistent-return-statements)
"""Imports a file using importDXF.insert().

If no document exist, it is created.
Expand All @@ -96,9 +96,9 @@
"""
dxf = convertToDxf(filename)
if dxf:
import importDXF

Check warning on line 99 in src/Mod/Draft/importDWG.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Import outside toplevel (importDXF) (import-outside-toplevel)
# Warning: function doesn't return?
doc = importDXF.insert(dxf, docname)

Check failure on line 101 in src/Mod/Draft/importDWG.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Assigning result of a function call, where the function has no return (assignment-from-no-return)
return doc
return

Expand All @@ -121,9 +121,9 @@
str
The same `filename` input.
"""
import importDXF

Check warning on line 124 in src/Mod/Draft/importDWG.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Import outside toplevel (importDXF) (import-outside-toplevel)
import os

Check warning on line 125 in src/Mod/Draft/importDWG.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Import outside toplevel (os) (import-outside-toplevel)
import tempfile

Check warning on line 126 in src/Mod/Draft/importDWG.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Import outside toplevel (tempfile) (import-outside-toplevel)
outdir = tempfile.mkdtemp()
_basename = os.path.splitext(os.path.basename(filename))[0]
dxf = outdir + os.sep + _basename + ".dxf"
Expand All @@ -135,8 +135,8 @@
def get_libredwg_converter(typ):
"""Find the LibreDWG converter.

It searches the FreeCAD parameters database, then searches the OS search path
on Linux and Windows systems. There are no standard installation paths.
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.

Expand All @@ -150,7 +150,7 @@
str
The full path of the converter.
"""
import os

Check warning on line 153 in src/Mod/Draft/importDWG.py

View workflow job for this annotation

GitHub Actions / Lint / Lint

Import outside toplevel (os) (import-outside-toplevel)
import platform

p = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Draft")
Expand All @@ -161,25 +161,24 @@
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() == "Linux":
for sub in os.getenv("PATH").split(":"):
path = sub + "/" + typ
if os.path.exists(path) and os.path.isfile(path):
return path
elif platform.system() == "Windows":
for sub in os.getenv("PATH").split(";"):
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 on Linux and Windows systems.
It searches the FreeCAD parameters database, then searches for common paths.

Parameters
----------
Expand All @@ -199,26 +198,29 @@
if "ODAFileConverter" in path: # path set manually
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
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
Copy link

@Nailig Nailig Jul 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a check if macOS, else unknown system, same for other if-then-else statements...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree. Two reasons:

  1. FreeCAD officially only supports 3 operating system. Anybody who chooses a different OS is doing so at their own risk.
  2. The if-then-else statements only result in a default path which is then checked. So if an unkown OS is used and the suggested path does not exist there is no problem.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Let’s say we disagree about programming practices.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In any case I don't know what we would do right now on a system that is not mac, linux or windows... I think if someone comes up with a new config, then we see about what paths to use...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Roy-043 FreeBSD fixes are submitted to FreeCAD from time to time. So while it is unofficial, it still supported
https://github.com/search?q=repo%3AFreeCAD%2FFreeCAD%20freebsd&type=code

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 on Linux and Windows systems.
It searches the FreeCAD parameters database, then searches for common paths.

Parameters
----------
Expand All @@ -237,15 +239,17 @@

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")
for sub in os.listdir(path):
if "qcad" in sub:
path = path + "/" + sub + "/" + "dwg2dwg"
break
elif platform.system() == "Windows":
path = os.path.expandvars("%ProgramFiles%\\QCAD\\dwg2dwg.bat").replace("\\", "/")
else: # for macOS
path = "/Applications/QCAD.app/Contents/Resources/dwg2dwg"

if os.path.exists(path) and os.path.isfile(path):
return path
Expand Down