Identifying a shape parent in visio

2019-09-06 15:40发布

问题:

Hii i have a visio document which is an BPMN template .In that BPMN document i have placed a BPMN shape and also other shapes from Engineering,FLowChart,Networking,Basic shapes etc by choosing Moreshapes->

and so my BPMN visio document looks like below

in that BPMN and Task named are a BPMN shapes,Computer is a shape from Network->Computer ,Rectangle is from General->BasicShapes like that the other shape also . (ie) In a BPMN visio document , with the BPMN shape i have the other shapes also.

How to identify the shape parent in c#..???

eg: BPMN named shape is shape from BPMN. Task named shape is shape from BPMN. computer named shape is shape from Network. Rectangle named shape is shape from General.

回答1:

I must admit, this is more difficult than I would have expected. The stencils are actually in the documents collection and they do not contain shapes, only masters. Anyway: this is how you get the parent stencil name for each shape of your active drawing:

using Visio = Microsoft.Office.Interop.Visio;

var visio = (Visio.Application) System.Runtime.InteropServices.Marshal.GetActiveObject("Visio.Application");
var vsd = visio.ActiveDocument;
foreach(Visio.Shape shape in vsd.Pages[1].Shapes) {
  foreach(Visio.Document stencil in visio.Documents) {
    if (stencil.Type == Visio.VisDocumentTypes.visTypeStencil) {
      foreach(Visio.Master sh in stencil.Masters) {
        if (sh.Name == shape.Name) {
          Console.WriteLine(stencil.Name);
          break;
        }
      }
    }
  }
}


回答2:

There is no dependency between shapes and stencils; the shapes may come to the drawing from any stencil (or from other drawings for example). I.e. shapes do not store information from which stencil they "originate".

For example, you can create your own stencil and put shapes there; thus, when you drop a shape from that stencil, they will come from that stencil.

The only order is how Microsoft arranged them between stencils.. Note that the same shape may occur in more than one built-in stencil.

When you drop a shape, it's "master" is copied to the document, and all connections to the "original" stencil are broken - the shape does not "know" where it came from.

What for do you need this sort of information, anyways?