Book a Demo

Author Topic: JScript - Add New copy of existing element to diagram.  (Read 8116 times)

leonhardtk

  • EA User
  • **
  • Posts: 21
  • Karma: +0/-0
    • View Profile
JScript - Add New copy of existing element to diagram.
« on: April 19, 2022, 10:54:07 pm »
I'm trying to add existing object to a diagram as a new element.
My object is a boundary box with a logo as the alternate image.  The object exists in the image library and "New" copies of this element exist in my Sparx repository.
Currently, I navigate to an existing diagram, copy the object, then "Paste as New Element" on my new diagram.
I'm not real smart on JSCRIPT (or any language), but the JScript i've "hijacked" creates a Title Block for my diagrams.  Not perfect, but it's getting there.
I now want to add a Logo in the upper left corner of my diagram with the Title Block to the right of this.  I think I can figure out the offsets for the Title Block Position, but no idea how to get the logo in the upper left corner.

Is there a function in JSCRIPT that allows me basically Paste as New element (by copying a T_OBJECT or T_DIAGRAMOBJECTS entry)?  Ideally it keeps the Coordinates, etc...but just creates an identical copy on my diagram.

I appreciate any assistance you might be able to provide.



Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: JScript - Add New copy of existing element to diagram.
« Reply #1 on: April 19, 2022, 11:03:39 pm »
Why do you want to create a copy of the element? Why not simply reuse the same element on all diagram you want?
This would allow you to change the logo on all diagrams by changing a single element.

In that case you simply need to create a new DiagramObject using diagram.DiagramObject.AddNew() and fill in the ElementID of your logo element.

Geert

leonhardtk

  • EA User
  • **
  • Posts: 21
  • Karma: +0/-0
    • View Profile
Re: JScript - Add New copy of existing element to diagram.
« Reply #2 on: April 19, 2022, 11:15:35 pm »
I agree, but when the team approached about adding the logo to the TitleBlock, I did a cursory look and they were pasting as new.  I like creating as a link as you suggest for the exact reason you suggest.

Thanks, I'll give this a try!

leonhardtk

  • EA User
  • **
  • Posts: 21
  • Karma: +0/-0
    • View Profile
Re: JScript - Add New copy of existing element to diagram.
« Reply #3 on: April 19, 2022, 11:50:26 pm »
When I entered what I thought I understood:

var myLogo = 12345;
diagram.DiagramObject.AddNew();

I received an error that "diagram" is undefined.  So I borrowed from another part of the script:
var myLogo = 12345;
currentDiagram = Repository.GetTreeSelectedObject();
currentDiagram.DiagramOjbect.AddNew( myLogo);

Now I get a type mismatch error..
Do I need to tell AddNew to expect an object_ID?
 

leonhardtk

  • EA User
  • **
  • Posts: 21
  • Karma: +0/-0
    • View Profile
Re: JScript - Add New copy of existing element to diagram.
« Reply #4 on: April 20, 2022, 01:13:44 am »
I've found another thread you had responded to, reference to another page...this made sense and I'm making progress:
var myLogoID = 12345;
currentDiagram = Repository.GetTreeSelectedObject();
NewElement = currentDiagram.DiagramObjects.AddNew( "","");
NewElement.ElementID = myLogoID;
NewElement.Update;

The almost works!  It places the boundary box on the diagram!  However, the boundary box is an empty boundary box but doesn't show the alternate image!
I don't know the available methods to add the alternate image...or copy the rest of the characteristics from the original in the new diagram object...
Thoughts?

KSL

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: JScript - Add New copy of existing element to diagram.
« Reply #5 on: April 20, 2022, 01:36:27 am »
t_diagram.objectstyle has an entry like

ImageID=1708599719;

along with other semiclon separated blurb. Here you have to add the right id from your image.
In the API you refer that as diagramObject.Style.

Good luck.

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: JScript - Add New copy of existing element to diagram.
« Reply #6 on: April 20, 2022, 01:45:16 am »
You'll have to investigate the diagramObjects in the database to discover the different (t_diagramObjects), and then update the corresponding fields using the API.

Geert

leonhardtk

  • EA User
  • **
  • Posts: 21
  • Karma: +0/-0
    • View Profile
Re: JScript - Add New copy of existing element to diagram.
« Reply #7 on: April 20, 2022, 02:10:58 am »
t_diagram.objectstyle has an entry like

ImageID=1708599719;

along with other semiclon separated blurb. Here you have to add the right id from your image.
In the API you refer that as diagramObject.Style.

Good luck.

q.

This is the rabbit I'm chasing now.   
My latest attempt is:
var myLogoID = 12345;
currentDiagram = Repository.GetTreeSelectedObject();
NewElement = currentDiagram.DiagramObjects.AddNew( "l=19;r=84;t=-26;b=-95","");  \\This correctly sizes my new object!
NewElement.ElementID = myLogoID;
/* my attempts at updating t_diagramObjects.objectstyle:

NewElement.ElementStyle = "ImageID=1122334455;lwth=0;NSL=0;"  \\ This isn't correct, so I tried what you have
NewElement.Element.Style = "ImageID=1122334455;lwth=0;NSL=0;"  \\ Still no dice...so I tried based on Object...?
NewElement.ObjectStyle = "ImageID=1122334455;lwth=0;NSL=0;"  \\ Still no dice....
NewElement.Object.Style = "ImageID=1122334455;lwth=0;NSL=0;"  \\ Still no dice...
*/
NewElement.Update;

