Ejemplo n.º 1
0
        protected override void OnClick()
        {
            IEnumLayer   EnumLayer    = ArcMap.Document.FocusMap.get_Layers(null, false);
            ILayer       pLayer       = EnumLayer.Next();
            ProgressForm progressForm = new ProgressForm();

            progressForm.Show();


            while (null != pLayer)
            {
                //string b = pLayer.Name.Substring(pLayer.Name.LastIndexOf('_') + 1);
                if ("Point" == pLayer.Name.Substring(pLayer.Name.LastIndexOf('_') + 1) ||
                    "Line" == pLayer.Name.Substring(pLayer.Name.LastIndexOf('_') + 1) ||
                    "Polygon" == pLayer.Name.Substring(pLayer.Name.LastIndexOf('_') + 1)
                    )
                {
                    break;
                }
                pLayer = EnumLayer.Next();
            }//加入的图层中至少有点线面中的一个,点线面图层中有DWG字段,表明了shp的来源

            progressForm.AddProgress(10);
            IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
            IFeature      pfeature      = pFeatureLayer.Search(null, false).NextFeature();

            IDataLayer   DataLayer       = pLayer as IDataLayer;
            IDatasetName tem_DatasetName = DataLayer.DataSourceName as IDatasetName;

            string PathName = tem_DatasetName.WorkspaceName.PathName;

            string DatasetName  = pfeature.get_Value(pfeature.Fields.FindField("DWG")) as string;//获取生成shp的DWG文件名称
            string MDB_FullName = PathName;

            //*******************************************开 始 拓 扑 检 查*****************************************************
            IWorkspaceFactory iwsf             = new AccessWorkspaceFactoryClass();
            IFeatureWorkspace ifws             = (IFeatureWorkspace)iwsf.OpenFromFile(MDB_FullName, 0);
            IFeatureDataset   topocheckDataset = ifws.OpenFeatureDataset(DatasetName);

            IFeatureClassContainer MyFeatureClassContainer = topocheckDataset as IFeatureClassContainer;


#if true
            TopologyChecker topocheck = new TopologyChecker(topocheckDataset);
            topocheck.PUB_TopoBuild(DatasetName + "_TopoCheck");//创建拓扑层
            progressForm.AddProgress(20);

            List <IFeatureClass> allFeatureClass_tem = topocheck.PUB_GetAllFeatureClass();
            //List<IFeatureClass> TopoClass;
            List <IFeatureClass> allFeatureClass = new List <IFeatureClass>();

            IFeatureClass topocheckFeatureClass_Point   = null;
            IFeatureClass topocheckFeatureClass_Line    = null;
            IFeatureClass topocheckFeatureClass_Polygon = null;

            foreach (IFeatureClass tem in allFeatureClass_tem)
            {
                switch (tem.AliasName.Substring(tem.AliasName.LastIndexOf('_') + 1))
                {
                case "Point":
                    allFeatureClass.Add(tem);
                    topocheckFeatureClass_Point = tem;
                    break;

                case "Line":
                    allFeatureClass.Add(tem);
                    topocheckFeatureClass_Line = tem;
                    break;

                case "Polygon":
                    allFeatureClass.Add(tem);
                    topocheckFeatureClass_Polygon = tem;
                    break;

                default:

                    break;
                }
            }//只对点线面图层进行拓扑检查

            topocheck.PUB_AddFeatureClass(allFeatureClass);
            progressForm.AddProgress(10);

            List <string> ErrorInfo = null;
            topocheck.PUB_AddRuleToTopology(TopologyChecker.TopoErroType.线要素不能自相交, topocheckFeatureClass_Line, out ErrorInfo);
            progressForm.AddProgress(10);
            topocheck.PUB_AddRuleToTopology(TopologyChecker.TopoErroType.线要素之间不能相交, topocheckFeatureClass_Line, out ErrorInfo);
            progressForm.AddProgress(10);
            topocheck.PUB_AddRuleToTopology(TopologyChecker.TopoErroType.点要素之间不相交, topocheckFeatureClass_Point, out ErrorInfo);
            progressForm.AddProgress(10);
            topocheck.PUB_AddRuleToTopology(TopologyChecker.TopoErroType.面要素间无重叠, topocheckFeatureClass_Polygon, out ErrorInfo);
            progressForm.AddProgress(10);
            System.IO.File.WriteAllLines(MDB_FullName.Substring(0, MDB_FullName.LastIndexOf('\\')) + "\\拓扑错误报告.txt", ErrorInfo.ToArray());
            //生成检查报告
            progressForm.AddProgress(10);

            //----------------------------------------------打 开 错 误 报 告 器------------------------------------------------------
            EnumLayer = ArcMap.Document.FocusMap.get_Layers(null, false);

            ArcMap.Document.FocusMap.ClearLayers();
            ArcMap.Document.FocusMap.AddLayers(EnumLayer, true);
            pLayer = EnumLayer.Next();

            while (null != pLayer)
            {
                if ((DatasetName + "_Polygon") == pLayer.Name)
                {
                    pLayer = EnumLayer.Next();
                    continue;
                }
                DirectRender(pLayer);
                pLayer = EnumLayer.Next();
            }

            ESRI.ArcGIS.Framework.ICommandBars CommandBars = ArcMap.Application.Document.CommandBars;
            UID uid = new UIDClass();
            uid.Value = "esriEditor.StartEditingCommand";
            //bool isEditable = true;
            try
            {
                ESRI.ArcGIS.Framework.ICommandItem CommandItem = CommandBars.Find(uid, false, false);
                if (CommandItem != null)
                {
                    CommandItem.Execute();
                }

                uid.Value   = "esriEditorExt.ErrorWindowCommand";
                CommandItem = CommandBars.Find(uid, false, false);
                if (CommandItem != null)
                {
                    CommandItem.Execute();
                }
            }
            catch
            {
                //isEditable = false;//如果初次打开出现不可编辑的错误,下面将尝试重新添加图层并打开错误编辑器
            }

            /*
             * if (!isEditable)
             * {
             *  try
             *  {
             *      ESRI.ArcGIS.Framework.ICommandItem CommandItem = CommandBars.Find(uid, false, false);
             *      if (CommandItem != null)
             *      {
             *          CommandItem.Execute();
             *      }
             *
             *      uid.Value = "esriEditorExt.ErrorWindowCommand";
             *      CommandItem = CommandBars.Find(uid, false, false);
             *      if (CommandItem != null)
             *      {
             *          CommandItem.Execute();
             *      }
             *
             *  }catch{}
             *
             *
             * }*/
            //------------------------------------------完成打开错误报告器---------------------------------------------
            progressForm.Close();
#endif
        }
