RESOLVED: Clearing the display of trackPolygons

3135
6
07-18-2011 08:12 AM
by Anonymous User
Not applicable
Original User: Scole

I have a tool in an add-in in which the user draws a polygon on the screen using trackPolygon and then the geometry of the polygon is used to query another data layer. This all works just fine but as the add-in is used multiple times, the display gets cluttered with multiple polygons.

How can I flush the display to remove any previously drawn polygons? I tried inserting Application.ActiveMapDisplay.CancelTracking into my code either at the beginning or end of my code (an onClick event) but it does not appear to do anything.

Thanks!
Steve
0 Kudos
6 Replies
by Anonymous User
Not applicable
Original User: aivanov

Steve,

I assume you add these polygons as graphics to display, right? If so, you'd need to keep track of graphics you add to the display, then you'd need to remove these graphics.

This is the code to add graphic:

ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay.Graphics.Add(graphic);
This is the code to remove graphic:

ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay.Graphics.Remove(graphic);
Hope it helps!
0 Kudos
by Anonymous User
Not applicable
Original User: Scole

Andriy- that was EXACTLY what I needed to know, thank you! I'm actually going to use the clear() method at the beginning of my onClick event instead of seeking out a specific item.

Don't know how I missed that from my scouring of the SDK documentation but thanks again for pointing me to it!

Cheers,
Steve
0 Kudos
BrianLocke
Occasional Contributor II
Note that when you use the Clear() it will remove ALL Graphics collections, I would not use this for the instance that someone had other graphics in there it will remove those as well. using remove(graphicname) will remove that graphic only, I would watch out though I have found that if declaring a new graphic and and using the same name will only remove the last created graphic, and the graphics collection will only return that one instance if you get a count, I am still trying to find a way around this..... trying to iterate through all the graphics created but it seems to forget about the others .
0 Kudos
by Anonymous User
Not applicable
Original User: BorseleCountyCouncil

Brian,
Because of the way iterators/enumerators work in .Net, you can't safely change the list while you're going through it.
It's a bit like working through a linked list, deleting the current element and then using it's next pointer to move on.

Here are 2 workarounds:
1. Work backwards : for (int i = list.Count - 1; i >= 0; --i) list.RemoveAt(i);

2. Create a temporary list in which you place the elements to delete while iterating. When finished iterating, call the list function which removes all elements given an enumerator (your delete list).

Hope this helps.
0 Kudos
BrianLocke
Occasional Contributor II
ahh thanks very much, yeah def still learning. have left ole VB and began using C# aww sooo much better!!!.  Now I am still a  bit stuck though looking at the Mapdisplay.graphics trying to pull a particular item from the Collection.  still trying to figure out the Contains method kinda of seems 'not too powerful' I guess if you need to move it comes in help.  Also doesn't look like you can use LINQ against the collection either???

Brian
want-to-be-programmer
0 Kudos
by Anonymous User
Not applicable
Original User: aaronnoriega

ahh thanks very much, yeah def still learning. have left ole VB and began using C# aww sooo much better!!!.  Now I am still a  bit stuck though looking at the Mapdisplay.graphics trying to pull a particular item from the Collection.  still trying to figure out the Contains method kinda of seems 'not too powerful' I guess if you need to move it comes in help.  Also doesn't look like you can use LINQ against the collection either???

Brian
want-to-be-programmer



I haven't tested this code, but this should give you a general idea on how to employ LINQ to query graphic attributes
public class WidgetItem{//user class to define associated attributes to Graphic Objects
 public string Name { get; set; }
 public DateTime DateAdded { get; set; }
 public Graphic Graphic { get; set; }
 
 public WidgetItem (){
  this.DateAdded = DateTime.Now;
 }
}


List<WidgetItem> widgets = new List<WidgetItem>();
// Create and populate Widget Objects
WidgetItem wi = new WidgetItem();
wi.Name = "Brian";
wi.Graphic = somegraphic; //probably the result of a track() operation
widgets.Add(wi);


var removeus = (from w in widgets
   where w.Name.StartsWith("B") && w.Graphic != null
   select w.Graphic);

ActiveMapDisplay.Graphics.Remove(removeus);



You can further increase the capabilities of the WidgetItem class by implementing the IComparable<T> and IEquatable<T> templates; as well as overriding the GetHashCode and Equals functions.
0 Kudos