Пример #1
0
        //工程设置添加文件
        private void addFile11(TextBox tx, string tablename)
        {
            IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass();
            string            GdbPath          = Application.StartupPath + "\\GISData.gdb";
            string            MdbPath          = Application.StartupPath + "\\GISData.mdb";
            IWorkspaceFactory pWks             = new AccessWorkspaceFactoryClass();
            IWorkspace        pFwk             = pWks.OpenFromFile(MdbPath, 0) as IWorkspace;

            IGxDialog dlg = new GxDialog();
            IGxObjectFilterCollection filterCollection = dlg as IGxObjectFilterCollection;

            filterCollection.AddFilter(new GxFilterFeatureClasses(), true);
            IEnumGxObject enumObj;

            dlg.AllowMultiSelect = true;
            dlg.Title            = "添加数据";
            dlg.DoModalOpen(0, out enumObj);
            if (enumObj != null)
            {
                enumObj.Reset();
                int       a     = 0;
                IGxObject gxObj = enumObj.Next();
                while (gxObj != null)
                {
                    Console.WriteLine(a++);
                    if (gxObj is IGxDataset)
                    {
                        IGxDataset gxDataset = gxObj as IGxDataset;
                        IDataset   pDataset  = gxDataset.Dataset;
                        switch (pDataset.Type)
                        {
                        case esriDatasetType.esriDTFeatureClass:
                            IFeatureClass pFc = pDataset as IFeatureClass;

                            ISpatialReference pSpatialReference = (pFc as IGeoDataset).SpatialReference;    //空间参考
                            CommonClass       common            = new CommonClass();
                            if (pSpatialReference.Name != common.GetConfigValue("SpatialReferenceName"))
                            {
                                MessageBox.Show("空间参考错误");
                                break;
                            }
                            else
                            {
                                tx.Text = pDataset.BrowseName;
                                tx.Name = pDataset.BrowseName;
                            }
                            break;

                        case esriDatasetType.esriDTFeatureDataset:
                            IFeatureDataset pFeatureDs = pDataset as IFeatureDataset;
                            //do anyting you like
                            break;

                        case esriDatasetType.esriDTRasterDataset:
                            IRasterDataset rasterDs = pDataset as IRasterDataset;
                            //do anyting you like
                            break;

                        case esriDatasetType.esriDTTable:
                            ITable pTable = pDataset as ITable;
                            //do anyting you like
                            break;

                        case esriDatasetType.esriDTTin:
                            ITin pTin = pDataset as ITin;
                            //do anyting you like
                            break;

                        case esriDatasetType.esriDTRasterCatalog:
                            IRasterCatalog pCatalog = pDataset as IRasterCatalog;
                            //do anyting you like
                            break;

                        default:
                            break;
                        }
                    }
                    else if (gxObj is IGxLayer)
                    {
                        IGxLayer gxLayer = gxObj as IGxLayer;
                        ILayer   pLayer  = gxLayer.Layer;
                        break;
                        //do anything you like
                    }
                    gxObj = enumObj.Next();
                }
            }
        }
