Book a Demo

Author Topic: COM Invalid Cast in EA Add-In  (Read 5772 times)

fwoolz

  • EA User
  • **
  • Posts: 435
  • Karma: +0/-0
  • We have met the enemy, and he is us.<Pogo, 1970>
    • View Profile
COM Invalid Cast in EA Add-In
« on: June 02, 2009, 12:21:28 pm »
I recall encountering this problem a long time ago when I was more active in tinkering with add ins, and I seem to remember getting around it, but...

Here is the offending code (C#):

EA.Element nelem = (EA.Element)(currdiag.DiagramObjects.AddNew(currelem.Name + i.ToString(), currelem.Type));

The error:

System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'EA.Element'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{20073882-CCA7-4E64-BE9C-D381216A4225}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

In the above, currelem is an EA element passed to the method in which the offending line appears, currdiag is the current diagram, and i is an index appended to the name of the current element.

Further info:
EA: 7.5 build 845
VS 2008
.NET 3.5 SP1

Thanks!
Fred Woolsey
Interfleet Technology Inc.

Always be ready to laugh at yourself; that way, you beat everyone else to the punch.


Aaron B

  • EA Administrator
  • EA User
  • *****
  • Posts: 941
  • Karma: +18/-0
    • View Profile
Re: COM Invalid Cast in EA Add-In
« Reply #1 on: June 02, 2009, 01:06:47 pm »
Hello Fred,

DiagramObjects.AddNew() returns type EA.DiagramObject, not EA.Element.

A DiagramObject is an instance of an Element on a Diagram.

Try:
EA.DiagramObject nelem = (EA.DiagramObject)(currdiag.DiagramObjects.AddNew("",""));
nelem.ElementID = currelem.ElementID;
nelem.Update();

You do not need to specify anything for the Name and Type parameters of DiagramObject.AddNew(), but you can use them to set position information if you like as shown in the following example:
http://www.sparxsystems.com/uml_tool_guide/sdk_for_enterprise_architect/addandmanagediagrams.html

fwoolz

  • EA User
  • **
  • Posts: 435
  • Karma: +0/-0
  • We have met the enemy, and he is us.<Pogo, 1970>
    • View Profile
Re: COM Invalid Cast in EA Add-In
« Reply #2 on: June 02, 2009, 01:12:07 pm »
That's it!!!

Thanks for the quick reply. I will note, however, that this isn't well-documented in the EA help files...

Cheers,
Fred W
Fred Woolsey
Interfleet Technology Inc.

Always be ready to laugh at yourself; that way, you beat everyone else to the punch.


fwoolz

  • EA User
  • **
  • Posts: 435
  • Karma: +0/-0
  • We have met the enemy, and he is us.<Pogo, 1970>
    • View Profile
Re: COM Invalid Cast in EA Add-In
« Reply #3 on: June 02, 2009, 01:40:52 pm »
BTW - Here's the code that finally got me what I wanted in the first place:

EA.Package pkg = currrep.GetPackageByID(currelem.PackageID);
EA.Element nelem = (EA.Element) pkg.Elements.AddNew(currelem.Name + i.ToString(), currelem.Type);
EA.DiagramObject nobj = (EA.DiagramObject)currdiag.DiagramObjects.AddNew(currelem.Name + i.ToString(), currelem.Type);
nobj.ElementID = nelem.ElementID;
nobj.Update();

I still need to go back and remove the redundant name and type definitions, but it works.

Thanks again for your help!
Fred Woolsey
Interfleet Technology Inc.

Always be ready to laugh at yourself; that way, you beat everyone else to the punch.


fwoolz

  • EA User
  • **
  • Posts: 435
  • Karma: +0/-0
  • We have met the enemy, and he is us.<Pogo, 1970>
    • View Profile
Re: COM Invalid Cast in EA Add-In
« Reply #4 on: June 02, 2009, 01:45:55 pm »
BTW Redux -

After a think, I realized that it makes perfect sense that DiagramObject <> Element, since a single Element can appear on 0..* diagrams. DOH!! My bad.
Fred Woolsey
Interfleet Technology Inc.

Always be ready to laugh at yourself; that way, you beat everyone else to the punch.