Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating statecharts in Visio using c#

Can anyone point me to an example of how to programatically create a statechart in visio? I can create blank pages, drop shapes, open template etc, but when I try to add transitions it complains that the page is not the right type.

Can't find a sample anywhere.

Alternatively: I can save the user actions to create the chart as a macro. Can I run that programatically?

Thanks.

< edit >
Step away from the PC for 2 minutes and you realise you should have put the code snippet in the question and not try to put it in comments. Forest: meet trees...

Visio.Document umlStencil = visioApp.Documents.OpenEx(@"UMLSTA_M.vss", (short)VisOpenSaveArgs.visOpenDocked);  
Visio.Page page = visioDoc.Pages.Add();  
Visio.Shape s1 = page.Drop(umlStencil[@"State"], 5.0, 5.0);  
Visio.Shape s2 = page.Drop(umlStencil[@"State"], 5.0, 5.0);  
Visio.Shape transition = page.Drop(umlStencil[@"Transition"], 1.0, 1.0);  

As you can see, pretty similar to the snippet in the answer below.
< / edit >

like image 770
John3136 Avatar asked Sep 29 '11 05:09

John3136


1 Answers

This is the code that I ran with Visual Studio 2010 against both Visio 2007 and Visio 2010.

var visioApp = new Visio.Application();

// Load the UML Statechart stencil (docked)
var stencil_open_flags = Visio.VisOpenSaveArgs.visOpenDocked;
var umlStencil = visioApp.Documents.OpenEx(@"UMLSTA_M.vss", (short)stencil_open_flags);

// create a new empty doc based on the UML Model Template
var doc = visioApp.Documents.AddEx("UMLMOD_U.VST", Visio.VisMeasurementSystem.visMSUS, 0, 0); 
var page = doc.Pages.Add();

// Find and manually change the diagram's title 
var watermark = page.Shapes["Watermark Title"];
var LockTextEdit_cell = watermark.CellsU["LockTextEdit"];
LockTextEdit_cell.FormulaForceU = "GUARD(0)";
watermark.Text = "MyTitle";
LockTextEdit_cell.FormulaForceU = "GUARD(1)";

// Find the masters we need
var state_master = umlStencil.Masters["State"];
var transition_master = umlStencil.Masters["Transition"];

// Drop the masters into the page
var s1 = page.Drop(state_master, 5.0, 5.0);
var s2 = page.Drop(state_master, 1.0, 1.0);
var transition = page.Drop(transition_master, 3.0, 3.0);
like image 172
saveenr Avatar answered Sep 25 '22 02:09

saveenr