Creating statecharts in Visio using c#
Asked Answered
I

1

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 >

Ihs answered 29/9, 2011 at 5:30 Comment(2)
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.Builder
Unfortunately, I believe the "4 space" behavior is only valid for answers and not for comments.Builder
B
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);
Builder answered 4/10, 2011 at 23:54 Comment(6)
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.Ihs
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...Ihs
If I click 'Properties' on the transitions I get This: <quote> 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. </quote> I'm guessing I need to do something when I create my page.Ihs
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.Ihs
I think the code above now handles it. It cheats by setting the title directly.Builder
That worked. Thanks! There is still a bit of weirdness, but I think that might just be Visio ;-)Ihs

© 2022 - 2024 — McMap. All rights reserved.