Monday 26 June 2017

arcgis 10.0 - How to save and load elements to/from a layer in correct z-order?


I copy elements from one layer to another like this:


IGraphicsContainer container = _mapControl.Map as IGraphicsContainer;
IGraphicsContainerSelect containerSelect = container as IGraphicsContainerSelect;

containerSelect.SelectAllElements();
IEnumElement enumElement = containerSelect.SelectedElements;

enumElement.Reset();

IElement element = enumElement.Next();
while (element != null)
{
int zorder = 0; // How to get this?
layerContainer.AddElement(element, zorder);
element = enumElement.Next();
}


This works fine so far but I obviously like to preserve the element's zorder. I tried to get the zorder with graphicsContainer.GetElementOrder(enumElements) which returns an object (COM object). Well, but what to do with that one, eh?


How can I get the zorder of an element?


Update The enumElement is already in the correct order (Thanks, Kirk). I now took a deeper look into it. The elements get saved in the correct z-order, but when I load the layer from file (with IMapControl.AddLayerFromFile) they appear in reversed z-order every time. I tried to save them with reversed z-order but on the loaded layer they still appear in the wrong z-order.


How can I load the layer with the elements in correct z-order?



Answer



I got it. AddElements ignores the z-order. SelectedElements is in the correct z-order, but you have to add them with AddElements in reversed order (I already tried this, but with use of the - ignored - z-order parameter).


One more hint: When one selects an element in the UI and changes the z-order, the element needs to be unselected before the change gets saved, so I do containerSelect.UnselectAllElements(); first.


containerSelect.UnselectAllElements(); // see hint
containerSelect.SelectAllElements();
IEnumElement enumElement = containerSelect.SelectedElements;

enumElement.Reset();
IElement element = enumElement.Next();
List elemente = new List();
while (element != null)
{
elemente.Add(element);
element = enumElement.Next();
}
for (int j = elemente.Count - 1; j >= 0; j--)
{ // add in reversed order

layerContainer.AddElement(elemente[j], 0); // z-order parameter is ignored
}

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...