Get UML model elements from IDataObject


The new home for Visual Studio documentation is Visual Studio 2017 Documentation on

The latest version of this topic can be found at 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.

System_CAPS_ICON_note.jpg Note

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.

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 Modeling SDK for Visual Studio - Domain-Specific Languages.

Programming with the UML API
Define a menu command on a modeling diagram