New FeaturePython is grey

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: New FeaturePython is grey

Post by bernd »

wmayer wrote:
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
You must return True. ...
Thanks Werner. I will adapt this to my FemShellThicknessTaskPanel.

New problems araises with my App::FeaturePython. Assumed the App::FeaturePython is a new Fem Object. I would like to be able to use drag and drop in a MechanicalAnalysis object. How do I get the classTypeID or what do I need to add in src/Mod/Fem/Gui/ViewProviderAnalysis.cpp in Line 181 (https://github.com/FreeCAD/FreeCAD/blob ... s.cpp#L181)

code for creation of a App::FeaturePython and MechanicalAnalysis to test drag & drop:

Code: Select all

# 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
import MechanicalAnalysis
FreeCAD.newDocument()

MechanicalAnalysis.makeMechanicalAnalysis('myFEMAnalysis')

myobjApp = FreeCAD.ActiveDocument.addObject("App::FeaturePython","MyObject")
MyFeaturePython(myobjApp)
_ViewProviderMyFeaturePython(myobjApp.ViewObject)

FreeCAD.ActiveDocument.recompute()
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: New FeaturePython is grey

Post by bernd »

I have new problems with FeaturePython ViewProvider ... It seams not possible to call unsetEdit from within the setEdit !? I'm not able to unset the edit mode of an object. How to reproduce:

- start FreeCAD and make a new document
- run the code
- double click on the object
- the object is still in edit mode (it is yellow), but it should be not !


Code: Select all

# 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"

    def doubleClicked(self, vobj):
        doc = FreeCADGui.getDocument(vobj.Object.Document)
        if not doc.getInEdit():
            doc.setEdit(vobj.Object.Name)
        else:
            FreeCAD.Console.PrintError('Active Task Dialog found! Please close this one first!\n')
        return True

    def setEdit(self, vobj, mode=0):
        if False:
            pass
            # open the task panels if test is ok
        else:
            FreeCAD.Console.PrintError('Test is not ok we do not open the task panel, insted unset Edit mode.\n')
            self.unsetEdit(vobj)
        return

    def unsetEdit(self, vobj, mode=0):
        print 'unset Edit'
        return


# create some objects
from pivy import coin

myobjApp = FreeCAD.ActiveDocument.addObject("App::FeaturePython","MyObject")
MyFeaturePython(myobjApp)
_ViewProviderMyFeaturePython(myobjApp.ViewObject)

FreeCAD.ActiveDocument.recompute()
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: New FeaturePython is grey

Post by DeepSOIC »

bernd wrote:It seams not possible to call unsetEdit from within the setEdit !? I'm
I also experienced that. As a workaround, you can just use doubleClicked(), test conditions there and not invoke setEdit. But I also would like if it was possible to resetEdit directly from setEdit. Also, Gui.ActiveDocument.resetEdit() seems to work much bettern than the method of viewprovider, for reasons unknown to me.
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: New FeaturePython is grey

Post by bernd »

DeepSOIC wrote:
bernd wrote:It seams not possible to call unsetEdit from within the setEdit !? I'm
I also experienced that.
Good to know it was not a mistake from my side.
DeepSOIC wrote:As a workaround, you can just use doubleClicked(), test conditions there and not invoke setEdit. But I also would like if it was possible to resetEdit directly from setEdit.
I had this idea too, but I do not konw how to invoke the doubleClicked from my commandclass from outside from another module as it is done for the setEdit by object.ViewObject.startEditing() What I did was an helper def outside the viewprovider class which returns true or false and this is called either from the doubleClicked or from the commandmodule from outside the viewprovider. On True I call object.ViewObject.startEditing() But it would be much smarter if this check could be inside the setEdit. See call https://github.com/berndhahnebach/FreeC ... ult.py#L52 and helper dev https://github.com/berndhahnebach/FreeC ... lt.py#L123
DeepSOIC wrote:Also, Gui.ActiveDocument.resetEdit() seems to work much bettern than the method of viewprovider
But also does not work if called from inside setEdit
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: New FeaturePython is grey

Post by bernd »

Found a smart way by overwriting startEditing, but it only works on double click. If startEditing is called from outside of the ViewProvider it does not work :( . I would assume it should work from outside too, but werner probably will explain why not ...

here we are, the code:

Code: Select all

# 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.ViewObject = vobj
        self.Object = vobj.Object
        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"

    def doubleClicked(self, vobj):
        print 'double clicked'
        doc = FreeCADGui.getDocument(vobj.Object.Document)
        if not doc.getInEdit():
            #doc.setEdit(vobj.Object.Name)
            self.startEditing()
        else:
            FreeCAD.Console.PrintError('Active Task Dialog found! Please close this one first!\n')
        return True

    def startEditing(self,mode=0):
        "sets this object in edit mode"
        print 'start editing'
        print self.Object.MySwitch
        if self.Object.MySwitch:
            print 'START edit mode'
            return self.ViewObject.startEditing(mode)
        else:
            print 'NO edit mode'
            return False

    def setEdit(self, vobj, mode=0):
        print 'set Edit'
        # open the task panels if test is ok
        return True

    def unsetEdit(self, vobj, mode=0):
        print 'unset Edit'
        return


# create some objects
from pivy import coin

myobjApp = FreeCAD.ActiveDocument.addObject("App::FeaturePython","MyObject")
MyFeaturePython(myobjApp)
_ViewProviderMyFeaturePython(myobjApp.ViewObject)

FreeCAD.ActiveDocument.recompute()
new document
run code from above
than:
MySwitch is False --> make double click on object --> no edit mode
MySwitch is True --> make double click on object --> edit mode

MySwitch is True --> run App.ActiveDocument.MyObject.ViewObject.startEditing() --> edit mode
MySwitch is False -->run App.ActiveDocument.MyObject.ViewObject.startEditing() --> edit mode !?! --> should be no edit mode !?!
wmayer
Founder
Posts: 20305
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: New FeaturePython is grey

Post by wmayer »

I have new problems with FeaturePython ViewProvider ... It seams not possible to call unsetEdit from within the setEdit !? I'm not able to unset the edit mode of an object. How to reproduce:
Calling unsetEdit inside setEdit doesn't make sense because at the point when entering setEdit nothing has happened to the view provider. The setEdit method implements the relevant part (e.g. opening a task panel) and when it returns true the calling instance does the highlighting and signalling.

The setEdit of the ViewProviderPythonFeatureT class is broken and doesn't handle correctly the return value of the Python class. This is fixed now in git commit f556359

So, you have to return "False" in your setEdit method to avoid to get it in edit mode.
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: New FeaturePython is grey

Post by bernd »

:D

code to test:

Code: Select all

# 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()
        self.Object = vobj.Object
        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"

    def doubleClicked(self, vobj):
        doc = FreeCADGui.getDocument(vobj.Object.Document)
        if not doc.getInEdit():
            doc.setEdit(vobj.Object.Name)
        else:
            FreeCAD.Console.PrintError('Active Task Dialog found! Please close this one first!\n')
        return True

    def setEdit(self, vobj, mode=0):
        print self.Object.MySwitch
        if self.Object.MySwitch:
            print 'START edit mode'
            return True
        else:
            print 'NO edit mode'
            return False

    def unsetEdit(self, vobj, mode=0):
        print 'unset Edit'
        return


# create some objects
from pivy import coin

myobjApp = FreeCAD.ActiveDocument.addObject("App::FeaturePython","MyObject")
MyFeaturePython(myobjApp)
_ViewProviderMyFeaturePython(myobjApp.ViewObject)

FreeCAD.ActiveDocument.recompute()

new document
run code from above
than:
MySwitch is False --> make double click on object --> no edit mode
MySwitch is True --> make double click on object --> edit mode

MySwitch is True --> run App.ActiveDocument.MyObject.ViewObject.startEditing() --> edit mode
MySwitch is False -->run App.ActiveDocument.MyObject.ViewObject.startEditing() --> edit mode
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: New FeaturePython is grey

Post by bernd »

BTW:
What is best returned from the unsetEdit method. Or the other way around ... Does it makes a difference if I would return, return True or return False?
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: New FeaturePython is grey

Post by bernd »

bernd wrote: Wed Mar 01, 2017 12:49 pm What is best returned from the unsetEdit method? Or the other way around ... Does it makes a difference if I would return, return True or return False?
up
jaisejames
Posts: 384
Joined: Sat Sep 24, 2016 6:51 am

Re: New FeaturePython is grey

Post by jaisejames »

bernd wrote: Fri Sep 04, 2015 1:10 pm
the icon of the App::FeaturePython is coloured :D
Thank for example. I having same issue. It is solved by this. :)
Post Reply