Jak: pobieranie elementów modelu UML z IDataObject
Gdy użytkownik przeciągnie elementów z jakiegokolwiek źródła na diagramie, przeciąganych elementów są zakodowane w System.Windows.Forms.IDataObject.Kodowanie, zależy od typu obiektu źródłowego.Poniższy fragment demonstruje, jak pobrać elementy, gdy źródło jest UML diagram.
[!UWAGA]
Większość operacji, które trzeba wykonać w modelach UML może być wykonywane za pomocą typów w określonych w zestawy Microsoft.VisualStudio.Uml.Interfaces i Microsoft.VisualStudio.ArchitectureTools.Extensibility.Jednak w tym celu należy użyć niektórych klas, które są częścią implementacji narzędzia do modelowania diagramów UML.Na przykład ShapeElement w tym fragmencie nie jest taka sama, jak UML IShape.Aby zmniejszyć ryzyko wprowadzanie modelu UML i diagramów w niespójnym stanie, jest lepiej unikać stosowania metod tych klas wykonania, z wyjątkiem przypadku gdy nie ma innego sposobu.
Przykładowy kod
Projekt musi odwoływać się do następujących .NET zespoły:
Microsoft.VisualStudio.Modeling.Sdk.11.0
Microsoft.VisualStudio.Modeling.Sdk.Diagrams.11.0
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;
}
});
}
Aby uzyskać więcej informacji o ElementGroupPrototype i Store , w której wykonywane są narzędzia do modelowania UML, zobacz Wizualizacja i modelowania SDK - języków.