The (experimental) journey of getting App Arch Guid Knowledge in VSTS2010 with Edward continuous…
- The VSTA Layer Diagram and the P^P App Arch Guide 2.0.
- 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.
- Get and Load the Model and Diagram.
- Get the Store and start a transaction.
- Create the shape.
- Create the ModelElement and associate it with the shape.
- Add the combination to the design surface [diagram].
- Add child's [nested shapes] if necessary.
- create the dependencies.
- commit the transaction.
- 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)
6: LayerModel currentLayerModel = null;
7: Store store = new Store();
8: Type modelTypes = new Type
14: using (Transaction t = store.TransactionManager.BeginTransaction("Reading diagram"))
16: currentLayerModel = LayerDesignerSerializationHelper.Instance.LoadModelAndDiagram(store, fileName, fileName + ".DIAGRAM", null, null);
19: return currentLayerModel;
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=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
2: <Reference Include="Microsoft.VisualStudio.Modeling.Sdk.Diagrams.10.0, Version=10.0.0.0, 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;
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.
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.
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..!