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.

NPM Error: node: not found


I tried to install a package (slap) via npm. For this, you have to install nodejs (may be through the package-manager). And I ran the following command npm install and got errors. One of those was node: not found. Here is the complete error:

○ → sudo npm install -g slap@latest
[sudo] password for mandeep: 
/usr/local/bin/slap -> /usr/local/lib/node_modules/slap/slap.js

> runas@3.1.1 install /usr/local/lib/node_modules/slap/node_modules/runas
> node-gyp rebuild

/bin/sh: 1: node: not found
gyp: Call to 'node -e "require('nan')"' returned exit status 127 while in binding.gyp. while trying to load binding.gyp
gyp ERR! configure error 
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack at ChildProcess.onCpExit (/usr/share/node-gyp/lib/configure.js:354:16)
gyp ERR! stack at emitTwo (events.js:87:13)
gyp ERR! stack at ChildProcess.emit (events.js:172:7)
gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:200:12)
gyp ERR! System Linux 4.4.0-22-generic
gyp ERR! command "/usr/bin/nodejs" "/usr/bin/node-gyp" "rebuild"
gyp ERR! cwd /usr/local/lib/node_modules/slap/node_modules/runas
gyp ERR! node -v v4.2.6
gyp ERR! node-gyp -v v3.0.3
gyp ERR! not ok 
/usr/local/lib
`-- (empty)

npm ERR! Linux 4.4.0-22-generic
npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "install" "-g" "slap@latest"
npm ERR! node v4.2.6
npm ERR! npm v3.5.2
npm ERR! code ELIFECYCLE

npm ERR! runas@3.1.1 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the runas@3.1.1 install script 'node-gyp rebuild'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the runas package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node-gyp rebuild
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs runas
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls runas
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR! /home/mandeep/npm-debug.log
npm ERR! code 1

To solve this, I created a symlink /usr/bin/nodejs to /usr/bin/node and it worked fine. Here is it how to do that:

sudo ln -s /usr/bin/nodejs /usr/bin/node

Compiling FreeCAD on Ubuntu 16.04


Read my another post to compile FreeCAD on Arch: https://mandeep7.wordpress.com/2016/03/30/compiling-freecad-from-source-on-arch-linux/

Getting the source:

git clone https://github.com/FreeCAD/FreeCAD free-cad-code

Getting the dependencies:

sudo apt install build-essential cmake python python-matplotlib libtool libcoin80-dev libsoqt4-dev libxerces-c-dev libboost-dev libboost-filesystem-dev libboost-regex-dev libboost-program-options-dev libboost-signals-dev libboost-thread-dev libboost-python-dev libqt4-dev libqt4-opengl-dev qt4-dev-tools python-dev python-pyside pyside-tools oce-draw libeigen3-dev libqtwebkit-dev libshiboken-dev libpyside-dev libode-dev swig libzipios++-dev libfreetype6 libfreetype6-dev liboce-foundation-dev liboce-modeling-dev liboce-ocaf-dev liboce-visualization-dev liboce-ocaf-lite-dev libsimage-dev checkinstall python-pivy python-qt4 doxygen libspnav-dev

Compiling:

Now first we’ll create a separate directory for storing our build files. This will be out-of-source build. Which means the source code directory will not get modified and there won’t be any issue with git.

mkdir build

cd build

cmake ../free-cad-code

Here free-cad-code is my source code directory.

While running cmake, I got an error:

make[2]: *** No rule to make target ‘/usr/lib/libfreeimage.so’, needed by ‘lib/libSMDS.so’. Stop.

This was solved by creating a symlink from /usr/lib/x86_64-linux-gnu/libfreeimage.so to /usr/lib/libfreeimage.so

sudo ln -s /usr/lib/x86_64-linux-gnu/libfreeimage.so /usr/lib/libfreeimage.so

If the above cmake process worked successfully then proceed to the following or just analyze the error and install the missing dependencies. Now type:

make

to start the compilation.

To speed up the compilation, I did: make -j4 instead of make. It will make the full utilization of CPU and memory. Here 4 is the number of cores. You may find yours using command: nproc or simply run make -j$(nproc)

But wait, I got into a problem due to that. The C++ compiler ran out of memory while compilation. It threw an error:

c++: internal compiler error: Killed (program cc1plus)

Please submit a full bug report, with preprocessed source if appropriate.

See <file:///usr/share/doc/gcc-5/README.Bugs> for instructions.

It was due to the full memory utilization and compiler ran out of memory. I got help regarding this by one of the mentors (ickby) on #freecad irc channel.

Then I tried with make -j2 and it worked flawlessly.

Execution:
Execute using the following command:
./bin/FreeCAD

 

Source: http://www.freecadweb.org/wiki/?title=CompileOnUnix

Pandoc: convert document formats


I had to submit some text to mediawiki. As it supports special markdown, but I had a file on Google Docs. Initially, I looked up for some online tools for conversion. For them I downloaded the Doc in html format. But that didn’t work. For example: http://pandoc.org/try. It doesn’t support docx as input. But it support HTML. But it didn’t like much the HTML produced from Google Doc.

The thing that finally worked for me was that I installed pandoc on my system and then did the conversion.

Ubuntu guys can install is simply using:

sudo apt-get install pandoc

Then I read the manpage and found the -t flag useful. I searched for “mediawiki” in the manpage and that did the job.

The actual command that I fired was:

pandoc -t mediawiki input.docx > output

Then there will be a file named output will be created in the current directory in mediawiki style markdown format.

Update: More about pandoc can be seen at http://pandoc.org/ and the formatting help for mediawiki content can be found at https://www.mediawiki.org/wiki/Help:Formatting