A Little Help With PyQt Radio Buttons Please

Need help, or want to share a macro? Post here!
User avatar
quick61
Posts: 3793
Joined: Sat Aug 24, 2013 2:49 am
Location: u.S.A.

A Little Help With PyQt Radio Buttons Please

Postby quick61 » Mon Feb 03, 2014 8:00 pm

I have been reworking my Airfoil Import & Scale macro to include the option of either having the result end with a DWire or a BSpline. I know that individually the scripting for the Dwire or BSpline work on their own. I have gotten as far as having the dialog show, but it all kind of stops there. I know there is something wrong with the buttons - if/else routines, but I'm not figuring it out, and no errors in the report view make it kind of hard for me to know what is wrong. Not finding any good examples of how it is suppose to work. That's my main problem.
importer.png
importer.png (27.15 KiB) Viewed 3411 times
The second little one is when the script produces the airfoil, it also includes a "folder" in the tree view. Is there a way to not have it do that?

Thanks for any pointers you can offer.

Mark

Here is the macro as it stands at the moment. Looks like a couple of long lines got wrapped. -

Code: Select all

# # # #
#
# AIRFOIL IMPORT & SCALE v2.
# 
# Makes a scaled airfoil from either a DWire or Bspline
#
# # # #


from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QLineEdit, QRadioButton
import FreeCAD, FreeCADGui, Draft
import importAirfoilDAT

# Selcet .dat airfoil data file to be imported

filename = QtGui.QFileDialog.getOpenFileName(QtGui.qApp.activeWindow(),'Open An Airfoil File','*.dat')

class p():

    def proceed(self):
        if radio1.isChecked():
            try:
                scalefactor=float(self.s1.text())
                f1=str(filename)
                importAirfoilDAT.insert(f1,"Unnamed")
                Draft.scale(App.ActiveDocument.DWire,delta=App.Vector(scalefactor,scalefactor,scalefactor),center=App.Vector(0,0,0),legacy=True)
            
            except:
                FreeCAD.Console.PrintError("Error, not a valid .dat file\n")

            self.close()

        if radio2.isChecked():
            try:
                scalefactor=float(self.s1.text())
                f1=str(filename)
                importAirfoilDAT.insert(f1,"Unnamed")
                points = FreeCAD.ActiveDocument.ActiveObject.Points
                Draft.makeBSpline(points)
                Draft.scale(App.ActiveDocument.BSpline,delta=App.Vector(scalefactor,scalefactor,scalefactor),center=App.Vector(0,0,0),legacy=True)
                App.getDocument("Unnamed").removeObject("DWire")

            except:
                FreeCAD.Console.PrintError("Error, not a valid .dat file\n")

            self.close()

    def close(self):
        self.dialog.hide()


    def __init__(self):
        self.dialog = None
        self.s1 = None


        # Make dialog box and get the scale size

        self.dialog = QtGui.QDialog()
        self.dialog.resize(350,100)
        self.dialog.setWindowTitle("Airfoil Import & Scale")
        la = QtGui.QVBoxLayout(self.dialog)
        t1 = QtGui.QLabel("Chord Length")
        la.addWidget(t1)
        self.s1 = QtGui.QLineEdit()
        la.addWidget(self.s1)

        # Add radio buttons to select between DWire and BSpline

        self.radio1 = QRadioButton("Make DWire")
        self.radio2 = QRadioButton("Make BSpline")

            #set default to DWire & make radio buttons

        self.radio1.setChecked(True)
        la.addWidget(self.radio1)
        la.addWidget(self.radio2)

        # Add OK box

        okbox = QtGui.QDialogButtonBox(self.dialog)
        okbox.setOrientation(QtCore.Qt.Horizontal)
        okbox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
        la.addWidget(okbox)
        QtCore.QObject.connect(okbox, QtCore.SIGNAL("accepted()"), self.proceed)
        QtCore.QObject.connect(okbox, QtCore.SIGNAL("rejected()"), self.close)
        QtCore.QMetaObject.connectSlotsByName(self.dialog)
        self.dialog.show()
        self.dialog.exec_()

p()
This post made with 0.0% Micro$oft products - GOT LINUX?
User avatar
wandererfan
Posts: 3685
Joined: Tue Nov 06, 2012 5:42 pm

Re: A Little Help With PyQt Radio Buttons Please

Postby wandererfan » Mon Feb 03, 2014 8:30 pm

quick61 wrote:I know there is something wrong with the buttons - if/else routines, but I'm not figuring it out, and no errors in the report view make it kind of hard for me to know what is wrong.
missing "self"?

Code: Select all

    def proceed(self):
        if self.radio1.isChecked():
wf
User avatar
quick61
Posts: 3793
Joined: Sat Aug 24, 2013 2:49 am
Location: u.S.A.

Re: A Little Help With PyQt Radio Buttons Please

Postby quick61 » Mon Feb 03, 2014 8:53 pm

Edit - got it... closed=True

