添加形状到新Visio文档 [英] Adding Shapes to a New Visio Document

查看:216
本文介绍了添加形状到新Visio文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码,创建一个新的Visio文档,并增加了一个矩形。它的工作原理,但我不喜欢不必打开另一个文件,以从中获得大师集合。问题是新的文档有一个空大师形状的集合。我找不到在文档类中的方法为形状添加到收藏大师和所有我能找到添加形状例子假设你有一个现有的文档。有没有更好的方法做我想要什么?



  //创建新的应用程序
Visio.Application VA =新Microsoft.Office.Interop.Visio.Application();

//添加一个文件
va.Documents.Add(@);

// Visio.Documents vdocs = va.Documents;

//我们需要这个文件来获得其硕士Shapes集合
//因为我们的新文档现在没有
Visio.Document VU = vdocs.OpenEx(@C:\\ \\Program文件(x86)\Microsoft Office\Office12\1033\Basic_U.vss(短)Microsoft.Office.Interop.Visio.VisOpenSaveArgs.visOpenDocked);

//的工作文件设置为我们的新文件
Visio.Document VD = va.ActiveDocument;

//设置工作页面,活动页面
Microsoft.Office.Interop.Visio.Page VP = va.ActivePage;

//如果我们尝试这从我们的新文件
大师集合//我们得到一个运行的时间,因为我们的主人集合为空
Visio.Master VM =似曾相识。 Masters.get_ItemU(@矩形);
Visio.Shape visioRectShape = vp.Drop(VM,4.25,5.5);
visioRectShape.Text = @矩形的文字。


解决方案

您是对的 - 大师集合是只读的。文件通常以一个空大师集合出发。收集得到由从模板文件下探大师填充。



如果你想创建一个预填充大师收集一个新的文档,那么你可以创建自己的模板( .VST),然后立足于你的新文档。例如:

  Visio.Document VDOC = vDocs.Add(MyTemplateFile.vst); 



通常你会打包模具和模板在一起,然后总是从各自的下降大师创建的形状模板文件(.VSS)。



大师也有MatchByName属性。当这个属性被设置为相同的高手存在于图纸文件的主人收集真实,Visio将首先检查删除的高手。如果它是主实例将被丢弃。如果不是一个新的主将根据原来的模版加入。看看这两个环节的更多信息:





如果你真的想在代码中创建自己的主人,你可以得出/删除页面上自己的形状,然后使用Document.Drop方法将其添加到收藏大师



此外,如果你想使用一个主命名那么你就需要通过收集大师循环来检查它是否存在使用它之前。


I have this code that creates a new Visio document and adds a rectangle. It works, but I don't like having to open another document to get the Masters collection from it. The issue is the new document has an empty Masters shape collection. I couldn't find a method in the Document class to add shapes to the Masters collection and all the examples I could find for adding shapes assumed you had an existing document. Is there a better way to do what I want?

// 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.";

解决方案

You're right - the Masters collection is ReadOnly. Documents normally start off with an empty masters collection. The collection gets populated by dropping masters from a stencil document.

If you want to create a new document with a pre-populated Masters collection then you could create your own template (.vst) and then base your new document on that. For example:

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

Normally you would package your stencils and templates together and then always create shapes by dropping a master from the respective stencil document (.vss).

Masters also have a MatchByName property. Dropping a master when this property is set to true, Visio first checks that a master of the same exists in the drawing document masters collection. If it does an instance of that master will be dropped. If not a new master will be added based on the original stencil. Have a look at these two links for more information:

If you really want to create your own masters in code, you can draw / drop your own shapes on the page and then use the Document.Drop method to add it to the masters collection.

Also if you want to use a master by name then you'll need to loop through the masters collection to check that it exists before you use it.

这篇关于添加形状到新Visio文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