Exemple #1
0
        /// <summary>
        /// 删除几何形状指定的顶点。
        /// 顶点的会与几何形状的顶点进行IPoint::Compare比较,
        /// 因此顶点必须是通过几何形状的顶点获取,才能成功删除。
        /// 如果删除的结果导致几何形状为空,则几何形状接口不为空,只是setEmpty。
        /// </summary>
        /// <param name="geometry">需要删除顶点的ESRI几何形状接口</param>
        /// <param name="point">指定要删除的顶点ESRI点接口</param>
        /// <returns>是否成功删除</returns>
        public static bool GeometryRemoveVertex(IGeometry geometry, IPoint point)
        {
            bool deleted = false;

            if (IsValidGeometry(geometry) && IsValidGeometry(point))
            {
                switch (geometry.GeometryType)
                {
                case esriGeometryType.esriGeometryPoint:

                    #region 点几何形状

                    if (geometry is IPoint)
                    {
                        if ((geometry as IPoint).Compare(point) == 0)
                        {
                            geometry.SetEmpty();
                            deleted = true;
                        }
                    }

                    #endregion

                    break;

                case esriGeometryType.esriGeometryMultipoint:

                    #region 多点几何形状

                    if (geometry is IPointCollection)
                    {
                        for (int i = 0; i <= (geometry as IPointCollection).PointCount - 1; i++)
                        {
                            if ((geometry as IPointCollection).get_Point(i).Compare(point) == 0)
                            {
                                (geometry as IPointCollection).RemovePoints(i, 1);
                                deleted = true;
                                break;
                            }
                        }
                    }

                    #endregion

                    break;

                case esriGeometryType.esriGeometryPolyline:

                    #region 多义线几何形状

                    if (geometry is IPointCollection)
                    {
                        for (int i = 0; i <= (geometry as IPointCollection).PointCount - 1; i++)
                        {
                            if ((geometry as IPointCollection).get_Point(i).Compare(point) == 0)
                            {
                                (geometry as IPointCollection).RemovePoints(i, 1);
                                deleted = true;
                                break;
                            }
                        }
                    }

                    #endregion

                    break;

                case esriGeometryType.esriGeometryPolygon:

                    #region 面几何形状

                    if (geometry is IGeometryCollection)
                    {
                        bool removed = false;
                        for (int i = 0; i <= (geometry as IGeometryCollection).GeometryCount - 1; i++)
                        {
                            IGeometry partGeom = (geometry as IGeometryCollection).get_Geometry(i);
                            if (IsValidGeometry(partGeom) && partGeom is IRing && partGeom is IPointCollection)
                            {
                                if ((partGeom as IRing).IsClosed)
                                {
                                    #region 闭合的环(在编辑草图中存在)

                                    if ((partGeom as IRing).FromPoint.Compare(point) == 0 ||
                                        (partGeom as IRing).ToPoint.Compare(point) == 0)
                                    {
                                        if ((partGeom as IPointCollection).PointCount <= 2)
                                        {
                                            partGeom = null;
                                            removed  = true;
                                        }
                                        else
                                        {
                                            IPoint startPoint = (partGeom as IPointCollection).get_Point(1);
                                            (partGeom as IPointCollection).RemovePoints(
                                                (partGeom as IPointCollection).PointCount - 1, 1);
                                            (partGeom as IPointCollection).RemovePoints(0, 1);
                                            //(partGeom as IRing).FromPoint = startPoint;
                                            (partGeom as IRing).Close();
                                            removed = true;
                                        }
                                    }
                                    else
                                    {
                                        for (int j = 0; j <= (partGeom as IPointCollection).PointCount - 1; j++)
                                        {
                                            if ((partGeom as IPointCollection).get_Point(j).Compare(point) == 0)
                                            {
                                                (partGeom as IPointCollection).RemovePoints(j, 1);
                                                removed = true;
                                                break;
                                            }
                                        }
                                    }

                                    #endregion
                                }
                                else
                                {
                                    #region 非闭合的环(在编辑草图中应该不存在)

                                    for (int j = 0; j <= (partGeom as IPointCollection).PointCount - 1; j++)
                                    {
                                        if ((partGeom as IPointCollection).get_Point(j).Compare(point) == 0)
                                        {
                                            (partGeom as IPointCollection).RemovePoints(j, 1);
                                            removed = true;
                                            break;
                                        }
                                    }

                                    #endregion
                                }
                            }
                            if (removed)
                            {
                                (geometry as IGeometryCollection).RemoveGeometries(i, 1);
                                if (IsValidGeometry(partGeom))
                                {
                                    IGeometryCollection geometryCollection = new Multipoint() as IGeometryCollection;
                                    IGeometryBridge     geometryBridge     = new GeometryEnvironment() as IGeometryBridge;
                                    IGeometry[]         geometryArray      = { partGeom };
                                    geometryBridge.InsertGeometries((IGeometryCollection)geometry, i,
                                                                    ref geometryArray);
                                }
                                deleted = true;
                                break;
                            }
                        }
                    }

                    #endregion

                    break;

                default:
                    break;
                }
            }
            return(deleted);
        }