Creating Inkscape Extensions
My task currently is to generate the graphics library for the components that we are going to use in our schematic editor. To be more precise, it is not my objective to create all the graphics for more than a thousand components, but rather find a way to automate and simplify this process as mush as possible and thus, it will not necessary be me who is going to generate that actual graphics. The method we started experimenting with is to use Inkscape, and write extensions for it to the job.
As I mentioned in a previous blog, we are using Fritzing parts library as a base for our components library. Fritzing does not allow the use of its graphics, but we can use its .fzb files. Each component has an fzb file, and from it we can get the component name, how many pins does it have, and the names of these pins. Therefore, these fzb files are going to be the input for our extensions. The final output should be a JointJS element definition.
In order to achieve this, I created 2 extensions for Inkscape. The first one is called (import from fzb file) and the second one (export to JointJS). The first extension takes an fzb file as its input. It should then parse it, extract the component name, the number of pins (ports) it has, and the names of these pins. Then it will create a basic element in Inkscape. This element consist of a sqaure (to represent the body of the component), the name of the element on top of it, and the pins of that components (represented by circles) with the name of each pin next to that pin.
Some important points the extension also takes care of are, first, is that this element is created at a new layer, and this layer has a class called “base”. This is important for the second extension when exporting to JointJS format as it will search for the element that has a class “base”. The second point is that all the port in the element has class with the format “<element_name>_pin<pin_number>”. For an LCD, e.g., the pins classes names will be:
t3_lcd_pin0, t3_lcd_pin1, t3_lcd_pin2..
and so on. This is also important when creating the JointJS file. These classes will then be used to specify that these svg circles are ports, and to specify the name that will be used for generating the user constraints file.
Once this extension is used, the output will then look something like this:
Then it can be modified and changed to look like the real components. Any changes can be done at this step, whether they are resizing, changing color or adding new elements and shapes, given that these shapes are all within the same “base” layer. After some editing the element might look like the figure below, and now it is ready to be exported.
The next extension generates the JointJS element definition, and saves it to a new file. The extension dialog (currently) takes as inputs the element name, number of pins, and the ports names (that should be used when generating the User Constraints file). The extension then gets the xml tree of the opened Inkscape file, iterate through the elements to find an object that has a class named “base”. Once it is found, it will serialize the object and construct the JointJS file for that component. It also uses the pins class names to make them JointJS ports and assign them their UCF names.
The use of two extension makes the process of generating the JointJS element library much easier than doing it manually. The tasks of reading the fzb files, determining the names of the pins, creating the JointJS file, and specifying the ports are all now automated, and we can focus on doing some visual editing for the components only.
Finally I would like to mention some notes regarding the creation of the Inkscape extensions. Extensions for Inkscape are written in Python. There are some tutorials available for writing basic extensions, such as Hello_world extension. The extension also needs an “inx” file that will contain the interface for that extension. It is basically an xml file, and is used to specify the extension name, what parameter the user should enter, and what Python file these inputs should be sent to. To install the extension, both these files should then simply be put at “/usr/share/inkscape/extensions/” to be available for all users, or at “~/.config/inkscape/extensions/” for a specific user.
The basic functionality of the extension are working now. But there are many improvements that need to be done. Next week I will be focusing on improving the extensions.