FEM: move working path methods from solver run to utils

This commit is contained in:
Bernd Hahnebach
2019-09-05 18:20:55 +02:00
parent e6a96e1100
commit 2afd93bb55
4 changed files with 79 additions and 71 deletions

View File

@@ -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

View File

@@ -28,7 +28,6 @@ __url__ = "http://www.freecadweb.org"
import os
import os.path
import tempfile
import threading
import shutil
@@ -128,7 +127,7 @@ 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
@@ -136,7 +135,7 @@ def _isPathValid(m, path):
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
@@ -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)
@@ -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:

View File

@@ -684,8 +684,8 @@ class FemToolsCcx(QtCore.QRunnable, QtCore.QObject):
"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)

View File

@@ -27,8 +27,16 @@ __author__ = "Markus Hovorka, Bernd Hahnebach"
__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
@@ -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):
"""