Pages

Thursday, June 13, 2013

Detecting that shape is being copied in the visio control

Let`s say you are developing c# application that is using visio control for drawings.
Easy, scalable with the only major drawback that users will have to install MS Visio on their PCs.

Today I had to determine if the new shape is being copied rather then created from stencil. For example, you may want to copy some properties to shape from the original and if this shape is new it needs to be initialized somehow (those properties can`t be setted in the stencil). You can do it in the ShapeAdded event of the page object. But how to check if the shape is a copy? The answer is simple - check the Application.IsInScope[] property. Here is a little snippet:

private void page_ShapeAdded(Shape Shape)
{
    //it might be simple copy by Copy-Paste
    //or a drag-copied shape
    bool shapeIsACopy = VisioApplication.IsInScope[(int)VisUICmds.visCmdUFEditPaste] || 
              VisioApplication.IsInScope[(int)VisUICmds.visCmdEditPasteSpecial] || 
              VisioApplication.IsInScope[(int)VisUICmds.visCmdDragDuplicate]; 

    if (Shape.Master.NameU == MyShapes.TargetedShapeMaster && !shapeIsACopy)
        SetDefaultProperties(Shape);
}