如何使用python导出企业架构图链接到csv ?



我想导出Enterprise Architect模型关系链接到CSV文件。

我是用python做的

import win32com.client
def ea():
try:
eaApp = win32com.client.Dispatch('EA.App')
eaRep = eaRep.Repository
except:
sys.exit()
try:
eaRep.OpenFile2("C:\path-to-model.eap", 1, 0)
package_guid = "{ABC34Hs-*****}"
dia = eaRep.getDiagramByGUID(package_guid)
res = eaRep.SQLQuery(f"select * from t_diagram where ea_guid = '{package_guid}'")
print(res)
for do in dia.diagramobjects:
elem = eaRep.getElementByID(do.elementId)
if elem.name == "my-diagram":
print(elem.Name, elem.Type, do.left, do.right, do.top)
print(elem.Notes)
except:
pass
ea()

这里我只得到模型图的细节,如几何点,一些更多的信息,如包id,图id,类型等。

  1. 我们如何使用Python提取模型的所有关系链接到CSV/Excel文件?
  2. Python是EA链接导出工作的真正好的语言偏好吗?因为我没有看到win32com.client维护良好的文档。

感谢

用相应的操作修改Geerts的答案:

import xml.etree.ElementTree as ET
def query(self, sql):
root = ET.fromstring(self.eaRep.SQLQuery (sql))
data = root.getchildren()
if len(data) == 0: return []
ds = data[0][0]
rows = []
for row in ds:
cols = []
for col in row.getchildren(): cols.append(col.text)
rows.append(cols)
return rows

这里的self.eaRep是您的eaRep,因为上面是在我自己的存储库包装器类中使用的。返回结果为数组的数组。

关于Python:放心。我用了很多年了。win32的东西没有很好的文档,是的,但是一旦你得到了句柄(这在几乎所有情况下都很容易),你就不必再担心了。

不确定您所说的"模型的关系链接"是什么意思,但是如果您正在谈论关系,那么您将需要检查t_connector表。

该表包含关系细节,包括作为目标元素的源的Object_ID。

由于EA存储库是一个数据库,获取所需信息的最简单方法是使用查询。

select os.Name as StartElement, c.Name, c.Connector_Type
, c.Stereotype, ot.Name as TargetElement
from ((t_connector c
inner join t_object os on os.Object_ID = c.Start_Object_ID)
inner join t_object ot on ot.Object_ID = c.End_Object_ID)

您没有指定需要从连接器获得哪些信息,因此我添加了几列作为说明。

当你通过Repository.SQLQuery传递它时,你会得到一个XML字符串,你可以用XML库解析。

我想Python和其他语言一样好。
我个人对处理EA的偏好是c#和VBScript

最新更新