Who would of thought it would of been that simple? Well, I guess that would of been you wandererfan. Thank you! It's working much better now. Got a little trouble with the bspline not closing, but I'll hack away on that unless there is another quick pointer for me. :)

Mark
This post made with 0.0% Micro$oft products - GOT LINUX?
User avatar
quick61
Posts: 3793
Joined: Sat Aug 24, 2013 2:49 am
Location: u.S.A.

Re: A Little Help With PyQt Radio Buttons Please

Postby quick61 » Mon Feb 03, 2014 10:16 pm

Alright, now I have a couple of new problems.

The script works for the BSpline and scales properly when run from the editor, but when launched from the Execute Macro dialog or from a shortcut on the tool bar, it fails to scale.

The second problem is that adding another airfoil (DWire) in the same document causes the first airfoil to lose it's scale and the second airfoil is much, much larger than the input. Is there some sort of initializing that needs to be done or something else?

Mark
This post made with 0.0% Micro$oft products - GOT LINUX?
User avatar
shoogen
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: A Little Help With PyQt Radio Buttons Please

Postby shoogen » Mon Feb 03, 2014 10:29 pm

quick61 wrote:The second problem is that adding another airfoil (DWire) in the same document causes the first airfoil to lose it's scale and the second airfoil is much, much larger than the input. Is there some sort of initializing that needs to be done or something else?

Code: Select all

