Skip to content

Commit

Permalink
FEM: move working path methods from solver run to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
berndhahnebach committed Sep 6, 2019
1 parent e6a96e1 commit 2afd93b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/Mod/Fem/femexamples/manager.py
Expand Up @@ -62,7 +62,7 @@ def run_analysis(doc, base_name, filepath=""):
doc.saveAs(save_fc_file)

# get analysis workig dir
from femsolver.run import _getBesideDir as getpath
from femtools.femutils import getBesideDir as getpath
working_dir = getpath(solver)

# run analysis
Expand Down
68 changes: 5 additions & 63 deletions src/Mod/Fem/femsolver/run.py
Expand Up @@ -28,7 +28,6 @@

import os
import os.path
import tempfile
import threading
import shutil

Expand Down Expand Up @@ -128,15 +127,15 @@ def _isPathValid(m, path):
if setting == settings.BESIDE:
if t == settings.BESIDE:
base = os.path.split(m.directory.rstrip("/"))[0]
return base == _getBesideBase(m.solver)
return base == femutils.getBesideBase(m.solver)
return False
if setting == settings.TEMPORARY:
return t == settings.TEMPORARY
if setting == settings.CUSTOM:
if t == settings.CUSTOM:
firstBase = os.path.split(m.directory.rstrip("/"))[0]
customBase = os.path.split(firstBase)[0]
return customBase == _getCustomBase(m.solver)
return customBase == femutils.getCustomBase(m.solver)
return False


Expand All @@ -146,13 +145,13 @@ def _createMachine(solver, path, testmode):
if path is not None:
_dirTypes[path] = None
elif setting == settings.BESIDE:
path = _getBesideDir(solver)
path = femutils.getBesideDir(solver)
_dirTypes[path] = settings.BESIDE
elif setting == settings.TEMPORARY:
path = _getTempDir(solver)
path = femutils.getTempDir(solver)
_dirTypes[path] = settings.TEMPORARY
elif setting == settings.CUSTOM:
path = _getCustomDir(solver)
path = femutils.getCustomDir(solver)
_dirTypes[path] = settings.CUSTOM
m = solver.Proxy.createMachine(solver, path, testmode)
oldMachine = _machines.get(solver)
Expand All @@ -162,63 +161,6 @@ def _createMachine(solver, path, testmode):
return m


def _getTempDir(solver):
return tempfile.mkdtemp(prefix="fcfemsolv_")


def _getBesideDir(solver):
base = _getBesideBase(solver)
specificPath = os.path.join(base, solver.Label)
specificPath = _getUniquePath(specificPath)
if not os.path.isdir(specificPath):
os.makedirs(specificPath)
return specificPath


def _getBesideBase(solver):
fcstdPath = solver.Document.FileName
if fcstdPath == "":
error_message = (
"Please save the file before executing the solver. "
"This must be done because the location of the working "
"directory is set to \"Beside *.FCStd File\"."
)
App.Console.PrintError(error_message + "\n")
if App.GuiUp:
QtGui.QMessageBox.critical(
FreeCADGui.getMainWindow(),
"Can't start Solver",
error_message
)
raise MustSaveError()
return os.path.splitext(fcstdPath)[0]


def _getCustomDir(solver):
base = _getCustomBase(solver)
specificPath = os.path.join(
base, solver.Document.Name, solver.Label)
specificPath = _getUniquePath(specificPath)
if not os.path.isdir(specificPath):
os.makedirs(specificPath)
return specificPath


def _getCustomBase(solver):
path = settings.get_custom_dir()
if not os.path.isdir(path):
error_message = "Selected working directory doesn't exist."
App.Console.PrintError(error_message + "\n")
if App.GuiUp:
QtGui.QMessageBox.critical(
FreeCADGui.getMainWindow(),
"Can't start Solver",
error_message
)
raise DirectoryDoesNotExistError("Invalid path")
return path