Ejemplo n.º 2
0
        protected override void OnClick()
        {
#if true
#if true
            if (0 >= ArcMap.Document.FocusMap.LayerCount)
            {
                MessageBox.Show("未加入图层");
                return;
            }
            IEnumLayer EnumLayer = ArcMap.Document.FocusMap.get_Layers(null, false);
            ILayer     tem_Layer = EnumLayer.Next();
            // string d = tem_Layer.Name;
            IDataLayer   DataLayer;
            IDatasetName tem_DatasetName;
            string       tem_Name;
            DataLayer       = tem_Layer as IDataLayer;
            tem_DatasetName = DataLayer.DataSourceName as IDatasetName;
            tem_Name        = tem_DatasetName.WorkspaceName.PathName;
            string tem_SubstringRef = tem_Name.Substring(tem_Name.LastIndexOf('.') + 1);

            while (null != tem_Layer)
            {
                DataLayer       = tem_Layer as IDataLayer;
                tem_DatasetName = DataLayer.DataSourceName as IDatasetName;
                tem_Name        = tem_DatasetName.WorkspaceName.PathName;
                string tem_Substring = tem_Name.Substring(tem_Name.LastIndexOf('.') + 1);
                if (tem_Substring != tem_SubstringRef)
                {
                    MessageBox.Show("图层来自不同的dwg", "错误");
                    return;
                }

                tem_Layer = EnumLayer.Next();
            }
            if ("mdb" == tem_SubstringRef)
            {
                MessageBox.Show("图层来自个人地理数据库,如需重新生成,请清空图层后加入shp文件", "提示");
                return;
            }

            EnumLayer.Reset();
            tem_Layer = EnumLayer.Next();
            string DatasetName = tem_Layer.Name.Substring(0, tem_Layer.Name.LastIndexOf('_'));
            //string DatasetName = tem_SubstringRef.Substring(tem_SubstringRef.LastIndexOf('\\') + 1);

            OpenFileDialog OFD = new OpenFileDialog();
            OFD.Multiselect      = true;
            OFD.Title            = "请选择要加入的个人地理数据库";
            OFD.Filter           = "(*.mdb)|*.mdb";
            OFD.InitialDirectory = "C:\\";
            OFD.Multiselect      = false;
            string MDB_FullName = null;
            if (OFD.ShowDialog() == DialogResult.OK)
            {
                MDB_FullName = OFD.FileName;
            }
            if (null == MDB_FullName)
            {
                return;
            }
            string MDB_Path = MDB_FullName.Substring(0, MDB_FullName.LastIndexOf('\\') + 1);
            string MDB_Name = MDB_FullName.Substring(MDB_Path.Length, MDB_FullName.LastIndexOf('.') - MDB_Path.Length);



            EnumLayer = ArcMap.Document.FocusMap.get_Layers(null, false);
            tem_Layer = EnumLayer.Next();

            //string a = tem_Layer.Name.Substring(DatasetName.Length + 1);
            string[] shps              = new string[ArcMap.Document.FocusMap.LayerCount];
            int      i                 = 0;
            bool     isPointExist      = false;
            bool     isLineExist       = false;
            bool     isPolygonExist    = false;
            bool     isAnnotationExist = false;

            while (null != tem_Layer)
            {
                shps[i] = tem_Name + "\\" + tem_Layer.Name + ".shp";
                switch (tem_Layer.Name.Substring(DatasetName.Length + 1))
                {
                case "Point":
                    isPointExist = true;
                    break;

                case "Line":
                    isLineExist = true;
                    break;

                case "Polygon":
                    isPolygonExist = true;
                    break;

                case "Annotation":
                    isAnnotationExist = true;
                    break;
                }
                tem_Layer = EnumLayer.Next();
                i++;
            }

            if (!isPointExist)
            {
                DialogResult Result = MessageBox.Show("未加入点图层,是否继续", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (Result == DialogResult.No)
                {
                    return;
                }
            }
            if (!isLineExist)
            {
                DialogResult Result = MessageBox.Show("未加入线图层,是否继续", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (Result == DialogResult.No)
                {
                    return;
                }
            }
            if (!isPolygonExist)
            {
                DialogResult Result = MessageBox.Show("未加入面图层,是否继续", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (Result == DialogResult.No)
                {
                    return;
                }
            }
            if (!isAnnotationExist)
            {
                DialogResult Result = MessageBox.Show("未加入注记层,是否继续", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (Result == DialogResult.No)
                {
                    return;
                }
            }
#endif


#if false
            string MDB_FullName = "C:\\Users\\JabinGuo\\Desktop\\dwg\\test.mdb";
            string DatasetName  = "db";
            string MDB_Path     = "C:\\Users\\JabinGuo\\Desktop\\dwg\\";
            string MDB_Name     = "test";


            OpenFileDialog OFD = new OpenFileDialog();
            OFD.Multiselect      = true;
            OFD.Title            = "请选择文件";
            OFD.Filter           = "shp files(*.shp)|*.shp|所有文件(*.*)|*.*";
            OFD.InitialDirectory = "C:\\";
            IEnumLayer EnumLayer = ArcMap.Document.FocusMap.get_Layers(null, false);
            //AxMapControl mc = new AxMapControl();
            // ICommand command = new ESRI

            string[] shps = null;
            if (OFD.ShowDialog() == DialogResult.OK)
            {
                shps = OFD.FileNames;
            }

            if (null == shps || shps.Length < 1)
            {
                return;//如果没有选择文件,退出函数
            }
#endif
            ProgressForm progressForm = new ProgressForm();
            progressForm.Show();

            C_MDBBuilder buil = new C_MDBBuilder();

            //***********************************************
            IWorkspaceFactory iwsf = new AccessWorkspaceFactoryClass();
            IFeatureWorkspace ifws = null;


            if (File.Exists(MDB_FullName))
            {
                ifws = (IFeatureWorkspace)iwsf.OpenFromFile(MDB_FullName, 0);

                try
                {
                    ifws.OpenFeatureDataset(DatasetName).Delete();
                }
                catch { }
            }
            //如果mdb存在,那么dataset也有可能存在,尝试删除dataset
            ArcMap.Document.FocusMap.ClearLayers();
            foreach (string shpName in shps)
            {
                bool isFeatureClassExists = buil.MDB_Builder(shpName, MDB_Path, MDB_Name, DatasetName);//将shp导入mdb文件
                progressForm.AddProgress(20 / (shps.Length));
            }

            //**********************************开 始 渲 染**************************************

            EnumLayer = ArcMap.Document.FocusMap.get_Layers(null, false);
            ILayer pLayer = EnumLayer.Next();

            while (null != pLayer)
            {
                if ((DatasetName + "_Point") == pLayer.Name || (DatasetName + "_Line") == pLayer.Name)
                {
                    DirectRender(pLayer);//渲染的方法
                }
                pLayer = EnumLayer.Next();
            }

            progressForm.AddProgress(10);
            //*********************************渲 染 结 束*****************************************

            //*********************************开 始 标 注 转 注 记**********************************
            //IEnumLayer EnumLayer = ArcMap.Document.FocusMap.get_Layers(null, false);
            //ILayer pLayer = EnumLayer.Next();
            EnumLayer = ArcMap.Document.FocusMap.get_Layers(null, false);
            pLayer    = EnumLayer.Next();

            while (((DatasetName + "_Annotation") != pLayer.Name) && (null != pLayer) && ((DatasetName + "_AnnotationZJ") != pLayer.Name))
            {
                pLayer = EnumLayer.Next();
            }
            if (null == pLayer)
            {
                progressForm.Close();
                return;
            }
            if (((DatasetName + "_AnnotationZJ") == pLayer.Name))
            {
                progressForm.Close();
                MessageBox.Show("注记层已存在");
                return;
            }
            MyAnnotation myAnnotation = new MyAnnotation();

            string errstring = null;
            myAnnotation.LabelConvertToAnnotation(ArcMap.Document.FocusMap, pLayer, false, out errstring);//生成的注记层名字为“标注层名称+ZJ”

            progressForm.AddProgress(20);

            IGeoFeatureLayer single_useGeoFeatureLayer = pLayer as IGeoFeatureLayer;
            single_useGeoFeatureLayer.DisplayAnnotation = false;//隐藏标注

            ifws = (IFeatureWorkspace)iwsf.OpenFromFile(MDB_FullName, 0);
            //IFeatureDataset MyfeatureDataset = ifws.OpenFeatureDataset(DatasetName);
            IFeatureClassContainer MyFeatureClassContainer  = ifws.OpenFeatureDataset(DatasetName) as IFeatureClassContainer;
            IEnumFeatureClass      enumFeatureClass         = MyFeatureClassContainer.Classes;
            IFeatureClass          MyAnnotationFeatureClass = enumFeatureClass.Next();
            while (null != MyAnnotationFeatureClass && ((DatasetName + "_AnnotationZJ") != MyAnnotationFeatureClass.AliasName))
            {
                MyAnnotationFeatureClass = enumFeatureClass.Next();
            }
            //获取生成的注记层
            if (null == MyAnnotationFeatureClass)
            {
                MessageBox.Show("未找到注记层,请检查是否生成注记层", "警告");
                return;
            }

            progressForm.AddProgress(10);
            //IFeatureClass MyAnnotationFeatureClass = ifws.OpenFeatureClass("AnnotationZJ");//在mdb空间下打开FeatureClass,所以不同dataset下不能有重名FeatureClass
            //IFeatureCursor AnnotationFeatureCursor = MyAnnotationFeatureClass.Search(null, false);
            //IFeature MyAnnotationFeature = AnnotationFeatureCursor.NextFeature();
            enumFeatureClass = MyFeatureClassContainer.Classes;
            IFeatureClass LabelFeatureClass = enumFeatureClass.Next();
            while (null != LabelFeatureClass && ((DatasetName + "_Annotation") != LabelFeatureClass.AliasName))
            {
                LabelFeatureClass = enumFeatureClass.Next();
            }//获取原注记层
            if (null == LabelFeatureClass)
            {
                MessageBox.Show("未找到原注记层,可能造成生成的注记层要素缺失", "警告");
                return;
            }

            IFeature       MyAnnotationFeature;
            IFeatureCursor AnnotationFeatureCursor = MyAnnotationFeatureClass.Search(null, false);
            MyAnnotationFeature = AnnotationFeatureCursor.NextFeature();

            while (null != MyAnnotationFeature)
            {
                int      LabelFeatureID = (int)MyAnnotationFeature.get_Value(MyAnnotationFeature.Fields.FindField("FeatureID"));
                IFeature LabelFeature   = LabelFeatureClass.GetFeature(LabelFeatureID);
                string   st             = LabelFeature.get_Value(LabelFeature.Fields.FindField("Text_")) as string;
                myAnnotation.ConfigAnnotation(MyAnnotationFeature, LabelFeature.Shape, st, 8, esriTextVerticalAlignment.esriTVABottom, esriTextHorizontalAlignment.esriTHALeft);
                MyAnnotationFeature = AnnotationFeatureCursor.NextFeature();
            }//逐个要素修改生成的注记
            progressForm.AddProgress(20);
            EnumLayer = ArcMap.Document.FocusMap.get_Layers(null, false);
            pLayer    = EnumLayer.Next();
            while (null != pLayer)
            {
                if ((DatasetName + "_Annotation") == pLayer.Name)
                {
                    ArcMap.Document.FocusMap.DeleteLayer(pLayer);
                }
                pLayer = EnumLayer.Next();
            }//移除原注记层
            progressForm.AddProgress(10);
            progressForm.Close();

            //******************************标 注 转 注 记 结 束********************************************

            //Thread.Sleep(1000);
#endif
            #region 标注转注记
#if false
            List <string> shps = new List <string>();
            shps.Add("C:\\Users\\JabinGuo\\Desktop\\注记\\Annotation.shp");
            C_MDBBuilder buil = new C_MDBBuilder();
            bool         isFeatureClassExists = buil.MDB_Builder(shps[0], "C:\\Users\\JabinGuo\\Desktop\\dwg\\", "test", "db");//将shp导入mdb文件

            IWorkspaceFactory iwsf = new AccessWorkspaceFactoryClass();

            IFeatureWorkspace ifws = (IFeatureWorkspace)iwsf.OpenFromFile("C:\\Users\\JabinGuo\\Desktop\\dwg\\test.mdb", 0);

            IEnumLayer EnumLayer = ArcMap.Document.FocusMap.get_Layers(null, false);
            ILayer     pLayer    = EnumLayer.Next();
            while ("Annotation" != pLayer.Name)
            {
                pLayer = EnumLayer.Next();
            }
            //ArcMap.Document.FocusMap.get_Layer(0);

            //IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
            //IGeoFeatureLayer pGeoFeatureLayer = pFeatureLayer as IGeoFeatureLayer;
            MyAnnotation myAnnotation = new MyAnnotation();

            string errstring = null;
            myAnnotation.LabelConvertToAnnotation(ArcMap.Document.FocusMap, pLayer, true, out errstring);//生成的注记层名字为“标注层名称+ZJ”

            IGeoFeatureLayer single_useGeoFeatureLayer = pLayer as IGeoFeatureLayer;
            single_useGeoFeatureLayer.DisplayAnnotation = false;//隐藏标注

            IFeatureClass MyAnnotationFeatureClass = ifws.OpenFeatureClass("AnnotationZJ");
            //IFeatureCursor AnnotationFeatureCursor = MyAnnotationFeatureClass.Search(null, false);
            //IFeature MyAnnotationFeature = AnnotationFeatureCursor.NextFeature();

            IFeatureClass LabelFeatureClass = ifws.OpenFeatureClass("Annotation");

            IFeature MyAnnotationFeature = MyAnnotationFeatureClass.GetFeature(10);//选定要修改的要素

            int      LabelFeatureID = (int)MyAnnotationFeature.get_Value(MyAnnotationFeature.Fields.FindField("FeatureID"));
            IFeature LabelFeature   = LabelFeatureClass.GetFeature(LabelFeatureID);

            myAnnotation.ConfigAnnotation(MyAnnotationFeature, LabelFeature.Shape, "kkk", 12, esriTextVerticalAlignment.esriTVACenter, esriTextHorizontalAlignment.esriTHACenter);

            //myAnnotation.ConfigFields(MyAnnotationFeatureClass, AnnotationFeatureCursor, MyAnnotationFeature);//保留此函数,可以用于修改要素字段
#endif
            #endregion

            #region 拓扑检查
    #if false
            IFeatureDataset topocheckDataset = ifws.OpenFeatureDataset("db");
            TopologyChecker topocheck        = new TopologyChecker(topocheckDataset);
            topocheck.PUB_TopoBuild("addinTopo");
            topocheck.PUB_AddFeatureClass(null);
            List <IFeatureClass> allFeatureClass = topocheck.PUB_GetAllFeatureClass();

            IFeatureClass topocheckFeatureClass = null;
            foreach (IFeatureClass tem in allFeatureClass)
            {
                if ("" == tem.AliasName)
                {
                    topocheckFeatureClass = tem;
                    break;
                }
            }

            topocheck.PUB_AddRuleToTopology(TopologyChecker.TopoErroType.线要素必须不相交, topocheckFeatureClass);
    #endif
            #endregion

            #region 渲染
#if false
            List <string> shps = new List <string>();
            shps.Add("C:\\Users\\JabinGuo\\Desktop\\渲染\\Polygon.shp");
            C_MDBBuilder buil = new C_MDBBuilder();
            bool         isFeatureClassExists = buil.MDB_Builder(shps[0], "C:\\Users\\JabinGuo\\Desktop\\dwg\\", "test", "db");//将shp导入mdb文件

            IWorkspaceFactory iwsf = new AccessWorkspaceFactoryClass();

            IFeatureWorkspace ifws = (IFeatureWorkspace)iwsf.OpenFromFile("C:\\Users\\JabinGuo\\Desktop\\dwg\\test.mdb", 0);


            DirectRender(ArcMap.Document.FocusMap.get_Layer(0));
#endif
            #endregion



            #region 拓扑检查
#if false
            List <string> shps = new List <string>();
            shps.Add("C:\\Users\\JabinGuo\\Desktop\\shp\\2010.6.20-500_t3\\Line.shp");

            C_MDBBuilder buil = new C_MDBBuilder(shps, "C:\\Users\\JabinGuo\\Desktop\\dwg\\", "test", "db");

            AccessWorkspaceFactory MyWorkSpaceFactory = new AccessWorkspaceFactoryClass();
            IFeatureWorkspace      a         = (IFeatureWorkspace)MyWorkSpaceFactory.OpenFromFile("C:\\Users\\JabinGuo\\Desktop\\dwg\\test.mdb", 0);
            IFeatureDataset        k         = a.OpenFeatureDataset("db");
            TopologyChecker        topocheck = new TopologyChecker(k);

            topocheck.PUB_TopoBuild("addinTopo");
            topocheck.PUB_AddFeatureClass(null);
            List <IFeatureClass> test = topocheck.PUB_GetAllFeatureClass();

            IFeatureClass ifc = test[0];
            topocheck.PUB_AddRuleToTopology(TopologyChecker.TopoErroType.线要素必须不相交, test[0]);
#endif
            #endregion
        }