Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: marine1981 on November 16, 2010, 12:29:02 am

Title: Insert a new Element in a available Element
Post by: marine1981 on November 16, 2010, 12:29:02 am
I search the available diagram. How I get this diagram? I select the Package and then I create a “New Package” with a “New Diagram”. Now, I want insert the New Diagram in the available diagram”. I use the OnPostNewPackage-Event with C#

My tree:
Quote
+ Package
++ Diagram
++ New Element for the “New Diagram”
++ NewPackage
+++ New Diagram
Code: [Select]
EA.Package ParentPackage = Repository.GetPackageByID(NewPackage.ParentID)

for(short i=0; i < ParentPackage.Diagrams.Count-1; i++)
{
      MessageBox.Show(“Do somethinkg” + i);

}  
Title: Re: Insert a new Diagram in a available Diagram
Post by: Geert Bellekens on November 16, 2010, 12:40:14 am
Sven,

First of all, you can use foreach on EA.Collection, that is a bit more elegant then your for loop.
Further you need to add Something to the Diagram.DiagramObjects collection to put something on the diagram.

Geert
Title: Re: Insert a new Diagram in a available Diagram
Post by: marine1981 on November 16, 2010, 01:08:13 am
I have already inserted a Element into the Diagram. Later, I want connect the both "Elements".
Title: Re: Insert a new Diagram in a available Diagram
Post by: Geert Bellekens on November 16, 2010, 01:11:34 am
To Connect the elements use Element.Connectors on the source and fill in the ID of the target on the new Connector before saving it.

Geert
Title: Re: Insert a new Diagram in a available Diagram
Post by: marine1981 on November 16, 2010, 01:28:49 am
How I can use the EA.Collection with foreach?

EA.Collection DiagramSearch = ParentPackage.Diagrams. ....;//?
foreach(short i in DiagramSearch)
{
 MessageBox.Show("Test: " + Convert.ToString(i));
}
Title: Re: Insert a new Diagram in a available Diagram
Post by: stao on November 16, 2010, 11:54:05 am
foreach (EA.Diagram actDiagram in ParentPackage.Diagrams) {

       actDiagram...();
}
Title: Re: Insert a new Diagram in a available Diagram
Post by: marine1981 on November 16, 2010, 08:29:01 pm
I have got under a selected Package a Diagram in the Project Browser. How I can get the Diagram? It exists only one Diagram. How I can do this one?
Title: Re: Insert a new Diagram in a available Diagram
Post by: Geert Bellekens on November 16, 2010, 08:44:52 pm
If you are sure there is only one diagram you can do
Package.Diagrams.GetAt(0)
But you better put a test before that, so something like
Code: [Select]
if (myPackage.Diagrams.Count > 0)
{
   myDiagram = (EA.Diagram)Package.Diagrams.GetAt(0);
}

Geert
Title: Re: Insert a new Element in a available Element
Post by: marine1981 on November 17, 2010, 12:42:51 am
It does work well. Now, I want conntect both elements.
I have already the ID vom the new element. Now, I need the ElementID from the Element with Stereotype "MyStereotypeName". How I can search the right element in the Diagram?
Title: Re: Insert a new Element in a available Element
Post by: Geert Bellekens on November 17, 2010, 12:49:03 am
Quote
My Idea:
Not much of an idea then  ;D

I think you best loop the DiagramObjects of your diagram, get the ElementID of the diagramObject, get the element using Repository.GetElementByID and check the stereotype.

Geert
Title: Re: Insert a new Element in a available Element
Post by: marine1981 on November 17, 2010, 01:01:40 am
It's late ;)
My Idea is:

Code: [Select]
foreach(EA.Element ParentElement in ParentDiagramObject)
{

   ParentElement = Repository.GetElementByID(ParentElement.ElementID);
   if(ParentElement.Stereotype == "StereotypeName");
   {
     //Conntect the elements
    }
}
Title: Re: Insert a new Element in a available Element
Post by: marine1981 on November 17, 2010, 06:28:13 pm
My problem is very simple. I don't kow, how i can get the upper element from the diagram(not Project Browser). The upper element has got a clear stereotyp. The other elements in the diagram has got the same stereotyp. How I can get the the element with the clear stereotype?

