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 FreeCAD.so file is.

locate FreeCAD.so

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 site.py.

For example,

sys.path.append(“/home/mandeep/Desktop/build/lib”)

The site.py 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 https://docs.python.org/2/library/site.html

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

Sourcehttp://www.freecadweb.org/wiki/index.php?title=Embedding_FreeCAD

Referencehttp://stackoverflow.com/a/15109881/3784226

I said “sed”


Now it’s the turn for macros be fetched from the wiki page at http://www.freecadweb.org/wiki/index.php?title=Macros_recipes. 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 http://freecadweb.org ?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 http://stackoverflow.com/a/10613688. 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 https://github.com/mandeeps708/scripts/tree/master/FC-Wiki-template. 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.

See: https://developer.github.com/v3/repos/contents/#response-if-content-is-a-submodule

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: https://github.com/mandeeps708/Github-API-Fun/blob/master/get-submodules.py

More about repository methods in PyGithub: http://pygithub.github.io/PyGithub/v1/github_objects/Repository.html

Rate Limit: https://developer.github.com/v3/#rate-limiting

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 https://github.com/FreeCAD/FreeCAD-addons.

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: https://github.com/mandeeps708/Github-API-Fun. Actually, this repository has also been created using the API itself (without opening github.com 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. https://github.com/FreeCAD/FreeCAD-addons/. 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: https://developer.github.com/libraries. I am going to explore PyGithub.

Install PyGithub:

pip install pygithub

For more, read: http://pygithub.github.io/PyGithub/v1/introduction.html

Stay tuned!

BeautifulSoup


FreeCAD manages its macros its wiki: http://www.freecadweb.org/wiki/index.php?title=Macros_recipes. Let’s take an example of a macro “Macro Image Scaling” whose code is at http://www.freecadweb.org/wiki/index.php?title=Macro_Image_Scaling. 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: https://github.com/mandeeps708/scripts/blob/master/FC_getMacro/fcmacrobs.py.

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.

 

FreeCAD Plugins


There are basically two ways to add functionality to FreeCAD.
1. Workbenches
2. Macros

Workbenches

Workbenches can be thought of the set of tools grouped together to perform a specific task. Workbenches can be C++ or Python modules. The C++ ones written earlier are bundled with FreeCAD itself like Part, PartDesign, Mesh. More information regarding this can be found at http://www.freecadweb.org/wiki/?title=Workbenches.

Workbenches that are being written in Python are managed at https://github.com/FreeCAD/FreeCAD-addons so that they can be separated out from FreeCAD and plugged in later on. Hence, it becomes easy to handle Python modules as they don’t need compilation. Python workbenches are Arch, openSCAD, ship etc.

Some are hybrid (combination of C++ and Python) like PathDraft etc.

Macros

Macros can be visualized as single-file python scripts. These are having .FCMacro file extension. There are two ways to record macros.

1. Interactive: Go to Menu > Macro > Macro recording > Enter any name > Click ‘Record‘ > Do whatever you want to do and then click Macro > Stop macro recording. Read more: http://www.freecadweb.org/wiki/index.php?title=Macros

2. Python: Go to MenuMacro >  Macros > Create > Enter file name > Click ‘OK‘ > Write python code and save (Ctrl+S).

Macros are located at the FreeCAD wiki: http://www.freecadweb.org/wiki/index.php?title=Macros_recipes. Later, it was thought to port those macros to GitHub: https://github.com/FreeCAD/FreeCAD-macros. As one doesn’t need to know programming to create Macros, hence a normal user finds it difficult to use git and GitHub and most macro authors don’t have a GitHub account so they didn’t move the macros there. And the wiki is very easy for people to put their macros.

 

Hence, the idea is to implement a plugin manager that would be managing both Workbenches and the Macros without giving much hassle to the users to install them, via a single interface.

Currently, the installation directory for ~/.FreeCAD/Mod (in GNU/Linux at least). To see the User Data directory, go to FreeCAD Python console and paste:

print FreeCAD.getUserAppDataDir()

This will print the directory where to store the plugins. Mod is for containing the workbenches and the Macro directory is for storing the user created macros.