Book a Demo

Author Topic: Custom Export of a txt file from EA with detailed info  (Read 7922 times)

jack89

  • EA User
  • **
  • Posts: 49
  • Karma: +0/-1
    • View Profile
Custom Export of a txt file from EA with detailed info
« on: September 18, 2020, 11:59:45 pm »
Hi all,

I'm using EA to model the architecture of a car and I use an external software made with python in order to simulate my described system.

My python script takes in input a config_file.txt that contains a list of car's parameters (height, width, aerodynamic coefficient and so on...).

My question is: If I define the parameters needed in my script in my EA project, is it possible a custom export from EA? In this way I can generate a config_file.txt directly from EA.

Thank you in advance for your support  :)


qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Custom Export of a txt file from EA with detailed info
« Reply #1 on: September 19, 2020, 12:09:45 am »
There's a CSV export in EA. Further the EAScriptLib has a template export script (VB/Java stuff) which you could adapt.

(Of course you could access EA directly from Python to read what is needed.)

q.

jack89

  • EA User
  • **
  • Posts: 49
  • Karma: +0/-1
    • View Profile
Re: Custom Export of a txt file from EA with detailed info
« Reply #2 on: September 19, 2020, 12:41:01 am »
Hi qwerty,

thanks for your answer. Can you explain better how python can interact with EA? Can you link an example/tutorial?

Thank you  :)

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Custom Export of a txt file from EA with detailed info
« Reply #3 on: September 19, 2020, 02:19:14 am »
Actually it's pretty simple:
Code: [Select]
            app = win32com.client.GetActiveObject("EA.App")
            repository = app.Repository
That will give you EA's repository object from where you can do anything described in the API. You just have to have an instance of EA running with the repository in question being opened.

q.

jack89

  • EA User
  • **
  • Posts: 49
  • Karma: +0/-1
    • View Profile
Re: Custom Export of a txt file from EA with detailed info
« Reply #4 on: September 22, 2020, 12:08:24 am »
Hi qwerty,
thank you. I made some exercises on python and EA, but I get into troubles when I'm trying to navigate inside my model.

As I wrote before, I'm interested in getting some info from diagrams and generate a txt file. Can you write an example of python script that loops in every diagram and estract info from certain elements? (notes maybe or time duration constraint).

Than you in advance

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Custom Export of a txt file from EA with detailed info
« Reply #5 on: September 22, 2020, 01:47:13 am »
Code: [Select]
dia = rep.getDiagramByGUID("{970DBE15-BF48-4ead-AD4A-DC21CFB4D194}")
for do in dia.diagramobjects:
    elem = rep.getElementByID(do.elementId)
    print (elem.name, "at", do.left, do.right, do.top, do.bottom)
Hope that's intuitive enough.

q.

jack89

  • EA User
  • **
  • Posts: 49
  • Karma: +0/-1
    • View Profile
Re: Custom Export of a txt file from EA with detailed info
« Reply #6 on: September 22, 2020, 05:29:41 pm »
Hi qwerty, thank you it works!.

I have two questions more:

  • How can I get text element content in a diagram? For example, if I insert a text element called "Delay_motor:100s" in a diagram, how can I get this value in python? (I mean a sort of scraping)
  • How can I create a python dictionary Diagram Name : GUID? In that way I can loop in every diagram

My goal is to create a script that extract certain text info from a certain diagram.

Thank you again for your amazing support

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Custom Export of a txt file from EA with detailed info
« Reply #7 on: September 22, 2020, 05:37:51 pm »
I think you better look into using Repository.SQLQuery to get your info.

Iterating a model in code is painfully slow. It can easily take hours to go over each diagram and each diagram element using the API (on a somewhat larger model)

I have a bunch of scripts stored in github https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library
These are vbscripts, not pythong, but the usage of the API is exactly the same.

Geert

Uffe

  • EA Practitioner
  • ***
  • Posts: 1859
  • Karma: +133/-14
  • Flutes: 1; Clarinets: 1; Saxes: 5 and counting
    • View Profile
