Example #1
0
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (Validate(true) == false)
            {
                return;
            }
            string locType = label1.Tag != null?label1.Tag.ToString() : "";

            if (string.IsNullOrEmpty(locType))
            {
                return;
            }
            object          createLoc;
            IWorkspace2     workspace2 = null;
            IObjectTemplate template   = null;

            if (_isSingle)
            {
                template = _template;
            }
            else
            {
                template = _plugin.TemplateDatabase.Templates.FirstOrDefault(c => c.Name == cmbTemplate.SelectedItem.ToString());
            }

            if (template == null)
            {
                return;
            }
            if (locType.Contains("Dataset"))
            {
                IGxDataset      pDataset = txtDB.Tag as IGxDataset;
                IFeatureDataset fDataset = ((IGxObject)pDataset).InternalObjectName.Open();
                workspace2 = fDataset.Workspace as IWorkspace2;
                createLoc  = fDataset;
            }
            else
            {
                IGxDatabase       pDatabase = txtDB.Tag as IGxDatabase;
                IFeatureWorkspace workspace = ((IGxObject)pDatabase).InternalObjectName.Open();
                workspace2 = workspace as IWorkspace2;
                createLoc  = workspace;
            }

            if (createLoc == null)
            {
                return;
            }
            string      fcName     = txtName.EditValue.ToString().Trim();
            IWorkspace2 workSpace2 = createLoc is IFeatureDataset
                ? ((IFeatureDataset)createLoc).Workspace as IWorkspace2
                : createLoc as IWorkspace2;

            if (workSpace2.NameExists[esriDatasetType.esriDTFeatureClass, fcName])
            {
                MessageService.Current.Warn("该名称已经存在,请重新输入!");
                return;
            }
            ISpatialReference pSpatialReference = _map.SpatialReference;

            IFieldsEdit pFieldsEdit = new Fields() as IFieldsEdit;
            IField      pField      = FieldHelper.CreateOIDField();

            pFieldsEdit.AddField(pField);
            if (locType.Contains("Dataset"))
            {
                IGeoDataset pFDataset = createLoc as IGeoDataset;
                pField            = FieldHelper.CreateGeometryField(template.GeometryType, pFDataset.SpatialReference);
                pSpatialReference = pFDataset.SpatialReference;
                pFieldsEdit.AddField(pField);
            }
            else
            {
                pField = FieldHelper.CreateGeometryField(template.GeometryType, _map.SpatialReference);
                pFieldsEdit.AddField(pField);
            }
            string keyName = "";

            foreach (YTField ytField in template.Fields)
            {
                pField = ytField.CreateField();
                pFieldsEdit.AddField(pField);
                if (ytField.IsKey)
                {
                    keyName = ytField.Name;
                }
            }

            IFeatureClass pClass = WorkspaceOperator.CreateFeatureClass(createLoc, txtName.Text, pSpatialReference, template.FeatureType,
                                                                        template.GeometryType, (IFields)pFieldsEdit, null, null, "");

            if (pClass == null)
            {
                MessageService.Current.Info("创建失败!");
                return;
            }
            if (pClass != null && chkIndex.Checked == false)
            {
                MapHelper.AddFeatureLayer((IBasicMap)_map, pClass);
                MessageService.Current.Info("创建成功并已经加载图层!");
                DialogResult = DialogResult.OK;
                return;
            }
            IEnvelope pEnv = new Envelope() as IEnvelope;

            pEnv.PutCoords(Convert.ToDouble(txtXMin.Text), Convert.ToDouble(txtYMin.Text), Convert.ToDouble(txtXMax.Text), Convert.ToDouble(txtYMax.Text));

            IWorkspaceEdit pWksEdit = ((IDataset)pClass).Workspace as IWorkspaceEdit;

            pWksEdit.StartEditing(false);
            pWksEdit.StartEditOperation();
            IndexHelper.CreateGridIndex(pClass, pEnv, Convert.ToDouble(txtWidth.Text),
                                        Convert.ToDouble(txtHeight.Text), keyName);
            pWksEdit.StopEditOperation();
            pWksEdit.StopEditing(true);
            MapHelper.AddFeatureLayer((IBasicMap)_map, pClass);
            MessageService.Current.Info("创建成功并已经加载图层!");
            DialogResult = DialogResult.OK;
        }