Line drawing function

Introduction
This page shows how advanced functionality can easily be created in Python. In this exercise, we will build a new tool that draws a line. This tool can then be linked to a FreeCAD command, and that command can be called by any element in the interface, like a menu item or a toolbar button.

The main script
First we will write a script containing all our functionality. Then we will save this in a file and import it in FreeCAD, so all classes and functions will be available to FreeCAD. So, launch your favorite code editor, and type the following lines:

Detailed explanation
In Python, when you want to use functions from another module you need to import it. In our case we will need functions from the Part Module, for creating the line, and from the Gui module, for accessing the 3D view. We also need the complete contents of the coin library so we can directly use all coin objects like, etc.

Here we define our main class. Why do we use a class and not a function? The reason is that we need our tool to stay "alive" while we are waiting for the user to click on the screen. A function ends when its task has been done, but an object (a class defines an object) stays alive until it is destroyed.

In Python, every class or function can have a documentation string (docstring). This is particularly useful in FreeCAD, because when you call that class in the interpreter, the description string will be displayed as a tooltip.

Python classes can always contain an function, which is executed when the class is called to create an object. Here we will put everything we want to happen when our line tool begins.

In a class you usually want to prepend to variable names to make the variables easily accessible to all functions inside and outside the class. Here we will use to access and manipulate the active 3D view.

Here we create an empty list that will contain the 3D points sent by the function.

This is the important part. Since we are dealing with a coin3D scene, we use a coin callback mechanism that allows a function to be called every time a certain scene event happens. In our case we are creating a callback for SoMouseButtonEvent events, and we bind it to the function. Now every time a mouse button is pressed or released the function will be executed.

Note that there is also an alternative to called  which does not rely on pivy. But since pivy is a very efficient and natural way to access any part of a coin scene, it is much better to use it wherever you can.

Now we define the function that will be executed when a mouse button is pressed in a 3D view. This function will receive an argument that we will call. From this event callback we can access the event object, which contains several pieces of information (more info here).

The function will be called when a mouse button is pressed or released. But we only want to pick a 3D point when a button is pressed, otherwise we would end up with two 3D points very close together. So we must check for that here.

Here we get the screen coordinates of the mouse cursor.

This function gives us a FreeCAD vector (x,y,z) containing the 3D point that lies on the focal plane, just under our mouse cursor. If you are in camera view, imagine a ray coming from the camera, passing through the mouse cursor, and hitting the focal plane. That is the location of our 3D point. If we are in orthogonal view, the ray is parallel to the view direction.

We add our new point to the stack.

Do we have enough points already? if yes, then let's draw the line!

Here we use the function from the Part Module that creates a line from two FreeCAD vectors. The line is not bound to any object in our active document, so nothing appears on the screen.

The FreeCAD document can only accept shapes from the Part module. Shapes are the most generic type of the Part module. So we must convert our line to a shape before adding it to the document.

The Part module has a very handy function that creates a new object in the document and binds a shape to it. We could also have created a new object in the document first and then bound the shape to it manually.

Since we are done with our line we remove the callback mechanism here.

Testing the script
Now let's save our script in a folder where the FreeCAD Python interpreter can find it. When importing modules, the interpreter will look in the following places: the Python installation paths, the FreeCAD folder, and all FreeCAD  (module) folders. So the best solution is to create a new folder in one of the folders. Let's create a folder there and save our script in it as.

Now everything is ready. Let's start FreeCAD, create a new document, and in the Python interpreter issue:

If no error message appears our exercise script has been loaded successfully. We can now check its contents with:

The command is a built-in Python command that lists the contents of a module. We can check that our class is there with:

Now let's test it:

Click two times in the 3D view and bingo: here is our line! To repeat it just type again.

Registering the script
For our new line tool to be really useful, and to avoid having to type all that stuff, it should have a button in the interface. One way to do this is to transform our new folder into a full FreeCAD workbench. This is easy, all that is needed is to put a file called inside the  folder. will contain the instructions to create a new workbench, and add our new tool to it. Besides that we will also need to change our exercise code a bit, so the tool is recognized as an official FreeCAD command. Let's start by creating an file, and writing the following code in it:

By now you probably understand the above script. We create a new class that we call, we give it a title , and we define an function that will be executed when the workbench is loaded into FreeCAD. In that function, we load the contents of our exercise file, and append the FreeCAD commands found inside to a command list. Then, we make a toolbar called "My Scripts" and we assign our command list to it. Currently, of course, we only have one tool, so our command list contains only one element. Then, once our workbench is ready, we add it to the main interface.

But this still won't work because a FreeCAD command must be formatted in a certain manner to work, we will need to change our tool. Our new script should look like this:

What we did here is transform our function into an  function. When FreeCAD commands are run, they automatically execute the function. We also added a function, that informs FreeCAD where it can find the icon for the tool, and what will be the name and tooltip of our tool. Any, or  image will work as an icon, it can be any size, but it is best to use a size that is close to the final aspect, like 16x16, 24x24 or 32x32. Then we add the class as an official FreeCAD command with the  method.

That's it, now we just need to restart FreeCAD and we'll have a nice new workbench with our brand new line tool!

So you want more?
If you liked this exercise, why not try to improve this little tool? There are many things that can be done, for example:
 * Add user feedback: until now we did a very bare tool, the user might be a bit lost when using it. So we could add some feedback, telling the user what to do next. You could issue messages to the FreeCAD console. Have a look in the module.
 * Add a possibility to type the 3D points coordinates manually. Look at the Python function for example.
 * Add the possibility to add more than 2 points.
 * Add events for other things: Now we just check for Mouse button events, what if we would also do something when the mouse is moved, like displaying current coordinates?
 * Give a name to the created object.

Don't hesitate to ask questions or share ideas on the forum!