多块图形合并(自动合并相交块)

一、背景:

  在PDF上画一系列的块,获取该系列的块,自动分组(自动判断块是否相交,相交则视为一组,独立的块为一组),返回各组块的点集;窗体显示点集路径图标。

  效果:支持 单块、多块(包含多异形块)为一个图形

二、自动分组,返回组块点集合。代码如下:

        #region 一资源多区域锚点

        /// <summary>
        /// 获取锚点区域 支持1到多
        /// </summary>
        /// <param name="annotPosInfo"></param>
        /// <returns></returns>
        public string GetResMultiRegionAnnotPointsInfo(string annotPosInfo)
        {
            string annotPointsInfo = string.Empty;
            //获取所有锚点块信息
            List<AnnotPosModel> lstAnnotPosMdl = GetResAnnotPoses(annotPosInfo);
            if (lstAnnotPosMdl == null || lstAnnotPosMdl.Count <= 0)
            {
                return annotPointsInfo;
            }
            //待处理的锚点块集 ( Y轴、X轴递增排序)
            List<AnnotPosModel> dealAnnotPosMdls = new List<AnnotPosModel>();
            var sortAnnotPosMdls = lstAnnotPosMdl.OrderBy(mdl => mdl.Top).OrderBy(mdl => mdl.Left);
            dealAnnotPosMdls.AddRange(sortAnnotPosMdls);
            //当前处理的锚点块
            AnnotPosModel curAnnotPosMdl = dealAnnotPosMdls.First();
            dealAnnotPosMdls.Remove(curAnnotPosMdl);
            //相交锚点块集
            List<AnnotPosModel> relAnnotPosModels = new List<AnnotPosModel>();
            relAnnotPosModels.Add(curAnnotPosMdl);
            annotPointsInfo = GetResMultiRegionAnnotPointsInfo(dealAnnotPosMdls, relAnnotPosModels, curAnnotPosMdl);
            return annotPointsInfo;
        }

        /// <summary>
        /// 获取锚点区域 支持1到多
        /// </summary>
        /// <param name="annotPosInfo"></param>
        /// <returns></returns>
        public List<string> GetResMultiRegionAnnotPointsInfo2(string annotPosInfo)
        {
            List<string> lstAnnotPointsInfo = new List<string>();
            string annotPointsInfo = string.Empty;
            //获取所有锚点块信息
            List<AnnotPosModel> lstAnnotPosMdl = GetResAnnotPoses(annotPosInfo);
            if (lstAnnotPosMdl == null || lstAnnotPosMdl.Count <= 0)
            {
                return lstAnnotPointsInfo;
            }
            //待处理的锚点块集 ( Y轴、X轴递增排序)
            List<AnnotPosModel> dealAnnotPosMdls = new List<AnnotPosModel>();
            var sortAnnotPosMdls = lstAnnotPosMdl.OrderBy(mdl => mdl.Top).OrderBy(mdl => mdl.Left);
            dealAnnotPosMdls.AddRange(sortAnnotPosMdls);
            //当前处理的锚点块
            AnnotPosModel curAnnotPosMdl = dealAnnotPosMdls.First();
            dealAnnotPosMdls.Remove(curAnnotPosMdl);
            //相交锚点块集
            List<AnnotPosModel> relAnnotPosModels = new List<AnnotPosModel>();
            relAnnotPosModels.Add(curAnnotPosMdl);
            lstAnnotPointsInfo = this.GetResMultiRegionAnnotPointsInfo2(dealAnnotPosMdls, relAnnotPosModels, curAnnotPosMdl);
            return lstAnnotPointsInfo;
        }

        /// <summary>
        /// 获取锚点区域 支持1到多
        /// </summary>
        /// <param name="dealAnnotPosMdls"></param>
        /// <param name="relAnnotPosModels"></param>
        /// <param name="curAnnotPosMdl"></param>
        /// <returns></returns>
        private string GetResMultiRegionAnnotPointsInfo(List<AnnotPosModel> dealAnnotPosMdls , List<AnnotPosModel> relAnnotPosModels, AnnotPosModel curAnnotPosMdl)
        {
            string annotPointsInfo = string.Empty;
            if ((dealAnnotPosMdls == null ) || (relAnnotPosModels == null) || (curAnnotPosMdl == null))
            {
                LogHelper.Instance.Error("获取一资源多锚点区域点集合失败(GetResMultiRegionAnnotPointsInfo)。" );
                return annotPointsInfo;
            }
            //设置当前锚点相交区域内的锚点块集
            this.SetAnnotRelRegionAnnotPoints(dealAnnotPosMdls, relAnnotPosModels, curAnnotPosMdl);
            //仍存在待处理的锚点块,则遍历已相交的链表
            while (dealAnnotPosMdls.Count > 0)
            {
                int curIndex = relAnnotPosModels.FindIndex(mdl => mdl == curAnnotPosMdl);
                int nextIndex = curIndex + 1;
                if (relAnnotPosModels.Count == nextIndex)   //遍历已相交的链表完毕
                {
                    if (!string.IsNullOrEmpty(annotPointsInfo))
                    {
                        annotPointsInfo += "&&";
                    }
                    //球场相交锚点点集合信息
                    annotPointsInfo += this.GetMergeAnnotPointsInfo(relAnnotPosModels);
                    //设置下一块相交区域
                    curAnnotPosMdl = dealAnnotPosMdls.First(); //相交锚点块集
                    dealAnnotPosMdls.Remove(curAnnotPosMdl);
                    relAnnotPosModels = new List<AnnotPosModel>();
                    relAnnotPosModels.Add(curAnnotPosMdl);
                    //设置当前锚点相交区域内的锚点块集
                    this.SetAnnotRelRegionAnnotPoints(dealAnnotPosMdls, relAnnotPosModels, curAnnotPosMdl);
                }
                else
                {
                    curAnnotPosMdl = relAnnotPosModels[nextIndex];
                    ////设置当前锚点相交区域内的锚点块集
                    this.SetAnnotRelRegionAnnotPoints(dealAnnotPosMdls, relAnnotPosModels, curAnnotPosMdl);
                }
            }
            //else
            {
                //输出信息退出
                if (!string.IsNullOrEmpty(annotPointsInfo))
                {
                    annotPointsInfo += "&&";
                }
                annotPointsInfo += this.GetMergeAnnotPointsInfo(relAnnotPosModels); 
            }

            return annotPointsInfo;
        }


        /// <summary>
        /// 获取锚点区域 支持1到多
        /// </summary>
        /// <param name="dealAnnotPosMdls"></param>
        /// <param name="relAnnotPosModels"></param>
        /// <param name="curAnnotPosMdl"></param>
        /// <returns></returns>
        private List<string> GetResMultiRegionAnnotPointsInfo2(List<AnnotPosModel> dealAnnotPosMdls, List<AnnotPosModel> relAnnotPosModels, AnnotPosModel curAnnotPosMdl)
        {
            List<string> lstAnnotPointsInfo = new List<string>();
            if ((dealAnnotPosMdls == null) || (relAnnotPosModels == null) || (curAnnotPosMdl == null))
            {
                LogHelper.Instance.Error("获取一资源多锚点区域点集合失败(GetResMultiRegionAnnotPointsInfo)。");
                return null;
            }
            //设置当前锚点相交区域内的锚点块集
            this.SetAnnotRelRegionAnnotPoints(dealAnnotPosMdls, relAnnotPosModels, curAnnotPosMdl);
            //仍存在待处理的锚点块,则遍历已相交的链表
            while (dealAnnotPosMdls.Count > 0)
            {
                int curIndex = relAnnotPosModels.FindIndex(mdl => mdl == curAnnotPosMdl);
                int nextIndex = curIndex + 1;
                if (relAnnotPosModels.Count == nextIndex)   //遍历已相交的链表完毕
                {
                    //求相交锚点点集合信息
                    string annotPointsInfo = this.GetMergeAnnotPointsInfo(relAnnotPosModels);
                    if (!string.IsNullOrEmpty(annotPointsInfo))
                    {
                        lstAnnotPointsInfo.Add(annotPointsInfo);
                    }
                    //设置下一块相交区域
                    curAnnotPosMdl = dealAnnotPosMdls.First(); //相交锚点块集
                    dealAnnotPosMdls.Remove(curAnnotPosMdl);
                    relAnnotPosModels = new List<AnnotPosModel>();
                    relAnnotPosModels.Add(curAnnotPosMdl);
                    //设置当前锚点相交区域内的锚点块集
                    this.SetAnnotRelRegionAnnotPoints(dealAnnotPosMdls, relAnnotPosModels, curAnnotPosMdl);
                }
                else
                {
                    curAnnotPosMdl = relAnnotPosModels[nextIndex];
                    ////设置当前锚点相交区域内的锚点块集
                    this.SetAnnotRelRegionAnnotPoints(dealAnnotPosMdls, relAnnotPosModels, curAnnotPosMdl);
                }
            }
            //else
            {
                string annotPointsInfo = this.GetMergeAnnotPointsInfo(relAnnotPosModels);
                if (!string.IsNullOrEmpty(annotPointsInfo))
                {
                    lstAnnotPointsInfo.Add(annotPointsInfo);
                }
            }

            return lstAnnotPointsInfo;
        }


        /// <summary>
        /// 设置锚点相交的区域点块集
        /// </summary>
        /// <param name="dealAnnotPosMdls"></param>
        /// <param name="relAnnotPosModels"></param>
        /// <param name="curAnnotPosMdl"></param>
        private void SetAnnotRelRegionAnnotPoints(List<AnnotPosModel> dealAnnotPosMdls, List<AnnotPosModel> relAnnotPosModels, AnnotPosModel curAnnotPosMdl)
        {
            if((dealAnnotPosMdls == null ) || (relAnnotPosModels == null) || (curAnnotPosMdl == null))
            {
                return;
            }
            //遍历剩下待处理的所有锚点块
            foreach (AnnotPosModel annotPosMdl in dealAnnotPosMdls)
            {
                //检查是否相交
                if (AnnotsIsIntersect(curAnnotPosMdl, annotPosMdl))
                {
                    relAnnotPosModels.Add(annotPosMdl);
                }
            }
            //待处理的锚点块集中移除相交的锚点块
            foreach (AnnotPosModel relAnnotPosMdl in relAnnotPosModels)
            {
                dealAnnotPosMdls.Remove(relAnnotPosMdl);
            }
        }

        /// <summary>
        /// 判断两个锚点块是否相交
        /// </summary>
        /// <param name="annotPosMdl1"></param>
        /// <param name="annotPosMdl2"></param>
        /// <returns></returns>
        public bool AnnotsIsIntersect(AnnotPosModel annotPosMdl1, AnnotPosModel annotPosMdl2)
        {
            bool isIntersect = false;
            //上下区分
            AnnotPosModel topAnnotPosMdl = annotPosMdl1;
            AnnotPosModel downAnnotPosMdl = annotPosMdl2;
            if(topAnnotPosMdl.Top < downAnnotPosMdl.Top)
            {
                topAnnotPosMdl = annotPosMdl2;
                downAnnotPosMdl = annotPosMdl1;
            }
            //是否相交
            if(downAnnotPosMdl.Top <= topAnnotPosMdl.Top && downAnnotPosMdl.Top >= topAnnotPosMdl.Bottom && 
                ((downAnnotPosMdl.Left >= topAnnotPosMdl.Left &&downAnnotPosMdl.Left<=topAnnotPosMdl.Right) ||
                (downAnnotPosMdl.Right >= topAnnotPosMdl.Left && downAnnotPosMdl.Right <= topAnnotPosMdl.Right)
                ))
            {
                isIntersect = true;
            }
            return isIntersect;
        }

        
        #endregion
