Ejemplo n.º 1
0
        private static bool DealWithCurrTwoCurvres(ref Curve currCurve, ref Curve preCurve, out Line tempLine, double tolerance)
        {
            bool isMovedCurrCurve = false;

            tempLine = null;

            if (preCurve.GetEndPoint(1).IsAlmostEqualTo(currCurve.GetEndPoint(0)))
            {//首尾相连,不需要处理
            }
            else
            {//需要处理
                bool isParallel = CurveUtils.IsAlmostParallelToByAngle(preCurve, currCurve);
                if (isParallel)
                {//平行,求距离,距离小于300mm,将第二条直线移动到第一条直线的延长线上;否则直接相连
                    double distance = preCurve.DistanceTo(currCurve);
                    if (distance <= tolerance /*&& distance > 1e-6*/)
                    {
                        isMovedCurrCurve = true;
                        MoveCurrCurveAndExtend(ref currCurve, preCurve, distance);
                    }
                    else
                    {
                        //直接相连
                        var pts = CurveUtils.ComputeClosestEndPoints(preCurve, currCurve);
                        if (!pts[0].IsAlmostEqualTo(pts[1]))
                        {
                            tempLine = Line.CreateBound(pts[0], pts[1]);
                        }
                    }
                }
                else
                {//不平行,延长两条线,求交点
                    ExtendTwoCurves(ref currCurve, ref preCurve);
                }
            }

            return(isMovedCurrCurve);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 检查是否首位相连,如果不首位相连,则将他以一定的规则首尾相连
        /// </summary>
        /// <param name="loop"></param>
        /// <param name="tolerance"></param>
        /// <returns>返回首位相连的Loop</returns>
        /// <remarks>
        /// 顺序遍历前后连条直线,如果不首位相连,则
        /// 1.如果他们不平行,延长他们使之相交
        /// 2.如果平行,检查距离是否大于给定tolerance:如果大于,在他们之间添加直线使之连接;如果小于,则平移后一条直线使之在前面一条直线的延长线上,在用直线相连
        /// </remarks>
        public static CurveLoop ValidateCurveLoop(this CurveLoop loop, double tolerance = 1 *MathHelper.Mm2Feet)
        {
            List <Curve> lines = new List <Curve>();

            var           enumerator = loop.GetEnumerator();
            Queue <Curve> queue      = new Queue <Curve>(2);

            while (enumerator.MoveNext())
            {
                Curve currCurve = enumerator.Current;
                queue.Enqueue(currCurve);

                if (queue.Count != 2)
                {
                    continue;
                }

                Curve preCurve = queue.Dequeue();
                //处理当前两条直线
                DealWithCurrTwoCurvres(ref currCurve, ref preCurve, out Line tempLine, tolerance);

                lines.Add(preCurve);
                if (tempLine != null)
                {
                    lines.Add(tempLine);
                }

                //更新一个在队列中的Curve
                queue.Dequeue();
                queue.Enqueue(currCurve);
            }

            #region while循环之后 需要判断第一条和最后一条直线的关系
            Curve lastCurve  = queue.Dequeue();
            Curve firstCurve = lines.First();
            bool  isMoved2   = DealWithCurrTwoCurvres(ref firstCurve, ref lastCurve, out Line tempLine2, tolerance);

            lines.Add(lastCurve);
            if (tempLine2 != null)
            {
                lines.Add(tempLine2);
            }
            //把firstCurve替换了
            lines.RemoveAt(0);
            lines.Insert(0, firstCurve);

            //如果移动过currCurve(firstCurve)
            if (isMoved2)
            {
                List <Curve> parallelFirstCurves   = new List <Curve>(); //第一条线 后面(相邻的) 和第一条线平行的线
                Curve        notParallelFirstCurve = null;               //第一条线 后面 第一个和他不平行的线
                for (int i = 1; i < lines.Count; i++)
                {
                    var tempLine = lines[i--];
                    lines.Remove(tempLine);

                    if (CurveUtils.IsAlmostParallelTo(firstCurve, tempLine))
                    {
                        parallelFirstCurves.Add(tempLine);
                        continue;
                    }
                    else
                    {
                        notParallelFirstCurve = tempLine;
                        break;
                    }
                }

                if (parallelFirstCurves.Count == 0)
                {
                    ExtendTwoCurves(ref notParallelFirstCurve, ref firstCurve);
                    lines.Insert(1, notParallelFirstCurve);
                }
                else
                {//先把所有和第一条直线平行的线,平移一定距离
                    double distance  = firstCurve.DistanceTo(parallelFirstCurves.First());
                    XYZ    dir       = parallelFirstCurves.First().GetNormalInXoy(firstCurve.GetEndPoint(0));
                    var    transform = Transform.CreateTranslation(dir * distance);

                    for (int i = 0; i < parallelFirstCurves.Count; i++)
                    {
                        parallelFirstCurves[i] = parallelFirstCurves[i].CreateTransformed(transform);
                    }

                    //自后向前一次重新加入
                    var lastParallelCurve = parallelFirstCurves.Last();
                    ExtendTwoCurves(ref notParallelFirstCurve, ref lastParallelCurve);
                    lines.Insert(1, notParallelFirstCurve);

                    for (int i = parallelFirstCurves.Count - 1; i > -1; i--)
                    {
                        lines.Insert(1, parallelFirstCurves[i]);
                    }
                }
            }
            #endregion

            return(CurveLoop.Create(lines));
        }