Creating statecharts in Visio using c#

我是研究僧i 提交于 2019-12-08 19:28:24

问题


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 >


回答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);


来源:https://stackoverflow.com/questions/7592860/creating-statecharts-in-visio-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!