前面已经介绍了如何制作自定义符号,已经将自定义样式文件转化为ArcEngine可用的“ServerStyle”格式,接下来就要读取它,这也是最重要的一步。
在用ArcMap新建自定义符号时,步骤有"样式管理器"—>"样式文件"—>"样式子类"—>"样式"这样来层层获取,ArcEngine的思想跟ArcMap一致,因此编写代码获取自定义样式符号时,也是遵循这样的步骤的:
1.新建样式管理器类对象
IStyleGallery pStyleGaller = new ServerStyleGallery();//样式管理集工具
2.设置目标样式文件
IStyleGalleryStorage pStyleGalleryStorage = pStyleGaller as IStyleGalleryStorage;//样式文件管理
pStyleGalleryStorage.TargetFile = sServerStylePath;//sServerStylePath为一字符串型,表示ServerStyle文件位置
3.获取样式子类
可通过遍历样式文件里的子类,然后找到与目标子类名称相同的样式子类即可
IStyleGalleryClass pStyleGalleryClass = null;//样式子类对象
for (int i = 0; i < pStyleGaller.ClassCount; i++)
{
pStyleGalleryClass = pStyleGaller.get_Class(i);
if (pStyleGalleryClass.Name != sGalleryClassName)//找到与目标子类名称相同的样式子类为止,这里的sGalleryClassName为类似"Marker Symbols"英文名称
continue;
IEnumStyleGalleryItem pEnumSyleGalleryItem = pStyleGaller.get_Items(sGalleryClassName, sServerStylePath, "");//获取样式子类
4.获取样式符号
可通过遍历样式子类的样式符号,找到与目标样式名称相同的方式获取
pEnumSyleGalleryItem.Reset();//将样式子类符号指针重置指向首部
IStyleGalleryItem pStyleGallerItem = pEnumSyleGalleryItem.Next(); //获取符号样式
while (pStyleGallerItem != null)
{
if (pStyleGallerItem.Name == symbolName)
{ //依次遍历知道找到与目标样式名称相同的
//获取符号
ISymbol pSymbol = pStyleGallerItem.Item as ISymbol;
System.Runtime.InteropServices.Marshal.ReleaseComObject(pEnumSyleGalleryItem);//释放Com对象,否则会出错
System.Runtime.InteropServices.Marshal.ReleaseComObject(pStyleGalleryClass);
return pSymbol;
}
pStyleGallerItem = pEnumSyleGalleryItem.Next();
}
完整代码如下
1 public static ISymbol GetSymbol(string sServerStylePath, string sGalleryClassName, string symbolName) 2 { 3 try 4 { 5 //ServerStyleGallery对象 6 IStyleGallery pStyleGaller = new ServerStyleGallery();//样式管理集工具 7 8 IEnumStyleGalleryItem pEnumSyleGalleryItem = null;//样式子项 9 IStyleGalleryItem pStyleGallerItem = null; 10 IStyleGalleryClass pStyleGalleryClass = null; 11 //使用IStyleGalleryStorage接口的AddFile方法加载ServerStyle文件 12 IStyleGalleryStorage pStyleGalleryStorage = pStyleGaller as IStyleGalleryStorage;//样式文件管理 13 14 bool styleExit = false; 15 pStyleGalleryStorage.TargetFile = sServerStylePath;//直接设置目标文件 16 for (int i = 0; i < pStyleGaller.ClassCount; i++) 17 { 18 19 pStyleGalleryClass = pStyleGaller.get_Class(i); 20 if (pStyleGalleryClass.Name != sGalleryClassName) 21 continue; 22 //获取EnumStyleGalleryItem对象 23 24 pEnumSyleGalleryItem = pStyleGaller.get_Items(sGalleryClassName, sServerStylePath, "");//获取符号集 25 26 pEnumSyleGalleryItem.Reset(); 27 //遍历pEnumSyleGalleryItem 28 pStyleGallerItem = pEnumSyleGalleryItem.Next(); 29 while (pStyleGallerItem != null) 30 { 31 32 33 if (pStyleGallerItem.Name == symbolName) 34 { 35 //获取符号 36 ISymbol pSymbol = pStyleGallerItem.Item as ISymbol; 37 System.Runtime.InteropServices.Marshal.ReleaseComObject(pEnumSyleGalleryItem); 38 System.Runtime.InteropServices.Marshal.ReleaseComObject(pStyleGalleryClass); 39 return pSymbol; 40 } 41 pStyleGallerItem = pEnumSyleGalleryItem.Next(); 42 } 43 } 44 //System.Runtime.InteropServices.Marshal.ReleaseComObject(pEnumSyleGalleryItem); 45 //System.Runtime.InteropServices.Marshal.ReleaseComObject(pStyleGalleryClass); 46 return null; 47 } 48 catch (Exception Err) 49 { 50 SimpleMarkerSymbol sym = new SimpleMarkerSymbol(); 51 sym.Style = esriSimpleMarkerStyle.esriSMSDiamond; 52 IRgbColor color = new RgbColor(); 53 color.Red = 197; 54 color.Green = 0; 55 color.Blue = 35; 56 sym.Color = color; 57 ISymbol pSymbol = sym as ISymbol; 58 59 MessageBox.Show(Err.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 60 return pSymbol; 61 } 62 } 63 }