Calling a dialog box

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
davecoventry
Posts: 286
Joined: Mon Dec 09, 2013 8:11 pm
Location: Johannesburg
Contact:

Calling a dialog box

Post by davecoventry »

I've used the example here to generate a dialog box which has 4 fields, the dx, dy, dz and the distance between two points.

It works fine if I input from the python console:

Code: Select all

 import dialog
>>> from PySide import QtGui
>>> d = QtGui.QWidget()
>>> d.ui=dialog.Ui_Dialog()
>>> d.ui.setupUi(d,[5,5,10])
>>> d.show()
However, I'm trying to pick 2 points with which to populate the fields in my dialog box.

Code: Select all

import FreeCADGui
import math
import dialog
from PySide import QtGui
basepoint=None
def pointHandler(val,point):
  global basepoint
  if val==0:
    basepoint=point
  else:
    dx=point.x-basepoint.x
    dy=point.y-basepoint.y
    dz=point.z-basepoint.z
    print "dx=\n",dx
    print "dy=\n",dy
    print "dz=\n",dz
    print math.sqrt(dx*dx+dy*dy+dz*dz)
    d = QtGui.QWidget()
    d.ui=dialog.Ui_Dialog()
    d.ui.setupUi(d,[dx,dy,dz])
    d.show()
class pointSnapper:
  def __init__(self,val):
    self.point = None
    self.val=val
    FreeCADGui.Snapper.getPoint(callback=self.clicked)
  def clicked(self,point,extra):
    self.point = point
    if self.val==1:
      pointHandler(0,point)
      pointSnapper(2)
    else:
      pointHandler(1,point)

s = pointSnapper(1)
The dialog box doesn't appear, although the correct results are printed to the command shell.

There are no errors printed.

How do I get my dialog box to appear?
~ Dave
triplus
Veteran
Posts: 9471
Joined: Mon Dec 12, 2011 4:45 pm

Re: Calling a dialog box

Post by triplus »

Hi Dave.

You use:

Code: Select all

import dialog
But you didn't provide the dialog.py related code. Therefore trying to help would only be related to guessing.

P.S. And note that to avoid any potential module clashes use prefix. Like for example DaveDialog.py as dialog.py is such generic name and it could (likely not) already be in use. ;)
User avatar
davecoventry
Posts: 286
Joined: Mon Dec 09, 2013 8:11 pm
Location: Johannesburg
Contact:

Re: Calling a dialog box

Post by davecoventry »

Thanks for the response.

The dialog.py file is pretty much the same as the the example I linked to with a few minor modifications.

Code: Select all

from PySide import QtCore, QtGui
import FreeCADGui, Part
import DraftTools, WorkingPlane
from pivy.coin import *
import FreeCAD
import math

class Ui_Dialog(object):
  def setupUi(self, Dialog,sizes):
    import FreeCAD as App
    Dialog.setObjectName("Dialog")
    Dialog.resize(264, 231)
    self.label_x = QtGui.QLabel(Dialog)
    self.label_x.setGeometry(QtCore.QRect(40, 70, 59, 14))
    self.label_x.setObjectName("label_x")
    self.label_y = QtGui.QLabel(Dialog)
    self.label_y.setGeometry(QtCore.QRect(40, 100, 59, 14))
    self.label_y.setObjectName("label_y")
    self.label_z = QtGui.QLabel(Dialog)
    self.label_z.setGeometry(QtCore.QRect(40, 130, 59, 14))
    self.label_z.setObjectName("label_z")
    self.label_d = QtGui.QLabel(Dialog)
    self.label_d.setGeometry(QtCore.QRect(40, 150, 59, 14))
    self.label_d.setObjectName("label_d")
    self.Edit_x = QtGui.QLineEdit(Dialog)
    self.Edit_x.setGeometry(QtCore.QRect(120, 60, 113, 22))
    self.Edit_x.setObjectName("Edit_x")
    self.Edit_x.setText(str(sizes[0]))
    self.Edit_y = QtGui.QLineEdit(Dialog)
    self.Edit_y.setGeometry(QtCore.QRect(120, 90, 113, 22))
    self.Edit_y.setObjectName("Edit_y")
    self.Edit_y.setText(str(sizes[1]))
    self.Edit_z = QtGui.QLineEdit(Dialog)
    self.Edit_z.setGeometry(QtCore.QRect(120, 120, 113, 22))
    self.Edit_z.setObjectName("Edit_z")
    self.Edit_z.setText(str(sizes[2]))
    self.Edit_dist = QtGui.QLineEdit(Dialog)
    self.Edit_dist.setGeometry(QtCore.QRect(120, 150, 113, 22))
    self.Edit_dist.setObjectName("Edit_dist")
    self.Edit_dist.setText(str(math.sqrt(sizes[0]*sizes[0]+sizes[1]*sizes[1]+sizes[2]*sizes[2])))
    self.label = QtGui.QLabel(Dialog)
    self.label.setGeometry(QtCore.QRect(60, 20, 59, 14))
    self.label.setObjectName("label")
    self.getpointmode=True

    self.retranslateUi(Dialog)
    QtCore.QMetaObject.connectSlotsByName(Dialog)

  def retranslateUi(self, Dialog):
    Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
    self.label_x.setText(QtGui.QApplication.translate("Dialog", "X Value", None, QtGui.QApplication.UnicodeUTF8))
    self.label_y.setText(QtGui.QApplication.translate("Dialog", "Y Value", None, QtGui.QApplication.UnicodeUTF8))
    self.label_z.setText(QtGui.QApplication.translate("Dialog", "Z Value", None, QtGui.QApplication.UnicodeUTF8))
    self.label_d.setText(QtGui.QApplication.translate("Dialog", "Distance", None, QtGui.QApplication.UnicodeUTF8))
    self.label.setText(QtGui.QApplication.translate("Dialog", "Distance between points", None, QtGui.QApplication.UnicodeUTF8))
