Skip to content

Commit

Permalink
v2.46 -- do not require dd object be selected if only one dd object i…
Browse files Browse the repository at this point in the history
…n active document for add, remove, edit tooltip, rename, and move group commands
  • Loading branch information
mwganson committed Apr 8, 2022
1 parent 869e406 commit 5b52c52
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 58 deletions.
129 changes: 74 additions & 55 deletions DynamicDataCmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
__title__ = "DynamicData"
__author__ = "Mark Ganson <TheMarkster>"
__url__ = "https://github.com/mwganson/DynamicData"
__date__ = "2022.03.16"
__version__ = "2.45"
version = 2.45
__date__ = "2022.04.08"
__version__ = "2.46"
version = 2.46
mostRecentTypes=[]
mostRecentTypesLength = 5 #will be updated from parameters

Expand Down Expand Up @@ -238,9 +238,9 @@ def getHelp(self):
#Gui.addCommand("DynamicDataCreateObject", DynamicDataCreateObjectCommandClass())

class MultiTextInput(QtGui.QDialog):
def __init__(self):
def __init__(self, obj):
QtGui.QDialog.__init__(self)

self.obj = obj
layout = QtGui.QGridLayout()
#layout.setColumnStretch(1, 1)
self.addAnotherProp = False
Expand Down Expand Up @@ -347,8 +347,7 @@ def on_edit_finished(self):
self.valueEdit.setEnabled(True)
self.tooltipEdit.setEnabled(True)
propertyName = self.nameEdit.text()
obj = FreeCADGui.Selection.getSelection()[0]
if hasattr(obj,'dd'+propertyName):
if hasattr(self.obj,'dd'+propertyName):
self.label2.setText('Property name already exists')
else:
self.label2.setText('')
Expand All @@ -363,6 +362,8 @@ class DynamicDataAddPropertyCommandClass(object):
global mostRecentTypes
global mostRecentTypesLength

def __init__(self):
self.obj = None

def getPropertyTypes(self):
return propertyTypes
Expand All @@ -378,7 +379,7 @@ def Activated(self):
global mostRecentTypesLength

doc = FreeCAD.ActiveDocument
obj = Gui.Selection.getSelectionEx()[0].Object
obj = self.obj
if not 'FeaturePython' in str(obj.TypeId):
FreeCAD.Console.PrintError('DynamicData Workbench: Cannot add property to non-FeaturePython objects.\n')
return
Expand All @@ -394,7 +395,7 @@ def Activated(self):
recent.insert(0,mostRecentTypes[ii])
pg.SetString('mru'+str(ii), mostRecentTypes[ii])

dlg = MultiTextInput()
dlg = MultiTextInput(obj)
dlg.setWindowFlags(windowFlags)
dlg.setWindowTitle("DynamicData")
dlg.label.setText("Old-style name;group;tip;value syntax\nstill supported in Name field\n\nIn Value field:\nUse =expr for expressions, e.g. =Box.Height\n")
Expand Down Expand Up @@ -554,13 +555,15 @@ def Activated(self):
def IsActive(self):
if not FreeCAD.ActiveDocument:
return False
selection = Gui.Selection.getSelectionEx()
if not selection:
return False
if not hasattr(selection[0].Object,"DynamicData"):
return False

return True
selection = Gui.Selection.getSelection()
if len(selection) == 1 and hasattr(selection[0],"DynamicData"):
self.obj = selection[0]
return True
objs = [obj for obj in FreeCAD.ActiveDocument.Objects if hasattr(obj, "DynamicData")]
if len(objs) == 1:
self.obj = objs[0]
return True
return False

def __init__(self):
#global mostRecentTypes
Expand Down Expand Up @@ -754,9 +757,7 @@ def getPropertiesOfGroup(self,obj,group):
def Activated(self):
doc = FreeCAD.ActiveDocument
selection = Gui.Selection.getSelection()
if not selection:
return
obj = selection[0]
obj = self.obj
#remove the property
window = FreeCADGui.getMainWindow()
items = self.getGroups(obj)
Expand Down Expand Up @@ -794,22 +795,24 @@ def Activated(self):
except Exception as ex:
FreeCAD.Console.PrintError(f"Cannot move {prop}, only dynamic properties are supported\n")
doc.commitTransaction()
FreeCADGui.Selection.removeSelection(obj)
FreeCADGui.Selection.addSelection(obj)
if obj in selection:
FreeCADGui.Selection.removeSelection(obj)
FreeCADGui.Selection.addSelection(obj)
doc.recompute()
return

def IsActive(self):
if not FreeCAD.ActiveDocument:
return False
selection = Gui.Selection.getSelection()
if not selection:
return False
obj = selection[0]
if len(self.getGroups(obj))==0:
return False
return True

if len(selection) == 1 and hasattr(selection[0],"DynamicData"):
self.obj = selection[0]
return True
objs = [obj for obj in FreeCAD.ActiveDocument.Objects if hasattr(obj, "DynamicData")]
if len(objs) == 1:
self.obj = objs[0]
return True
return False
########################################################################################
# Rename a custom dynamic property

Expand Down Expand Up @@ -888,7 +891,6 @@ def getInExprs(self, obj, prop):

def Activated(self):
doc = FreeCAD.ActiveDocument
#win = FreeCADGui.getMainWindow()
obj = self.obj
prop = self.getProperty(obj) #string name of property
if not prop:
Expand All @@ -915,19 +917,24 @@ def Activated(self):
inExpr[0].setExpression(inExpr[1], inExpr[2].replace(prop,newName))
obj.removeProperty(prop)
obj.Document.commitTransaction()
FreeCADGui.Selection.removeSelection(obj)
FreeCADGui.Selection.addSelection(obj)
if obj in FreeCADGui.Selection.getSelection():
FreeCADGui.Selection.removeSelection(obj)
FreeCADGui.Selection.addSelection(obj)
doc.recompute()
return

