Get UML model elements from IDataObject
The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.
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.
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, |
Your project must reference the following .NET assemblies:
Microsoft.VisualStudio.Modeling.Sdk.[version]
Microsoft.VisualStudio.Modeling.Sdk.Diagrams.[version]
System.Windows.Forms
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 =
dragEvent.Data.
GetData(typeof(ElementGroupPrototype))
as ElementGroupPrototype;
// Locate the originals in the implementation store.
IElementDirectory implementationDirectory =
dragEvent.DiagramClientView.Diagram.Store.ElementDirectory;
return prototype.ProtoElements.Select(
prototypeElement =>
{
ModelElement element = implementationDirectory
.FindElement(prototypeElement.ElementId);
ShapeElement shapeElement = element as ShapeElement;
if (shapeElement != null)
{
// Dragged from a diagram.
return shapeElement.ModelElement as IElement;
}
else
{
// 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