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:
Visual Studio Model Bus allows you to access models and elements within them, and provides a standard method of making links between one model and another. For more information, see How to: Integrate UML Models with Other Models and Tools.
You can open a model in read-only mode. For more information, see How to: Read a UML Model in Program Code.
Opening Models and Diagrams in Visual Studio
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.10.0
Microsoft.VisualStudio.Modeling.Sdk.Diagrams.10.0
Microsoft.VisualStudio.Shell.Immutable.10.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;
...
}
}
}