def IsActive(self):
if not FreeCAD.ActiveDocument:
return False
selection = Gui.Selection.getSelectionEx()
if not selection:
return False
self.obj = selection[0].Object
return True
selection = Gui.Selection.getSelection()
if len(selection) == 1 and hasattr(selection[0],"DynamicData"):
self.obj = selection[0]
return True
objs = [obj for obj in FreeCAD.ActiveDocument.Objects if hasattr(obj, "DynamicData")]
if len(objs) == 1:
self.obj = objs[0]
return True
return False

#Gui.addCommand("DynamicDataRenameProperty", DynamicDataRenamePropertyCommandClass())
########################################################################################
Expand Down Expand Up @@ -996,19 +1003,24 @@ def Activated(self):
obj.Document.openTransaction(f"Set tooltip of {prop}")
obj.setDocumentationOfProperty(prop, newTip)
obj.Document.commitTransaction()
FreeCADGui.Selection.removeSelection(obj)
FreeCADGui.Selection.addSelection(obj)
if obj in FreeCADGui.Selection.getSelection():
FreeCADGui.Selection.removeSelection(obj)
FreeCADGui.Selection.addSelection(obj)
doc.recompute()
return

def IsActive(self):
if not FreeCAD.ActiveDocument:
return False
selection = Gui.Selection.getSelectionEx()
if not selection:
return False
self.obj = selection[0].Object
return True
selection = Gui.Selection.getSelection()
if len(selection) == 1 and hasattr(selection[0],"DynamicData"):
self.obj = selection[0]
return True
objs = [obj for obj in FreeCAD.ActiveDocument.Objects if hasattr(obj, "DynamicData")]
if len(objs) == 1:
self.obj = objs[0]
return True
return False

#Gui.addCommand("DynamicDataSetTooltip", DynamicDataSetTooltipCommandClass())

Expand All @@ -1020,6 +1032,9 @@ def IsActive(self):
class DynamicDataRemovePropertyCommandClass(object):
"""Remove Property Command"""

def __init__(self):
self.obj = None

def GetResources(self):
return {'Pixmap' : os.path.join( iconPath , 'RemoveProperty.svg'),
'MenuText': "&Remove Property",
Expand All @@ -1028,13 +1043,14 @@ def GetResources(self):

def getProperties(self,obj):
props = [p for p in obj.PropertiesList if self.isDynamic(obj,p)]
dlg = None
if props:
dlg = SelectObjects(props,"Select dynamic properties to remove")
dlg.all.setCheckState(QtCore.Qt.Unchecked)
ok = dlg.exec_()
if not ok:
return []
return dlg.selected
return dlg.selected if dlg else []

def isDynamic(self,obj,prop):
if prop == "DynamicData":
Expand All @@ -1051,10 +1067,8 @@ def isDynamic(self,obj,prop):

def Activated(self):
doc = FreeCAD.ActiveDocument
selection = Gui.Selection.getSelectionEx()
if not selection:
return
obj = selection[0].Object
selection = Gui.Selection.getSelection()
obj = self.obj
#remove the property
window = QtGui.QApplication.activeWindow()
items = self.getProperties(obj)
Expand All @@ -1067,18 +1081,24 @@ def Activated(self):
except Exception as ex:
FreeCAD.Console.PrintError(f"DynamicData::Exception cannot remove {item}\n{ex}")
obj.Document.commitTransaction()
FreeCADGui.Selection.removeSelection(obj)
FreeCADGui.Selection.addSelection(obj)
if obj in selection:
FreeCADGui.Selection.removeSelection(obj)
FreeCADGui.Selection.addSelection(obj)
doc.recompute()
return

def IsActive(self):
if not FreeCAD.ActiveDocument:
return False
selection = Gui.Selection.getSelectionEx()
if not selection:
return False
return True
selection = Gui.Selection.getSelection()
if len(selection) == 1 and hasattr(selection[0],"DynamicData"):
self.obj = selection[0]
return True
objs = [obj for obj in FreeCAD.ActiveDocument.Objects if hasattr(obj, "DynamicData")]
if len(objs) == 1:
self.obj = objs[0]
return True
return False

#Gui.addCommand("DynamicDataRemoveProperty", DynamicDataRemovePropertyCommandClass())

Expand Down Expand Up @@ -1215,7 +1235,6 @@ def Activated(self):
FreeCAD.Console.PrintWarning('DynamicData: skipping existing property: '+name+'\n')
continue


FreeCAD.ActiveDocument.commitTransaction()
doc.recompute()
if len(aliases)==0:
Expand Down
6 changes: 3 additions & 3 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<package format="1">
<name>DynamicData</name>
<description>Container object for holding custom properties, alternative to spreadsheet</description>
<version>2.45</version>
<date>2022.03.16</date>
<version>2.46</version>
<date>2022.04.08</date>
<maintainer email="[email protected]">TheMarkster</maintainer>
<license file="LICENSE">LGPL-2.1</license>
<url type="repository" branch="master">https://github.com/mwganson/DynamicData</url>
Expand All @@ -13,7 +13,7 @@
<name>DynamicData</name>
<classname>DynamicDataWorkbench</classname>
<description>Container object for holding custom properties, alternative to spreadsheet</description>
<version>2.45</version>
<version>2.46</version>
<subdirectory>./</subdirectory>
<icon>Resources/icons/DynamicDataLogo.svg</icon>
<freecadmin>0.18.0</freecadmin>
Expand Down

0 comments on commit 5b52c52

Please sign in to comment.