Pages

Wednesday, January 16, 2013

How to disable glue in Visio

Recently I was implementing text labels for shapes inside visio control. This task was not very hard - I had to add new shape for labels into our stencil, add action for specific shapes and drop labels with that action.
Here is the method to add action for shape:

class VisioUtility
{
    public static string GetCellValue(Shape shape, VisSectionIndices section, VisRowIndices row, VisCellIndices column)
    {
        Cell cell = GetCell(shape, section, row, column);
        return cell.Formula;
    }

    public static Cell GetCell(Shape shape,VisSectionIndices section,VisRowIndices row, VisCellIndices column)
 {
  return shape.get_CellsSRC((short)section,(short)row,(short)column);
  }

    public static void AddSectionRow(Shape shape, short sectionIndex, VisRowIndices rowIndex, VisRowTags rowTag)
    {
        shape.AddRow(sectionIndex, (short)rowIndex, (short)rowTag);
    }

    public static void SetCellValue(Shape shape, VisSectionIndices section, VisRowIndices row, VisCellIndices column, string value)
    {
        Cell cell = GetCell(shape, section, row, column);
        if (cell.Formula != value)
        {   //in case of UNDO...
            try
            {
                cell.FormulaU = value;
            }
            catch (System.Runtime.InteropServices.COMException ex) //7580 - workaround
            {
                //handle the exception
            }
        }
    }
}

class VisioForm
{
    public static void AddAction(Shape element, string actionMarker, string title, bool isSubMenu, bool beginGroup)
    {
        //Check if this menu item is already exists
        short newItemNumber = 0;
        while (element.get_CellsSRCExists((short)VisSectionIndices.visSectionAction, (short)(VisRowIndices.visRowAction + newItemNumber), (short)VisCellIndices.visActionAction, 0) != 0)
        {
            string currentTitle = VisioUtility.GetCellValue(element, VisSectionIndices.visSectionAction, VisRowIndices.visRowAction + newItemNumber, VisCellIndices.visActionMenu);

            if (VisioUtility.StripQuotes(currentTitle) == title)
                return;

            newItemNumber++;
        }

        //add new item as last one
        newItemNumber = element.get_RowCount((short)VisSectionIndices.visSectionAction);

        VisioUtility.AddSectionRow(element, (short)VisSectionIndices.visSectionAction, VisRowIndices.visRowAction + newItemNumber, (short)VisRowTags.visTagDefault);
        Cell cell = element.get_CellsSRC((short)VisSectionIndices.visSectionAction, (short)(VisRowIndices.visRowAction + newItemNumber), (short)VisCellIndices.visActionAction);
        //set action marker, so we can handle it after
        cell.FormulaU = string.Format("RUNADDONWARGS(\"QueueMarkerEvent\",\"{0}\")", actionMarker);

        //set title
        VisioUtility.SetCellValue(element, VisSectionIndices.visSectionAction, VisRowIndices.visRowAction + newItemNumber, VisCellIndices.visActionMenu, title);

        if (isSubMenu)
            VisioUtility.SetCellValue(element, VisSectionIndices.visSectionAction, VisRowIndices.visRowAction + newItemNumber, VisCellIndices.visActionFlyoutChild, "1");

        if (beginGroup)
            VisioUtility.SetCellValue(element, VisSectionIndices.visSectionAction, VisRowIndices.visRowAction + newItemNumber, VisCellIndices.visActionBeginGroup, "1");
    }
}

And I had to handle MarkerEvent for visio application
visioApp.MarkerEvent += new EApplication_MarkerEventEventHandler(visioApp_MarkerEvent);

The main problem I faced with this task is how to disable glue for those labels, as for visio document GlueSettings we have visGlueToGeometry. I asked for this question on visguy forum and got few interesting advices, however they were not what I was looking for. The most usefull advice was to create a group and set IsSnapTarget to False. I gave up on this path, since creating the group was too complex.


The solution turned to be very easy. There is cell that disables connect for geometry NoSnap, so simply put True there!

No comments:

Post a Comment