View Code

  辅助代码:

  

        #region 异形锚点位置 手动计算

        /// <summary>
        /// 获取异形锚点点集合信息
        /// </summary>
        /// <param name="annotPosInfo">锚点位置信息</param>
        /// <returns></returns>
        public string GetResAnnotPointsInfo(string annotPosInfo)
        {
            string annotPointsInfo = string.Empty;
            List<AnnotPosModel> lstAnnotPosMdl = GetResAnnotPoses(annotPosInfo);
            annotPointsInfo = this.GetResAnnotPointsInfo(lstAnnotPosMdl);   //3个以下手动计算得到
            //this.GetMergeAnnotPointsInfo(lstAnnotPosMdl);      //直接通过C#程序处理多个锚点;
            return annotPointsInfo;
        }

        /// <summary>
        /// 获取异形锚点点集合
        /// </summary>
        /// <param name="lstAnnotPosMdl">锚点位置集</param>
        /// <returns></returns>
        public string GetResAnnotPointsInfo(List<AnnotPosModel> lstAnnotPosMdl)
        {
            string annotPointsInfo = string.Empty;
            int annotCount = lstAnnotPosMdl.Count;
            switch (annotCount)
            {
                case 0:
                    return string.Empty;
                case 1:
                    annotPointsInfo = this.GetAnnotPointsInfo(lstAnnotPosMdl[0]);
                    break;
                case 2:
                    annotPointsInfo = this.Get2AnnotMergePointsInfo(lstAnnotPosMdl[0], lstAnnotPosMdl[1]);
                    break;
                case 3:
                    //annotPointsInfo = this.Get3AnnotMergePointsInfo(lstAnnotPosMdl);
                    annotPointsInfo = this.GetMergeAnnotPointsInfo(lstAnnotPosMdl);
                    break;
                default:
                    annotPointsInfo = this.GetMergeAnnotPointsInfo(lstAnnotPosMdl);      //处理多个锚点
                    break;
            }
            return annotPointsInfo;
        }

        /// <summary>
        /// 获取资源锚点位置集
        /// </summary>
        /// <param name="annotPosInfo"></param>
        /// <returns></returns>
        public List<AnnotPosModel> GetResAnnotPoses(string annotPosInfo)
        {
            List<AnnotPosModel> lstAnnotPosMdl = new List<AnnotPosModel>();
            string[] arryInfo = annotPosInfo.Split(',');
            int pageNum = 0;
            float left = 0.0f, top = 0.0f, right = 0.0f, bottom = 0.0f;
            if (arryInfo.Length < 6
            || !int.TryParse(arryInfo[1], out pageNum)
            || !MathConverter.TryObjectToFloat(arryInfo[2], out left)   //解决区域、语言变更引起的“识别不出小数点问题”如:转换时“123.456”转换时不认识"."
            || !MathConverter.TryObjectToFloat(arryInfo[3], out top)
            || !MathConverter.TryObjectToFloat(arryInfo[4], out right)
            || !MathConverter.TryObjectToFloat(arryInfo[5], out bottom))
            {
                LogHelper.Instance.Error("锚点位置错误:" + annotPosInfo);
                return lstAnnotPosMdl;
            }
            //添加主锚点
            AnnotPosModel annotPosMdl = new AnnotPosModel();
            annotPosMdl.PageNum = pageNum;
            annotPosMdl.Left = left;
            annotPosMdl.Top = top;
            annotPosMdl.Right = right;
            annotPosMdl.Bottom = bottom;
            lstAnnotPosMdl.Add(annotPosMdl);
            //添加追加锚点
            if (arryInfo.Length > 6)
            {
                int moreAnnots = (arryInfo.Length - 6) / 4;
                for (int i = 0; i < moreAnnots; i++)
                {
                    int iposIndex = i * 4 + 6;
                    //float left, top, right, bottom;
                    if (!float.TryParse(arryInfo[iposIndex], out left)
                        || !float.TryParse(arryInfo[iposIndex + 1], out top)
                        || !float.TryParse(arryInfo[iposIndex + 2], out right)
                        || !float.TryParse(arryInfo[iposIndex + 3], out bottom))
                    {
                        continue;
                    }
                    annotPosMdl = new AnnotPosModel();
                    annotPosMdl.PageNum = pageNum;
                    annotPosMdl.Left = left;
                    annotPosMdl.Top = top;
                    annotPosMdl.Right = right;
                    annotPosMdl.Bottom = bottom;
                    lstAnnotPosMdl.Add(annotPosMdl);
                }
            }
            return lstAnnotPosMdl;
        }

        /// <summary>
        /// 获取锚点的点集合
        /// </summary>
        /// <param name="annotPosMdl"></param>
        /// <returns></returns>
        public string GetAnnotPointsInfo(AnnotPosModel annotPosMdl)
        {
            string annotPointsInfo = string.Empty;
            float left = annotPosMdl.Left;
            float top = annotPosMdl.Top;
            float right = annotPosMdl.Right;
            float bottom = annotPosMdl.Bottom;
            annotPointsInfo = left + "," + top + ";" + right + "," + top + ";" + right + "," + bottom + ";" + left + "," + bottom + ";";
            return annotPointsInfo;
        }

        /// <summary>
        /// 获取锚点的点集合
        /// </summary>
        /// <param name="topAnnotPosMdl"></param>
        /// <returns></returns>
        public string Get2AnnotMergePointsInfo(AnnotPosModel annotPosMdl1, AnnotPosModel annotPosMdl2)
        {
            AnnotPosModel topAnnotPosMdl = annotPosMdl1;
            AnnotPosModel bottomAnnotPosMdl = annotPosMdl2;
            if(annotPosMdl1.Top < annotPosMdl2.Top)
            {
                topAnnotPosMdl = annotPosMdl2;
                bottomAnnotPosMdl = annotPosMdl1;
            }
            string annotPointsInfo = string.Empty;
            float midle = (topAnnotPosMdl.Bottom + bottomAnnotPosMdl.Top) / 2.0f;
            float left1 = topAnnotPosMdl.Left;
            float top1 = topAnnotPosMdl.Top;
            float right1 = topAnnotPosMdl.Right;
            float bottom1 = topAnnotPosMdl.Bottom;
            float left2 = bottomAnnotPosMdl.Left;
            float top2 = bottomAnnotPosMdl.Top;
            float right2 = bottomAnnotPosMdl.Right;
            float bottom2 = bottomAnnotPosMdl.Bottom;
            
            if(topAnnotPosMdl.Bottom > bottomAnnotPosMdl.Top )
            {
                bottom1 = midle;
                top2 = midle;
            }
            if (topAnnotPosMdl.Right > bottomAnnotPosMdl.Right)
            {
                annotPointsInfo = left1 + "," + top1 + ";" + right1 + "," + top1 + ";" + right1 + "," + bottom1 + ";" + right2 + "," + bottom1 + ";" + right2 + "," + bottom2 + ";" + left2 + "," + bottom2 + ";" + left2 + "," + top2 + ";" + left1 + "," + top2 + ";";
            }
            else
            {
                annotPointsInfo = left1 + "," + top1 + ";" + right1 + "," + top1 + ";" + right1 + "," + top2 + ";" + right2 + "," + top2 + ";" + right2 + "," + bottom2 + ";" + left2 + "," + bottom2 + ";" + left2 + "," + bottom1 + ";" + left1 + "," + bottom1 + ";";
            }
            return annotPointsInfo;
        }

        /// <summary>
        /// 获取锚点的点集合
        /// </summary>
        /// <param name="topAnnotPosMdl"></param>
        /// <returns></returns>
        private string Get3AnnotMergePointsInfo(List<AnnotPosModel> annotPosMdls)
        {
            string annotPointsInfo = string.Empty;
            if (annotPosMdls.Count < 3)
            {
                return annotPointsInfo;
            }
            List<AnnotPosModel> lstAnnotPosMdl = new List<AnnotPosModel>();
            lstAnnotPosMdl.AddRange(annotPosMdls.OrderByDescending(obj => obj.Top));
            //边界
            float left = 0.0f, top = 0.0f, right = 0.0f, bottom = 0.0f;
            foreach (var posMdl in lstAnnotPosMdl)
            {
                if (posMdl.Left < left || left == 0.0f)
                {
                    left = posMdl.Left;
                }
                if (posMdl.Top > top || top == 0.0f)
                {
                    top = posMdl.Top;
                }
                if (posMdl.Right > right || right == 0.0f)
                {
                    right = posMdl.Right;
                }
                if (posMdl.Bottom < bottom || bottom == 0.0f)
                {
                    bottom = posMdl.Bottom;
                }
            }
            float left1 = lstAnnotPosMdl[0].Left;
            float top1 = lstAnnotPosMdl[0].Top;
            float right1 = lstAnnotPosMdl[0].Right;
            float bottom1 = lstAnnotPosMdl[0].Bottom;

            float left2 = lstAnnotPosMdl[1].Left;
            float top2 = lstAnnotPosMdl[1].Top;
            float right2 = lstAnnotPosMdl[1].Right;
            float bottom2 = lstAnnotPosMdl[1].Bottom;

            float left3 = lstAnnotPosMdl[2].Left;
            float top3 = lstAnnotPosMdl[2].Top;
            float right3 = lstAnnotPosMdl[2].Right;
            float bottom3 = lstAnnotPosMdl[2].Bottom;

            annotPointsInfo = left1 + "," + top1 + ";" + right + "," + top1 + ";" + right + "," + bottom2 + ";" + right3 + "," + bottom2 + ";" + right3 + "," + bottom3 + ";" + left + "," + bottom3 + ";" + left + "," + top2 + ";" + left1 + "," + top2 + ";";
            return annotPointsInfo;
        }

        /// <summary>
        /// 获取合并锚点的点集合信息
        /// </summary>
        /// <returns></returns>
        public List<Point> GetResAnnotPointsByPointsInfo(string annotPointsInfo)
        {
            List<Point> resAnnotPoints = new List<Point>();
            string[] arrPointInfo = annotPointsInfo.Split(';');
            foreach (var pointInfo in arrPointInfo)
            {
                if(string.IsNullOrEmpty(pointInfo))
                {
                    continue;
                }
                string[] arrPosInfo = pointInfo.Split(',');
                Point pt = new Point();
                float x = 0f, y = 0f;
                if (arrPosInfo.Count() == 2 && float.TryParse(arrPosInfo[0], out x) && float.TryParse(arrPosInfo[1], out y))
                {
                    pt.X = x;
                    pt.Y = y;
                }
                resAnnotPoints.Add(pt);
            }
            return resAnnotPoints;
        }

        /// <summary>
        /// 获取合并锚点的点集合信息
        /// </summary>
        /// <returns></returns>
        public List<Point> GetResAnnotPoints(string annotPosInfo)
        {
            string annotPointsInfo = GetResAnnotPointsInfo(annotPosInfo);
            return GetResAnnotPointsByPointsInfo(annotPointsInfo);
        }

        #endregion

        #region 合并锚点 调用合并方法自动合并


        private Path GetMergeAnnotsPath(List<AnnotPosModel> lstAnnotPosMdl)
        {
            Path myPath = new Path();
            if (lstAnnotPosMdl != null && lstAnnotPosMdl.Count > 0)
            {
                int annotsCount = lstAnnotPosMdl.Count;
                // Create a rectangle geometry to add to the Path
                PathGeometry pathGeom = new PathGeometry();
                GeometryGroup myGeometryGroup = new GeometryGroup();
                foreach (AnnotPosModel annotPosMdl in lstAnnotPosMdl)
                {
                    RectangleGeometry myRectGeometry = new RectangleGeometry();
                    myRectGeometry.Rect = new Rect(annotPosMdl.Left, annotPosMdl.Bottom, annotPosMdl.Right - annotPosMdl.Left, annotPosMdl.Top - annotPosMdl.Bottom);
                    pathGeom = GeometryGroup.Combine(pathGeom, myRectGeometry, GeometryCombineMode.Union, Transform.Identity);
                }
                //var pathExample = myGeometryGroup.GetFlattenedPathGeometry();
                myPath.Data = pathGeom;
            }
            return myPath;
        }

        /// <summary>
        /// 获取合并锚点的点集合
        /// </summary>
        /// <param name="myPath"></param>
        private List<Point> GetMergeAnnotPoints(Path myPath)
        {
            List<Point> lstMergeAnnotPoints = new List<Point>();
            PathGeometry g = myPath.Data.GetFlattenedPathGeometry();
            foreach (var f in g.Figures)
                foreach (var s in f.Segments)
                    if (s is PolyLineSegment)
                        foreach (var pt in ((PolyLineSegment)s).Points)
                        {
                            lstMergeAnnotPoints.Add(pt);    //Debug.WriteLine(pt);
                        }
            return lstMergeAnnotPoints;
        }


        /// <summary>
        /// 获取异形锚点点集合
        /// </summary>
        /// <param name="annotPosInfo">锚点位置信息</param>
        /// <returns></returns>
        public List<Point> GetMergeAnnotPoints(string annotPosInfo)
        {
            string annotPointsInfo = string.Empty;
            List<AnnotPosModel> lstAnnotPosMdl = GetResAnnotPoses(annotPosInfo);
            return this.GetMergeAnnotPoints(lstAnnotPosMdl);
        }


        /// <summary>
        /// 获取合并锚点的点集合
        /// </summary>
        /// <param name="myPath"></param>
        public List<Point> GetMergeAnnotPoints(List<AnnotPosModel> lstAnnotPosMdl)
        {
            List<Point> rtnPoints = new List<Point>();
            if (lstAnnotPosMdl != null)
            {
                Path myPath = GetMergeAnnotsPath(lstAnnotPosMdl);
                rtnPoints = GetMergeAnnotPoints(myPath);
            }
            return rtnPoints;
        }

        /// <summary>
        /// 获取合并锚点的点集合信息
        /// </summary>
        /// <returns></returns>
        public string GetMergeAnnotPointsInfo(List<Point> resAnnotPoints)
        {
            string annotPointsInfo = string.Empty;
            foreach (Point annotPoint in resAnnotPoints)
            {
                annotPointsInfo = annotPointsInfo + annotPoint.X + "," + annotPoint.Y + ";";
            }
            return annotPointsInfo;
        }

        /// <summary>
        /// 获取合并锚点的点集合信息
        /// </summary>
        /// <returns></returns>
        public string GetMergeAnnotPointsInfo(List<AnnotPosModel> lstAnnotPosMdl)
        {
            List<Point> resAnnotPoints = this.GetMergeAnnotPoints(lstAnnotPosMdl);
            return this.GetMergeAnnotPointsInfo(resAnnotPoints);
        }

        #endregion
