# create a new FreeCAD Document object
myobj = FreeCAD.ActiveDocument.addObject("App::FeaturePython","MyObject")
# create instanzes of class _MyFeaturePython and _ViewProviderMyFeaturePython
_MyFeaturePython(myobj)
_ViewProviderMyFeaturePython(myobj.ViewObject)
# recompute
FreeCAD.ActiveDocument.recompute()
Why is the new object grey in treeview ? It looks like it is not visible? How do I get some color ? Visibility is set to true.
Usually when a feature is greyed out in the tree, it is because either it has no view provider, or the view provider has some problems... Maybe some more methods (getDisplayMode, etc...) of the view provider are mandatory, I don't remember exactly which ones are or not... Look into src/Mod/TemplatePyMod/FeaturePython.py, you should have all of them there.
class MyFeaturePython:
def __init__(self, obj):
"'''Add a custom property to the FeaturePython'''"
obj.addProperty("App::PropertyBool","MySwitch","Base","ToolTipSwitchMyFeaturePython")
obj.MySwitch = False
obj.Proxy = self
def execute(self, fp):
"'''Do something when doing a recomputation, this method is mandatory'''"
FreeCAD.Console.PrintMessage("Recompute MyFeaturePython\n")
FreeCAD.newDocument()
myobjPart = FreeCAD.ActiveDocument.addObject("Part::FeaturePython","MyObject") # coloured icon in tree
MyFeaturePython(myobjPart)
myobjPart.ViewObject.Proxy = 0
myobjApp = FreeCAD.ActiveDocument.addObject("App::FeaturePython","MyObject") # grey icon in tree
MyFeaturePython(myobjApp)
myobjApp.ViewObject.Proxy = 0
# recompute
FreeCAD.ActiveDocument.recompute()
Mhh why is Part::FeaturePython coloured and App::FeaturePython grey in TreeView? Since I do not have a shape in my scripted object I would tend to use App::FeaturePython but it looks very poor in TreeView. What should I use if I do not need a shape in my scripted object?
Indeed you should be able to use App::FeaturePython without problems, it is used for ex. for the dimension object in Draft...
My guess is that the Part::FeaturePython comes with a "pre-filled" view provider. When you are using App::FeaturePython, probably there are some things you need to do in your python class that are mandatory, that you didn't do (but that exist in Part::FeaturePython)... Adding a default Display Mode maybe?
def attach(self, vobj):
self.standard = coin.SoGroup()
vobj.addDisplayMode(self.standard,"Standard");
def getDisplayModes(self,obj):
"'''Return a list of display modes.'''"
return ["Standard"]
def getDefaultDisplayMode(self):
"'''Return the name of the default display mode. It must be defined in getDisplayModes.'''"
return "Standard"
# simple scripted object class definition:
class MyFeaturePython:
def __init__(self, obj):
"'''Add a custom property to the FeaturePython'''"
obj.addProperty("App::PropertyBool","MySwitch","Base","ToolTipSwitchMyFeaturePython")
obj.MySwitch = False
obj.Proxy = self
def execute(self, fp):
"'''Do something when doing a recomputation, this method is mandatory'''"
FreeCAD.Console.PrintMessage("Recompute MyFeaturePython\n")
class _ViewProviderMyFeaturePython:
"A View Provider for the MyFeaturePython object"
def __init__(self, vobj):
vobj.Proxy = self
def attach(self, vobj):
self.standard = coin.SoGroup()
vobj.addDisplayMode(self.standard,"Standard");
def getDisplayModes(self,obj):
"'''Return a list of display modes.'''"
return ["Standard"]
def getDefaultDisplayMode(self):
"'''Return the name of the default display mode. It must be defined in getDisplayModes.'''"
return "Standard"
# create some objects
from pivy import coin
FreeCAD.newDocument()
myobjPart = FreeCAD.ActiveDocument.addObject("Part::FeaturePython","MyObject")
MyFeaturePython(myobjPart)
_ViewProviderMyFeaturePython(myobjPart.ViewObject)
myobjApp = FreeCAD.ActiveDocument.addObject("App::FeaturePython","MyObject")
MyFeaturePython(myobjApp)
_ViewProviderMyFeaturePython(myobjApp.ViewObject)
# recompute
FreeCAD.ActiveDocument.recompute()
New problems I'm not able to solve ... I added a simple EditTaksPanel but neither on call of def accept nor def reject the def unsetEdit of the ViewProvider is called ?!? What do I need to call in def reject or def accept to close the dialog ?!
How to reproduce: Copy the code into the PythonKnsole, double click on the new object. Try to click on OK or Cancel the dialog stays open
def doubleClicked(self,vobj):
taskd = _FemShellThicknessTaskPanel(self.Object)
taskd.obj = vobj.Object
#taskd.update() # When would this be needed ?
FreeCADGui.Control.showDialog(taskd)
close the TaskPanel in the accept and reject defs off the TaskPanel: