[譯]Dia Python Plugin Tutorials

緣起

之前在寫關於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 some mail 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 library
like [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, dia
def center_objects (objs) :
    r = objs[0].bounding_box
    cx = (r.right + r.left) / 2
	cy = (r.bottom + r.top) / 2
	for o in objs[1:] :
		r = o.bounding_box
		(x, y) = o.properties["obj_pos"].value
		dx = (r.right + r.left) / 2 - cx
		dy = (r.bottom + r.top) / 2 - cy
		o.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語言的。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章