Add Custom Compartments to Element

When you display an element on a diagram in 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. 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

Topic

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.

Two examples of shape scripts that you might use are provided below.

 

Shape Editor

 

Drawing Methods

 

Query Methods

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 our compartment name to Properties.

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

       {

               SetCompartmentName("Properties");

       }

 

      //Check if our 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).

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Drawing Methods

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 then  adds the child compartments as for Example 1.

In this case, however, the script checks whether a child has either the Part stereotype or the custom stereotype MyStereotype applied to it. If there are two child elements, one a Part and the other using MyStereotype, two compartments are created, called Parts and My Stereotype.

In order to display the compartments, AppendCompartmentText must be called.

Note that SetCompartmentName and AppendCompartmentText do not accept new line characters; that is, the following call is not supported:

 

SetCompartmentName(_T("My Name \n My New Name"))

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Drawing Methods

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

Learn more