How to: Open a UML Model by Using the Visual Studio API

You can also open models and diagrams in the Visual Studio user interface by using the API.

If you only want to read a model in program code without making it visible to the user, you can use the following methods:

To open a model in the user interface, use the standard Visual Studio API EnvDTE.DTE. There are two useful casts that you can perform on modeling project items:

  • EnvDTE.Project can be cast to and from IModelingProject, if the project is a modeling project, and if the project is loaded in the current AppDomain.

  • EnvDTE.ProjectItem can be cast to and from IDiagramContext, if the item is a UML diagram.

For the following example, your project should import these references:

  • EnvDTE

  • Microsoft.VisualStudio.ArchitectureTools.Extensibility

  • Microsoft.VisualStudio.Modeling.Sdk.11.0

  • Microsoft.VisualStudio.Modeling.Sdk.Diagrams.11.0

  • Microsoft.VisualStudio.Shell.Immutable.11.0

  • Microsoft.VisualStudio.Uml.Interfaces

  • System.ComponentModel.Composition

This example opens a UML model in Visual Studio:

using EnvDTE; // Visual Studio API for loading diagrams
using 
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Modeling; 
using Microsoft.VisualStudio.Modeling.ExtensionEnablement;  
   // for ICommandExtension and other handler types
using Microsoft.VisualStudio.Uml.Classes; 
   // for basic UML types
using Microsoft.VisualStudio.ArchitectureTools.Extensibility.Uml;
   // for model construction methods
using EnvDTE;
using Microsoft.VisualStudio.ArchitectureTools.Extensibility;
Microsoft.VisualStudio.ArchitectureTools.Extensibility.Presentation; 
                             // for IDiagram 
...

In a Visual Studio extension, you can make this declaration to obtain access to the host service provider:

[Import] public Microsoft.VisualStudio.Shell.SVsServiceProvider ServiceProvider {get;set;}
...

In a method, you can access a project, for example, the current project:

DTE dte = (DTE)ServiceProvider.GetService(typeof(DTE));
Project project = dte.ActiveDocument.ProjectItem.ContainingProject;
IModelingProject modelingProject = project as IModelingProject;
if (modelingProject == null) return; // not a modeling project

// Access the model's store and contents.
IModelStore store = modelingProject.Store;
foreach (IElement element in store.Root.OwnedElements) {...}

// Open all the project's diagrams.
foreach (ProjectItem item in project.ProjectItems)
{ 
     IDiagramContext modelingItem = item as IDiagramContext;
     if (modelingItem == null)
         continue; // not a model diagram
     IDiagram diagram = modelingItem.CurrentDiagram;
     if (diagram == null)
     {
        // Diagram is closed. Open it.
        item.Open().Activate();
        diagram = modelingItem.CurrentDiagram;
     }
     // Access the shapes.
     foreach (IShape<IElement> shape 
               in diagram.GetChildShapes<IElement>())
     {
       IElement displayedElement = shape.Element;
       ...
     }
   }
} 
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft