zoukankan      html  css  js  c++  java
  • IStyleGallery.AddItem、UpdateItem、RemoveItem用法

      今天做arcengine的符号库管理,做好了符号文件symbol.StyleServer。用到方法AddItem、UpdateItem、RemoveItem。怎么都报错。网上同样问题的人也有一些,就是不见人回答,只有自己琢磨。最终搞定了。写下来,备用。

      参考了一篇这样的文章IStyleGallery 和IstyleGalleryItem以及IStyleGalleryStorage接口理解.

    注意:用AddItem、UpdateItem、RemoveItem之前,必须用到IStyleGalleryStorage接口的TargetFile属性。如果没有设置这个属性,就会报错,错误信息如下:

    对 COM 组件的调用返回了错误 HRESULT E_FAIL。” 错误代码“-2147467259”

    读取ServerStyle文件代码如下:

            private string styleName = "3D Basic.ServerStyle";
            public void ReadStyleServer()
            {
                IStyleGallery tStyleGallery = new ServerStyleGalleryClass();
                IStyleGalleryStorage tStyleGalleryStorage = tStyleGallery as IStyleGalleryStorage;
                tStyleGalleryStorage.AddFile(styleName);
                // tStyleGalleryStorage.TargetFile = styleName_TargetFile;
                IEnumStyleGalleryItem tStyleGalleryItems = tStyleGallery.get_Items("Marker Symbols", styleName, "");
                tStyleGalleryItems.Reset();
                IStyleGalleryItem tStyleGalleryItem = tStyleGalleryItems.Next();
                int tIndex = 0;
                try
                {
                    while (tStyleGalleryItem != null)
                    {
                        string tName = tStyleGalleryItem.Name;
                        tStyleGalleryItem.Name = tName + tIndex;
                        tIndex++;
                        tStyleGallery.UpdateItem(tStyleGalleryItem);//这个地方报错 
                        tStyleGalleryItem = tStyleGalleryItems.Next();
                    }
                }
                catch (System.Runtime.InteropServices.COMException ex)
                {
                    string tErrorMessage = ex.Message +
                        ex.ErrorCode;
                }
                finally
                {
                    //释放这个接口,不然再次读取时会报错
                      ReleaseCom(tStyleGalleryItems);
                    ReleaseCom(tStyleGallery);
                }
            }
            private void ReleaseCom(object o)
            {
                while (System.Runtime.InteropServices.Marshal.ReleaseComObject(o) > 0)
                {
    
                }
            }

    要想更新,必须用到TargetFile属性。具体为什么必须用这个,原因不明,可能是文件独占。所以只能走一个弯。思路如下:

    (1)先把"3D Basic.ServerStyle"备份一个临时文件,备份的文件名称"Temp.ServerStyle"

    (2)设置TargetFile,使其指向"Temp.ServerStyle"

    (3)开始更新,更新完毕后,拷贝"Temp.ServerStyle"覆盖原来的"3D Basic.ServerStyle"

    代码如下(只写数据跟新,不写文件拷贝和覆盖)

            private string styleName_TargetFile = "Temp.ServerStyle";
            private string styleName = "3D Basic.ServerStyle";
            public void ReadStyleServer()
            {
                IStyleGallery tStyleGallery = new ServerStyleGalleryClass();
                IStyleGalleryStorage tStyleGalleryStorage = tStyleGallery as IStyleGalleryStorage;
                tStyleGalleryStorage.AddFile(styleName);
                tStyleGalleryStorage.TargetFile = styleName_TargetFile;
                IEnumStyleGalleryItem tStyleGalleryItems = tStyleGallery.get_Items("Marker Symbols", styleName, "");
                tStyleGalleryItems.Reset();
                IStyleGalleryItem tStyleGalleryItem = tStyleGalleryItems.Next();
                int tIndex = 0;
                try
                {
                    while (tStyleGalleryItem != null)
                    {
                        string tName = tStyleGalleryItem.Name;
                        tStyleGalleryItem.Name = tName + tIndex;
                        tIndex++;
                        tStyleGallery.UpdateItem(tStyleGalleryItem);
                        tStyleGalleryItem = tStyleGalleryItems.Next();
                    }
                }
                catch (System.Runtime.InteropServices.COMException ex)
                {
                    string tErrorMessage = ex.Message +
                        ex.ErrorCode;
                }
                finally
                {
                    //释放这个接口,不然再次读取时会报错
                      ReleaseCom(tStyleGalleryItems);
                    ReleaseCom(tStyleGallery);
                }
            }
            private void ReleaseCom(object o)
            {
                while (System.Runtime.InteropServices.Marshal.ReleaseComObject(o) > 0)
                {
    
                }
            }
    不再报错,顺利运行。AddItem、RemoveItem方法类似。AddItem的时候,如果目标文件不存在,接口会自动创建目标文件。
    作者: cglnet
    本文版权归cglNet和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
  • 相关阅读:
    CentOS 7.0安装Zimbra 8.6邮件服务器
    centos7备份还原与grub2引导和rescue模式修改root密码
    通过grub硬盘安装centos7
    CentOS系统中常用查看系统信息和日志命令小结
    安装完最小化 RHEL/CentOS 7 后需要做的 30 件事情(一)
    linux 下shell脚本执行多个命令的方法
    centos Crontab
    抓取某网站信息时遇到的问题及解决 The character set provided in ContentType is invalid. Cannot read content as string using an invalid character set
    HttpClient不必每次新建实例而RestSharp推荐新建实例的原因
    .net core读取json配置文件
  • 原文地址:https://www.cnblogs.com/cglNet/p/2649861.html
Copyright © 2011-2022 走看看