Arcgis engine定位捕捉点

Arcgis engine定位捕捉点

前言
所谓捕捉,即能够及时获得点范围内的最近邻点,实现这个功能AE提供了IHitTest接口,该接口只有一个HitTest方法。
使用HitTest方法获得的捕捉点的类型有如下几种
Arcgis engine定位捕捉点_第1张图片

/// 
        /// 获取捕捉点
        /// 
        /// 
        /// 
        /// 
        /// 
        public static IPoint getSnapPoint(AxMapControl mainMap, IPoint QueryPoint, IFeatureLayer pFeatureLayer,string snapStyle)
        {
            IPoint pHitPoint = null;
            IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;

            ITopologicalOperator pTopologicalOperator = QueryPoint as ITopologicalOperator;
            double bufferLength = convertPixelsToMapUnits(mainMap.ActiveView, 10);
            IGeometry pGeometry = pTopologicalOperator.Buffer(bufferLength).Envelope as IGeometry;

            ISpatialFilter pSpatialFilter = new SpatialFilterClass();
            pSpatialFilter.GeometryField = pFeatureClass.ShapeFieldName;
            pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelCrosses;
            pSpatialFilter.Geometry = pGeometry;

            IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);
            IFeature pFeature=pFeatureCursor.NextFeature();

            IPoint hitPoint=new PointClass();
            double searchRadius = convertPixelsToMapUnits(mainMap.ActiveView, 10);
            double hitDistance = 0;
            int hitPartIndex = 0;
            int hitSegmentIndex = 0;
            bool bRightSide = false;
            ***//关键点在此句,以上都是为得到IHitTest 做准备***
            IHitTest pHitTest = pFeature.Shape as IHitTest;
            while (pFeature != null)
            {                       
                switch (snapStyle)
                {
                    case "顶点":
                        if (pHitTest.HitTest(QueryPoint, searchRadius, esriGeometryHitPartType.esriGeometryPartVertex, hitPoint, hitDistance, hitPartIndex, hitSegmentIndex, bRightSide))
                        {
                            pHitPoint = hitPoint;
                        }
                        break;
                    case "边界点":
                        if (pHitTest.HitTest(QueryPoint, searchRadius, esriGeometryHitPartType.esriGeometryPartBoundary, hitPoint, hitDistance, hitPartIndex, hitSegmentIndex, bRightSide))
                        {
                            pHitPoint = hitPoint;
                            break;
                        }
                        break;
                    case "线中点":
                        if (pHitTest.HitTest(QueryPoint, searchRadius, esriGeometryHitPartType.esriGeometryPartMidpoint, hitPoint, hitDistance, hitPartIndex, hitSegmentIndex, bRightSide))
                        {
                            pHitPoint = hitPoint;
                            break;
                        }
                        break;
                    case "面中心":
                        if (pHitTest.HitTest(QueryPoint, searchRadius, esriGeometryHitPartType.esriGeometryPartCentroid, hitPoint, hitDistance, hitPartIndex, hitSegmentIndex, bRightSide))
                        {
                            pHitPoint = hitPoint;
                            break;
                        }
                        break;
                    case "端点":
                        if (pHitTest.HitTest(QueryPoint, searchRadius, esriGeometryHitPartType.esriGeometryPartEndpoint, hitPoint, hitDistance, hitPartIndex, hitSegmentIndex, bRightSide))
                        {
                            pHitPoint = hitPoint;
                            break;
                        }
                        break;

                }
                if (pHitPoint != null)
                    break;
                pFeature = pFeatureCursor.NextFeature();
                pHitTest = pFeature.Shape as IHitTest;
            }
            return pHitPoint;
        }
        /// 
        /// 像素个数转为地图上数字
        /// 
        /// 
        /// 
        /// 
        public static double convertPixelsToMapUnits(IActiveView pActiveView, double pixelUnits)
        {
            double realWorldDisplayExtent;
            int pixelExtent;
            double sizeOfOnePixel;
            pixelExtent = pActiveView.ScreenDisplay.DisplayTransformation.get_DeviceFrame().right - pActiveView.ScreenDisplay.DisplayTransformation.get_DeviceFrame().left;
            realWorldDisplayExtent = pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.Width;
            sizeOfOnePixel = realWorldDisplayExtent / pixelExtent;
            return pixelUnits * sizeOfOnePixel;
        }

你可能感兴趣的:(Arcgis,engine)