Re: Custom Export of a txt file from EA with detailed info
« Reply #8 on: September 22, 2020, 05:53:16 pm »
Hi Jack,


  • How can I get text element content in a diagram? For example, if I insert a text element called "Delay_motor:100s" in a diagram, how can I get this value in python? (I mean a sort of scraping)

Getting data from a text element in a diagram is the same as any other type of element.

Everything in a diagram is a DiagramObject (or DiagramLink for connectors). Every DiagramObject is backed by an Element. EA just chooses to hide some of those elements in the browser. So it will show classes, actors and components, but will not show texts, notes or boundaries (sometimes referred to as diagram-only elements).

But you access them in the same way and they have all the same properties, though many of them might not be used (text elements typically don't have attributes, for instance) and in some cases the notes property might have a special format (as is the case with hyperlinks).

HTH,


/Uffe
My theories are always correct, just apply them to the right reality.

jack89

  • EA User
  • **
  • Posts: 49
  • Karma: +0/-1
    • View Profile
Re: Custom Export of a txt file from EA with detailed info
« Reply #9 on: September 22, 2020, 06:45:18 pm »
I think you better look into using Repository.SQLQuery to get your info.

Iterating a model in code is painfully slow. It can easily take hours to go over each diagram and each diagram element using the API (on a somewhat larger model)

I have a bunch of scripts stored in github https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library
These are vbscripts, not pythong, but the usage of the API is exactly the same.

Geert
Thank you, I will check inside your repository, but I have to get familiar with VB.

Hi Jack,


  • How can I get text element content in a diagram? For example, if I insert a text element called "Delay_motor:100s" in a diagram, how can I get this value in python? (I mean a sort of scraping)

Getting data from a text element in a diagram is the same as any other type of element.

Everything in a diagram is a DiagramObject (or DiagramLink for connectors). Every DiagramObject is backed by an Element. EA just chooses to hide some of those elements in the browser. So it will show classes, actors and components, but will not show texts, notes or boundaries (sometimes referred to as diagram-only elements).

But you access them in the same way and they have all the same properties, though many of them might not be used (text elements typically don't have attributes, for instance) and in some cases the notes property might have a special format (as is the case with hyperlinks).

HTH,


/Uffe



Ok, got it. Can you write a simple example regarding how to access to the text property of text elements? If I perform the following code:

Code: [Select]
for do in dia.DiagramObjects:
elem = o.GetElementByID(do.ElementID)
print (elem.Name, "at", do.left, do.right, do.top, do.bottom)

The output only contains lifelines, fragments but not text elements.

Thank you!

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Custom Export of a txt file from EA with detailed info
« Reply #10 on: September 22, 2020, 06:45:40 pm »
2: I have a wrapper for repository where I defined an operation Query (below is a cut out which might be incomplete)
Code: [Select]
   
import win32com.client
from singleton import Singleton
import xml.etree.ElementTree as ET

@Singleton
class Repository:
    def __init__(self):
        try:
            app = win32com.client.GetActiveObject("EA.App")
            self.eaRep = app.Repository
            models = self.eaRep.models
            done = True
        except Exception as e:
            print (e)
            done = False
        self.base = self.eaRep.connectionstring
        if os.path.exists(self.base):
            path, self.base = os.path.splitext(self.base.lower())
        else:
            self.base = "server"
        self.wildcard = "*" if self.base == ".eap" else "%"

    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

    def __getattr__(self, name):
        sname = name.lower()
        if sname in self._snames:
            self._logger.log("W1InvalidCase", name)
            return self._snames[sname]

        return getattr(self.eaRep, name)

So with that it would be
Code: [Select]
rows = rep.query("SELECT ea_guid FROM t_diagram")
for row in rows:
    print(row[0])
q.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Custom Export of a txt file from EA with detailed info
« Reply #11 on: September 22, 2020, 07:32:33 pm »
The output only contains lifelines, fragments but not text elements.

Thank you!
The text is stored in the element.notes.

q.