Author Topic: C# Plugin: Product Line Feature Diagram in UML  (Read 18755 times)

Edoardo

  • EA User
  • **
  • Posts: 20
  • Karma: +0/-0
    • View Profile
C# Plugin: Product Line Feature Diagram in UML
« on: August 12, 2009, 01:14:51 am »
Hi all,

I'm writing a plugin in C# for EA.

The goal of this application is to create an UML class diagram in an EA project starting from a set of functional requirements.

Basically this plugin takes in input some requirements in a project in EA and their relations (requires, excludes, extends), it generates a list of objects that represent a Feature Diagram of a Product Line.

The features of the product line are then organized in classes and groups which have properties, gerarchy and so on.

The algorithms for creating the objects are just ready.

I would like to learn how can I use the API to create a C# code that make me create and manipulate UML classes, stereotypes and constraints.

Thank you in advance.

Edoardo.

Edoardo

  • EA User
  • **
  • Posts: 20
  • Karma: +0/-0
    • View Profile
Re: C# Plugin: Product Line Feature Diagram in UML
« Reply #1 on: August 13, 2009, 07:27:45 pm »
Ok, maybe I made it more complicated that it is.

All I need to do now is to create a UML class diagram from scratch using C# code in my plugin.

Could someone give me a starting point?

Thanks.

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: C# Plugin: Product Line Feature Diagram in UML
« Reply #2 on: August 13, 2009, 09:39:24 pm »
If all you need to do is designate a newly created diagram as a Class Diagram, that should be easy.

Create the diagram using the AddNew method of the parent package (or element). Set the Type parameter to "Logical" to specify a Class Diagram.

Or do you mean something else?
No, you can't have it!

Edoardo

  • EA User
  • **
  • Posts: 20
  • Karma: +0/-0
    • View Profile
Re: C# Plugin: Product Line Feature Diagram in UML
« Reply #3 on: August 13, 2009, 09:59:17 pm »
Yes, but I need to do it using the SDK. I need to write in C# some code that does the job and embed it into the plugin I'm developing.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13226
  • Karma: +550/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: C# Plugin: Product Line Feature Diagram in UML
« Reply #4 on: August 14, 2009, 04:30:27 pm »
It's really not that hard to create a class using the API.
Here are the steps you should take to create a class in a package.
- get a reference to the  repository: If you are using an add-in you get the Repository in the "EA_MenuClick" operation.
- select the package you want:
Code: [Select]
EA.Package myPackage = Repository.Repository.GetPackageByID(123)- add a class to the package
Code: [Select]
EA.Element myNewClass = (EA.Element)myPackage.Elements.AddNew("ClassName","Class")- save the class
Code: [Select]
myNewClass.Update();
Hope this helps

Geert
« Last Edit: August 14, 2009, 04:35:55 pm by Geert.Bellekens »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13226
  • Karma: +550/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: C# Plugin: Product Line Feature Diagram in UML
« Reply #5 on: August 14, 2009, 04:35:30 pm »
Hmm, should have read you question more carefully. (I thought you needed to create a class, not a class diagram)
Anyway the same principle is used but not you are not adding an Element in the Elements collection, but a diagram in the Diagrams collection:
Code: [Select]
EA.Diagram myDiagram = (EA.Diagram)myPackage.Diagrams.AddNew("myDiagramName","Logical")
myDiagram.Update()

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: C# Plugin: Product Line Feature Diagram in UML
« Reply #6 on: August 14, 2009, 09:32:05 pm »
Geert's code is right on the mark. [No surprise there!]

One thing to pay close attention to is the cast to EA.Diagram - Geert's example does this. The AddNew method of any EA collection returns a result of object type. You must cast this to the appropriate type before you use it in your code. [Unless you are using a weakly typed language of course.]
No, you can't have it!

Edoardo

  • EA User
  • **
  • Posts: 20
  • Karma: +0/-0
    • View Profile
Re: C# Plugin: Product Line Feature Diagram in UML
« Reply #7 on: August 15, 2009, 12:22:44 am »
Thanks a lot!

