Book a Demo

Author Topic: Add-In : How to add Object Diagram (C#)  (Read 4798 times)

The apprentice

  • EA Novice
  • *
  • Posts: 19
  • Karma: +0/-0
    • View Profile
Add-In : How to add Object Diagram (C#)
« on: April 06, 2018, 06:46:21 pm »
Hello

I try to add a new Interface in my Class Diagram with the Add-In on Visual Studio.

According to this example : https://www.sparxsystems.com/enterprise_architect_user_guide/9.3/automation/addandmanagediagrams.html

I have this :

 
Code: [Select]
EA.Package toto;
            toto = EaRepository.GetPackageByID(1);
           
            EA.Package package = (EA.Package)toto.Packages.GetByName("Use Case Model");
            EA.Diagram diagram = (EA.Diagram)package.Diagrams.AddNew("Logicial Diagram","Class");
            diagram.Update();
            EA.Element a = (EA.Element)package.Elements.AddNew("ReferenceType", "Interface");
            a.Update();
            //this cause the error in Enterprise Architect
            EA.Element o = (EA.Element)diagram.DiagramObjects.AddNew("test", "Interface");

            // o.ElementID = a.ElementID is impossible due its protection level so I tried this :
            o = a;
            o.Update();
            diagram.Update();

I have my new diagram and my new interface but I want insert the interface into the diagram.

But I have the following error :
"EA_MenuClick : Unable to cast COM object of type 'System._ComObject' to interface type 'EA.Element'. This operation failed because the QyeryInterface call on the COM component for the interface with IID'{xxxxxx-xx-xx-xxx-xxx}'failed due to the following error : (Exception from HRESULT : 0x80004002(E_NOINTERFACE))."

How to do ?

Please forgive my english, Thank you.


qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Add-In : How to add Object Diagram (C#)
« Reply #1 on: April 06, 2018, 07:19:59 pm »
Code: [Select]
EA.Element o = (EA.Element)diagram.DiagramObjects.AddNew("test", "Interface");
should be

Code: [Select]
EA.Element o = (EA.Element)diagram.DiagramObjects.AddNew("l=10;r=110;t=-20;b=-80", "");You can probably guess the meaning of the first parameter.

You then need
Code: [Select]
o.ElementId = a.ElementId;
a.Update();
(my C# is, well, not present...)

q.
« Last Edit: April 06, 2018, 07:22:18 pm by qwerty »

The apprentice

  • EA Novice
  • *
  • Posts: 19
  • Karma: +0/-0
    • View Profile
Re: Add-In : How to add Object Diagram (C#)
« Reply #2 on: April 06, 2018, 07:47:16 pm »

Code: [Select]
EA.Element o = (EA.Element)diagram.DiagramObjects.AddNew("l=10;r=110;t=-20;b=-80", "");You can probably guess the meaning of the first parameter.


The coordinates of the interface I guess.
It doesn't work, I have the same error.

You then need
Code: [Select]
o.ElementId = a.ElementId;
a.Update();

I can't do this because the attribute ElementID is private ( impossible to create a setter)

Thank you.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Add-In : How to add Object Diagram (C#)
« Reply #3 on: April 07, 2018, 03:09:01 am »
Then it's likely a C# issue (which I'm not fluent in). You need to wait for the language gurus to help you out.

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: Add-In : How to add Object Diagram (C#)
« Reply #4 on: April 07, 2018, 03:59:52 am »
The problem you are having (you should always include the actual error message) is because you are trying to cast an EA.DiagramObject to an EA.Element

Something like this should work a lot better.
Code: [Select]
var diagramObject = (EA.DiagramObject)diagram.DiagramObjects.AddNew("l=10;r=110;t=-20;b=-80", "");
Geert

The apprentice

  • EA Novice
  • *
  • Posts: 19
  • Karma: +0/-0
    • View Profile
Re: Add-In : How to add Object Diagram (C#)
« Reply #5 on: April 17, 2018, 11:00:27 pm »
The problem you are having (you should always include the actual error message) is because you are trying to cast an EA.DiagramObject to an EA.Element

Something like this should work a lot better.
Code: [Select]
var diagramObject = (EA.DiagramObject)diagram.DiagramObjects.AddNew("l=10;r=110;t=-20;b=-80", "");
Geert

It Works, Thank you.