View Code

 

三、界面图标显示

private SDKPointHelper mSDKPointOper;
        private ReadingResModel mReadingResMdl;

        /// <summary>
        ///  福昕SDK
        /// </summary>
        private AxFoxitPDFSDK FoxitPDFSDK { get; set; }

        public WinAnnotOutline(ReadingResModel readingResMdl, AxFoxitPDFSDK foxitPDFSDK)
        {
            InitializeComponent();
            mReadingResMdl = readingResMdl;
            mSDKPointOper = new SDKPointHelper();
            mSDKPointOper.FoxitPDFSDK = foxitPDFSDK;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            System.Drawing.Point ltPoint = mSDKPointOper.GetPageLeftTopScreenPoint(mReadingResMdl.PageIndex);
            this.Left = ltPoint.X;
            this.Top = ltPoint.Y;
            //System.Drawing.RectangleF pageRectangle = mSDKPointOper.GetPageRectangle(mReadingResMdl.PageIndex);
            System.Drawing.Point rbPoint = mSDKPointOper.GetPageRightBottomScreenPoint(mReadingResMdl.PageIndex); 
            this.Height = (double)rbPoint.Y;
            this.Width = (double)rbPoint.X;
            //List<System.Drawing.Point> lstResAnnotPoints = mSDKPointOper.GetResAnnotsScreenPoints(mReadingResMdl.PageIndex,mReadingResMdl.OriTreePos);
            //this.ShowResAnnotsPath(lstResAnnotPoints);
            ////this.ShowResAnnotsPath2(lstResAnnotPoints);
            List<List<System.Drawing.Point>> lstResAnnotPoints = mSDKPointOper.GetMultiRegionResAnnotsScreenPoints(mReadingResMdl.PageIndex,mReadingResMdl.OriTreePos);
            this.ShowResMultiRegionAnnotsPath(lstResAnnotPoints);
        }

        private void ShowResAnnotsPath(List<System.Drawing.Point> lstResAnnotPoints)
        {
            PathGeometry pathGeometry = new PathGeometry();
            Point lastPoint = new Point(0, 0);
            Point firstPoint = new Point(0, 0);
            LineGeometry line = new LineGeometry();
            foreach (var annotPoint in lstResAnnotPoints)
            {
                Point pt = new Point();
                pt.X = annotPoint.X - this.Left + 4;
                pt.Y = annotPoint.Y - this.Top + 4;
                if (lastPoint.X != 0 || lastPoint.Y != 0)
                {
                    line.StartPoint = lastPoint;
                    line.EndPoint = pt;
                    pathGeometry.AddGeometry(line);
                }
                else
                {
                    firstPoint = pt;
                }
                lastPoint = pt;
            }
            line.StartPoint = lastPoint;
            line.EndPoint = firstPoint;
            pathGeometry.AddGeometry(line);
            mResAnnotsPath.Data = pathGeometry;
            //SolidColorBrush mySolidColorBrush = new SolidColorBrush();
            //mySolidColorBrush.Color = Color.FromArgb(255, 204, 204, 255);
            //mResAnnotsPath.Fill = mySolidColorBrush;
        }




        private void ShowResAnnotsPath2(List<System.Drawing.Point> lstResAnnotPoints)
        {
            // Create a Path to be drawn to the screen.
            Path myPath = new Path();
            myPath.Stroke = Brushes.Black;
            myPath.StrokeThickness = 1;
            SolidColorBrush mySolidColorBrush = new SolidColorBrush();
            mySolidColorBrush.Color = Color.FromArgb(255, 204, 204, 255);
            myPath.Fill = mySolidColorBrush;

            PathGeometry pathGeometry = new PathGeometry();
            Point lastPoint = new Point(0, 0);
            Point firstPoint = new Point(0, 0);
            LineGeometry line = new LineGeometry();
            foreach (var annotPoint in lstResAnnotPoints)
            {
                Point pt = new Point();
                pt.X = annotPoint.X - this.Left;
                pt.Y = annotPoint.Y - this.Top;
                if (lastPoint.X != 0 || lastPoint.Y != 0)
                {
                    line.StartPoint = lastPoint;
                    line.EndPoint = pt;
                    pathGeometry.AddGeometry(line);
                }
                else
                {
                    firstPoint = pt;
                }
                lastPoint = pt;
            }
            line.StartPoint = lastPoint;
            line.EndPoint = firstPoint;
            pathGeometry.AddGeometry(line);
            myPath.Data = pathGeometry;
            // Add path shape to the UI.
            StackPanel mainPanel = new StackPanel();
            mainPanel.Children.Add(myPath);
            this.Content = mainPanel;
        }

        private void ShowResMultiRegionAnnotsPath(List<List<System.Drawing.Point>> lstResAnnotPoints)
        {
            PathGeometry pathGeometry = new PathGeometry();
            foreach (var lstResAnnotPoint in lstResAnnotPoints)
            {
                PathGeometry PartPathGeometry = GetPointPathGeometry(lstResAnnotPoint);
                pathGeometry.AddGeometry(PartPathGeometry);
            }
            mResAnnotsPath.Data = pathGeometry;
        }

        private PathGeometry GetPointPathGeometry(List<System.Drawing.Point> lstResAnnotPoint)
        {
            PathGeometry pathGeometry = new PathGeometry();
            Point lastPoint = new Point(0, 0);
            Point firstPoint = new Point(0, 0);
            LineGeometry line = new LineGeometry();
            foreach (var annotPoint in lstResAnnotPoint)
            {
                Point pt = new Point();
                pt.X = annotPoint.X - this.Left + 4;
                pt.Y = annotPoint.Y - this.Top + 4;
                if (lastPoint.X != 0 || lastPoint.Y != 0)
                {
                    line.StartPoint = lastPoint;
                    line.EndPoint = pt;
                    pathGeometry.AddGeometry(line);
                }
                else
                {
                    firstPoint = pt;
                }
                lastPoint = pt;
            }
            line.StartPoint = lastPoint;
            line.EndPoint = firstPoint;
            pathGeometry.AddGeometry(line);
            return pathGeometry;
        }
View Code

 

posted @ 2019-05-31 14:09  慧由心生  阅读(656)  评论(0编辑  收藏  举报