Ejemplo n.º 1
0
 public bool AddGxFileLayer(IBasicMap pBasicMap, IGroupLayer pParentLayer, string sFileName)
 {
     try
     {
         if (sFileName == null)
         {
             sFileName = "";
         }
         if (pBasicMap == null)
         {
             return(false);
         }
         if (string.IsNullOrEmpty(sFileName))
         {
             OpenFileDialog dialog = new OpenFileDialog {
                 Filter      = "图层文件 (*.lyr)|*.lyr",
                 Multiselect = false,
                 Title       = "选择输入的图层文件"
             };
             if (dialog.ShowDialog() != DialogResult.OK)
             {
                 return(false);
             }
             sFileName = dialog.FileName;
             dialog    = null;
         }
         if (string.IsNullOrEmpty(sFileName))
         {
             return(false);
         }
         if (!File.Exists(sFileName))
         {
             Interaction.MsgBox("地图图层加载失败,图层文件 " + sFileName + " 不存在。", MsgBoxStyle.Exclamation, "错误警告");
             return(false);
         }
         IMapDocument document = null;
         document = new MapDocumentClass();
         if (!document.get_IsMapDocument(sFileName))
         {
             Interaction.MsgBox("文件 " + sFileName + " 无法加载。\r\n文件不是 MapDocument 文件。", MsgBoxStyle.Exclamation, "失败");
             return(false);
         }
         if (document.get_IsRestricted(sFileName))
         {
             Interaction.MsgBox("文件 " + sFileName + " 无法加载。\r\n文件受到限制,无权使用。", MsgBoxStyle.Exclamation, "失败");
             return(false);
         }
         document.Open(sFileName, null);
         ILayer pLayer = null;
         if (document.DocumentType == esriMapDocumentType.esriMapDocumentTypeLyr)
         {
             pLayer = document.get_Layer(0, 0);
             if (pLayer == null)
             {
                 Interaction.MsgBox("文件 " + sFileName + " 无法加载。\r\n图层文件或数据错误。", MsgBoxStyle.Exclamation, "加载失败");
             }
         }
         else
         {
             Interaction.MsgBox("文件 " + sFileName + " 无法加载。\r\n文件不是地图图层文件。", MsgBoxStyle.Exclamation, "加载失败");
         }
         document.Close();
         document = null;
         if (pLayer == null)
         {
             return(false);
         }
         if (pParentLayer == null)
         {
             pBasicMap.AddLayer(pLayer);
         }
         else
         {
             pParentLayer.Add(pLayer);
         }
         IMapLayers layers = pBasicMap as IMapLayers;
         if (pParentLayer == null)
         {
             layers.MoveLayer(pLayer, 0);
         }
         else if (pBasicMap is IMap)
         {
             layers.MoveLayerEx(pParentLayer, pParentLayer, pLayer, 0);
         }
         return(true);
     }
     catch (Exception exception)
     {
         this.mErrOpt.ErrorOperate(this.mSubSysName, "FunFactory.LayerFun", "AddGxFileLayer", exception.GetHashCode().ToString(), exception.Source, exception.Message, "", "", "");
         return(false);
     }
 }