Share via


방법: IDataObject에서 UML 모델 요소 가져오기

사용자가 소스의 요소를 다이어그램으로 끌어 오면 이 요소는 System.Windows.Forms.IDataObject에 인코딩됩니다. 인코딩은 소스 개체의 형식에 따라 달라집니다. 다음 코드 조각에서는 소스가 UML 다이어그램일 때 요소를 가져오는 방법을 보여 줍니다.

참고

UML 모델에서 해야 할 작업의 대부분은 Microsoft.VisualStudio.Uml.InterfacesMicrosoft.VisualStudio.ArchitectureTools.Extensibility 어셈블리에 정의된 형식을 사용하여 수행할 수 있습니다.이 경우를 제외하면 UML 모델링 도구의 구현에 포함된 일부 클래스를 사용해야 합니다.예를 들어 이 조각의 ShapeElement는 UML IShape와 동일하지 않습니다.UML 모델과 다이어그램이 일관되지 않은 상태가 되는 위험을 줄이려면 다른 방법이 없는 경우를 제외하고는 이러한 구현 클래스의 메서드를 사용하지 않는 것이 좋습니다.

코드 샘플

프로젝트에서는 다음 .NET 어셈블리를 참조해야 합니다.

Microsoft.VisualStudio.Modeling.Sdk.12.0

Microsoft.VisualStudio.Modeling.Sdk.Diagrams.12.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;
          }
        });
    }

UML 모델링 도구가 구현되는 Store와 ElementGroupPrototype에 대한 자세한 내용은 Visual Studio용 모델링 SDK - 도메인별 언어를 참조하십시오.

참고 항목

개념

UML API를 사용한 프로그래밍

방법: 모델링 다이어그램의 메뉴 명령 정의