now I only need to learn how to populate the diagram just created, inserting classes and relationships among them.

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: C# Plugin: Product Line Feature Diagram in UML
« Reply #8 on: August 16, 2009, 03:29:27 am »
You are most of the way there Edoardo,

Look up the DiagramObject and DiagramLink classes in the SDK. Remember that these are representations of the elements in your model, not the elements themselves. Thus you can have many DiagramObject instances (on several diagrams) for a given element. Use the ElementID (or ConnectorID) property of the DiagramObject (or DiagramLink) to specify what it represents.

Once you get started you'll figure out the rest in no time.

David
No, you can't have it!

Edoardo

  • EA User
  • **
  • Posts: 20
  • Karma: +0/-0
    • View Profile
Re: C# Plugin: Product Line Feature Diagram in UML
« Reply #9 on: August 17, 2009, 01:25:10 am »
Ok, I almost got it, but an example would really really be helpful.

Suppose I need to represent this situation:
- there are three classes: A, B, C
- B and C extends A

By now I only know how to create the diagram, the classes and how to insert them into the diagram.

But what about the links between them and their constraints?

More important: what's the code to set multiplicity (like 0...1 or 0...*) to the UML associations?

Would you be so kind to show me some code?

Thanks a lot!
« Last Edit: August 17, 2009, 05:26:26 am by e.varasi »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13226
  • Karma: +550/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: C# Plugin: Product Line Feature Diagram in UML
« Reply #10 on: August 17, 2009, 06:01:29 pm »
To get the relations between elements you need the
"Element.Connectors". These contain all kinds of relation between different elements.
Each of those Connectors has a Type field which indicates whether it is a "Generalization", "Association", etc..
To find all Connectors between two given Elements you loop all the connectors of an element and compare the "SupplierID" and "ClientID" with the Element.ElementID.
Once you have found a Connector which you wish to show on your diagram you add a DiagramLink to the Diagram.DiagramLinks collection.
Set the DiagramLink.ConnectorID to the Connector.ConnectorID.

The multiplicity of association can be set in the ConnectorEnd of the Connector.
The field ConnectorEnd.Cardinality contains this information in a string format.

Hope this helps

Geert

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: C# Plugin: Product Line Feature Diagram in UML
« Reply #11 on: August 17, 2009, 09:42:38 pm »
And one more thing.

Sometimes you will need to create a connector - this is not the same thing as adding a DiagramLink representing a connector that already exists; but once you get a diagram going missing connectors can quickly become evident. When you do so, remember to first add the connector to the Connectors collection of one element; this sets the ClientID of the new connector to the ElementID of the element that owns the collection. Next set the SupplierID of the connector to the ElementID of the element at the other end of the connector (which might be the same as the client end, making the connector a self reference). Then save the connector to the EA schema by calling the connector's Update method. This is one case where you must set an additional property before calling Update the first time.

The above does not apply when adding a new DiagramLink to a diagram. Remember that this merely draws a representation of a connector that must already exist in the model. [Even if you just added the connector via the above method it has to be created before you reference it on a diagram.] When adding a DiagramLink you create the link and set the ConnectorID attribute, then call Update to save the new link. Since the connector itself must already have a valid client and supplier the DiagramLink provides EA with (a pathway to) all the information needed to draw the link.

HTH, David
No, you can't have it!

Edoardo

  • EA User
  • **
  • Posts: 20
  • Karma: +0/-0
    • View Profile
Re: C# Plugin: Product Line Feature Diagram in UML
« Reply #12 on: August 18, 2009, 06:34:52 pm »
Thanks a lot to everybody for the help!

Edoardo

  • EA User
  • **
  • Posts: 20
  • Karma: +0/-0
    • View Profile
Re: C# Plugin: Product Line Feature Diagram in UML
« Reply #13 on: September 22, 2009, 08:40:52 pm »
Once I've created the diagram, I would like to order it's element. In EA there is the command Diagram->Layout Diagram.

Can I do that from the API automatically?

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13226
  • Karma: +550/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: C# Plugin: Product Line Feature Diagram in UML
« Reply #14 on: September 22, 2009, 08:47:47 pm »
I think its located in the project interface.

Geert