Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: StefanBrandmeier on November 21, 2023, 04:02:51 am

Title: How to best find the superclass of a given class in script
Post by: StefanBrandmeier on November 21, 2023, 04:02:51 am
Hi community,

I am looking to programmatically find the superclass of a given class in my diagram, trying to do this with the relationships of the class:

         //   find the superclass      
         while ( !connectorsEnumerator.atEnd() )
         {         
            connectorToCheck = connectorsEnumerator.item();
            if(connectorToCheck.Type = "Generalization")
            {
               parentElement = Repository.GetElementByID(connectorToCheck.SupplierID);
               break;
            }
            connectorsEnumerator.moveNext();
         }

Neither ClientID nor SupplierID of the connector seems to point to the superclass. What am I missing ?
Or is there a better way to find the superclass of a related element of a diagram element ?

Thanks for your thoughts on this.

Cheers
Stefan      
Title: Re: How to best find the superclass of a given class in script
Post by: Geert Bellekens on November 21, 2023, 04:46:21 am
There is a property Element.BaseClasses you can use.

Why are you using the connectorsEnumerator?

Can't you simply loop over the element.Connectors with something like a foreach?

Your logic should be fine otherwise.

Geert
Title: Re: How to best find the superclass of a given class in script
Post by: StefanBrandmeier on November 21, 2023, 07:41:19 pm
Hi Geert,

thanks for you quick and competent reply. I will try Element.BaseClasses which will be much simpler of course.
As to the iterator, I guess my JScript skill is pretty basic and I don't yet know all its possibilities. Thanks for the hint.

Cheers
Stefan
Title: Re: How to best find the superclass of a given class in script
Post by: StefanBrandmeier on November 21, 2023, 09:12:31 pm
Here's some working code, based on Geerts suggestion, if someone is interested:

         var baseClasses as EA.Collection;
                  
         //   find the superclass, don't consider if multiple superclasses exist.
         baseClasses = relatedElement.BaseClasses;
         if (baseClasses.Count == 1)
         {
            parentElement = baseClasses.GetAt(0);
         }