My idea was:
Code: [Select]
foreach(EA.Element ParentElement in ParentDiagramObject)
{
//a sample code
MessageBox.Show(ParentElement.Stereotype);

}
But it does not work :(
Title: Re: Insert a new Element in a available Element
Post by: Geert Bellekens on November 17, 2010, 06:35:09 pm
So do you need the element that owns the diagram, or an element that is shown on the diagram?

Geert
Title: Re: Insert a new Element in a available Element
Post by: marine1981 on November 17, 2010, 06:45:40 pm
I need the first element(main element) from the diagram. Is the first element the owner of the diagram?
Title: Re: Insert a new Element in a available Element
Post by: Geert Bellekens on November 17, 2010, 06:52:21 pm
How do you determine the "first" or "main" object in a diagram?
In a diagram all objects are created equal  ;)
The owning element is the element which owns the diagram, which means the diagram is nested under the element in the project browser like
Package -> Onwer of element
- Element -> Owner of diagram
  - Diagram

Geert
Title: Re: Insert a new Element in a available Element
Post by: marine1981 on November 17, 2010, 07:14:23 pm
The first element is the first element which was created on the diagram. The other elements which later was inserted on the diagram should connected to the first element. My structure Diagram, Package and Element Structure in the Project Browser is not normal.

+ 1. Layer
++ 2. Layer
+++ 3. Layer

My structure:
Code: [Select]
+Select Package
+First Element
++ Diagram
++ New Package
++ New first Element for the 2. Diagram
+++ 2. Diagram
+++ ...
Title: Re: Insert a new Element in a available Element
Post by: Geert Bellekens on November 17, 2010, 07:24:04 pm
EA doesn't record the order in which elements are created on diagrams, but in your case it seems that you can work with the owner of the diagram.
So if you want to connect all elements shown on the diagram to the owner element I would do something like:
Code: [Select]
EA.Element ownerElement = myRepository.GetElementByID (myDiagram.ParentID);
foreach (EA.DiagramObject myDiagramObject in myDiagram.DiagramObjects)
{
   if (myDiagramObject.ElementID != ownerElement.ElementID)
   {
     EA.Connector newCon = ownerElement.Connectors.AddNew("","Association");
    newCon.SupplierID = myDiagramObject.ElementID;
    newCon.Update();
    }
}

Geert
Title: Re: Insert a new Element in a available Element
Post by: marine1981 on November 17, 2010, 08:00:09 pm
Thanks. How I define the owner element on the diagram? Does EA define the owner element automatically?
Title: Re: Insert a new Element in a available Element
Post by: Geert Bellekens on November 17, 2010, 08:04:41 pm
If you create the new diagram under the Element then its ElementID is stored in the Diagram.ParentID.

Geert
Title: Re: Insert a new Element in a available Element
Post by: marine1981 on November 17, 2010, 09:10:17 pm
I get a error message:
EA_OnPostNewPackage: Can't find matching ID
mm...
Title: Re: Insert a new Element in a available Element
Post by: Geert Bellekens on November 17, 2010, 09:17:38 pm
What's in the Diagram.ParentID?

