Add Custom Compartments to Element

When you display an element on a diagram in normal, rectangular format, it is possible to show a number of compartments within that framework to reveal various added characteristics such as Attributes, Operations, Notes and Test Scripts, using the diagram Properties and element Feature and Compartment Visibility dialogs. If you want to reveal other added characteristics, such as Ports and Parts, you can use a Shape Script to add custom compartments to the diagram display of the element. You would usually add this Shape Script to a Stereotype element in a Profile.

Access    Profile Stereotype element: ( F9 ) > General | Initial Value: browse    or
Settings | UML Types > Stereotypes (specify stereotype): Shape Script, Assign

Add custom compartments to elements

Process

Description

See also

Develop script

For the selected stereotype, open the Shape Editor.

In the script, replace shape main  with shape ChildElement.

You can keep shape main if you prefer, to adjust some properties of the main element (such as color); however, the main shape then requires a call to DrawNativeShape() in order to work correctly.

At this point, you can use the HasProperty query method to search child elements for specific properties (such as stereotypes) to be displayed in compartments.

Examples of shape scripts which define custom compartments are provided below.

 

Shape Editor

Drawing Methods

Query Methods

Display Element/Connector Properties

 

Example 1 - Without Adjusting the Parent Element

 

//Add compartments for Child elements.

shape ChildElement

{

      //Check if a child element has the property stereotype, if so set the compartment name to Properties.

       if(HasProperty("stereotype", "property"))

       {

               SetCompartmentName("Properties");

       }

 

      //Check if the child element has a public scope and add the + symbol if so to the child compartment.

       if(HasProperty("scope", "public"))

       {

               AppendCompartmentText("+");

       }

 

      //Add the child elements name to the child compartment.

       AppendCompartmentText("#NAME#");

}

 

The Shape Script checks all child elements to see if they have a stereotype of property. If this stereotype is found, the SetCompartmentName function sets a compartment called Properties.

The script then checks whether the child has a public scope and, if it does, appends the + symbol .

Finally, the AppendCompartmentText function adds the child's name to the compartment.

If a compartment has already been declared by SetCompartmentName, any additional children that fall under the same compartment are automatically added to it without having to declare a new compartment name (that is, all children with the stereotype property end up in the Properties compartment).

 

 

Example 2 - Adjust the Color of the Parent Element and Add Child Compartments

 

//Shape main affects the parent

shape main

{

       //Set the color of the parent element to red

       setfillcolor(255,0,0);

       //draw the parents native shape

       drawnativeshape();

}

 

//Shape ChildElement adds Child Compartments to the parent.

shape ChildElement

{

        if(HasProperty("stereotype", "part"))

        {

               SetCompartmentName("Parts");

         }

        else if(HasProperty("stereotype", "mystereotype"))

        {

              SetCompartmentName("My Stereotype");

         }

 

         AppendCompartmentText("#NAME#");

}

 

The shape main  section sets the color of the main element to red and adds child compartments based upon stereotyped child elements.

The script checks whether a child element has either the stereotype value "part" or "mystereotype" applied to it.  If there are multiple child elements, having a combination of "part" and "mystereotype" stereotypes, two compartments are created, called Parts and My Stereotype.

In order to display the compartments, AppendCompartmentText must be called to insert content into the compartment.

Values passed to SetCompartmentName and AppendCompartmentText can not contain  new line characters.

 

 

Example 3 - Only list  child element in compartment if not already visible on the current diagram

 

 

shape childElement

{

 //Check if the child element is on the diagram or not.

 if(hasproperty("IsVisible", "false"))

 {

         //Create a compartment for parts.

         if(hasproperty("type", "part"))

         {

                 SetCompartmentName("Parts");

         }

         //Create a compartment for ports.

         else if(hasproperty("type", "port"))

         {

                 SetCompartmentName("Ports");

         }

 

         //Add child element name to compartment.

         AppendCompartmentText("#NAME#");

 }

 

}

 

This script adds custom compartments for Port and Part elements belonging to the current element but are not visible on the current diagram.

The IsVisible property returns true if the child element is already visible on the current diagram; false if the child element is not visible.

This can be used to prevent the child element from being listed in the custom compartment if it is already visible on the diagram, avoiding display of redundant information.

 

 

Notes

If you use punctuation within a compartment name, it is stripped out when the script is saved; for example, Ports, Parts and Properties becomes Ports Parts and Properties
Visibility of each individual custom compartment defined by a shape script can be controlled using the Feature and Compartment Visibility dialog.

Learn more