Author Topic: Insert a new Element in a available Element  (Read 19732 times)

marine1981

  • EA User
  • **
  • Posts: 58
  • Karma: +0/-0
    • View Profile
Re: Insert a new Element in a available Element
« Reply #15 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
+++ ...

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13295
  • Karma: +557/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Insert a new Element in a available Element
« Reply #16 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

marine1981

  • EA User
  • **
  • Posts: 58
  • Karma: +0/-0
    • View Profile
Re: Insert a new Element in a available Element
« Reply #17 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?

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13295
  • Karma: +557/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Insert a new Element in a available Element
« Reply #18 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

marine1981

  • EA User
  • **
  • Posts: 58
  • Karma: +0/-0
    • View Profile
Re: Insert a new Element in a available Element
« Reply #19 on: November 17, 2010, 09:10:17 pm »
I get a error message:
EA_OnPostNewPackage: Can't find matching ID
mm...

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13295
  • Karma: +557/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Insert a new Element in a available Element
« Reply #20 on: November 17, 2010, 09:17:38 pm »
What's in the Diagram.ParentID?

Geert

marine1981

  • EA User
  • **
  • Posts: 58
  • Karma: +0/-0
    • View Profile
Re: Insert a new Element in a available Element
« Reply #21 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
« Last Edit: November 17, 2010, 10:51:27 pm by marine1981 »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13295
  • Karma: +557/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Insert a new Element in a available Element
« Reply #22 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

marine1981

  • EA User
  • **
  • Posts: 58
  • Karma: +0/-0
    • View Profile
Re: Insert a new Element in a available Element
« Reply #23 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?

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13295
  • Karma: +557/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Insert a new Element in a available Element
« Reply #24 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

marine1981

  • EA User
  • **
  • Posts: 58
  • Karma: +0/-0
    • View Profile
Re: Insert a new Element in a available Element
« Reply #25 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#
« Last Edit: November 18, 2010, 06:57:38 pm by marine1981 »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13295
  • Karma: +557/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Insert a new Element in a available Element
« Reply #26 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

marine1981

  • EA User
  • **
  • Posts: 58
  • Karma: +0/-0
    • View Profile
Re: Insert a new Element in a available Element
« Reply #27 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
+++ ...
« Last Edit: November 18, 2010, 08:41:12 pm by marine1981 »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13295
  • Karma: +557/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Insert a new Element in a available Element
« Reply #28 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

marine1981

  • EA User
  • **
  • Posts: 58
  • Karma: +0/-0
    • View Profile
Re: Insert a new Element in a available Element
« Reply #29 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".
« Last Edit: November 18, 2010, 10:50:26 pm by marine1981 »