Step 1 continued. Trapping the point in 3d space.

This post is an agglomeration of my research notes/links for developing this project. It’s a bit garbled, but  I think contains some interesting stuff

So I ran across a small critter in sketch edit that hopefully will get fixed soon.(See prior post).   Now that I understand it, I think I should be able to work around it for now.

My posting goal here is to select a point in 3d space and having it display in a dialog box.   There are some instructions here on how to build a dialog box.

First off, since I run debugging, I’m running qt creator as opposed to qt designer.   qt creator has qt designer inside of it.  To create a ui file in Qt Creator  you need to select File=>New File Project,  select QT , QT Designer Form.

So this is Qt designer with everything I think I should need.FormDesign  This is stored in a file with a “ui” extension and contains xml data.

To convert the xml into python you do this.

jonas@jonas-laptop:~$ cd Desktop/PointFinder
jonas@jonas-laptop:~/Desktop/PointFinder$ ls *.ui
jonas@jonas-laptop:~/Desktop/PointFinder$ pyuic pointFindandTransfer.ui >

I named it so I could just copy the sample scripts for the tutorial.  I dumped into the bin directory ran this script in the python console in FreeCAD:
>>> from PyQt4 import QtGui
>>> import mywidget
>>> d = QtGui.QWidget()
>>> d.ui = mywidget.Ui_Dialog()
>>> d.ui.setupUi(d)
And here is the result: Hmm.. Need to do a little tuning up on placement before I start adding code.
Screenshot-DialogRevisedDialogOk… I think the revised dialog looks a little better
So I have the frame roughed out.  Next is the step is to hook up the plumbing.   Time to start messing with the signals and slots.

Sooo looking at the tutorial, widgets generate some type of signal and you need to connect that to a slot or a custom function.  (So does that infer that a slot is some kind of canned function?)

So in the tutorial this line which hooks up everything:


I wanted to read up a little more bore on signal stuff. When I did a some googling I ran across this in zedcode as the top rank. This is really a nice website that chocked full of useful stuff (A couple of interesting tutorials on SQLite but I want to stay on task) Hmmm.  A couple of little points of note, apparently  this Signal and slot syntax has been reworked to look more like python standard coding.    The example zedcode sites is this:

QtCore.QObject.connect(button, QtCore.SIGNAL('clicked()'), self.onClicked)

This is the old style API.


The new style adheres more to the Python standards.

The old style is more familiar to me, but what zed calls the new style is definitely more understandable.   Referring to the new syntax we have 3 parameters passed.

  • event source
  • event object
  • event target

Using the freeCAD tutorial the event source is self.create.    The ‘self’ is the python sort of reminds me of the c++ ‘this’

Here’ a quote that I think explains it.:
"Python's all for making things explicit, making it obvious what's what, and although it doesn't do it entirely everywhere, it does do it for instance attributes. That's why assigning to an instance attribute needs to know what instance to assign to, and that's why it needs self." Ok… It looks like there’s been  discussions over this, Bruce Eckel vs Guido von Rossum , so I’m not feeling dumb about wondering about this. (To be honest, this is still a twinge of fuzziness in my head about this)

In the tutorial, self.create is first used in this line:

self.create = QtGui.QPushButton(Dialog)

Information on the this can be found here:

Which has this interesting tidbit:
“A push button emits the signal clicked() when it is activated by the mouse, the Spacebar or by a keyboard shortcut. Connect to this signal to perform the button’s action. Push buttons also provide less commonly used signals, for example, pressed() and released().”A push button emits the signal clicked() when it is activated by the mouse, the Spacebar or by a keyboard shortcut. Connect to this signal to perform the button’s action. Push buttons also provide less commonly used signals, for example, pressed() and released().”

Ok I think I follow that next parameter
QtCore.QObject.connect(self.create,QtCore.SIGNAL(“pressed()”),self.createPlane)  Ok.. when I searched on the bold, I was expecting  definative list of  the names of the different types of signals that could be connected.. I’m not finding that…  But hey.. Time to get off this side trip  and get back on the freeCAD highway…

As fascinating as the following section was I was able to find a solution w/n FreeCAD.  By going into Edit->preferences  and then selecting Preferences–>General–>output window–>Redirect internal python I was able to to get what needed.   I left these notes in since I think it’s a pretty cool solution, but for now I don’t need it.

Sooo.  I got my signals figured out and for the life of me I couldn’t figure out why I could get something something that was working in the python terminal not working as a script.   I had to stop and ask for directions(

One thing that I haven’t quite wrapped my head around is why the error messages were not being displayed even though I’ve been able to trap them.
According to directions I should: “You should enable the option to redirect internal Python errors to the output window. Then you’ll see what happened.”

I did a little googling a I found a nice error handling solution here that I think I would like to try.
Basically a class is setup and it sends the error the console,a file, and a dialogbox. Unfortunately the dialog box is for wx not qt… But still it’s something to base it off.
I found a interesting blurb here  and here   That have me what I needed to  figure out to google to figure out how to get this into FreeCAD

Things are looking up,  I coming close to accomplishing my step 1) goals.
One thing I wanted to add to my form was what I initially called the path, but now am calling the selection.   I thought initially I might be able to pull something out of the Python API but, I’m not sure there is a wrapper for that.  I found the spot in C++ where it gets generated.
It’s in SelectionView.CPP lines 70-83:
void SelectionView::OnChange(Gui::SelectionSingleton::SubjectType &rCaller,

Gui::SelectionSingleton::MessageType Reason)
std::string temp;

if (Reason.Type == SelectionChanges::AddSelection) {
// insert the selection as item
temp = Reason.pDocName;
temp += “.”;
temp += Reason.pObjectName;
if (Reason.pSubName[0] != 0 ) {
temp += “.”;
temp += Reason.pSubName;
(Sorry about the loss of indent)

Step1DoneWell, I’m calling step 1 complete, although there’s a few things in the code I a little uncertain about.
In the middle of all this,  I was using gedit to write the code before importing it into freecad and running it.  That sort of got old and I start searching for a python IDE.

I ran across “Eric Python IDE” in synaptic which looks interesting to me.
I like having my IDE using the same toolkits as the project that I’m working in.  (When I was into Heekcad I was using code blocks with uses wxwidgets).

Eric uses PyQt, same as FreeCAD.   I’m not necessary looking to run freeCAD from the Eric IDE, but it would be nice to be able to use code completion.    It has code collapse as well as some other creature comforts which make it appealing.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *