7

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 >

John3136
  • 27,345
  • 3
  • 44
  • 64
  • Could you share a simplified sample of the C# code that you are using to create the statechart and that replicates the problem? That would help track down how to avoid the problem. – saveenr Oct 04 '11 at 22:06
  • Unfortunately, I believe the "4 space" behavior is only valid for answers and not for comments. – saveenr Oct 04 '11 at 23:55

1 Answers1

1

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);
saveenr
  • 7,249
  • 3
  • 17
  • 19
  • That is basically the code I have, but the problem occurs when I drop the transition. My workaround is to just draw the chart using "normal" shapes (i.e. not an actual state chart diagram) - currently working back to trying to do a statechart so I can get the exact error message. – John3136 Oct 05 '11 at 00:35
  • Ok: very scary - my code now works. The diagram is not labeled as a statechart though. I didn't change anything "important" - one observation: previously Visio displayed all the UML templates, now it is just the statechart one - I'm guessing that is how it knows that it is a statechart. I have no idea what I changed to cause this - my only changes were to modify my methods that pick shapes from a stencil based on whether I'm trying UML or using my workaround... not ready to give up - I don't trust things I don't understand... – John3136 Oct 05 '11 at 00:56
  • If I click 'Properties' on the transitions I get This: UML shape exists on a drawing page which is not part of a UML model diagram. This shape is designed to work in drawings created using the UML Model Diagram template. I'm guessing I need to do something when I create my page. – John3136 Oct 05 '11 at 00:59
  • **very very close** The page created is now a UML model so I can "open" the transitions. Only problem is that the page claims to be a "Static Structure" diagram. – John3136 Oct 05 '11 at 02:39
  • I think the code above now handles it. It *cheats* by setting the title directly. – saveenr Oct 05 '11 at 05:36
  • That worked. Thanks! There is still a bit of weirdness, but I think that might just be Visio ;-) – John3136 Oct 05 '11 at 22:49