As can tell; i have no idea what I'm doing...just throwing spitballs hoping one sticks...

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: JScript - Add New copy of existing element to diagram.
« Reply #8 on: April 20, 2022, 09:18:51 am »
I will give it a try tomorrow. Shouldn't be that difficult. If you know the traps ;-)

q.

P.S. Just spotted a couple of issues in your code. Too late for details, but Geert will likely spot them as well and he's likely up earlier than me.
« Last Edit: April 20, 2022, 05:41:58 pm by qwerty »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: JScript - Add New copy of existing element to diagram.
« Reply #9 on: April 20, 2022, 02:30:21 pm »
There's no need to start guessing. Read the documentation: https://sparxsystems.com/enterprise_architect_user_guide/15.2/automation/diagramobjects.html

There is even a method to help you set a property (imageID) of the style: SetStyleEx
But first make sure you actually need to update the style by querying the database and comparing how a manually set example looks like.

Geert

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: JScript - Add New copy of existing element to diagram.
« Reply #10 on: April 20, 2022, 05:49:09 pm »
@Geert: Muhaha. I just read that section about SetStyle at the bottom and it's typical Sparxian. They explain it with BCol etc. and then they use a different (obviously redundant way) to actually set the color. One shouldn't blame users to not read that stuff :-/ (Not as offense against the documenter but as against the API designers.)

This (Python) snippet worked for me:

Code: [Select]
dia = rep.getDiagramByGUID("{B223AF42-7C2C-4c5b-B840-5DB18FE3AA77}")
dObj = dia.diagramObjects.getAt(0)
dObj.setStyleEx("ImageID", "1708599719")
dObj.update

You even don't need to reload the diagram (that was different in former versions I'd guess).

q.
« Last Edit: April 20, 2022, 06:03:06 pm by qwerty »

leonhardtk

  • EA User
  • **
  • Posts: 21
  • Karma: +0/-0
    • View Profile
Re: JScript - Add New copy of existing element to diagram.
« Reply #11 on: April 20, 2022, 10:53:42 pm »
Yes.  I agree exactly with what you two are saying.  As I was trying figure this out, I was running SQL.  This showed me that a pretty empty DiagramObject row was created.  Based a link Geert provided on another FORUM response, I discovered how to update the coords (and solved my type-mismatch!).
I also noticed the difference between the original and the new diagram object is the empty ObjectStyle column in t_diagramobjects.

So my only remaining problem is how to update the value in the t_diagramobjects for that new object in my JSCRIPT.  But therein lies the problem!

The Element class doesn't seem to have a diagram objects method...?  And the DiagramObjects Class DOES have a style method (as referenced above).
So my problem is how do I use the Diagram Objects method to update the t_diagramObjects.ObjectStyle column.

I attempted this in my code snippet that I posted yesterday (probably has errors, my working script is on another network and hand-typed it here; sorry!).

NewElement = currentDiagram.DiagramObjects.AddNew( "l=19;r=84;t=-26;b=-95",""); solved one of my problems; it correctly sizes and places my boundary box in the correct location.

The missing value from the diagramObjects.ObjectStyle is:
ImageID=1122334455;lwth=0;NSL=0;DUID=12AD43EI112;

my several attempts to update the diagram object (which failed!) were:

NewElement.ElementStyle = "ImageID=1122334455;lwth=0;NSL=0;" 
NewElement.Element.Style = "ImageID=1122334455;lwth=0;NSL=0;" 
NewElement.ObjectStyle = "ImageID=1122334455;lwth=0;NSL=0;"
NewElement.Object.Style = "ImageID=1122334455;lwth=0;NSL=0;" 

As you can tell above, I've tried to update exactly what you're referring to...but I have no clue how to!
I'm thinking there might be away to add to the currentDiagram.DiagramObjects.AddNew()
such as currentDiagram.DiagramObjects.AddNew("ImageID=1122334455;lwth=0;NSL=0;", "DIAGRAMOBJECTS)  Or something like  that.

Again, I appreciate any assistance,

KSL

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: JScript - Add New copy of existing element to diagram.
« Reply #12 on: April 20, 2022, 11:46:21 pm »
Q just posted an example of what you need to do to set the style.

I'm not sure what else we can tell you. :-\

Geert

leonhardtk

  • EA User
  • **
  • Posts: 21
  • Karma: +0/-0
    • View Profile
Re: JScript - Add New copy of existing element to diagram.
« Reply #13 on: April 26, 2022, 02:08:49 am »
Not to worry.  I have no clue how to incorporate the python code into my JScript.
As this was the last item, I just used a DB hammer to update the column using the Repository.Execute().

I used:
Repository.Execute("UPDATE t_diagramObjects SET ObjectStyle = 'ImageID=1122334455;lwth=0;NSL=0;' WHERE Diagram_ID =" + NewDiagID + " AND Object_ID =" + MyNewObjectID) ;
I then use a Session.Output (Repository.SqlQuery("......" ); to verify in the DB update worked...but not necessary.
NOTE:  MyNewObjectID is the orginal object_ID I'm linking to my selected diagram.

This solved my problem; while not 'perfect' it IS functional.

Thanks for all the inputs.

KSL.
« Last Edit: April 26, 2022, 02:27:20 am by leonhardtk »