Author Topic: Python COM AddNew Class and Image  (Read 9967 times)

FSXManu

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Python COM AddNew Class and Image
« on: March 02, 2022, 07:36:33 pm »
Hi

I'm pretty new to this so sorry if this is a "duh, it's so easy" question :D
So I work with Python 3.8 and I get the COM Object of my repo and now I need to search for a specific package and see if a specific class already exists. If it exists I need to create an Image/png object which relates to that class.
If the class does not exist I need to create it and then attach the png/Image object.
My biggest problem is how can I create an Image with the AddNew method for the COM Object?

Could anyone help me?
Thx
Manuel

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13404
  • Karma: +567/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Python COM AddNew Class and Image
« Reply #1 on: March 02, 2022, 07:39:31 pm »
What exactly do you mean by "create Image/png object"?

Can you describe what you do manually to create that in EA?

Geert

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Python COM AddNew Class and Image
« Reply #2 on: March 02, 2022, 10:12:50 pm »
The image is (most likely) set somewhere in the diagramObject for the element. Most likely somewhere in the styleEx. Would need to look after that...

q.

FSXManu

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: Python COM AddNew Class and Image
« Reply #3 on: March 07, 2022, 07:13:44 pm »
What exactly do you mean by "create Image/png object"?

Can you describe what you do manually to create that in EA?

Geert

Sorry I shoul've clarified it a bit better. It should look like in the screenshot below:
So the structure in EA would look something like this:
Packacke
--Class
----StateMachine
------Artifact Image (this is the png image)

I especially struggle with the AddNew method because I can't pass the Image correctly

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Python COM AddNew Class and Image
« Reply #4 on: March 07, 2022, 07:32:04 pm »
An image can never be an element.

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13404
  • Karma: +567/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Python COM AddNew Class and Image
« Reply #5 on: March 07, 2022, 07:49:40 pm »
Quote
Can you describe what you do manually to create that in EA?

Sorry I shoul've clarified it a bit better. It should look like in the screenshot below:
So the structure in EA would look something like this:
Packacke
--Class
----StateMachine
------Artifact Image (this is the png image)

I especially struggle with the AddNew method because I can't pass the Image correctly

You haven't actually answerred the question but only described the end result somewhat.
The reason I'm asking is because you'll have to replicate the manual steps using the API.

I'm guessing you'll have to create an artifact, add an image to the image library, and then link the image to the artifact, but it all depends on what you actually do to ge the desired end result.

Geert
« Last Edit: March 07, 2022, 09:45:49 pm by Geert Bellekens »

FSXManu

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: Python COM AddNew Class and Image
« Reply #6 on: March 07, 2022, 07:51:21 pm »
An image can never be an element.

q.

Ah okay. Sorry did some digging I think it's like Class->SubElement->Statechart/Diagram->image artifact
Does that make more sense?

FSXManu

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: Python COM AddNew Class and Image
« Reply #7 on: March 07, 2022, 09:12:12 pm »
An image can never be an element.

q.

I found a thread from 2014 in which you answered with the steps one needs to do.
I wonder if you ever did it since then or tested it. Sorry I'm completely new to this.

https://www.sparxsystems.com/forums/smf/index.php?topic=5373.0


FSXManu

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: Python COM AddNew Class and Image
« Reply #8 on: March 09, 2022, 07:45:10 pm »
Quote
Can you describe what you do manually to create that in EA?

Sorry I shoul've clarified it a bit better. It should look like in the screenshot below:
So the structure in EA would look something like this:
Packacke
--Class
----StateMachine
------Artifact Image (this is the png image)

I especially struggle with the AddNew method because I can't pass the Image correctly

You haven't actually answerred the question but only described the end result somewhat.
The reason I'm asking is because you'll have to replicate the manual steps using the API.

I'm guessing you'll have to create an artifact, add an image to the image library, and then link the image to the artifact, but it all depends on what you actually do to ge the desired end result.

Geert

Hi again

Yes So what I do in EA itself is open an item of type StateMachine and then in the Toolbox I drag an "Image Asset" into it and select my local png file. So this is what I need to do via the api I'm just not sure how/I haven't figured it out yet

