views:

683

answers:

4

In .NET is it possible to generate class diagrams (.cd) programmatically (C#)? If so how?

PD: Obviously I'm not asking for directions of how to generate this using the IDE. I know that I can drag and drop the classes to a ClassDiagram item.

A: 

Select the .cs files you want a class diagram for, and select View Class diagram, and a .cd file will be generated and displayed.

Timothy Carter
That's not from code.
Raúl Roa
yeah it is... the code is the input, so, from code... ;-P
Shog9
I think I understand what you mean now, but that answer I don't know.
Timothy Carter
@Shog9 I love wise guys like you :)
Raúl Roa
A: 

Yes, you simply open Visual Studio and right click on either your .cs file or your Project and select View Class Diagram. The .cd is automatically created.

Alexander Kahoun
+3  A: 

I just opened a .cd file using notepad, it's plain XML... I shouldn't be that hard to generate it programmatically.

ybo
A: 

I apologize for the half answer, but to see if this actually possible will take quite a bit of time.

If you look at the XML format, it's fairly cryptic, particularly the Hash entry, which if you delete, then the IDE will delete the entry(I tried that much).

To actually automate it you would need to use the VS automation APIs, EnvDTE. The APIs are pretty cryptic, and also generalized. I couldn't find a specific class or anything dealing with Class Diagrams in look through the documentation.

If it's important enough for you to spend a couple hours on, you would start by creating one manually so that you can inspect it with the automation API. With the EnvDTE COM object(either CoCreated or supplied in a VS macro) use DTE.Solution.Open to open your solution. Then use DTE.Solution.Projects to find your project with the Class Diagram, and Project.ProjectItems to find the class Diagram files.

From there, I would first look to see if the Kind property on the ProjectItem indicated something special, and then fiddle with other properties and reflect over the Object property of the ProjectItem to see if you get something special.

If you just need to generate it once, then take a look at the DTE.Commands property to get a list of available commands. The command invoked by right clicking on the menu should be in there somewhere. Then you could use DTE.Windows.Items(Constants.vsWindowKindSolutionExplorer).Object as UIHeirarchy to get the solution explorer window and fiddle with UIHeirarchy and UIHeirarchyItem to select the files and then invoke the command.

Sorry I can't provide a full example, but since I don't have the tree walking code handy, it would take a couple hours.

Darren Clark