Using Qt UI files with PySide in FreeCAD

In Qt Designer app, create an interface. For example, I firstly created a form with two text widgets and then one push button.

qt ui design

Paste the following in the FreeCAD Python console:

from PySide import QtCore, QtGui

class CalculateSum:
    def __init__(self):
        # Importing the form from Qt UI file.
        self.form = FreeCADGui.PySideUic.loadUi("/home/mandeep/ui-design/inputform.ui")

        # Setting on-click behavior of Calculate button
        QtCore.QObject.connect(self.form.calculateButton, QtCore.SIGNAL("clicked()"), self.accept)

    def accept(self):
        """Will be called when Calculate button is clicked"""
        # Take inputs from both fields.
        v1 = int(self.form.val1.text())
        v2 = int(self.form.val2.text())
        print("Sum of "+str(v1)+" and "+str(v2)+": "+str(v1+v2))
# Show up in FreeCAD.
if FreeCAD.GuiUp:

You will see the widget (Form) inputform.ui in the Combo View Tasks panel in FreeCAD. Enter values and click the Calculate button and the result will be printed in the Report View.


Import FreeCAD in Python

If you’ve compiled FreeCAD, but don’t know how to import it as a package in Python then read on.

First of all, you need to locate where the file is.


Mine is in /home/mandeep/Desktop/build/lib.

To be able to do:

import FreeCAD

You’ll have to go to that directory and run python and if you now try to import, it will work. But most probably, you won’t agree to use it as it might feel impractical.

Another method is to append this path to sys.path. For this, you need to edit a file

For example,


The would probably be where your python lib directory is. It may probably look like: /usr/lib/python2.7. But I am using the Python within the virtualenv so the python lib is within the virtualenv e.g. venv/lib/python2.7. Here venv is my virtualenv.

Read more about this at

The idea to edit the file is that it gets initialized with Python. So you can now import your packages from anywhere.



I said “sed”

Now it’s the turn for macros be fetched from the wiki page at We’ll need to parse the links (and macro names) from there. So we need some pattern to be able to parse similar data (i.e. links). The common thing was that the links had ?title=Macro… So it was suggested by mentors that we can have a template with the links that can have a specific class.

So in the wiki source, the links are written as: [[Macro makeCube|Macro MakeCube]]. Here 1st argument (before the pipe) is the name of the page/URL. The second one is the link text that appears on the wiki. If we omit the second argument, then the first one is taken as the link text and URL.

So aim was to convert something like [[Macro makeCube|Macro MakeCube]] to {{MacroLink|Macro makeCube}}. Here MacroLink is the name of the template. Which is replaced by the tags we specify. Here it would be replaced by a span with class “MacroLink”.

Okay, so what’s the problem! There are more than 100 entries there and one would simply edit it one by one manually. So “sed” was there to rescue.

Now I had to look for some pattern to be able to replace text. So as we have to remove the content after the pipe (‘|’) and replace the [[]] with {{}} and add template tag and a pipe.

I am not good with regular expressions. On searching something similar, I found So I got the idea and used it like:

sed ‘s/|Macro.*\]\]/\}\}/i’ input.txt > wikinew.txt

It selects the text starting from “|Macro” till “]]” and replace it by “}}” as we need it at the end. Hence, the content after pipe would be removed too. Now we’ll have entries like [[Macro makeCube}}.

sed -i.bak ‘s/\[\[Macro/\{\{MacroLink|Macro/’ wikinew.txt

Now it was the turn for the prefix. The above command will select text like [[Macro and will replace it by {{MacroLink|Macro. As we needed to add a template “MacroLink” to it. That fulfilled the need. Although, 2-3 entries needed to be modified manually. The final page is here (not sure if it would exist). Code

The code is I’m done!

Fetching GitHub submodules

Yesterday, I took the first step towards fetching information from GitHub. Today added code for checking if the repository contains submodules and will return that instance. Then we may use it to access submodule information like name, submodule_git_url etc.


I used x.raw_data.get(typeto check if x a submodule or not. Actually raw_data contains many other attributes (like html_url, git_url, name, sha, type etc.) that provide additional info. about the instance itself. And for fetching the original repository link, I used: x.raw_data.get(submodule_git_url).

Here is the code for the particular file:

More about repository methods in PyGithub:

Rate Limit:

For requests using Basic Authentication or OAuth, you can make up to 5,000 requests per hour. For unauthenticated requests, the rate limit allows you to make up to 60 requests per hour. Unauthenticated requests are associated with your IP address, and not the user making requests.

GitHub API Returns

Hi everyone,

On 11 June, I tried experimenting with parsing the submodules data from

I started working on PyGithub today (12 June). Actually, it’s a third-party library wrapper written in Python for the GitHub API. The day went off struggling with PyGithub to work. I couldn’t understand much at the beginning. As the night elapsed, it started working for me. 😛

I started with some basic example like the fetching of the names of the files. You may see the repository here: Actually, this repository has also been created using the API itself (without opening in the browser). Finally got slept at around 7 AM.

Now next is to do get if the instance fetched is a submodule or not. It seems to be much easier to be done now.

GitHub API

As I fetched macro code using BeautifulSoup. Now the FreeCAD workbenches are hosted at one place i.e. I needed to fetch the file details from this repository from GitHub. It was already done in Python by parsing the HTML and scraping it. I tried a Python module called BeautifulSoup that makes it easy to play with the HTML within Python.

But these aren’t the permanent solutions. We’ll have to make frequent changes in our code if the source website (GitHub) changes. So I thought of using GitHub API. It’ll be more of a stable solution.

Here is the list of libraries you can use in any programming language: I am going to explore PyGithub.

Install PyGithub:

pip install pygithub

For more, read:

Stay tuned!


FreeCAD manages its macros its wiki: Let’s take an example of a macro “Macro Image Scaling” whose code is at I needed to fetch the code from wiki and further process that.

Lately, I was doing some experiments to parse macro code from the FreeCAD wiki. Firstly, I used PyQuery. But it uses lxml which is a shared library and it was having some issues while packaging for Windows. As lxml is a shared lib and the available Python 2.7 lxml packages were built using VC9 (Visual C++) which isn’t compatible with FreeCAD’s python which is built with VC12. FreeCAD ships its own Python on Windows.

Then another day I found BeautifulSoup via Daman. It’s pretty good. You may see the use case here:

But while running this as macro in FreeCAD, the output gets printed to the report view. But after executing this macro, the report view kept on stopping at a particular character of fetched macro output code. This seemed to be some problem with length of string redirected to the report view. The code works fine in Python console.