alx

Persisting Layerreference in ArcMAP-Document

Discussion created by alx on Mar 28, 2011
Is is possible to persist a reference to a layer, which resides in the TOC, in an ArcMap-Document. To memorize only the layername doesn't solve all problems, since it's possible that two ore more layers have the same name.

There is an entry in the documentation which bodes well.
http://edndoc.esri.com/arcobjects/9.2/CPP_VB6_VBA_VCPP_Doc/COM/ExtendAO/Persistence.htm
... Often multiple references are held to the same object, for example, the same Layer in a Map may be referenced by IMap::Layer and ILegendItem::Layer. If each of these properties were called to persist, two separate copies of the Layer would be persisted in different sections of the stream. This would bloat file size and would also corrupt object references. To avoid this problem, ObjectStreams are used in ArcObjects to persist objects and maintain object references correctly when persisted�?�

This doesn't really work. It seems that one has to use the same stream as arcmap itself and not the extensionspecific one. So also the following code doesn't work.

   void IPersistStream.Load(IStream pstm)
    {
      IObjectStream oStream = pstm as IObjectStream;
      if (oStream != null)
      {
        IFeatureLayer pLayer = null;
        Guid g = typeof(IFeatureLayer).GUID;
        pLayer = oStream.LoadObject(ref g, pLayer) as IFeatureLayer ;
        if (pLayer != null)
        {
          ILayerGeneralProperties lProp = pLayer as ILayerGeneralProperties;
          lProp.LayerDescription = DateTime.Now.ToString();
        }
      }
    }

    void IPersistStream.Save(IStream pstm, int fClearDirty)
    {
      IFeatureLayer pLayer = ((IMxDocument)m_application.Document).FocusMap.Layer[1] as IFeatureLayer ;
      if (pLayer != null)
      {
        IObjectStream oStream = pstm as IObjectStream;
        if (oStream != null)
        {
          oStream.SaveObject(pLayer);
        }
      }
    }



Is there any hint which prevents the clumsy kind of memorizing references of toc-layers by layernames in an arcmapextension?

Outcomes