On your recommendation, I changed the filename to pt_dialog.py but this has made no difference.

I suppose it's possible that the reason the dialog is not displaying is an error in the pt_dialog.py file, but I would have thought that the fact that the dialog is displayed correctly when called directly from the python panel would indicate that this is unlikely.
~ Dave
triplus
Veteran
Posts: 9471
Joined: Mon Dec 12, 2011 4:45 pm

Re: Calling a dialog box

Post by triplus »

I took a quick look. If i use your work as a base and add a small modification i can get the dialog:

Code: Select all

import FreeCADGui
import math
import dialog
from PySide import QtGui
basepoint=None

mw = FreeCADGui.getMainWindow()

def pointHandler(val,point):
  global basepoint
  if val==0:
    basepoint=point
  else:
    dx=point.x-basepoint.x
    dy=point.y-basepoint.y
    dz=point.z-basepoint.z
    print "dx=\n",dx
    print "dy=\n",dy
    print "dz=\n",dz
    print math.sqrt(dx*dx+dy*dy+dz*dz)
    d = QtGui.QDialog(mw)
    d.ui=dialog.Ui_Dialog()
    d.ui.setupUi(d,[dx,dy,dz])
    d.show()
class pointSnapper:
  def __init__(self,val):
    self.point = None
    self.val=val
    FreeCADGui.Snapper.getPoint(callback=self.clicked)
  def clicked(self,point,extra):
    self.point = point
    if self.val==1:
      pointHandler(0,point)
      pointSnapper(2)
    else:
      pointHandler(1,point)

s = pointSnapper(1)
Hope this helps you!
User avatar
davecoventry
Posts: 286
Joined: Mon Dec 09, 2013 8:11 pm
Location: Johannesburg
Contact:

Re: Calling a dialog box

Post by davecoventry »

