Dia Python Plugin
Dia contains a plug-in that allows scripting in Python. Anyone that makes a Python script for Dia should post to the mailing list, as including some more scripts would help both debugging and creation of more scripts.
In the early years of the Dia Python facility, there was a great lack of documentation. To get you started, there was some mail from Hans Breuer on the Python plug-in object structure and syntax.
Here is a short note from Hans Breuer on how to use Python to create objects in a diagram.
If you need help getting started with Python for Dia on Windows: How to use Python with Dia on Windows
Python scripts provided with Dia
Nowadays, there are several Python scripts included in the Dia source package. Some of them are presented here.
generates a new diagram which contains all objects of dir(dia). Now fills attributes and operations by using Python reflexion ...
The classes of the following diagram are automatically generated by pydiadoc.py. The generation includes the class member as well as the member functions. The script did not do the layout of the diagram, nor the connections of the objects.
The diagram, in Dia's native format, is included in the Dia binary distribution as samples/Self/PyDiaObjects.dia (samples\Self\PyDiaObjects.dia on Windows).
diasvg.py is an SVG export filter for Dia implemented in Python. It is best represented by what it can do:
diasvg_import.py is an SVG import filter for Dia implemented in Python. Like most of the other Dia Python scripts, it serves two purposes. One is to identify weaknesses in PyDia. Making it work involved not only the development of the script itself, but also extending and fixing the underlying Python bindings implemented in C.
But of course this script can be used for real work. In some aspects it supersedes the Dia import filter in C, though both existing import filters will never support any SVG in the wild. (The linked-to bug report proposes the following:)
The real solution may be to [...] start from scratch with a capable svg library like [http://webcvs.cairographics.org/libsvg/]
Another 'export filter' is codegen.py, which converts a diagram made with objects from Dia's UML sheet into code in a user-selectable programming language (at one time, Python, C++, Pascal, and Java were supported; other languages may have been added since then). In contrast to the SVG export, this script uses the PyDia 'Object Renderer' interface. For code generation, it would not be useful to get the graphical representation of the diagram to export. Instead, the script implements only
iterating over the passed-in data's (which is of type dia.DiagramData) layers and their objects, to extract class as well as inheritance information from the respective object types ("UML - Class" and later "UML - Generalization")
which is implemented differently for each output programming language (C++, Python, and so on).
group_props.py is a prototype providing one of Dia's (formerly) most often requested core functionalities: the ability to change the properties of multiple objects at once. This functionality was built into Dia starting with version 0.97. group_props.py may still be useful as an example, though. It shows not only the integration of a Python script into the menu, but also some interaction done via pygtk.
An example dialog of group_props.py in action
Python scripts available elsewhere
Similar to codegen.py (provided with Dia - see above) is diasql.py, which converts a Dia diagram (containing objects from Dia's Database sheet) into SQL code. As stated in this mailing-list message, this Python script has been tested with MySQL.
Another Python script along the same line is sm_export.py, which converts a Dia diagram representing a finite state machine (FSM; the diagram needs to be drawn using objects from Dia's UML sheet) into a text file listing the states and transitions of the FSM.
State-Chart Code Generation (C++) from UML Diagram
Yet another code generator is State-Chart Code Generation (C++) from UML Diagram, which is similar in purpose to sm_export.py but produces C++ code. The mailing-list posting also includes a hint on how a Python script can store generic meta-information in the objects of a Dia diagram.
The Dia Script Launcher lets you define a list of shell scripts that can be launched from a dialog in Dia. The Script Launcher gives the scripts access to the active Dia diagram and to objects within the diagram.
Automatically draw arcs to connect selected objects
This Python script completely connects all selected "Standard - Ellipse" objects by means of "Standard - Arc" objects.
Scripting Dia in Python is simple. If you don't know where to start, you may as well ask on the mailing list. Maybe you will happen to get your script written as an example.
1 import sys, dia
3 def center_objects (objs) :
4 r = objs.bounding_box
5 cx = (r.right + r.left) / 2
6 cy = (r.bottom + r.top) / 2
7 for o in objs[1:] :
8 r = o.bounding_box
9 (x, y) = o.properties["obj_pos"].value
10 dx = (r.right + r.left) / 2 - cx
11 dy = (r.bottom + r.top) / 2 - cy
12 o.move (x - dx, y - dy)
14 def dia_objects_center_cb (data, flags) :
15 grp = data.get_sorted_selected()
16 if (len(grp) > 1) :
17 center_objects (grp)
18 data.update_extents ()
21 dia.register_callback ("Center Objects",