FSXManu

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13404
  • Karma: +567/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Python COM AddNew Class and Image
« Reply #9 on: March 09, 2022, 08:29:03 pm »
Hi again

Yes So what I do in EA itself is open an item of type StateMachine and then in the Toolbox I drag an "Image Asset" into it and select my local png file. So this is what I need to do via the api I'm just not sure how/I haven't figured it out yet

FSXManu
OK, so that is your next step then.
Create an element of type "Image Asset" in the package or element that owns the diagram you are working on.
Creating elements in done by using AddNew("myName", "EAUML::Image") on the .Elements collection of the owner. (could be a package or an element, depending on where your diagram is located)
Then you have to figure out how to import the actual image. The API won't help us there, so you'll have to figure out how to do it directly in the database

EA stores the image of your image asset in the table t_document

Code: [Select]
select d.* from t_object o
inner join t_document d on d.ElementID = o.ea_guid
where o.ea_guid = '{D15B1DD6-A86B-405c-ACFD-6B040386DBEE}'

by replacing the GUID above by the GUID of your image asset you'll find the record you'll need to recreate.
The difficult part will be to somehow fill in the bincontent with an SQL Query

This might depend on the actual database you are using, so you'll have to do a bit of research.
Here's a Stackoverflow question regarding this for SQL Server: https://stackoverflow.com/questions/1120689/how-can-i-insert-binary-file-data-into-a-binary-sql-field-using-a-simple-insert

The last thing you might want to do is put the image asset on your diagram.
That can be done by adding a diagramObject to the diagram.DiagramObject collection and setting the ElementID to point to your new image asset element.

Geert


« Last Edit: March 09, 2022, 08:53:29 pm by Geert Bellekens »

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Python COM AddNew Class and Image
« Reply #10 on: March 09, 2022, 08:44:44 pm »
Image assets did not exist in 2014. So my answer referred to "normal images" handled differently. Go with Geerts answer.

q.

FSXManu

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: Python COM AddNew Class and Image
« Reply #11 on: March 09, 2022, 11:18:17 pm »

Then you have to figure out how to import the actual image. The API won't help us there, so you'll have to figure out how to do it directly in the database

EA stores the image of your image asset in the table t_document

Code: [Select]
select d.* from t_object o
inner join t_document d on d.ElementID = o.ea_guid
where o.ea_guid = '{D15B1DD6-A86B-405c-ACFD-6B040386DBEE}'

by replacing the GUID above by the GUID of your image asset you'll find the record you'll need to recreate.
The difficult part will be to somehow fill in the bincontent with an SQL Query


Thank you for your help yeah I was able to use the AddNew method. Now I'm trying to insert the image into the documents table.
I read about the Execute() method which is undocumented but it would help. Problem is I always get the error "Execute() takes from 1 to 2 positional arguments but 3 were given"

My line which gives me that error:
Code: [Select]
repo.Execute("INSERT INTO T_DOCUMENT (BINCONTENT) VALUES (:1);", (image_bytes))

Do you have any idea how that works? I'll try other methods

FSXManu

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13404
  • Karma: +567/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Python COM AddNew Class and Image
« Reply #12 on: March 09, 2022, 11:48:18 pm »
You have to pass a single string to Repository.Execute, so more like:

Code: [Select]
repo.Execute("INSERT INTO T_DOCUMENT (BINCONTENT) VALUES (image_bytes)")
Geert

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Python COM AddNew Class and Image
« Reply #13 on: March 10, 2022, 02:12:49 am »
Having "image_bytes" replaced by the real bytes. If that's binary you will have a hard time.

q.

FSXManu

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: Python COM AddNew Class and Image
« Reply #14 on: March 16, 2022, 12:37:25 am »
You have to pass a single string to Repository.Execute, so more like:

Code: [Select]
repo.Execute("INSERT INTO T_DOCUMENT (BINCONTENT) VALUES (image_bytes)")
Geert

Okay I managed to create an entry in the t_document with my image.
But I struggle to link the document entry to a diagram. Any ideas? I tried to add an entry to diagram.DiagramObjects collection but it doesn't work.

FSXManu