Author Topic: Enterprise Architect API DiagramObject Update Fails  (Read 23 times)

arunraj

  • EA Novice
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Enterprise Architect API DiagramObject Update Fails
« on: July 21, 2018, 05:26:37 pm »
I am using the EA COM API via C# and trying to create the following hierarchy model:
Code: [Select]
Root (Package)
|
|--RootDiagram (Diagram:CompositeStructure)
|
|--TopPart (Element:Part, IsComposite=true)
    |
    |--TopPartDiagram (Diagram:CompositeStructure)
    |
    |--SubPart (Element:Part)
        |
        |-Port (Element:Port)

I use the Diagram.DiagramObjects.AddNew interface to add:
  • TopPart to the RootDiagram
  • SubPart to the TopPartDiagram
  • Port to the TopPartDiagram
Unfortunately the DiagramObject.Update succeeds only for 1 and 2. When I try to add the Port element, the process crashes with exception.

Via the GUI, I can add it by right clicking the SubPart and selecting Structural Elements. Why can't the automation API do the same thing?

qwerty

  • EA Guru
  • *****
  • Posts: 9437
  • Karma: +150/-139
  • I'm no guru at all
    • View Profile
Re: Enterprise Architect API DiagramObject Update Fails
« Reply #1 on: July 21, 2018, 06:00:37 pm »
That just sounds weird. DiagamObjects are used to put existing element in a diagram. To create them you first have to create the elements.

q.

arunraj

  • EA Novice
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Re: Enterprise Architect API DiagramObject Update Fails
« Reply #2 on: Today at 04:31:11 am »
I did not describe the entire steps because I thought those were obvious. Anyway for better clarity, here is the complete code:

Code: [Select]
EA.Repository oRepository = new EA.Repository();

oRepository.OpenFile("C:\\test.eap");

EA.Package oModel = (EA.Package)oRepository.Models.AddNew("Test", "");
if(!oModel.Update()) { Console.WriteLine("Error"); }

EA.Package oRootPackage = (EA.Package)oModel.Packages.AddNew("Root", "");
if(!oRootPackage.Update()) { Console.WriteLine("Error"); }

EA.Diagram oRootDiagram = (EA.Diagram)oRootPackage.Diagrams.AddNew("RootDiagram", "CompositeStructure");
if(!oRootDiagram.Update()) { Console.WriteLine("Error"); }

EA.Element oPartElement = (EA.Element)oRootPackage.Elements.AddNew("TopPart", "Part");
// Setting IsComposite here has weird results - setting it after creating the diagram gives expected result!
// oPartElement.IsComposite = true;
if(!oPartElement.Update()) { Console.WriteLine("Error"); }

EA.DiagramObject oPartDiagramObject = (EA.DiagramObject)oRootDiagram.DiagramObjects.AddNew("", "");
oPartDiagramObject.ElementID = oPartElement.ElementID;
if(!oPartDiagramObject.Update()) { Console.WriteLine("Error"); }

EA.Diagram oPartDiagram = (EA.Diagram)oPartElement.Diagrams.AddNew("TopPartDiagram", "CompositeStructure");
if(!oPartDiagram.Update()) { Console.WriteLine("Error"); }

oPartElement.IsComposite = true;
if(!oPartElement.Update()) { Console.WriteLine("Error"); }

EA.Element oSubPartElement = (EA.Element)oPartElement.EmbeddedElements.AddNew("SubPart", "Part");
if(!oSubPartElement.Update()) { Console.WriteLine("Error"); }

EA.DiagramObject oSubPartDiagramObject = (EA.DiagramObject)oPartDiagram.DiagramObjects.AddNew("", "");
oSubPartDiagramObject.ElementID = oSubPartElement.ElementID;
if(!oSubPartDiagramObject.Update()) { Console.WriteLine("Error"); }

EA.Element oPortElement = (EA.Element)oSubPartElement.EmbeddedElements.AddNew("Port", "Port");
if(!oPortElement.Update()) { Console.WriteLine("Error"); }

EA.DiagramObject oPortDiagramObject = (EA.DiagramObject)oPartDiagram.DiagramObjects.AddNew("", "");
oPortDiagramObject.ElementID = oPortElement.ElementID;
if(!oPortDiagramObject.Update()) { Console.WriteLine("Error"); }

oRepository.CloseFile();
oRepository.Exit();
oRepository = null;

The oPortDiagramObject.Update() is what fails. Am I doing something wrong here?