Sparx Systems Forum
Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started 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?
-
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
-
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?
-
There are several things going on here. AFAICS:
- The above code will get the element into the model, but not onto a diagram (via automation that is, you could always do this manually).
- EA creates two identifiers when it creates the new elememt: the ElementID Integer which is used as the primary key for the t_object table, and the ElementGUID string that is available as a UID for replication and other purposes (including our own).
- Because the ElementID is the PK in a repository table, it can not be manipulated by us, and is read only.
- You
can change the ElementGUID. First, take careful note of how it is formatted (it seems to be a 38 character string in a 40 character repository field but that should not be an issue; just do it the same way EA does). Before or immediately after you create your element, obtain your own GUID, perhaps with something like myGuid = GUID.NewGuid.ToString() (in .Net). Then set your element's ElementGUID property to your new GUID. You must do this before you call .Update() on the new element. You now have set the ElementGUID. After this point you cannot change the ElementGUID, lest you break the silver thread and trash your model...
- The next issue you have - the coordinates of an element - requires you to work at another level of abstraction. The element you have added to the model is just (and only) that, an element. Before you think about coordinates you will have to create a diagram, and copy (a reference to) the element onto it.
- Create the diagram via a code sequence similar to t-kouno's code. Get the acceptable "type" strings from the EA User Guide.
- Add a DiagramObject to the diagram, again using the same type of code sequence. You can use a blank name string here, and the type string can simply be "DiagramObject" since you are not creating an actual element in the model. Set the diagram object's ElementID to the ElementID of your (model) element. Now call the .Update() method of the diagram object. (You could do the next step before this, but for now pleaase bear with me.)
- You can now set the .left and .top properties of the diagram object to achieve what you want. You can also (I believe) size the element via the .bottom and .right properties.
HTH,
David
-
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?
-
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:- The root package.
- Inside the root package you will have:
[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]:
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
-
Hi,
try this:
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
-
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?
-
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?
-
Try this:
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
-
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 :-/
-
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
-
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.
-
That worked :-)
Just out of curiousity, why is the Y axis in negative numbers?
Bruno
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.
-
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.
-
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