Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: hansmaiser on March 01, 2006, 02:27:56 am

Title: Add an element to a diagram
Post by: hansmaiser on March 01, 2006, 02:27:56 am
Hello,

how can I add a element to a diagram?

here is a code snippet:


Set activityDiagram = activityPackage.Diagrams.AddNew("My Activity Diagram", "Activity")

Set activityElement = activityPackage.Elements.AddNew("Activity 1", "Activity")

activityDiagram.DiagramObjects.AddNew (activityElement)

activityDiagram.Update
activityPackage.Diagrams.Refresh


But I cant add an to the diagramm using DiagramObjects.AddNew (activityElement)

How can I do that?
Title: Re: Add an element to a diagram
Post by: Takeshi K on March 01, 2006, 04:16:55 am
You need more code like following after Elements.AddNew method:


Set activityDiagramObject = activityDiagram.DiagramObjects.AddNew ("")

activityDiagramObject.ElementID = activityElement .ElementID

activityDiagramObject.Update


(I don't test these code, so it may contain some error...)

--
t-kouno
Title: Re: Add an element to a diagram
Post by: hansmaiser on March 01, 2006, 05:26:09 am
Ok, I ve tried something like this, but I cant set ELEMENTID for activityDiagramObject?

activityDiagramObject.ElementID has read only access and I cant set the ID. How can I set the elementID?

An other question is, how do I set the coordinates of my activityDiagramObject in the diagram?
Title: Re: Add an element to a diagram
Post by: «Midnight» on March 01, 2006, 06:14:33 am
There are several things going on here. AFAICS:
HTH,
David
Title: Re: Add an element to a diagram
Post by: hansmaiser on March 01, 2006, 07:06:53 am
I am a little bit confused now...

What I have to do when i want create a new activity diagram and put an activity object onto the diagram?

I thougt that I have to do following (in VB6):

1. Get the Repository:

   Dim MyRep As New EA.Repository

   MyRep.OpenFile "c:\eatest.eap"


2. Get the Package in which I want to add the Diagram:

   Set RootPackage = MyRep.Models.GetAt(0)

3. Add new activity diagramm in the Package:

  Set activityDiagram = RootPackage.Diagrams.AddNew("My Diagram", "Activity")

4. Add new activity object in the package:

  Set activityObjectDef = activityPackage.Elements.AddNew("Activity 1", "Activity")

5. Put the activity object onto the activity diagram:

???


Now my questions are: Are the steps 1-4 correct? How can I put an object onto a diagram?

Title: Re: Add an element to a diagram
Post by: «Midnight» on March 01, 2006, 07:40:03 am
Hi again Hans,

You've done (almost) everything right to this point. [I am assuming you are calling .Update() on each thing you add to a collection via .AddNew() before anything else refers to the object in question. If you refer to a new item via its parent collecction you will also have to call .Update() on the parent collection first. Also, see the note after the list below.] If you bail out and open the EA eatest.eap file via the UI you will see:[You might not actually get this result. I cannot remember if you can create a diagram or element as a direct child of the root. You might have to add a package in between. You'll have no trouble though, based on your code so far.]

However, if you open the diagram you will find it blank. What you want to accomplish is the equivalent of dragging Activity 1 onto My Diagram via automation. Here's how [I speak VB.Net more than VB 6, so please forgive any minor typos]:
Code: [Select]
Dim MyDiagramObject as EA.DiagramObject
MyDiagramObject = activityDiagram.DiagramObjects.AddNew("", "DiagramObject")
MyDiagramObject.ElementID = activityObjectDef.ElementID
MyDiagramObject.Update()

At this point you can exit and open eatest.eap by hand and view the diagram again. You should now see Activity 1. You could get and set the .top and .left properties as well, and this would be reflected.

HTH,
David
Title: Re: Add an element to a diagram
Post by: Bruno.Cossi on March 01, 2006, 07:49:39 am
Hi,

try this:


Code: [Select]

       Dim Repository As EA.Repository = New EA.Repository
       Dim Package As EA.Package
       Dim Model As EA.Package
       Dim Diagram As EA.Diagram
       Dim Element As EA.Element
       Dim DiagramObject As EA.DiagramObject

       'First, let's connect to the repository
       Repository.OpenFile("C:\tmp.EAP")

       'For the purposes of this example, we will work with the first model we find (by default there is only one anyway)
       Model = Repository.Models.GetAt(0)

       'Let's create a new package
       Package = Model.Packages.AddNew("Test", "")
       Package.Update()

       'Now we create the activity
       Element = Package.Elements.AddNew("Your Activity", "Activity")
       Element.Update()

       'New empty diagram
       Diagram = Package.Diagrams.AddNew("Your Diagram", "Activity")
       Diagram.Update()

       'On the diagram, the diagram object
       DiagramObject = Diagram.DiagramObjects.AddNew("", "")

       'Here we say which of the existing elements are we putting on the diagram
       DiagramObject.ElementID = Element.ElementID
       DiagramObject.Update()

       'And we are done!


