Skip to content

Commit

Permalink
[Material] fix card selection bug
Browse files Browse the repository at this point in the history
- fixes bug number 7 of this list: https://forum.freecad.org/viewtopic.php?style=5&p=489666#p489666

- also fix warning about too short variable name
  • Loading branch information
donovaly committed Feb 19, 2023
1 parent 8a16e01 commit 14de59e
Showing 1 changed file with 43 additions and 7 deletions.
50 changes: 43 additions & 7 deletions src/Mod/Material/MaterialEditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def __init__(self, obj=None, prop=None, material=None, card_path="", category="S
self.materials = {}
self.cards = {}
self.icons = {}
self.initialIndex = -1
self.card_path = card_path
filePath = os.path.dirname(__file__) + os.sep
self.iconPath = (filePath + "Resources" + os.sep + "Icons" + os.sep)
Expand Down Expand Up @@ -130,8 +131,8 @@ def __init__(self, obj=None, prop=None, material=None, card_path="", category="S

if self.card_path:
# we need the index of this path
index = self.widget.ComboMaterial.findData(self.card_path)
self.chooseMaterial(index)
self.initialIndex = self.widget.ComboMaterial.findData(self.card_path)
self.chooseMaterial(self.initialIndex)

# TODO: What if material and card_name was given?
# In such case ATM material is chosen, give some feedback for all those corner cases.
Expand Down Expand Up @@ -501,28 +502,63 @@ def getColorHash(self, col, val=255):

def openfile(self):
"Opens a FCMat file"
if self.category == "Solid":
directory = self.directory + os.sep + "StandardMaterial"
else:
directory = self.directory + os.sep + "FluidMaterial"
filetuple = QtGui.QFileDialog.getOpenFileName(
QtGui.QApplication.activeWindow(),
"Open FreeCAD Material file",
self.directory,
directory,
"*.FCMat"
)
self.card_path = filetuple[0]
# we cannot simply execute findData(self.card_path) because e.g. on Windows
# the return path has "/" as folder separator, but the paths in the ComboMaterial
# have also some "\" in them. For example a path can look like this:
# D:/FreeCAD-build/data/Mod\Material\FluidMaterial\Air.FCMat
# To keep it simple, we take a path from the ComboMaterial and change only the
# material card filename
if self.initialIndex > -1:
path = self.widget.ComboMaterial.itemData(self.initialIndex)
# at first check if we have a uniform usage
# if a character is not present, rsplit delivers the initial string
testBackslash = path.rsplit('\\', 1)[0]
testSlash = path.rsplit('/', 1)[0]
if testBackslash == path:
path = testBackslash.rsplit('/', 1)[0] + '/'
elif testSlash == path:
path = testSlash.rsplit('\\', 1)[0] + '\\'
# since we don't know if we have to deal with slash or backslash, take the
# longest result as path
else:
pathBackslash = path.rsplit('\\', 1)[0]
pathSlash = path.rsplit('/', 1)[0]
if len(pathBackslash) > len(pathSlash):
path = pathBackslash + '\\'
else:
path = pathSlash + '/'
# we know that the return path has uniformly either / or \ but not yet what
testBackslash = self.card_path.rsplit('\\', 1)[0]
if testBackslash == self.card_path:
self.card_path = path + self.card_path.rsplit('/', 1)[1]
else:
self.card_path = path + self.card_path.rsplit('\\', 1)[1]
index = self.widget.ComboMaterial.findData(self.card_path)

# check if card_path is in known path, means it is in combo box already
# if not print message, and give some feedbach that the card parameter are loaded
if os.path.isfile(self.card_path):
if index == -1:
FreeCAD.Console.PrintMessage(
"Card path: {} not found in known cards."
"Card path: {} not found in known cards.\n"
"The material parameter only are loaded.\n"
.format(self.card_path)
)
from importFCMat import read
d = read(self.card_path)
if d:
self.updateMatParamsInTree(d)
materialDict = read(self.card_path)
if materialDict:
self.updateMatParamsInTree(materialDict)
self.widget.ComboMaterial.setCurrentIndex(0)
# set combo box to the none material after tree params
else:
Expand Down

0 comments on commit 14de59e

Please sign in to comment.