将形状添加到新Visio文档

本文关键字:Visio 文档 添加 | 更新日期: 2023-09-27 18:25:54

我有这样的代码,它可以创建一个新的Visio文档并添加一个矩形。它很有效,但我不喜欢打开另一个文档来从中获取Masters集合。问题是新文档有一个空的Masters形状集合。我在Document类中找不到将形状添加到Masters集合的方法,我能找到的所有添加形状的示例都假设您有一个现有的文档。有更好的方法做我想做的事吗?

// create the new application
Visio.Application va = new Microsoft.Office.Interop.Visio.Application();
        // add a document
        va.Documents.Add(@"");
       // Visio.Documents vdocs = va.Documents;
        // we need this document to get its Masters shapes collection
        // since our new document has none 
        Visio.Document vu = vdocs.OpenEx(@"C:'Program Files (x86)'Microsoft       Office'Office12'1033'Basic_U.vss", (short)Microsoft.Office.Interop.Visio.VisOpenSaveArgs.visOpenDocked);
        // set the working  document to our new document
        Visio.Document vd = va.ActiveDocument;
        // set the working page to the active page
        Microsoft.Office.Interop.Visio.Page vp = va.ActivePage;
      // if we try this from the Masters collection from our new document
      // we get a run time since our masters collection is empty
     Visio.Master vm  = vu.Masters.get_ItemU(@"Rectangle");
    Visio.Shape visioRectShape = vp.Drop(vm, 4.25, 5.5);
        visioRectShape.Text = @"Rectangle text.";

将形状添加到新Visio文档

你说得对-Masters集合是ReadOnly。文档通常以一个空的master集合开始。通过从模具文档中删除主控形状来填充集合。

如果你想用预先填充的Masters集合创建一个新文档,那么你可以创建自己的模板(.vst),然后以此为基础创建新文档。例如:

Visio.Document vDoc = vDocs.Add("MyTemplateFile.vst");

通常情况下,您会将模板和模板打包在一起,然后总是通过从相应的模板文档(.vss)中删除母版来创建形状。

Masters还具有MatchByName属性。当此属性设置为true时,Visio将删除母版,首先检查绘图文档母版集合中是否存在相同的母版。如果它这样做了,那么该master的一个实例将被删除。否则,将在原始模具的基础上添加新的母版。查看这两个链接以获取更多信息:

  • http://msdn.microsoft.com/en-us/library/aa201768%28office.10%29.aspx
  • http://msdn.microsoft.com/en-us/library/ff766298.aspx

如果您真的想在代码中创建自己的母版,可以在页面上绘制/放置自己的形状,然后使用Document.drop方法将其添加到母版集合中。

此外,如果你想按名称使用master,那么在使用它之前,你需要循环遍历master集合以检查它是否存在。

我想你会发现这本在线书籍非常有用:http://msdn.microsoft.com/en-us/library/aa245244(v=office.10).aspx