delete balloon callout

490
4
08-30-2011 05:32 AM
LindaMcCafferty
New Contributor II
Good Morning -

I am trying to loop through all the items in a graphicscontainer and delete all items before recreating a new map. 

My problem is that when the script runs, it deletes all text items but it does not delete the balloon callouts. 

IGraphicsContainer graphicsContainer = pMxDocument.PageLayout as IGraphicsContainer;
IElement element = null;

graphicsContainer.Reset();
element = graphicsContainer.Next();
while (element != null)
        {
         if (!(element is IMapFrame))
            {
              //added this to see if it ever finds a balloon callout, but it doesn't
              if (element is IBalloonCallout)                        
                 {
                     graphicsContainer.DeleteElement(element);
                  }

               graphicsContainer.DeleteElement(element);
               graphicsContainer.Reset();
               pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, element, pActiveView.Extent);
           }
              element = graphicsContainer.Next();
       }

Any ideas?

Linda
0 Kudos
4 Replies
NeilClemmons
Regular Contributor III
A balloon callout isn't a type of graphic element so you can't loop through the elements looking for it.  A callout is a text background object, which is a property of a text element that uses a formatted text symbol.  In order to determine if a graphic element has a callout you first need to determine whether or not it implements ITextElement.  If it does, then you need to check to see if the type of symbol is IFormattedTextSymbol.  If it is, then you need to see if the background object is set and if so if it is IBalloonCallout.  Or if you're just interested in deleting all text elements then just check the element type and delete it if it's ITextElement.
0 Kudos
LindaMcCafferty
New Contributor II
The problem is that when I manually go to Tools --> Select All Elements, the balloon callouts are not selected, but everything else is.  So when I programmatically select all elements with the graphicsContainer, it does not see these elements to delete. 

Attached is a screen capture when I select all the elements.   

The strange part is when I create the balloon callout, the TextElement gets put into an IElement and added like this.

                        IGraphicsContainer pGreaphicsContainer = pMxDocument.FocusMap as IGraphicsContainer;
                        pGreaphicsContainer.AddElement(pElement, 0);

A balloon callout isn't a type of graphic element so you can't loop through the elements looking for it.  A callout is a text background object, which is a property of a text element that uses a formatted text symbol.  In order to determine if a graphic element has a callout you first need to determine whether or not it implements ITextElement.  If it does, then you need to check to see if the type of symbol is IFormattedTextSymbol.  If it is, then you need to see if the background object is set and if so if it is IBalloonCallout.  Or if you're just interested in deleting all text elements then just check the element type and delete it if it's ITextElement.
0 Kudos
NeilClemmons
Regular Contributor III
You're adding the text elements to the map's graphics container.  Your code that deletes the elements is looping through the page layout's graphics container.  The map and the page layout are both graphics containers and contain their own set of graphic elements.
0 Kudos
LindaMcCafferty
New Contributor II
Neil -
Thank you so much for pointing that out to me!  When creating the balloon callout, I had to use the FocusMap as the graphics container. 

I concentrated so much on trying to delete the element, that I didn't pay attention to where it was added.  

If anyone is interested, the revised code is: 

            IMxDocument pMxDocument = (IMxDocument)m_application.Document;
            IActiveView pActiveView = pMxDocument.PageLayout as IActiveView;
            IGraphicsContainer graphicsContainer = pMxDocument.PageLayout as IGraphicsContainer;
            IGraphicsContainer mapGraphicsContainer = pMxDocument.FocusMap as IGraphicsContainer;
            IElement element = null;
            ICompositeLayer pCompositeLayer = null;
            ICompositeGraphicsLayer pCompositeGraphicsLayer = null;

            try
            {
                if (pMxDocument.ActiveView is IMap)
                {
                    pMxDocument.ActiveView = pMxDocument.PageLayout as IActiveView;
                }

                mapGraphicsContainer.Reset();
                element = mapGraphicsContainer.Next();
                while (element != null)
                {
                    if (!(element is IMapFrame))
                    {
                        mapGraphicsContainer.DeleteElement(element);
                        mapGraphicsContainer.Reset();
                    }
                    element = mapGraphicsContainer.Next();
                }

                graphicsContainer.Reset();
                element = graphicsContainer.Next();
                while (element != null)
                {
                    if (!(element is IMapFrame))
                    {
                        graphicsContainer.DeleteElement(element);
                        graphicsContainer.Reset();
                        pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, element, pActiveView.Extent);
                    }
                    element = graphicsContainer.Next();
                }

This deletes elements in both the PageLayout and FocusMap. 

You're adding the text elements to the map's graphics container.  Your code that deletes the elements is looping through the page layout's graphics container.  The map and the page layout are both graphics containers and contain their own set of graphic elements.
0 Kudos