[译]Dia Python Plugin Tutorials

来源:互联网 发布:在校大学生网络兼职 编辑:程序博客网 时间:2024/06/11 22:43

缘起

之前在写关于Ubuntu下的绘图软件时,说是要翻译Dia的文档,后来想想了,太长了,就只翻译了一下其中插件的部分。此外,我发现Dia和我的yong的输入法结合不是很好,中文输入有些问题。

正文

来源:https://wiki.gnome.org/Apps/Dia/Python 

1  Introduction

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.

Dia包含一个允许执行Python脚本的插件。任何为Dia编写Python脚本都应该张贴到邮件列表中,邮件列表中包含一些将有助于调试和创造更多的脚本的脚本。

In the early years of the Dia Python facility, there was a great lack of documentation. To get you started, there was somemail from Hans Breuer on the Python plug-in object structure and syntax.

早年,DiaPython的设施文档欠缺。为了帮助您开始,有一些关于在Python的插件的对象的结构和语法的邮件很有帮助(访问后发现,没什么太大的用处,或者我对Maillist的用法不太熟悉,不知道如何去阅读和使用)

Here is a short note from Hans Breuer on how to use Python to create objects in a diagram.

下面是汉斯·布鲁尔一个简短的说明上 如何使用Python创建图中的对象 。

If you need help getting started with Python for Dia on Windows:How to use Python with Dia on Windows。

如果您需要在WindowsDiaPython的帮助与入门:如何在Windows下用PythonDia

2  Python scripts provided with Dia

Nowadays, there are several Python scripts included in the Dia source package.Some of them are presented here.

如今,有很多Python脚本被包含在Dia的源码包中。其中有些展示在这里。

2.1  pydiadoc.py

generates a new diagram which contains all objects of dir(dia). Now fills attributes and operations by using Python reflexion ...

生成一个包含dia的所有对象的新的图。现在,通过Python的灵活性来填充属性和操作...
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.

pydiadoc.py会自动生成下面类型的图。生成包含类的成员,以及该成员函数。 该脚本没有做图的布局,也没有连接对象。


The diagram, in Dia's native format, is included in the Dia binary distribution as samples/Self/PyDiaObjects.dia(samples\Self\PyDia- Objects.dia on Windows).

上图以Dia的原生格式,包含在二进制Dia分配包的samples/Self/PyDiaObjects.dia中(windows samples\Self\PyDia- Objects.dia)。

2.2  diasvg.py

diasvg.py is an SVG export filter for Dia implemented in Python. It is best represented by what it can do:

diasvg.py 是SVG导出过滤。下图它可以做什么最佳表现(备注:原文中这里想展示的图是svg格式-一种矢量图形格式-的图,貌似该编辑器不支持,word也不支持,这里提供一个链接:https://wiki.gnome.org/Apps/Dia/Python?action=AttachFile&do=get&target=pydiadoc.svg,矢量图的好处就是可以任意放大而不失真)

It appears that this wiki does not support embedded SVG, but you can download the diagram here. You will need an SVG capable browser, or a standalone SVG viewer, in order to view the diagram.

 看来wiki支持嵌入式SVG的,但你可以此处下载图 。 您将需要一个 SVG的浏览器 或独立的SVG浏览器,以查看该图。

2.3  diasvg_import.py

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.

diasvg_import.py 是一个Python实现的DiaSVG导入过滤器。像大多数其他Dia的Python脚本,它有两个目的。 一 是识别PyDia弱点。使得它运行不仅涉及工作脚本本身的开发,而且还扩展和修正底层用C实现的Python绑定 

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:)

不过,当然这个脚本可以用于实际工作。 在一些方面 它取代用C实现的导入过滤,虽然现有导入过滤器将 永远不会支持任何SVG。(链接到的bug报告 提出如下:)

 

The real solution may be to [...]start from scratch with a capable svg librarylike [http://webcvs.cairographics.org/libsvg/]

2.4  codegen.py

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.

另一个导出过滤器是 codegen.py 该脚本将DiaUML片段中创建的对象的图转换为用户选择的编程语言的代码(此时,支持Python,C + +PascalJava,其他语言的支持以后添加)。相反 SVG输出,这个脚本使用PyDia “对象渲染器接口。对于代码生成,获得图形表示出口不起太大的作用。相反,该脚本的实现仅支持如下:

dia.!ExportFilter.begin_render()

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")

and

 

dia.!ExportFilter.end_render()

which is implemented differently for each output programming language (C++, Python, and so on).

2.5  group_props.py

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

group_props.py 是一个原先提供Dia大多数核心功能的原型:一次性改变多个对象的能力。此功能从0.97版本开始内建到Dia中。然而,group_props.py可能仍然是有用的。例如,它不仅显示了将Python脚本融合到菜单,也演示了通过PyGTK完成的一些互动。

group_props.py在对话框中的例子:

 

3  Python scripts available elsewhere

3.1  diasql.py

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.

Diasql.py和上文提到的codegen.py类似,它转换将含用Dia的数据库表的对象的Dia图转换为SQL代码。该Python脚本已经在MySQL中测试。 

3.2  sm_export.py

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.

在相同线上的另一个Python脚本是sm_export.py ,它转换将表示有限状态机的(FSM,使用从DiaUML表中的对象绘制的图)转换为列出FSM状态和转换的文本文件

3.3  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.

另一个代码生成器是UML中状态图代码生成(C + +),这和sm_export.py的目的相似,但生成的是C + +代码。邮件列表的发帖还包括有关Python脚本如何在Dia图中的对象存储通用的元信息的提示。

3.4  Script Launcher

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.

Dia的脚本启动器可以让你定义可从Dia对话框启动的shell脚本的列表。脚本启动activeDia图以及图中对象可访问的脚本。

3.5  Automatically draw arcs to connect selected objects

This Python script completely connects all selected "Standard - Ellipse" objects by means of "Standard - Arc" objects.

该Python脚本使用“标准-弧”对象完全连接所有选定“标准-椭圆”对象。

3.6  center.py

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.

在Dia中编写Python很简单。如果你不知道从哪里开始,你不妨问问邮件列表。简单的例子如下:

import sys, diadef center_objects (objs) :    r = objs[0].bounding_box    cx = (r.right + r.left) / 2cy = (r.bottom + r.top) / 2for o in objs[1:] :r = o.bounding_box(x, y) = o.properties["obj_pos"].valuedx = (r.right + r.left) / 2 - cxdy = (r.bottom + r.top) / 2 - cyo.move (x - dx, y - dy)def dia_objects_center_cb (data, flags) :grp = data.get_sorted_selected()if (len(grp) > 1) :center_objects (grp)data.update_extents ()dia.active_display().diagram.add_update_all()dia.register_callback ("Center Objects","<Display>/Objects/Center",dia_objects_center_cb)

小结

这篇文档中很多地方提到maillist,通过maillist的归档来学习,我很迷惑,不知道如何使用maillist。下一篇关于绘图软件图类的我已经想好了:是关于graphviz和dot语言的。


0 0