将AE对象序列化为二进制文件

当我们编写AE程序时,通常会遇到需要存储某个AE对象的情况,

比如Layer,Element,Map,Legend,NorthArrow等等这些

举个例子说明一下:在我们编辑Featurelayer时,我们可以容易的将Feature存储在Featureclass中,

同样,如果我们向Graphicscontainer中添加了Element,我们也希望可以容易的存储Element

这样每次加载时可以将Element顺利显示出来,但是不巧的是,AE中并没有提供存储Element的方法

这个时候,我们就需要将Graphicscontainer序列化成文件,这样就可以达到存储的目的了

要知道,Featureclass存储成shapefile也好,Geodatabase也罢,都是一种文件的组织形式,也都是一种特殊意义上的序列化。

好,下面,介绍序列化的方法:

首先可以序列化的对象必须实现了IPersistStream接口,

其中IPersistStream接口是Windos中的接口,派生自 IPersist,并增加了4个函数,从流(IStream)中读写组件属性信息。

下面是各个函数的意义: 

 

IsDirty()

组件内部属性是否发生了变化。为调用者是否需要保存信息提供依据

Load()

IStream 中读入信息,初始化组件属性

Save()

把属性信息保存到 IStream

GetSizeMax()

返回信息尺寸,以便调用者事先开辟空间

 

AE中用下面两个函数即可实现对象的序列化,说明已经写的很清楚了,一目了然啊。

这个是我做的序列化Layer的例子,大家可以下载使用(VB.Net)/Files/wall/TestESRIStream.rar

  ''' <summary>
  ''' 将AE中实现了IPersistStream接口的对象序列化为二进制文件
  ''' </summary>
  ''' <param name="pObject">对象</param>
  ''' <param name="pFilePath">文件名全路径(形如:“C:\file.blb”)</param>
  ''' <remarks></remarks>

  Sub WriteObject(ByVal pFilePath As String, ByVal pObject As Object)
    If Not TypeOf pObject Is IPersistStream Then
      MessageBox.Show("该对象不支持序列化!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
      Exit Sub
    End If
    Dim pMemoryBlobStream As IMemoryBlobStream = New MemoryBlobStream
    Dim pObjectStream As IObjectStream = New ObjectStream
    pObjectStream.Stream = pMemoryBlobStream
    Dim pPersistStream As IPersistStream = pObject
    pPersistStream.Save(pObjectStream, True)
    Try
      pMemoryBlobStream.SaveToFile(pFilePath)
    Catch ex As Exception
      MessageBox.Show("序列化文件路径不合法!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Try
  End Sub
  ''' <summary>
  ''' 从序列化文件中读取对象(反序列化)
  ''' </summary>
  ''' <param name="pObject">对象</param>
  ''' <param name="pFilePath">文件名全路径(形如:“C:\file.blb”)</param>
  ''' <remarks></remarks>
  Sub ReadObject(ByVal pFilePath As String, ByRef pObject As Object)
    If Not TypeOf pObject Is IPersistStream Then
      MessageBox.Show("该对象不支持序列化!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
      Exit Sub
    End If
    If Not System.IO.File.Exists(pFilePath) Then
      MessageBox.Show("序列化文件不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
      Exit Sub
    End If
    Dim pMemoryBlobStream As IMemoryBlobStream = New MemoryBlobStream
    pMemoryBlobStream.LoadFromFile(pFilePath)
    Dim pObjectStream As IObjectStream = New ObjectStream
    pObjectStream.Stream = pMemoryBlobStream
    Dim pPersistStream As IPersistStream = pObject
    pPersistStream.Load(pObjectStream)
  End Sub

posted @ 2010-06-09 17:52  zhh  阅读(319)  评论(0编辑  收藏  举报