Help to pass Variable into TaskPanel

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
johnwang
Veteran
Posts: 1345
Joined: Sun Jan 27, 2019 12:41 am

Help to pass Variable into TaskPanel

Post by johnwang »

Hi,

I got x1 in Class A and dL in Class aTaskPanel.

How do I pass x1 into Class aTaskPanel or pass dL back into Class A. Tried using Global in some places without success. Where should I declare these variables?

Regards,

John

Code: Select all

class aTaskPanel:
    def accept(self):
        ...
        dL = float(self.form.lengthInput.text())
        x2=x1+dL
        FreeCADGui.Control.closeDialog()
class A:
    def Activated(self):
        ...
        self.callback = self.view.addEventCallbackPivy(SoEvent.getClassTypeId(), self.getinput)
    def getinput(self, event_cb):
        ...
        x1=0
        panel = aTaskPanel()
        FreeCADGui.Control.showDialog(panel)
        self.view.removeEventCallbackPivy(SoEvent.getClassTypeId(), self.callback)
hfc series CAE workbenches for FreeCAD (hfcNastran95, hfcMystran, hfcFrame3DD, hfcSU2 and more)
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Help to pass Variable into TaskPanel

Post by onekk »

to pass some value when calling you have to ues a init() function.

Or you could use something similar.

Code: Select all

import module_vars as vars

vars.x1 = 10

class aTaskPanel:
    def accept(self):
        ...
        dL = float(self.form.lengthInput.text())
        vars.x2 = vars.x1 + dL
        FreeCADGui.Control.closeDialog()
  
obviously you have to have a module_vars file simply holding variable names.

I use it with many guis I have made to spread values across different module files, simply importing the vars module.


Hope this help

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
johnwang
Veteran
Posts: 1345
Joined: Sun Jan 27, 2019 12:41 am

Re: Help to pass Variable into TaskPanel

Post by johnwang »

onekk wrote: Fri Nov 26, 2021 8:11 am to pass some value when calling you have to ues a init() function.
Carlo D.
Thanks Carlo. I prefer to use the init() function.

From here, https://stackoverflow.com/questions/733 ... es-objects
it say: "If you want to get the value of self.var1 from Class2, you must initialize var1 outside the class , and make it global inside the function where you add it a number"

Its demo code works.

Code: Select all

var1 = 0
class class1 :

    def meth1 (self):
        global var1
        var1 += 1

class class2:

    def meth2 (self):
        print(var1)

obj1 = class1()
obj2 = class2()

obj1.meth1()
obj2.meth2()


But still can't figure out my version.
hfc series CAE workbenches for FreeCAD (hfcNastran95, hfcMystran, hfcFrame3DD, hfcSU2 and more)
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Help to pass Variable into TaskPanel

Post by onekk »

Sorry version of what?

It depends, on what you are doing, as I usually make complex programs, having a place where to store variables that spreads across different modules, is very useful.

But as usual, there are many ways to do things.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
johnwang
Veteran
Posts: 1345
Joined: Sun Jan 27, 2019 12:41 am

Re: Help to pass Variable into TaskPanel

Post by johnwang »

stackoverflow version into my program. can't make it work.

I'll try to make a module_vars file.
hfc series CAE workbenches for FreeCAD (hfcNastran95, hfcMystran, hfcFrame3DD, hfcSU2 and more)
Post Reply