Hope this helps!
Bruno
Title: Re: Add an element to a diagram
Post by: hansmaiser on March 01, 2006, 08:16:56 am
Thanks!! It works now :-)

My problem was that I tried to add an element onto the diagram and not a diagramObject...

How can I connect 2 activities with each other?
Title: Re: Add an element to a diagram
Post by: hansmaiser on March 01, 2006, 08:29:07 am
I have an other problem with setting left and right position of the diagramObject, I tried it like this:

activityDiagramObject.Left = 1000
activityDiagramObject.Top = 100
activityDiagramObject.Update


What is wrong here?
Title: Re: Add an element to a diagram
Post by: «Midnight» on March 01, 2006, 08:51:52 am
Try this:
Code: [Select]
Dim myLink as EA.Connector
myLink = Element1.Connectors.AddNew("", "Association")
myLink.SupplierID = Element2.ElementID
myLink.Update()

As before, check the EA User Guide for the allowed type strings for connectors - I've just grabbed one at random. You can also name the connection if you want.

If you don't want to have this connector show on a given diagram, look through the DiagramLinks collection of the diagram for a connector with the same ConnectorID, and delete this item from the DiagramLinks collection (versus deleting the Connector from the model).

Please explain what is going wrong when you set the Top and Left propertoes for your diagram object.

David
Title: Re: Add an element to a diagram
Post by: hansmaiser on March 01, 2006, 09:07:59 am
When I set the attributes left and top, it happends nothing to the DiagramObject, it is still in the upper left corner of the diagram :-/
Title: Re: Add an element to a diagram
Post by: «Midnight» on March 01, 2006, 09:39:06 am
Hans,

I tried this out with a diagram I have. Before I changed anything the Top property was set to -160. When I added 250 to it, the property returned 60. However, the element itself moved to the top of the diagram. My only idea is that the properties may be using a different coordinate system than I expected.

[Of course, with Sparx being in The Land of Aus, they might just be standing on their heads (as we northern hemisphere folks have always known) when they program this stuff.]

My suggestion is that you play around a bit, perhaps peruse the User Guide stuff on Automation and page sizes, and see what happens.

Please let me know if you get predictable results. I need to know this for an upcoming experiment where I'll need to put diagram objects in Boundary elements.

David
Title: Re: Add an element to a diagram
Post by: KP on March 01, 2006, 03:14:45 pm
Quote
When I set the attributes left and top, it happends nothing to the DiagramObject, it is still in the upper left corner of the diagram :-/


Something like this should work:

  DiagramObject.Left = 200
  DiagramObject.Top = -200
  DiagramObject.Right = 300
  DiagramObject.bottom = -300

- Numbers in the Y axis are negative and descending.
- If the diagram object does not represent a fair dinkum rectangle a default rectangle will be chosen.

Title: Re: Add an element to a diagram
Post by: Bruno.Cossi on March 01, 2006, 03:27:24 pm
That worked :-)
Just out of curiousity, why is the Y axis in negative numbers?

Bruno

Quote

Something like this should work:

   DiagramObject.Left = 200
   DiagramObject.Top = -200
   DiagramObject.Right = 300
   DiagramObject.bottom = -300

 - Numbers in the Y axis are negative and descending.
 - If the diagram object does not represent a fair dinkum rectangle a default rectangle will be chosen.


Title: Re: Add an element to a diagram
Post by: KP on March 01, 2006, 03:46:28 pm
Quote
That worked :-)
Just out of curiousity, why is the Y axis in negative numbers?

Bruno


It's Cartesian co-ordinates with the origin in the top left corner. I guess the alternative would be to have the origin in the bottom left corner, but the location of the bottom left corner depends on your screen/paper size, so that wouldn't work.
Title: Re: Add an element to a diagram
Post by: mikewhit on March 02, 2006, 02:16:28 am
No, the alternative would be to use positive values for 'sensible' directions ! ;-)

cf. screen coordinates systems that have origin at top LH, with positive X L->R and positive Y top->bottom

£ 2/240