Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: FSXManu on March 02, 2022, 07:36:33 pm

Title: Python COM AddNew Class and Image
Post by: FSXManu 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
Title: Re: Python COM AddNew Class and Image
Post by: Geert Bellekens 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
Title: Re: Python COM AddNew Class and Image
Post by: qwerty 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.
Title: Re: Python COM AddNew Class and Image
Post by: FSXManu 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
Title: Re: Python COM AddNew Class and Image
Post by: qwerty on March 07, 2022, 07:32:04 pm
An image can never be an element.

q.
Title: Re: Python COM AddNew Class and Image
Post by: Geert Bellekens 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
Title: Re: Python COM AddNew Class and Image
Post by: FSXManu 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?
Title: Re: Python COM AddNew Class and Image
Post by: FSXManu 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

Title: Re: Python COM AddNew Class and Image
Post by: FSXManu 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
Title: Re: Python COM AddNew Class and Image
Post by: Geert Bellekens 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 (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


Title: Re: Python COM AddNew Class and Image
Post by: qwerty 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.
Title: Re: Python COM AddNew Class and Image
Post by: FSXManu 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
Title: Re: Python COM AddNew Class and Image
Post by: Geert Bellekens 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
Title: Re: Python COM AddNew Class and Image
Post by: qwerty 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.
Title: Re: Python COM AddNew Class and Image
Post by: FSXManu 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
Title: Re: Python COM AddNew Class and Image
Post by: Geert Bellekens on March 16, 2022, 01:54:55 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
What isn't working? That is exactly what you need to do.

Geert
Title: Re: Python COM AddNew Class and Image
Post by: FSXManu on March 22, 2022, 07:25:10 pm

What isn't working? That is exactly what you need to do.

Geert

So now most things are fine. I got the Image Artifact in EA. The problem is when I open the Image in EA with double click it says it cannot open it because it's corrupted. But when I look at it in the db it's fine. I think the problem is that the Image is in the t_image table and needs a correct ImageID. I just couldn't figure out how to get the correct ImageID and that this needs to be created by EA or sth. Do you know anything about the ImageID in the t_image table?
Title: Re: Python COM AddNew Class and Image
Post by: Geert Bellekens on March 22, 2022, 11:22:13 pm
I don't think there's anything special about the imageID, it's an Identity column in SQL server, so it gets a new ID automatically, you only need to insert name, type and image.
Have you tried copying the values from another t_image record?
I'm guessing there's still something wrong with the value in t_image.image

Geert
Title: Re: Python COM AddNew Class and Image
Post by: FSXManu on March 24, 2022, 12:14:46 am
I don't think there's anything special about the imageID, it's an Identity column in SQL server, so it gets a new ID automatically, you only need to insert name, type and image.
Have you tried copying the values from another t_image record?
I'm guessing there's still something wrong with the value in t_image.image

Geert

Could be something wrong with the t_imga.image yes. I'm still investigating that. Since I have to create the record with SQL I also have to pass an ImageID since it doesn't get generated. This is primarily that the Image Manager recognises it afaik.

Title: Re: Python COM AddNew Class and Image
Post by: FSXManu on April 01, 2022, 05:56:55 pm
I don't think there's anything special about the imageID, it's an Identity column in SQL server, so it gets a new ID automatically, you only need to insert name, type and image.
Have you tried copying the values from another t_image record?
I'm guessing there's still something wrong with the value in t_image.image

Geert

Okay so I think the t_image entry is fine since that is the entry for the image manager and it shows up just fine there. On the other hand in the t_document table where it's needed to be an artifact and diagram object it's not showing correctly. Even though I took the t_image.image value and put it into t_document.bincontent withoug modifying it. What should go into the BINCONTENT column?

FSXManu
Title: Re: Python COM AddNew Class and Image
Post by: Geert Bellekens on April 01, 2022, 07:53:46 pm
Okay so I think the t_image entry is fine since that is the entry for the image manager and it shows up just fine there. On the other hand in the t_document table where it's needed to be an artifact and diagram object it's not showing correctly. Even though I took the t_image.image value and put it into t_document.bincontent withoug modifying it. What should go into the BINCONTENT column?

FSXManu
You shouldn't assume the format of t_image.image is going to be the same as the format of t_document.bincontent

The easiest way to figure out what needs to be done is doing it manually though the GUI first.
Then replicate the results using the API untill you have exactly the same info in the bincontent field.

Geert
Title: Re: Python COM AddNew Class and Image
Post by: FSXManu on April 11, 2022, 06:04:59 pm
You shouldn't assume the format of t_image.image is going to be the same as the format of t_document.bincontent

The easiest way to figure out what needs to be done is doing it manually though the GUI first.
Then replicate the results using the API untill you have exactly the same info in the bincontent field.

Geert

Yes I managed to make it work. I can import into the T_Image table via the importreferencedata command from the API. I just need to create an xml with the image first. This all worked pretty good. But since last Friday when I import the xml via this importreferencedata command in the code, it only creates one image and overwrites it with the next one so if I import an xml with 10 images in it there will be only one image in the table with the content of the last image. Even when I try it with the code that was working a week before I have the problem. Did you ever came accross this issue?
Title: Re: Python COM AddNew Class and Image
Post by: Geert Bellekens on April 11, 2022, 06:51:29 pm
I'm guessing your xml contains an error. Maybe they all have the same identifier?

Geert