Geert
Title: Re: Insert a new Element in a available Element
Post by: marine1981 on November 17, 2010, 10:45:49 pm
Here my code:
Code: [Select]
//Insert new element in upper Diagram
if(ParentPackage.Diagrams > 0)
  EA.Diagram ParentDiagram = (EA.Diagram)ParentPackage.Diagrams.GetAt(0);
  EA.DiagramObject ParentDiagramObject = (EA.DiagramObject)ParentDiagram.DiagramObjects.AddNew("","");
  ParentDiagramObject.ElementID = NewElement.ElementID;
  ParentDiagramObject.Update();
  //Connect both elements
  EA.Element ownerElement = Repository.GetElementByID(ParentDiagram.ParentID);
  foreach(EA.DiagramObject myDiagramObject in ParentDiagram.DiagramObjects)
  {
    if(myDiagramObject.ElementID != ownerElement.ElementID)
    {
       EA.Connector newCon = [color=#ff00ff]ownerElement.Connectors.AddNew("","Associtaion");[/color]
       newCon.SupplierID = myDiagramObjecg.ElementID;
       newCon.Update();
    }
  }

The error message:
The Type "object" can not implicit convert in "EA.Connector". It is already exist an explicit connversation.
Casting ist not helpful.

Do you have got any idea?

Thanks


Sven
Title: Re: Insert a new Element in a available Element
Post by: Geert Bellekens on November 18, 2010, 12:13:41 am
Yeah, you have to remember to always cast the objects returned by Collection.AddNew()
so
Code: [Select]
EA.Connector newCon = (EA.Connector)ownerElement.Connectors.AddNew("","Association");should work

Geert
Title: Re: Insert a new Element in a available Element
Post by: marine1981 on November 18, 2010, 05:45:24 pm
Hello,
I have tried it before but it does not work. I get the error message:
Quote
EA_OnPostNewPackage: Can't find matching ID
Do you have got any idea?
Title: Re: Insert a new Element in a available Element
Post by: Geert Bellekens on November 18, 2010, 06:01:02 pm
On which line?
Are you sure the diagram is nested under the element? (what does Diagram.ParentID have as value?)
Has the element been saved?

Geert
Title: Re: Insert a new Element in a available Element
Post by: marine1981 on November 18, 2010, 06:21:38 pm
Element saved?
What does it mean?
I get the error message in EA not in Visual C#
Title: Re: Insert a new Element in a available Element
Post by: Geert Bellekens on November 18, 2010, 07:43:39 pm
In the project browser, is the direct parent of the diagram an element or a package?
This should be reflected by the Diagram.ParentID. If it is owned by a package then the ParentID will be 0.

I'm sure you get the error in EA, but you'll need to debug to figure out which line is causing the problem.

Geert
Title: Re: Insert a new Element in a available Element
Post by: marine1981 on November 18, 2010, 08:20:28 pm
Now, I have insered a MessageBox.Show(Convert.ToString(ParentDiagram.ParentID));
And I get a 0 in the MessageBox.

I unfortunately use Visual C# 2008 Express. So debug does not work. :(
My structure:

+ 1. Layer
++ 2. Layer
+++ 3. Layer

Quote
+Select ParentPackage
+First (Parent)Element (to connect)
++ Parent Diagram (to connect both elements in this diagram)
++ New Package
++ New first Element for the 2. Diagram and the Parent Diagram (to connect)+++ 2. Diagram
+++ ...
Title: Re: Insert a new Element in a available Element
Post by: Geert Bellekens on November 18, 2010, 08:58:38 pm
Sven,

I know you cannot connect your debugger to EA.exe, but you can debug your code if you make a small test application that connects to the running EA instance and runs your code (or the part that matters)
this code will connect to the open instance of EA.
Code: [Select]
using System.Runtime.InteropServices;
...
            //connect to the currently running instance
            //if multiple instances are running it will connect to the instance that was first started.
            object obj = Marshal.GetActiveObject("EA.App");
            EA.App eaApp = obj as EA.App;
            wrappedModel = eaApp.Repository;

Geert
Title: Re: Insert a new Element in a available Element
Post by: marine1981 on November 18, 2010, 10:31:44 pm
Do you mean a seperate program like command-program(DOS)?
Or a integration in the Add-In?
Code: [Select]

object obj = Marshal.GetActiveObject("EA.App");
EA.App eaApp = obj as EA.App;
wrappedModel = eaApp.Repository;  

How I define/create the object wrappedModel because Visual don't know the object "wrappedModel".
Title: Re: Insert a new Element in a available Element
Post by: Geert Bellekens on November 18, 2010, 10:45:03 pm
Just a regular Windows Forms application with a button or something to start your test.
That's how I usually test my addins (avoids having to restart EA each time you recompile)

Geert
Title: Re: Insert a new Element in a available Element
Post by: marine1981 on November 18, 2010, 10:52:42 pm
How I can define/create the object wrappedModel becaus Visual doesn't know it.
Title: Re: Insert a new Element in a available Element
Post by: Geert Bellekens on November 18, 2010, 10:54:56 pm
wrappedModel is a variable I defined of type EA.Repository.
The snippet is part of my wrapper class for EA.Repository.

Geert
Title: Re: Insert a new Element in a available Element
Post by: marine1981 on November 18, 2010, 11:01:41 pm
I have created the application.
How I use the application to debug, now?
Title: Re: Insert a new Element in a available Element
Post by: Geert Bellekens on November 18, 2010, 11:07:41 pm
Start it and put a breakpoint in your operation, then you can go step by step.

Geert
Title: Re: Insert a new Element in a available Element
Post by: marine1981 on November 18, 2010, 11:16:26 pm
How I set under Debuggen=> Startoptions the command/arguments? Which folder does it need?
Title: Re: Insert a new Element in a available Element
Post by: Geert Bellekens on November 18, 2010, 11:25:38 pm
Sven, if you have created a windows forms application you should be able to just start it by clicking the green |> icon.

Geert
Title: Re: Insert a new Element in a available Element
Post by: marine1981 on November 18, 2010, 11:30:04 pm
Hello,
it is possible to start but i get a message "A Project with the Type "ClassLibery" cannot start directly".

Title: Re: Insert a new Element in a available Element
Post by: Geert Bellekens on November 18, 2010, 11:36:56 pm
That's because you have to start of project of type "Windows Forms Application"

Geert
Title: Re: Insert a new Element in a available Element
Post by: marine1981 on November 18, 2010, 11:44:10 pm
Yes, of course. Thats my problem so i cannot debug.