When you create an element such as a use case or an action, the user can see it in UML Model Explorer, but it does not always automatically appear in a diagram. In some cases, you must write code to display it. The following table summarizes the alternatives.

Type of element

For example

To display this, your code must

Classifier

Class

Component

Actor

Use Case

Create associated shapes on specified diagrams. You can create any number of shapes for each classifier.

diagram.Display<modelElementType>

(modelElement, parentShape,

xPosition , yPosition);

Set parentShape to null for a shape at the top level of the diagram.

To display one shape inside another:

IShape<IUseCase> usecaseShape =

useCaseDiagram.Display

(useCase,

subsystemShape,

subsystemShape.XPosition + 5,

subsystemShape.YPosition + 5);Note: If you perform Display inside an ILinkedUndo transaction, the method sometimes returns no IShape. But the shape is correctly created, and is accessible using IElement.Shapes().

Activate the window and scroll the diagram so that all the given shapes are visible. The shapes must all be on the diagram. If zoomToFit is true, the diagram will be scaled if necessary so that all of the shapes are visible.

To access the user's current diagram from a command or gesture extension

Declare this imported property in your class:

[Import]

IDiagramContext Context { get; set; }

In a method, access the diagram:

IClassDiagram classDiagram =

Context.CurrentDiagram as IClassDiagram;

Note

An instance of IDiagram (and its subtypes such as IClassDiagram) is valid only within the command you are processing. It is not recommended to keep an IDiagram object in a variable that persists while control is returned to the user.

The following code implements a menu command that aligns shapes neatly. The user must first place two or more shapes in approximate alignment, either vertically or horizontally. Then the align command can be used to align their centers.