Getting App Arch Guid Knowledge in VSTS2010 – Part3 Create Diagrams from Code

The (experimental) journey of getting App Arch Guid Knowledge in VSTS2010 with Edward continuous… 

previous steps…

  1. The VSTA Layer Diagram and the P^P App Arch Guide 2.0.
  2. Blueprints: Visual Studio 2010 CTP

In this step as promised in part 1 ‘Create Diagrams from Code’.

I used VSTemplate and the IWizard interface for the first solution, but now we have the Blueprints available in VSTS2010 we better can use that functionality to create the diagrams. For this post it actually doesn’t matter what you use as long as you can access the Visual Studio automation object model [DTE].

The steps you have to take.

  1. Get and Load the Model and Diagram.
  2. Get the Store and start a transaction.
  3. Create the shape.
  4. Create the ModelElement and associate it with the shape.
  5. Add the combination to the design surface [diagram].
  6. Add child's [nested shapes] if necessary.
  7. create the dependencies.
  8. commit the transaction.
  9. save the model and diagram.

In more detail.

Step 1. Get and Load the Model and Diagram.

   1:  LayerModel layerModel = LoadModel(dte.ActiveDocument.FullName);
   4:          private LayerModel LoadModel(string fileName)
   5:          {
   6:              LayerModel currentLayerModel = null;
   7:              Store store = new Store();
   8:              Type[] modelTypes = new Type[] 
   9:              {
  10:                  typeof(CoreDesignSurfaceDomainModel),
  11:                  typeof(LayerDesignerDomainModel)
  12:              };
  13:              store.LoadDomainModels(modelTypes);
  14:              using (Transaction t = store.TransactionManager.BeginTransaction("Reading diagram"))
  15:              {
  16:                  currentLayerModel = LayerDesignerSerializationHelper.Instance.LoadModelAndDiagram(store, fileName, fileName + ".DIAGRAM", null, null);
  17:                  t.Commit();
  18:              }
  19:              return currentLayerModel;
  20:          }

line number 1 is the calling method to the loadmodel function and as you can see i open / load the current in visual studio activated document, you can grab any diagram file. the first thing to look for [using reflector] are the modeltypes when loading a different kind of model. because i not only want add modelelements but also want to control the place of the shape on the design surface i have to loadmodelanddiagram. the .diagram file has knowledge of the position and the .layer file of the modelelements.

Before I forget it the usings are a bit tricky in this ctp. you have to add a reference to “microsoft.visualstudio.modeling.sdk.10.0” and “microsoft.visualstudio.modeling.sdk.diagrams.10.0” [and some more]. the problem is that these aren’t visible in the add reference dialogbox. so, you have to add these manually by adding them in the csproj file.

   1:  <Reference Include="Microsoft.VisualStudio.Modeling.Sdk.10.0, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
   2:  <Reference Include="Microsoft.VisualStudio.Modeling.Sdk.Diagrams.10.0, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />

the other references […teamarchitect.layerdesigner and …teamarchitect.layerdesigner.dslpackage] you need can be found in ..\..\program files\microsoft visual studio 10.0\common7\ide\privateassemblies\

Step 2. get the store and start a transaction.

   1:  Store store = layerModel.Store;
   2:  using (Transaction t = store.TransactionManager.BeginTransaction("Draw diagram"))

and now we can start to create shapes and place them on the designsurface.

Step 3 and 4. Create the shape, modelelement and combine them.

   1:  LayerShape layerShape = new LayerShape(store, null);
   2:  Layer layer = (Layer)store.ElementFactory.CreateElement(Layer.DomainClassId);
   3:  layer.Name = name;
   4:  layerShape.Associate(layer);

set some visual properties of the shape, you can completely tweak the looks of the shape.

   1:  layerShape.AbsoluteBounds = new RectangleD(0.5, 0.5, 7, 2.375);
   2:  layerShape.FillColor = Color.DarkOrange;

repeat this for all the layers you want to create… and after that

Step 5 and 6. Add them to the Model and Diagram and – or create nestedchilds.

   1:  diagram.NestedChildShapes.Add(layerShape);
   2:  layerShape.NestedChildShapes.Add(uiProcessComponentsShape);
   3:  LayerHasChildLayers child = new LayerHasChildLayers(PresentationPair.LayerPart, uiComponentsPair.LayerPart);

You have to tell the Model and the Diagram that they are nested… if you tell it only to the Diagram [visual layout] they look like if they are nested but in the Layer Diagram Explorer you can see they aren’t.

Step 7. Create the dependencies.

   1:  DependencyFromLayerToLayer dep = new DependencyFromLayerToLayer(PresentationLayer, CrossCuttingLayer);

Step 8 and 9. Commit transaction and Save the Model and Diagram.

   1:       t.Commit();
   2:  }
   3:  SerializationResult result = new SerializationResult();
   4:  LayerDesignerSerializationHelper.Instance.SaveModelAndDiagram(result, layerModel, dte.ActiveDocument.FullName, diagram, dte.ActiveDocument.FullName + ".DIAGRAM");


There is one big drawback with this solution. Because I load the model and diagram from a file, change it and save it back to the two files, Visual Studio recognizes that they are changed outside the IDE and popup a message [two times] if you want to reload it. Not something you want. A solution for this could be the Backplane. Edward did some early investigations around the backplane last year and the layer diagram is connected to the Backplane [not all VSTA diagrams are]. So, that would solve the reload problem. But, that’s something for another post… first Merry Christmas and a Happy New Year everyone, till next year..!

Comments (1) -

layer diagram is connected to the Back plane right  

Add comment