Пример #2
0
        //工程设置添加文件
        private void addFile(TextBox tx, string tablename)
        {
            try
            {
                IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass();

                IGxDialog dlg = new GxDialog();
                IGxObjectFilterCollection filterCollection = dlg as IGxObjectFilterCollection;
                filterCollection.AddFilter(new GxFilterFeatureClasses(), true);
                IEnumGxObject enumObj;
                dlg.AllowMultiSelect = true;
                dlg.Title            = "添加数据";
                dlg.DoModalOpen(0, out enumObj);
                if (enumObj != null)
                {
                    enumObj.Reset();
                    int       a     = 0;
                    IGxObject gxObj = enumObj.Next();
                    while (gxObj != null)
                    {
                        Console.WriteLine(a++);
                        if (gxObj is IGxDataset)
                        {
                            IGxDataset gxDataset = gxObj as IGxDataset;
                            IDataset   pDataset  = gxDataset.Dataset;
                            switch (pDataset.Type)
                            {
                            case esriDatasetType.esriDTFeatureClass:
                                IFeatureClass pFc = pDataset as IFeatureClass;

                                ISpatialReference pSpatialReference = (pFc as IGeoDataset).SpatialReference;    //空间参考
                                CommonClass       common            = new CommonClass();
                                if (pSpatialReference.Name != common.GetConfigValue("SpatialReferenceName"))
                                {
                                    MessageBox.Show("空间参考错误");
                                    break;
                                }
                                else
                                {
                                    string layerType = "";
                                    if (pDataset.Category.Contains("个人地理数据库") || pDataset.Category.Contains("Personal"))
                                    {
                                        layerType = "Access数据库";
                                    }
                                    else if (pDataset.Category.Contains("文件地理数据库") || pDataset.Category.Contains("File"))
                                    {
                                        layerType = "文件夹数据库";
                                    }

                                    IFields fields = pFc.Fields;
                                    Dictionary <string, List <string> > dicCustom = new Dictionary <string, List <string> >();
                                    Dictionary <string, List <string> > dicSys    = new Dictionary <string, List <string> >();

                                    ConnectDB db = new ConnectDB();
                                    DataTable dt = db.GetDataBySql("select FIELD_NAME,DATA_TYPE,MAXLEN from GISDATA_MATEDATA where REG_NAME  = '" + tablename + "'");
                                    DataRow[] dr = dt.Select(null);

                                    string errorString = "";

                                    for (int i = 0; i < dr.Length; i++)
                                    {
                                        string        FIELD_NAME = dr[i]["FIELD_NAME"].ToString();
                                        string        DATA_TYPE  = dr[i]["DATA_TYPE"].ToString();
                                        string        MAXLEN     = dr[i]["MAXLEN"].ToString();
                                        List <string> list1      = new List <string>();
                                        list1.Add(DATA_TYPE);
                                        list1.Add(MAXLEN);
                                        dicSys.Add(FIELD_NAME, list1);
                                    }

                                    for (int i = 0; i < fields.FieldCount; i++)
                                    {
                                        IField field = fields.get_Field(i);
                                        if (field.Name != pFc.ShapeFieldName && field.Name != pFc.OIDFieldName)
                                        {
                                            List <string> list1 = new List <string>();
                                            list1.Add(field.Type.ToString());
                                            list1.Add(field.Length.ToString());
                                            dicCustom.Add(field.Name.ToString(), list1);
                                            if (dicSys.ContainsKey(field.Name))
                                            {
                                                if (dicSys[field.Name][0] != field.Type.ToString())
                                                {
                                                    errorString += "字段类型错误:" + field.Name + "(" + dicSys[field.Name][0] + ");\r\n";
                                                }
                                                else if (dicSys[field.Name][1] != field.Length.ToString())
                                                {
                                                    errorString += "字段长度错误:" + field.Name + "(" + dicSys[field.Name][0] + ");\r\n";
                                                }
                                            }
                                            else
                                            {
                                                errorString += "多余字段:" + field.Name + ";\r\n";
                                            }
                                        }
                                    }

                                    foreach (KeyValuePair <string, List <string> > itemList in dicSys)
                                    {
                                        if (!dicCustom.ContainsKey(itemList.Key))
                                        {
                                            errorString += "缺少字段:" + itemList.Key + ";\r\n";
                                        }
                                    }

                                    if (errorString != "")
                                    {
                                        FormMessage message = new FormMessage("提示", errorString);
                                        message.ShowDialog();
                                        //MessageBox.Show(errorString);
                                    }
                                    else
                                    {
                                        string path = pDataset.Workspace.PathName.Replace(@"\", @"\\");
                                        Console.WriteLine(path);
                                        Boolean result = db.Update("update GISDATA_REGINFO set PATH= '" + path + "',DBTYPE = '" + layerType + "',TABLENAME = '" + pDataset.Name + "' where REG_NAME = '" + tablename + "'");
                                        common.updateXmlNode(tablename, path, layerType, pDataset.Name);
                                        tx.Text = pDataset.BrowseName;
                                        tx.Name = pDataset.BrowseName;
                                    }
                                }
                                break;

                            default:
                                break;
                            }
                        }
                        else if (gxObj is IGxLayer)
                        {
                            IGxLayer gxLayer = gxObj as IGxLayer;
                            ILayer   pLayer  = gxLayer.Layer;
                            break;
                            //do anything you like
                        }
                        gxObj = enumObj.Next();
                    }
                }
            }
            catch (Exception e)
            {
                LogHelper.WriteLog(typeof(FormMain), e);
            }
        }