Monday 4 March 2019

arcobjects - How can I implement Persistance in a Layer Extension?


Last time I checked, arcgis doesn't honor IPersistVariant in layer extensions. Rather advanced code has been provided here showing how to implement IPersistStream.


Is there something simpler?



Answer



You can easily bridge the implementation of IPersistStream with IPersistVariant using VariantStreamIO class. The code below is a simple demonstration which saves and loads a text value.


using System;
using System.Runtime.InteropServices;

using ESRI.ArcGIS.esriSystem;

namespace LayerExtensionPersistenceSample
{
[ComVisible(true)]
[ProgId("LayerExtensionPersistenceSample.LayerExtension")]
[Guid("...")]
[ClassInterface(ClassInterfaceType.None)]
public class LayerExtension : IPersistStream
{

private string _persistedTextValue;

#region Implementation of IPersistStream

public void GetClassID(out Guid classId)
{
classId = GetType().GUID;
}

public void IsDirty()

{
}

public void Load(IStream pstm)
{
var variantStreamIo = new VariantStreamIOClass();
variantStreamIo.Stream = pstm;

try
{

_persistedTextValue = (string)variantStreamIo.Read();
}
finally
{
Marshal.FinalReleaseComObject(pstm);
Marshal.FinalReleaseComObject(variantStreamIo);
}
}

public void Save(IStream pstm, int fClearDirty)

{
var variantStreamIo = new VariantStreamIOClass();
variantStreamIo.Stream = pstm;

try
{
variantStreamIo.Write(_persistedTextValue);
}
finally
{

Marshal.FinalReleaseComObject(pstm);
Marshal.FinalReleaseComObject(variantStreamIo);
}
}

public void GetSizeMax(out _ULARGE_INTEGER pcbSize)
{
// returning 0 seems to work without any problems
pcbSize = new _ULARGE_INTEGER();
pcbSize.QuadPart = 0;

}

#endregion
}
}

This way you can use the simpler IPersistVariant interface even when IPeristStream is enforced for some reason.


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...