Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question
    
Did you try element.Geometry.Envelope? –  Chaz Mar 20 '12 at 15:30
    
Have you been able to confirm that sometimes the order of the elements returned by enumElement.Next() is not based on z-order? –  Kirk Kuykendall Mar 20 '12 at 20:28
    
Kirk: Good tip. I didn't know they are in the correct order. But still there is a glitch somewhere (see Update). –  gumo Mar 23 '12 at 15:49
    
Sorry, I can't see what the Envelope should help me with that? Can you give more detail please? –  gumo Mar 23 '12 at 15:58
add comment

2 Answers

up vote 1 down vote accepted

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<IElement> elemente = new List<IElement>();
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 
}
share|improve this answer
add comment

I didn't find a way neither.. but one other solution you can try is through copy and paste (which does seem to maintain the zorder).

            IMxDocument mxDoc = ArcMap.Document as IMxDocument;
            IViewManager vm = mxDoc.FocusMap as IViewManager;
            ISelection sel = vm.ElementSelection; 

            sel.Copy();

            //Get to another dataframe/map
            IMap map = mxDoc.Maps.get_Item(1);
            mxDoc.ActiveView = (IActiveView)map; 
            IViewManager vm2 = map as IViewManager;
            ISelection sel2 = vm2.ElementSelection;

            sel2.Paste();
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.