App.ActiveDocument.DWire
DWire is the name of the Object. You should not hard code it into your script. But rather save a reference to the actual object.(Which isn't possible when importing a file). Maybe ActiveObject makes more sense in this situation.
User avatar
quick61
Posts: 3793
Joined: Sat Aug 24, 2013 2:49 am
Location: u.S.A.

Postby quick61 » Tue Feb 04, 2014 12:14 am

shoogen wrote:
quick61 wrote:The second problem is that adding another airfoil (DWire) in the same document causes the first airfoil to lose it's scale and the second airfoil is much, much larger than the input. Is there some sort of initializing that needs to be done or something else?

Code: Select all

App.ActiveDocument.DWire
DWire is the name of the Object. You should not hard code it into your script. But rather save a reference to the actual object.(Which isn't possible when importing a file). Maybe ActiveObject makes more sense in this situation.
That worked very well for the DWire. Thanks!

Code: Select all

Draft.scale(App.ActiveDocument.ActiveObject,de~ ...
However, it does not work for the BSpline. Every new foil brought in using a Bspline, desacles the one before it. Strange?

I would try to go about the Bspline some other way, but according to the Wiki page Draft WireToBSpline there is no way to script the Dwire to Bspline tool. I'll take it this is still the case?

Mark
This post made with 0.0% Micro$oft products - GOT LINUX?
User avatar
wandererfan
Posts: 3685
Joined: Tue Nov 06, 2012 5:42 pm

Re: A Little Help With PyQt Radio Buttons Please

Postby wandererfan » Tue Feb 04, 2014 2:31 am

quick61 wrote:However, it does not work for the BSpline. Every new foil brought in using a Bspline, desacles the one before it. Strange?

Code: Select all

                Draft.scale(App.ActiveDocument.BSpline,delta=App.Vector(scalefactor,scalefactor,scalefactor),center=App.Vector(0,0,0),legacy=True)
"BSpline" is still in the document after the first run. The second time the macro would create "BSpline001" with the new data. You'd have to scale that.

I can't get the BSpline part to run, but it's probably something in my OCC rather than your macro. I get
"...in createGeometry
spline.interpolate(fp.Points, True)
<type 'exceptions.Exception'>: BSplCLib::Interpolate
BSpline: BSplCLib::Interpolate"

OS: Ubuntu 12.04.4 LTS
Platform: 64-bit
Version: 0.14.3061 (Git)
Branch: BezierProto
Hash: acb86a2ff0d3d39528541d18fef8268ee49e5ba4
Python version: 2.7.3
Qt version: 4.8.1
Coin version: 3.1.3
SoQt version: 1.5.0
OCC version: 6.6.0
User avatar
quick61
Posts: 3793
Joined: Sat Aug 24, 2013 2:49 am
Location: u.S.A.

Re: A Little Help With PyQt Radio Buttons Please

Postby quick61 » Tue Feb 04, 2014 2:42 am

Here is the script as it stands now -

I took out DWire and BSpline as suggested by shoogen and changed them to ActiveObject. I also added closed=True to Draft.makeBSpline(points, closed=True)

The change with ActiveObject worked great for the DWire but has no affect with BSpline. not sure what to make of that. :?

Code: Select all

# # # #
#
# AIRFOIL IMPORT & SCALE v2.
# 
# Makes a scaled airfoil from either a DWire or Bspline
#
# # # #


from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QLineEdit, QRadioButton
import FreeCAD, FreeCADGui, Draft
import importAirfoilDAT

# Selcet .dat airfoil data file to be imported

filename = QtGui.QFileDialog.getOpenFileName(QtGui.qApp.activeWindow(),'Open An Airfoil File','*.dat')

class p():

    def proceed(self):
        if self.radio1.isChecked():
            try:
                scalefactor=float(self.s1.text())
                f1=str(filename)
                importAirfoilDAT.insert(f1,"Unnamed")
                Draft.scale(App.ActiveDocument.ActiveObject,delta=App.Vector(scalefactor,scalefactor,scalefactor),center=App.Vector(0,0,0),legacy=True)
            
            except:
                FreeCAD.Console.PrintError("Error, not a valid .dat file\n")

            self.close()

        if self.radio2.isChecked():
            try:
                scalefactor=float(self.s1.text())
                f1=str(filename)
                importAirfoilDAT.insert(f1,"Unnamed")
                points = FreeCAD.ActiveDocument.ActiveObject.Points
                Draft.makeBSpline(points, closed=True)
                Draft.scale(App.ActiveDocument.ActiveObject,delta=App.Vector(scalefactor,scalefactor,scalefactor),center=App.Vector(0,0,0),legacy=True)
                App.getDocument("Unnamed").removeObject("DWire")

            except:
                FreeCAD.Console.PrintError("Error, not a valid .dat file\n")

            self.close()

    def close(self):
        self.dialog.hide()


    def __init__(self):
        self.dialog = None
        self.s1 = None


        # Make dialog box and get the scale size

        self.dialog = QtGui.QDialog()
        self.dialog.resize(350,100)
        self.dialog.setWindowTitle("Airfoil Import & Scale")
        la = QtGui.QVBoxLayout(self.dialog)
        t1 = QtGui.QLabel("Chord Length")
        la.addWidget(t1)
        self.s1 = QtGui.QLineEdit()
        la.addWidget(self.s1)

        # Add radio buttons to select between DWire and BSpline

        self.radio1 = QRadioButton("Make DWire")
        self.radio2 = QRadioButton("Make BSpline")

            # set default to DWire & make radio buttons - Change self.radio1.setChecked(True) to
            # self.radio2.setChecked(True) to ste BSpline as default

        self.radio1.setChecked(True)
        la.addWidget(self.radio1)
        la.addWidget(self.radio2)

        # Add OK box

        okbox = QtGui.QDialogButtonBox(self.dialog)
        okbox.setOrientation(QtCore.Qt.Horizontal)
        okbox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
        la.addWidget(okbox)
        QtCore.QObject.connect(okbox, QtCore.SIGNAL("accepted()"), self.proceed)
        QtCore.QObject.connect(okbox, QtCore.SIGNAL("rejected()"), self.close)
        QtCore.QMetaObject.connectSlotsByName(self.dialog)
        self.dialog.show()
        self.dialog.exec_()

p()
This post made with 0.0% Micro$oft products - GOT LINUX?
User avatar
quick61
Posts: 3793
Joined: Sat Aug 24, 2013 2:49 am
Location: u.S.A.

Re: A Little Help With PyQt Radio Buttons Please

Postby quick61 » Tue Feb 04, 2014 4:07 am

wandererfan wrote:
quick61 wrote:However, it does not work for the BSpline. Every new foil brought in using a Bspline, desacles the one before it. Strange?

Code: Select all

                Draft.scale(App.ActiveDocument.BSpline,delta=App.Vector(scalefactor,scalefactor,scalefactor),center=App.Vector(0,0,0),legacy=True)
"BSpline" is still in the document after the first run. The second time the macro would create "BSpline001" with the new data. You'd have to scale that.

I can't get the BSpline part to run, but it's probably something in my OCC rather than your macro. I get
"...in createGeometry
spline.interpolate(fp.Points, True)
<type 'exceptions.Exception'>: BSplCLib::Interpolate
BSpline: BSplCLib::Interpolate"

OS: Ubuntu 12.04.4 LTS
Platform: 64-bit
Version: 0.14.3061 (Git)
Branch: BezierProto
Hash: acb86a2ff0d3d39528541d18fef8268ee49e5ba4
Python version: 2.7.3
Qt version: 4.8.1
Coin version: 3.1.3
SoQt version: 1.5.0
OCC version: 6.6.0

Sounds similar to the problem we were having in this help thread I'm on OCE 0.15 dev (OCC6.7) myself. Don't think I have regretted once getting that to compile. :)

Funny how this script has different behavior between being run form the editor and run from an icon link. I'm starting to think I should just drop the BSpline option as the DWire is working 100% and opt of some other feature like multiple copies and spacing/placement. I was going to add that on down the road, but if the BSpline is not going to work...

Mark
This post made with 0.0% Micro$oft products - GOT LINUX?
User avatar
yorik
Site Admin
Posts: 11717
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: A Little Help With PyQt Radio Buttons Please

Postby yorik » Tue Feb 04, 2014 11:45 am

There is a bug in Draft scale that prevents the legacy mode to work on bsplines, that I just fixed now.