def _getUniquePath(path):
postfix = 1
if path in _dirTypes:
Expand Down
4 changes: 2 additions & 2 deletions src/Mod/Fem/femtools/ccxtools.py
Expand Up @@ -684,8 +684,8 @@ def setup_working_dir(self, param_working_dir=None, create=False):
"Dir \'{}\' doesn't exist or cannot be created.\n"
.format(self.working_dir)
)
from femsolver.run import _getTempDir
self.working_dir = _getTempDir(self.solver)
from femtools.femutils import getTempDir
self.working_dir = getTempDir(self.solver)
FreeCAD.Console.PrintMessage(
"Dir \'{}\' will be used instead.\n"
.format(self.working_dir)
Expand Down
76 changes: 71 additions & 5 deletions src/Mod/Fem/femtools/femutils.py
Expand Up @@ -27,8 +27,16 @@
__url__ = "http://www.freecadweb.org"


import os
import sys

import FreeCAD
from femsolver import run
from femsolver import settings
from femsolver.run import _getUniquePath as getUniquePath
if FreeCAD.GuiUp:
import FreeCADGui
from PySide import QtGui


# analysis and its members
Expand Down Expand Up @@ -133,24 +141,82 @@ def is_derived_from(obj, t):
return obj.isDerivedFrom(t)


# ************************************************************************************************
# working dir
def get_pref_working_dir(solver_obj):
# _dirTypes from run are not used
# be aware beside could get an error if the document has not been saved
from femsolver import settings
from femsolver import run
dir_setting = settings.get_dir_setting()
if dir_setting == settings.TEMPORARY:
setting_working_dir = run._getTempDir(solver_obj)
setting_working_dir = getTempDir(solver_obj)
elif dir_setting == settings.BESIDE:
setting_working_dir = run._getBesideDir(solver_obj)
setting_working_dir = getBesideDir(solver_obj)
elif dir_setting == settings.CUSTOM:
setting_working_dir = run._getCustomDir(solver_obj)
setting_working_dir = getCustomDir(solver_obj)
else:
setting_working_dir = ""
return setting_working_dir


def getTempDir(obj):
from tempfile import mkdtemp
return mkdtemp(prefix="fcfemsolv_")


def getBesideDir(obj):
base = getBesideBase(obj)
specificPath = os.path.join(base, obj.Label)
specificPath = getUniquePath(specificPath)
if not os.path.isdir(specificPath):
os.makedirs(specificPath)
return specificPath


def getCustomDir(obj):
base = getCustomBase(obj)
specificPath = os.path.join(
base, obj.Document.Name, obj.Label)
specificPath = getUniquePath(specificPath)
if not os.path.isdir(specificPath):
os.makedirs(specificPath)
return specificPath


def getBesideBase(obj):
fcstdPath = obj.Document.FileName
if fcstdPath == "":
error_message = (
"Please save the file before executing a solver or creating a mesh. "
"This must be done because the location of the working directory "
"is set to \"Beside *.FCStd File\"."
)
FreeCAD.Console.PrintError(error_message + "\n")
if FreeCAD.GuiUp:
QtGui.QMessageBox.critical(
FreeCADGui.getMainWindow(),
"Can't start Solver or Mesh creation.",
error_message
)
raise run.MustSaveError()
return os.path.splitext(fcstdPath)[0]


def getCustomBase(solver):
path = settings.get_custom_dir()
if not os.path.isdir(path):
error_message = "Selected working directory doesn't exist."
FreeCAD.Console.PrintError(error_message + "\n")
if FreeCAD.GuiUp:
QtGui.QMessageBox.critical(
FreeCADGui.getMainWindow(),
"Can't start Solver or Mesh creation.",
error_message
)
raise run.DirectoryDoesNotExistError("Invalid path")
return path


# ************************************************************************************************
# other
def get_part_to_mesh(mesh_obj):
"""
Expand Down

0 comments on commit 2afd93b

Please sign in to comment.