Search code examples
c#automationms-officevisio

How do I get shapes in a swimlane shape object from visio automation?


I am using office automation to convert visio files to a specified xml format flowchart, and I need use the swimlane data as container of workflow process . so how can I get the relation between workflow shapes and swimlane ?

CODE

IVisio.Shape shape = o as IVisio.Shape;

double width = shape.Cells["Width"]
        .Result[Microsoft.Office.Interop.Visio.VisUnitCodes.visMillimeters];
double height = shape.Cells["Height"]
        .Result[Microsoft.Office.Interop.Visio.VisUnitCodes.visMillimeters];
double pinX = shape.Cells["PinX"]
        .Result[Microsoft.Office.Interop.Visio.VisUnitCodes.visMillimeters];
double pinY = shape.Cells["PinY"]
        .Result[Microsoft.Office.Interop.Visio.VisUnitCodes.visMillimeters];

Solution

  • To find the container relation can use the API with this method:

    public class ShapeWrapper
    {
        public IVisio.Shape Shape { get; set; }
    
        private List<ShapeWrapper> children = new List<ShapeWrapper>();
        public List<ShapeWrapper> Children { get { return this.children; } }
    
        public ShapeWrapper(IVisio.Shape shape)
        {
            Shape = shape;
        }
    }
    
    private void FindChildren(ShapeWrapper shapeWrapper, 
                                  List<IVisio.Shape> addedShapes)
    {
        IVisio.Selection children = shapeWrapper
           .Shape.SpatialNeighbors[
                (short)IVisio.VisSpatialRelationCodes.visSpatialContain, 
                0,
                (short)IVisio.VisSpatialRelationFlags.visSpatialFrontToBack];
    
        foreach (IVisio.Shape child in children)
        {
            if (!addedShapes.Contains(child))
            {
                 //MessageBox.Show(child.Text);
                 ShapeWrapper childWrapper = new ShapeWrapper(child);
                 shapeWrapper.Children.Add(childWrapper);
    
                 FindChildren(childWrapper, addedShapes);
            }
        }
    }