<type 'exceptions.TypeError'>
Traceback (most recent call last):
File "/usr/lib/freecad/Mod/Draft/DraftSnap.py", line 1043, in click
accept()
File "/usr/lib/freecad/Mod/Draft/DraftSnap.py", line 1053, in accept
callback(self.pt,obj)
File "/home/dave/.FreeCAD/Macro/FreeCAD-addons-master/dist.py", line 35, in clicked
pointHandler(1,point)
File "/home/dave/.FreeCAD/Macro/FreeCAD-addons-master/dist.py", line 21, in pointHandler
d.ui=pt_dialog.Ui_Dialog(mw)
TypeError: object() takes no parameters
:(

OS: Debian GNU/Linux 8.7 (jessie)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.16.
Build type: None
Python version: 2.7.9
Qt version: 4.8.6
Coin version: 4.0.0a
OCC version: 6.8.0.oce-0.17

Code: Select all

import FreeCADGui
import math
import pt_dialog
from PySide import QtGui
basepoint=None
mw = FreeCADGui.getMainWindow()
def pointHandler(val,point):
  global basepoint
  global mw
  if val==0:
    basepoint=point
  else:
    dx=point.x-basepoint.x
    dy=point.y-basepoint.y
    dz=point.z-basepoint.z
    print "dx=\n",dx
    print "dy=\n",dy
    print "dz=\n",dz
    print math.sqrt(dx*dx+dy*dy+dz*dz)
    d = QtGui.QWidget()
    d.ui=pt_dialog.Ui_Dialog(mw)
    d.ui.setupUi(d,[dx,dy,dz])
    d.show()
class pointSnapper:
  def __init__(self,val):
    self.point = None
    self.val=val
    FreeCADGui.Snapper.getPoint(callback=self.clicked)
  def clicked(self,point,extra):
    self.point = point
    if self.val==1:
      pointHandler(0,point)
      pointSnapper(2)
    else:
      pointHandler(1,point)

s = pointSnapper(1)
~ Dave
triplus
Veteran
Posts: 9471
Joined: Mon Dec 12, 2011 4:45 pm

Re: Calling a dialog box

Post by triplus »

Hi Dave.

I combined all the code you shared and added the small modification i mentioned earlier. Create a new macro and copy/paste the code in it.

Code: Select all

from PySide import QtCore, QtGui
import FreeCADGui, Part
import DraftTools, WorkingPlane
from pivy.coin import *
import FreeCAD
import math

basepoint=None
mw = FreeCADGui.getMainWindow()

class Ui_Dialog(object):
  def setupUi(self, Dialog,sizes):
    import FreeCAD as App
    Dialog.setObjectName("Dialog")
    Dialog.resize(264, 231)
    self.label_x = QtGui.QLabel(Dialog)
    self.label_x.setGeometry(QtCore.QRect(40, 70, 59, 14))
    self.label_x.setObjectName("label_x")
    self.label_y = QtGui.QLabel(Dialog)
    self.label_y.setGeometry(QtCore.QRect(40, 100, 59, 14))
    self.label_y.setObjectName("label_y")
    self.label_z = QtGui.QLabel(Dialog)
    self.label_z.setGeometry(QtCore.QRect(40, 130, 59, 14))
    self.label_z.setObjectName("label_z")
    self.label_d = QtGui.QLabel(Dialog)
    self.label_d.setGeometry(QtCore.QRect(40, 150, 59, 14))
    self.label_d.setObjectName("label_d")
    self.Edit_x = QtGui.QLineEdit(Dialog)
    self.Edit_x.setGeometry(QtCore.QRect(120, 60, 113, 22))
    self.Edit_x.setObjectName("Edit_x")
    self.Edit_x.setText(str(sizes[0]))
    self.Edit_y = QtGui.QLineEdit(Dialog)
    self.Edit_y.setGeometry(QtCore.QRect(120, 90, 113, 22))
    self.Edit_y.setObjectName("Edit_y")
    self.Edit_y.setText(str(sizes[1]))
    self.Edit_z = QtGui.QLineEdit(Dialog)
    self.Edit_z.setGeometry(QtCore.QRect(120, 120, 113, 22))
    self.Edit_z.setObjectName("Edit_z")
    self.Edit_z.setText(str(sizes[2]))
    self.Edit_dist = QtGui.QLineEdit(Dialog)
    self.Edit_dist.setGeometry(QtCore.QRect(120, 150, 113, 22))
    self.Edit_dist.setObjectName("Edit_dist")
    self.Edit_dist.setText(str(math.sqrt(sizes[0]*sizes[0]+sizes[1]*sizes[1]+sizes[2]*sizes[2])))
    self.label = QtGui.QLabel(Dialog)
    self.label.setGeometry(QtCore.QRect(60, 20, 59, 14))
    self.label.setObjectName("label")
    self.getpointmode=True

    self.retranslateUi(Dialog)
    QtCore.QMetaObject.connectSlotsByName(Dialog)

  def retranslateUi(self, Dialog):
    Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
    self.label_x.setText(QtGui.QApplication.translate("Dialog", "X Value", None, QtGui.QApplication.UnicodeUTF8))
    self.label_y.setText(QtGui.QApplication.translate("Dialog", "Y Value", None, QtGui.QApplication.UnicodeUTF8))
    self.label_z.setText(QtGui.QApplication.translate("Dialog", "Z Value", None, QtGui.QApplication.UnicodeUTF8))
    self.label_d.setText(QtGui.QApplication.translate("Dialog", "Distance", None, QtGui.QApplication.UnicodeUTF8))
    self.label.setText(QtGui.QApplication.translate("Dialog", "Distance between points", None, QtGui.QApplication.UnicodeUTF8))


def pointHandler(val,point):
  global basepoint
  global mw
  if val==0:
    basepoint=point
  else:
    dx=point.x-basepoint.x
    dy=point.y-basepoint.y
    dz=point.z-basepoint.z
    print "dx=\n",dx
    print "dy=\n",dy
    print "dz=\n",dz
    print math.sqrt(dx*dx+dy*dy+dz*dz)
    d = QtGui.QDialog(mw)
    d.ui=Ui_Dialog()
    d.ui.setupUi(d,[dx,dy,dz])
    d.show()


class pointSnapper:
  def __init__(self,val):
    self.point = None
    self.val=val
    FreeCADGui.Snapper.getPoint(callback=self.clicked)
  def clicked(self,point,extra):
    self.point = point
    if self.val==1:
      pointHandler(0,point)
      pointSnapper(2)
    else:
      pointHandler(1,point)


s = pointSnapper(1)
After run the macro to see what will be the result.
User avatar
davecoventry
Posts: 286
Joined: Mon Dec 09, 2013 8:11 pm
Location: Johannesburg
Contact:

Re: Calling a dialog box

Post by davecoventry »

It works!

Thanks very much!
~ Dave
triplus
Veteran
Posts: 9471
Joined: Mon Dec 12, 2011 4:45 pm

Re: Calling a dialog box

Post by triplus »

davecoventry wrote:It works!

Thanks very much!
You're welcome.
Post Reply