Book a Demo

Author Topic: How could I acquire class diagram object ?  (Read 6684 times)

December

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
How could I acquire class diagram object ?
« on: March 21, 2008, 07:03:05 pm »
Hello Everybody:
  I'm making EA addin project now by c# language.  I need acquire current selected class diagram object,then generate special codes. when I right click one of class diagram, I need acquire  current selected class diagram,it's only public property object.
Example :
EA.Collection collection = myrepository.GetCurrentDiagram().SelectedObjects;
How could I find public property in collection?

Martin Terreni

  • EA User
  • **
  • Posts: 672
  • Karma: +0/-0
  • Sorry, I can't write
    • View Profile
Re: How could I acquire class diagram object ?
« Reply #1 on: March 22, 2008, 08:10:07 pm »
I haven't really checked this, but maybe you should do some combination, like getting selected diagram and selected object separately from the repository.
Recursion definition:
If you don’t understand the definition read "Recursion definition".

Eric Johannsen

  • EA User
  • **
  • Posts: 43
  • Karma: +0/-0
  • Model Driven Business[ch0174]
    • View Profile
Re: How could I acquire class diagram object ?
« Reply #2 on: March 23, 2008, 04:25:34 pm »
I think this code snippet should work for you:

Code: [Select]
       static public EA.Element SelectedElement(EA.Repository rep)
        {
            object ret = null;

            EA.ObjectType ot = rep.GetContextItem(out ret);

            if (ot == EA.ObjectType.otElement)
            {
                return (EA.Element)ret;
            }
            else
            {
                return null;
            }
        }


December

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: How could I acquire class diagram object ?
« Reply #3 on: March 24, 2008, 12:20:42 pm »
Thanks! This method is help me find selected diagram,but How could I find ONLY public property? if  stereotyp == property &&  scope==public  then  get public property's name, type, note.
How to change to c# code?

Eric Johannsen

  • EA User
  • **
  • Posts: 43
  • Karma: +0/-0
  • Model Driven Business[ch0174]
    • View Profile
Re: How could I acquire class diagram object ?
« Reply #4 on: March 25, 2008, 02:39:03 pm »
I guess I'm not sure what you mean by finding a "property" on a diagram.  As far as I know, properties cannot be selected for processing by an add-in, just the thing wrapped in a DiagramObject (e.g. the class).

If you want to return the class, if any, that meets certain criteria you could expand the code snippet like this:

Code: [Select]
static public EA.Element SelectedElement(EA.Repository rep)
       {
           object ret = null;

           EA.ObjectType ot = rep.GetContextItem(out ret);

           if (ot == EA.ObjectType.otElement)
           {
               EA.Element elem = (EA.Element)ret;
               if (elem.Stereotype == "SomeStereotype" && elem.Visibility == "Public")
               {
                    return elem;
               }
               else
               {
                    return null;
               }
           }
           else
           {
               return null;
           }
       }


I would probably restructure the code a bit, but I offer it in this form so it's easy to see the differences to my last post.

If you explain a bit more precisely what you mean by selecting a property I can try and give you a better answer.

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: How could I acquire class diagram object ?
« Reply #5 on: March 26, 2008, 12:01:52 am »
December,

Are you talking about retrieving public attributes of an element that you see pictured on a diagram?

If so, you might be stumbling over the distinction between a DiagramObject (= the picture of a thing on a diagram) and its underlying element (the thing being pictured).

The attributes (and operations) belong to the element; the DiagramObject is merely an 'image' that shows them to the reader.

So what you need to do is retrieve a reference to the element itself, via the DiagramObject.ElementID attribute (which you can feed into Repository.GetElementByID). Now you can iterate through the element's Attributes collection, and for each Attribute object you can check the Visibility property. [Since the Attribute was retrieved from the Attributes collection you won't need to check its stereotype to determine if it is an Attribute. You will have to type cast it though, since EA collections return an Object. If you use a C# foreach statement the type cast should be automatic.]

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

Eric Johannsen

  • EA User
  • **
  • Posts: 43
  • Karma: +0/-0
  • Model Driven Business[ch0174]
    • View Profile
Re: How could I acquire class diagram object ?
« Reply #6 on: March 26, 2008, 05:08:12 am »
You're a good communicator Midnight.  I see you on here a lot, do you work for Sparx?

Here's a code example of what Midnight outlined:

Code: [Select]
List<EA.Method> publicPropertyList = new List<EA.Method>();
object ret = null;
EA.ObjectType ot = rep.GetContextItem(out ret);

if (ot == EA.ObjectType.otElement)
{
    EA.Element elem = (EA.Element)ret;

    foreach (EA.Method method in elem.Methods)
    {
        if (method.Stereotype == "property" && method.Visibility == "Public")
        {
            publicPropertyList.Add(method);
        }
    }
}


«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: How could I acquire class diagram object ?
« Reply #7 on: March 26, 2008, 10:02:23 am »
No Eric,

I'm on your side of the pond - although the other side of them ridges to your East, and far to the north. I'm just another ordinary user...

David
No, you can't have it!

December

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: How could I acquire class diagram object ?
« Reply #8 on: March 26, 2008, 08:26:15 pm »
Thanks ericjohannsen, Midnight ,I had resolved it.