Removing Graphic Overlay at Dockpane

689
3
Jump to solution
06-29-2020 08:47 PM
by Anonymous User
Not applicable

Hi Guys,

I have a button in a dockpane to add one graphic overlay on the map view based on the provided geometry like below code snippet.

I call RemoveDrawnGraphic method before AddGraphics is being called so that I will only have one graphic overlay.

Some how, from third time button click of drawing graphic overlay, RemoveDrawnGraphic method is not working anymore, no more overlay removal is performed. 

This is happening in arcgis pro 2.5, as long as I remembered, I didn't encountered it in 2.4.x .(I guessed)

Or my code has some issue? Uma Harano‌ ?

 

private async Task AddGraphics(Geometry geometry)
        {
            await QueuedTask.Run(() =>
            {
                this.polylineSymbol = SymbolFactory.Instance.ConstructLineSymbol();
                this.polylineGraphic = new CIMLineGraphic();
                this.polylineGraphic.Line = (Polyline)geometry;
                this.polylineGraphic.Symbol = polylineSymbol.MakeSymbolReference();
                this.CurrentGraphic = MapView.Active.AddOverlay(polylineGraphic);
            });
        }



 private async Task RemoveDrawnGraphics()
        {
            var ps = new ProgressorSource();
            ps.Message = "Removing overlay graphic to keep latest overlay only";
            if (this.CurrentGraphic != null)
            {
                this.polylineSymbol = null;
                this.polylineGraphic = null;
                this.CurrentGraphic.Dispose();
                this.CurrentGraphic = null;
               
            }
            await QueuedTask.Run(() => Task.Delay(1000).Wait(), ps.Progressor); //Lets give garbage collector 1 sec, seem overlay does not disappear adhoc

        }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

Hi Wolfgang Kaiser

I managed to solve this issue by using IDisposable array instead of using single object.

Not sure why it is solved because I noticed that another addin which hold multiple graphic overlay and these can remove and add very well. So it is solved for now.

Below is the sample code how it is solved.

 private async Task RemoveDrawnGraphics()
        {
            var ps = new ProgressorSource("Check and remove graphic...");
            if (this.CurrentGraphicList != null && this.CurrentGraphicList.Count > 0)
            {
                int totalGraphic = this.CurrentGraphicList.Count;
                for (int i = totalGraphic - 1; i > -1; i--)
                {
                    this.CurrentGraphicList[i].Dispose();
                }
                this.CurrentGraphicList = new List<IDisposable>();
            }

            await QueuedTask.Run(() => Task.Delay(1000).Wait(), ps.Progressor); //Lets give garbage collector 1 sec.
        }
        
      private async Task AddGraphics(Geometry geometry)
        {
            await QueuedTask.Run(() =>
            {
                this.polylineSymbol = SymbolFactory.Instance.ConstructLineSymbol();
                this.polylineGraphic = new CIMLineGraphic();
                
                this.polylineGraphic.Line = (Polyline)geometry;
                this.polylineGraphic.Symbol = polylineSymbol.MakeSymbolReference();
                //this.CurrentGraphic = MapView.Active.AddOverlay(polylineGraphic);
                IDisposable tmpGraphic = MapView.Active.AddOverlay(polylineGraphic);
                this.CurrentGraphicList.Add(tmpGraphic);
            });
        }

View solution in original post

0 Kudos
3 Replies
Wolf
by Esri Regular Contributor
Esri Regular Contributor

Hi Than,

 Can you try to simplify your code as shown below and try if it works:

private void RemoveDrawnGraphics()
{
  this.CurrentGraphic?.Dispose();
  this.CurrentGraphic = null;
}
0 Kudos
by Anonymous User
Not applicable

Hi Wolfgang Kaiser‌,

It is not working as well, any advice.

I even did try adding these and no hope.

Interesting part is after third click it is not removed. 

How I call to add Graphics is => I call RemoveDrawnGraphics first, and then AddGraphics with line geometry.

 System.GC.Collect();
 System.GC.WaitForPendingFinalizers();

0 Kudos
by Anonymous User
Not applicable

Hi Wolfgang Kaiser

I managed to solve this issue by using IDisposable array instead of using single object.

Not sure why it is solved because I noticed that another addin which hold multiple graphic overlay and these can remove and add very well. So it is solved for now.

Below is the sample code how it is solved.

 private async Task RemoveDrawnGraphics()
        {
            var ps = new ProgressorSource("Check and remove graphic...");
            if (this.CurrentGraphicList != null && this.CurrentGraphicList.Count > 0)
            {
                int totalGraphic = this.CurrentGraphicList.Count;
                for (int i = totalGraphic - 1; i > -1; i--)
                {
                    this.CurrentGraphicList[i].Dispose();
                }
                this.CurrentGraphicList = new List<IDisposable>();
            }

            await QueuedTask.Run(() => Task.Delay(1000).Wait(), ps.Progressor); //Lets give garbage collector 1 sec.
        }
        
      private async Task AddGraphics(Geometry geometry)
        {
            await QueuedTask.Run(() =>
            {
                this.polylineSymbol = SymbolFactory.Instance.ConstructLineSymbol();
                this.polylineGraphic = new CIMLineGraphic();
                
                this.polylineGraphic.Line = (Polyline)geometry;
                this.polylineGraphic.Symbol = polylineSymbol.MakeSymbolReference();
                //this.CurrentGraphic = MapView.Active.AddOverlay(polylineGraphic);
                IDisposable tmpGraphic = MapView.Active.AddOverlay(polylineGraphic);
                this.CurrentGraphicList.Add(tmpGraphic);
            });
        }
0 Kudos