Book a Demo

Author Topic: Adding a diagram via the C# AddInFramework  (Read 7045 times)

Cameron MacLeod

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Adding a diagram via the C# AddInFramework
« on: October 01, 2015, 01:07:38 am »
I am working on an add-in that will generate Thrift code from EA Interfaces and vice versa. I have been able to extract things from the interface, but the reverse operation is causing me some trouble. My add-in works via the right click menu on an interface to translate to Thrift and the right click menu on a diagram to add in an interface from a Thrift file.

I have read that to add a new element, package, diagram etc, you need to call AddNew() on the relevant collection that will contain it, but my current problem is that I cannot find the relevant collection.

I would like to add a diagramElement to model.selectedDiagram
Code: [Select]
private void GenFromThrift()
{
    // extract interface from thrift
    UML.Diagrams.DiagramElement d = model.selectedDiagram.diagramElements.AddNew("test", "Interface");
    d.Update();
    model.selectedDiagram.diagramElements.Refresh();
    // do things to interface just created
}
Where model is defined
Code: [Select]
public override void EA_FileOpen(EA.Repository Repository)
{
    // initialize the model
    this.model = new UTF_EA.Model(Repository);
}
UTF_EA and UML are
Code: [Select]
using UML = TSF.UmlToolingFramework.UML;
using UTF_EA = TSF.UmlToolingFramework.Wrappers.EA;
However selectedDiagram.diagramElements is a HashSet instead of a Collection and has no AddNew() method.

It is quite possible that I am going about this in entirely the wrong way, so any advice would be much appreciated.
« Last Edit: October 01, 2015, 01:08:22 am by notexactlyawe »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Adding a diagram via the C# AddInFramework
« Reply #1 on: October 01, 2015, 01:28:47 am »
Hi Cameron,

I'm happy to see that you are using my framework.
Currently there aren't too many "update" operations implemented in the framework. I've developed only those things I needed in the framework, and I haven't needed too much of those operations until now.

Adding elements to a diagram is one of those operations that is implemented however (it is used in the EA Navigator)
You can use
Code: [Select]
TSF.UmlToolingFramework.UML.Diagrams.Diagrams.addToDiagram(UML.Classes.Kernel.Element element) to add an element to a diagram.

Geert


Cameron MacLeod

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Adding a diagram via the C# AddInFramework
« Reply #2 on: October 01, 2015, 06:51:35 pm »
Hi Geert,

Thanks for the reply, I've now got that working with previously existing elements in separate diagrams. Is there an easy way to create new elements? Right now I'm doing something like this where the user has a diagram open and an element selected in the Project Browser
Code: [Select]
UTF_EA.Interface e = (UTF_EA.Interface) model.selectedElement;
// do things with e
model.currentDiagram.addToDiagram(e);
Cheers,
Cameron

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Adding a diagram via the C# AddInFramework
« Reply #3 on: October 01, 2015, 07:06:13 pm »
I don't think I've implemented something to add new elements yet.
But that isn't too difficult.
What you need to do is create a new operation in the UML layer to create an element.
Then use the Element.AddNew() operation int the EA wrapper to create the actual element in EA.
You can then use the resulting object in the factory method to create a new element.
If you have a working solution I'll be happy to merge your changes into the main repository.

Geert

PS. Reading my explanation I realize that it sounds more complicated then it actually is.

Cameron MacLeod

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Adding a diagram via the C# AddInFramework
« Reply #4 on: October 21, 2015, 08:37:45 pm »
Sorry for the late reply, I was called away onto other projects for a little while.

I have had a go at implementing this, but have run into a couple of problems. I created a method in the Factory class to add a DiagramElement given a Diagram object.

Code: [Select]
public UML.Diagrams.DiagramElement createDiagramElement(Diagram wrapper)
    {
      Model m = (Model)model;
      global::EA.Repository r =  m.getWrappedModel();
      global::EA.Collection c = r.GetElementSet("", 0);
      global::EA.Element baseElement = (global::EA.Element)c.AddNew("Test", "Interface");
      ElementWrapper e = new ElementWrapper(model as Model, baseElement);
      e.wrappedElement.Update();
      DiagramObjectWrapper d = new DiagramObjectWrapper(
          model as Model, (global::EA.DiagramObject)wrapper.wrappedDiagram.DiagramObjects.AddNew("l=200;r=400;t=200;b=600;", ""));
      d.wrappedDiagramObject.ElementID = e.wrappedElement.ElementID;
      if (!d.wrappedDiagramObject.Update())
      {
         Console.WriteLine(wrapper.wrappedDiagram.GetLastError());
      }
      wrapper.reFresh();
      return d;
    }
The problem here is that baseElement is null. The code above the declaration of baseElement works fine, but AddNew doesn't seem to do what is expected. Could you shed some light on what I'm doing wrong?

Cheers,
Cameron

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Adding a diagram via the C# AddInFramework
« Reply #5 on: October 21, 2015, 08:43:55 pm »
Cameron,

The problem is in your "trick" to get an EA.Collection
Code: [Select]
global::EA.Collection c = r.GetElementSet("", 0);That doesn't work like that.
You'll have to tell EA where it should add the element to. You can either add an element to Package.Elements or Element.Elements.

Geert