How to: Get UML Model Elements from IDataObject

When the user drags elements from any source onto a diagram, the dragged elements are encoded in a System.Windows.Forms.IDataObject. The encoding depends on the type of source object. The following fragment demonstrates how to retrieve the elements when the source is a UML diagram.


Most of the operations that you have to do on UML models can be performed by using the types in defined in the assemblies Microsoft.VisualStudio.Uml.Interfaces and Microsoft.VisualStudio.ArchitectureTools.Extensibility. But for this purpose, you have to use some classes that are part of the implementation of the UML modeling tools. For example, ShapeElement in this fragment is not the same as the UML IShape. To reduce the risk of putting the UML model and diagrams into an inconsistent state, it is better to avoid using the methods on these implementation classes, except where there is no alternative.

Code Sample

Your project must reference the following .NET assemblies:




using Microsoft.VisualStudio.Modeling;  
  // for ElementGroupPrototype
using Microsoft.VisualStudio.Modeling.Diagrams;  
  // for ShapeElement, DiagramDragEventArgs, DiagramPointEventArgs
  /// <summary>
  /// Retrieves UML IElements from drag arguments.
  /// Works for drags from UML diagrams.
  /// </summary>
  private IEnumerable<IElement> GetModelElementsFromDragEvent
                  (DiagramDragEventArgs dragEvent)
     //ElementGroupPrototype is the container for
     //dragged and copied elements and toolbox items.
     ElementGroupPrototype prototype =
                     as ElementGroupPrototype;
     // Locate the originals in the implementation store.
     IElementDirectory implementationDirectory = 
     return  prototype.ProtoElements.Select(
       prototypeElement => 
          ModelElement element = implementationDirectory
          ShapeElement shapeElement = element as ShapeElement;
          if (shapeElement != null)
            // Dragged from a diagram.
            return shapeElement.ModelElement as IElement;
            // Dragged from UML Model Explorer.
            return element as IElement;

For more information about ElementGroupPrototype and the Store in which the UML modeling tools are implemented, see Visualization and Modeling SDK - Domain-Specific Languages.

See Also


Programming with the UML API

How to: Define a Menu Command on a Modeling Diagram