public static ArcModelMini GetArcParametersFromThreePoints(UnitPoint startPoint, UnitPoint midPoint, UnitPoint endPoint) { ArcModelMini arcModel = new ArcModelMini(); arcModel.Clockwise = HitUtil.IsClockwiseByCross(startPoint, midPoint, endPoint); arcModel.Center = HitUtil.CenterPointFrom3Points(startPoint, midPoint, endPoint); arcModel.Radius = (float)HitUtil.Distance(arcModel.Center, startPoint); arcModel.StartAngle = (float)HitUtil.RadiansToDegrees(HitUtil.LineAngleR(arcModel.Center, startPoint, 0)); float EndAngle = (float)HitUtil.RadiansToDegrees(HitUtil.LineAngleR(arcModel.Center, endPoint, 0)); arcModel.SweepAngle = (float)HitUtil.CalAngleSweep(arcModel.StartAngle, EndAngle, arcModel.Clockwise); return(arcModel); }
/// <summary> /// 直线与直线,补偿求交点 /// </summary> /// <param name="p1"></param> /// <param name="p2"></param> /// <param name="p3"></param> /// <param name="indexCur"></param> /// <returns></returns> private static List <UnitPointBulge> CalCompensationsBy2Line(UnitPointBulge p1, UnitPointBulge p2, UnitPointBulge p3, int indexCur, CompensationModel compensationParam, bool isOutside) { var retPoints = new List <UnitPointBulge>(); var cPoints1 = GetCompensationsPoint(p1, p2, compensationParam.Size, isOutside); var cPoints2 = GetCompensationsPoint(p2, p3, compensationParam.Size, isOutside); //如果两点距离在误差范围内,当做同一个点处理 if (HitUtil.Distance(cPoints1.Item2.Point, cPoints2.Item1.Point) < errorRange) { retPoints.Add(new UnitPointBulge(cPoints1.Item2.Point, p2.Bulge, hasMicroConn: p2.HasMicroConn, position: indexCur)); return(retPoints); } var line1 = DrawingOperationHelper.GetLineEquation(cPoints1.Item1.Point, cPoints1.Item2.Point); var line2 = DrawingOperationHelper.GetLineEquation(cPoints2.Item1.Point, cPoints2.Item2.Point); UnitPoint point = DrawingOperationHelper.GetIntersectionPointBy2Line(line1.Item1, line1.Item2, line1.Item3, line2.Item1, line2.Item2, line2.Item3); if (double.IsNaN(point.X) || double.IsNaN(point.Y)) { //平行 retPoints.Add(new UnitPointBulge(cPoints1.Item2.Point, p2.Bulge, hasMicroConn: p2.HasMicroConn, position: indexCur)); return(retPoints); } else { bool pointInLine = HitUtil.IsPointInLine(cPoints1.Item1.Point, cPoints1.Item2.Point, point, errorRange); if (compensationParam.IsSmooth && !pointInLine) { //圆角处理,转换为带弧度的点 bool closewise = HitUtil.IsClockwiseByCross(cPoints1.Item1.Point, cPoints1.Item2.Point, p2.Point); double bulge = BulgeHelper.GetBulgeFromTwoPointsAndCenter(p2.Point, cPoints1.Item2.Point, cPoints2.Item1.Point, closewise); retPoints.Add(new UnitPointBulge(cPoints1.Item2.Point, bulge, hasMicroConn: p2.HasMicroConn, position: -1 - indexCur)); retPoints.Add(new UnitPointBulge(cPoints2.Item1.Point, p2.Bulge, hasMicroConn: p2.HasMicroConn, position: indexCur)); return(retPoints); } else { retPoints.Add(new UnitPointBulge(point, p2.Bulge, hasMicroConn: p2.HasMicroConn, position: indexCur)); return(retPoints); } } }
/// <summary> /// 圆弧到直线,补偿求交点 /// </summary> /// <param name="p1"></param> /// <param name="p2"></param> /// <param name="p3"></param> /// <param name="indexCur"></param> /// <returns></returns> private static List <UnitPointBulge> CalCompensationsByArcLine(UnitPointBulge p1, UnitPointBulge p2, UnitPointBulge p3, int indexCur, CompensationModel compensationParam, bool isOutside) { var retPoints = new List <UnitPointBulge>(); ArcModelMini arc = DrawingOperationHelper.GetArcParametersFromBulge(p1.Point, p2.Point, (float)p1.Bulge); var cPoints = GetCompensationsPoint(p2, p3, compensationParam.Size, isOutside); double r = arc.Clockwise ? arc.Radius - compensationParam.Size : arc.Radius + compensationParam.Size; if (isOutside) { r = arc.Clockwise ? arc.Radius + compensationParam.Size : arc.Radius - compensationParam.Size; } UnitPoint point1 = HitUtil.GetLinePointByDistance(arc.Center, p2.Point, r, true); //如果两点距离在误差范围内,当做同一个点处理 if (HitUtil.Distance(point1, cPoints.Item1.Point) < errorRange) { retPoints.Add(new UnitPointBulge(point1, p2.Bulge, hasMicroConn: p2.HasMicroConn, position: indexCur)); return(retPoints); } var line = DrawingOperationHelper.GetLineEquation(cPoints.Item1.Point, cPoints.Item2.Point); var intersects = DrawingOperationHelper.GetIntersectPointByLineAndCircle(line.Item1, line.Item2, line.Item3, arc.Center, r); var validPoint = new UnitPoint(double.NaN, double.NaN); UnitPoint intersecteArcPoint = new UnitPoint(double.NaN, double.NaN); //筛选有交点,如果有两个点就选距离p2近的点 if (intersects.Count == 1) { validPoint = intersects[0]; } else if (intersects.Count == 2) { validPoint = HitUtil.Distance(intersects[0], p2.Point) < HitUtil.Distance(intersects[1], p2.Point) ? intersects[0] : intersects[1]; } if (validPoint.IsEmpty) //|| !HitUtil.IsPointInLine(cPoints.Item1.Point, cPoints.Item2.Point, validPoint, errorRange)) { //求圆弧的切线的交点 if (arc.Clockwise != HitUtil.IsClockwiseByCross(p2.Point, point1, cPoints.Item1.Point)) { var l1 = DrawingOperationHelper.GetLineEquationByVerticalLine(point1, p2.Point); intersecteArcPoint = DrawingOperationHelper.GetIntersectionPointBy2Line(l1.Item1, l1.Item2, l1.Item3, line.Item1, line.Item2, line.Item3); } } bool completedSmooth = false; //是否完成圆角处理 if (compensationParam.IsSmooth) //圆角处理 { if (!intersecteArcPoint.IsEmpty || (!validPoint.IsEmpty && !HitUtil.IsPointInLine(cPoints.Item1.Point, cPoints.Item2.Point, validPoint, errorRange))) { bool closewise = HitUtil.IsClockwiseByCross(p2.Point, cPoints.Item1.Point, cPoints.Item2.Point); double bulge = BulgeHelper.GetBulgeFromTwoPointsAndCenter(p2.Point, point1, cPoints.Item1.Point, closewise); retPoints.Add(new UnitPointBulge(point1, bulge, hasMicroConn: p2.HasMicroConn, position: -1 - indexCur)); retPoints.Add(new UnitPointBulge(cPoints.Item1.Point, p2.Bulge, hasMicroConn: p2.HasMicroConn, position: indexCur)); completedSmooth = true; } } if (!completedSmooth) { if (!intersecteArcPoint.IsEmpty) { retPoints.Add(new UnitPointBulge(point1, position: indexCur)); retPoints.Add(new UnitPointBulge(intersecteArcPoint, position: indexCur)); } else if (!validPoint.IsEmpty) { retPoints.Add(new UnitPointBulge(validPoint, p2.Bulge, hasMicroConn: p2.HasMicroConn, position: indexCur)); return(retPoints); } else { retPoints.Add(new UnitPointBulge(point1, position: indexCur)); retPoints.Add(new UnitPointBulge(p2.Point, position: indexCur)); retPoints.Add(new UnitPointBulge(cPoints.Item1.Point, p2.Bulge, hasMicroConn: p2.HasMicroConn, position: indexCur)); } } return(retPoints); }
/// <summary> /// 圆弧到圆弧,补偿求交点 /// </summary> /// <param name="p1"></param> /// <param name="p2"></param> /// <param name="p3"></param> /// <param name="indexCur"></param> /// <returns></returns> private static List <UnitPointBulge> CalCompensationsBy2Arc(UnitPointBulge p1, UnitPointBulge p2, UnitPointBulge p3, int indexCur, CompensationModel compensationParam, bool isOutside) { var retPoints = new List <UnitPointBulge>(); ArcModelMini arc1 = DrawingOperationHelper.GetArcParametersFromBulge(p1.Point, p2.Point, (float)p1.Bulge); ArcModelMini arc2 = DrawingOperationHelper.GetArcParametersFromBulge(p2.Point, p3.Point, (float)p2.Bulge); double r1 = arc1.Clockwise ? arc1.Radius - compensationParam.Size : arc1.Radius + compensationParam.Size; double r2 = arc2.Clockwise ? arc2.Radius - compensationParam.Size : arc2.Radius + compensationParam.Size; if (isOutside) { r1 = arc1.Clockwise ? arc1.Radius + compensationParam.Size : arc1.Radius - compensationParam.Size; r2 = arc2.Clockwise ? arc2.Radius + compensationParam.Size : arc2.Radius - compensationParam.Size; } UnitPoint point1 = HitUtil.GetLinePointByDistance(arc1.Center, p2.Point, r1, true); UnitPoint point2 = HitUtil.GetLinePointByDistance(arc2.Center, p2.Point, r2, true); if (HitUtil.Distance(point1, point2) < errorRange) { retPoints.Add(new UnitPointBulge(point2, p2.Bulge, hasMicroConn: p2.HasMicroConn, position: indexCur)); return(retPoints); } var intersects = DrawingOperationHelper.GetIntersectPointBy2Circle(arc1.Center, r1, arc2.Center, r2); var validPoint = new UnitPoint(double.NaN, double.NaN); UnitPoint intersecteArcPoint = new UnitPoint(double.NaN, double.NaN); //筛选有交点,如果有两个点就选距离p2近的点 if (intersects.Count == 1) { validPoint = intersects[0]; } else if (intersects.Count == 2) { validPoint = HitUtil.Distance(intersects[0], p2.Point) < HitUtil.Distance(intersects[1], p2.Point) ? intersects[0] : intersects[1]; } bool closewise = HitUtil.IsClockwiseByCross(p2.Point, point1, point2); if (validPoint.IsEmpty) { if (arc1.Clockwise != closewise || arc2.Clockwise != closewise)//求两个圆弧的切线的交点 { var line1 = DrawingOperationHelper.GetLineEquationByVerticalLine(point1, arc1.Center); var line2 = DrawingOperationHelper.GetLineEquationByVerticalLine(point2, arc2.Center); intersecteArcPoint = DrawingOperationHelper.GetIntersectionPointBy2Line(line1.Item1, line1.Item2, line1.Item3, line2.Item1, line2.Item2, line2.Item3); } } bool completedSmooth = false; //是否完成圆角处理 if (compensationParam.IsSmooth) //圆角处理 { bool needSmooth = false; if (!validPoint.IsEmpty) { double lineAngle = HitUtil.LineAngleR(arc1.Center, validPoint, 0); bool pointInArc = HitUtil.IsPointInArc(HitUtil.RadiansToDegrees(lineAngle), HitUtil.RadiansToDegrees(arc1.StartAngle), HitUtil.RadiansToDegrees(arc1.EndAngle), arc1.Clockwise); needSmooth = !pointInArc; } if (!intersecteArcPoint.IsEmpty || needSmooth) { double bulge = BulgeHelper.GetBulgeFromTwoPointsAndCenter(p2.Point, point1, point2, closewise); retPoints.Add(new UnitPointBulge(point1, bulge, hasMicroConn: p2.HasMicroConn, position: -1 - indexCur)); retPoints.Add(new UnitPointBulge(point2, p2.Bulge, hasMicroConn: p2.HasMicroConn, position: indexCur)); completedSmooth = true; } } if (!completedSmooth) { if (!validPoint.IsEmpty) { retPoints.Add(new UnitPointBulge(validPoint, p2.Bulge, hasMicroConn: p2.HasMicroConn, position: indexCur)); return(retPoints); } else if (!intersecteArcPoint.IsEmpty) { retPoints.Add(new UnitPointBulge(point1, position: indexCur)); retPoints.Add(new UnitPointBulge(intersecteArcPoint, position: indexCur)); retPoints.Add(new UnitPointBulge(point2, p2.Bulge, hasMicroConn: p2.HasMicroConn, position: indexCur)); } else { retPoints.Add(new UnitPointBulge(point1, position: indexCur)); retPoints.Add(new UnitPointBulge(p2.Point, position: indexCur)); retPoints.Add(new UnitPointBulge(point2, p2.Bulge, hasMicroConn: p2.HasMicroConn, position: indexCur)); } } return(retPoints); }
public static List <IDrawObject> GetBridgeObjects(List <IDrawObject> drawObjects, UnitPoint p1, UnitPoint p2, BridgingModel param, out bool isChanged) { isChanged = false;//桥接是否改变了图形 List <BridgePoints> bridgePoints = new List <BridgePoints>(); #region 计算桥接点在图形中的点的坐标 drawObjects?.ForEach(drawObject => { if ((drawObject.FigureType == FigureTypes.Circle)) { Circle circle = drawObject as Circle; var bridges = BridgeHelper.GetBridgeByCircle(circle, p1, p2, param); bridgePoints.AddRange(bridges); } else if (drawObject.FigureType == FigureTypes.Arc) { ArcBase arc = drawObject as ArcBase; var bridges = BridgeHelper.GetBridgeByArc(arc, p1, p2, param); bridgePoints.AddRange(bridges); } else if (drawObject.FigureType == FigureTypes.LwPolyline) { MultiSegmentLineBase multiSegLine = drawObject as MultiSegmentLineBase; var bridges = BridgeHelper.GetBridgeByMultiSegLine(multiSegLine, p1, p2, param); bridgePoints.AddRange(bridges); } }); #endregion #region 根据桥接点坐标,按直线的起点距离排序,如果奇数个将移除最后一个,保证偶数个可以连接 bridgePoints.Sort((x, y) => { if (x.Distance > y.Distance) { return(1); } return(-1); }); if (bridgePoints.Count % 2 == 1) { bridgePoints.RemoveAt(bridgePoints.Count - 1); } #endregion #region 移除间距大于最大条件的点 List <BridgePoints> removes = new List <BridgePoints>(); for (int i = 0; i < bridgePoints.Count - 1; i += 2) { var bridge1 = bridgePoints[i]; var bridge2 = bridgePoints[i + 1]; if (Math.Abs(bridge1.Distance - bridge2.Distance) > param.MaxDistance) { removes.Add(bridge1); removes.Add(bridge2); } } removes.ForEach(e => bridgePoints.Remove(e)); #endregion if (bridgePoints.Count > 0) { List <IDrawObject> temps = new List <IDrawObject>(); //计算后的图形 List <IDrawObject> oldDraws = new List <IDrawObject>(); //不用计算的原图 #region 根据桥接点的位置把原图形拆为多段线 foreach (IDrawObject drawObject in drawObjects) { var points = bridgePoints.FindAll(b => b.Owner == drawObject); if (points.Count > 0) { List <IDrawObject> draws = null; if (drawObject.FigureType == FigureTypes.Circle) { draws = BridgeHelper.ConvertToMultiSegLine(drawObject as Circle, points); } else if (drawObject.FigureType == FigureTypes.Arc) { draws = BridgeHelper.ConvertToMultiSegLine(drawObject as ArcBase, points); } else if (drawObject.FigureType == FigureTypes.LwPolyline) { draws = BridgeHelper.ConvertToMultiSegLine(drawObject as MultiSegmentLineBase, points); } if (draws != null) { draws.RemoveAll(p => (p as MultiSegmentLineBase).Points.Count < 2); temps.AddRange(draws); } } else { oldDraws.Add(drawObject); } } #endregion #region 连接最新的桥接图形 for (int i = 0; i < bridgePoints.Count - 1; i += 2) { var bridge1 = bridgePoints[i]; var bridge2 = bridgePoints[i + 1]; bool clockwise1 = HitUtil.IsClockwiseByCross(p1, p2, bridge1.Point1.Point); bool clockwise2 = HitUtil.IsClockwiseByCross(p1, p2, bridge2.Point1.Point); UnitPointBulge point11 = !clockwise1 ? bridge1.Point1 : bridge1.Point2; UnitPointBulge point12 = clockwise1 ? bridge1.Point1 : bridge1.Point2; UnitPointBulge point21 = !clockwise2 ? bridge2.Point1 : bridge2.Point2; UnitPointBulge point22 = clockwise2 ? bridge2.Point1 : bridge2.Point2; MultiSegmentLineBase draw11 = temps.Find(d => (d as MultiSegmentLineBase).Points.Contains(point11)) as MultiSegmentLineBase; MultiSegmentLineBase draw12 = temps.Find(d => (d as MultiSegmentLineBase).Points.Contains(point12)) as MultiSegmentLineBase; MultiSegmentLineBase draw21 = temps.Find(d => (d as MultiSegmentLineBase).Points.Contains(point21)) as MultiSegmentLineBase; MultiSegmentLineBase draw22 = temps.Find(d => (d as MultiSegmentLineBase).Points.Contains(point22)) as MultiSegmentLineBase; if (draw11 == null) { draw11 = new MultiSegmentLineBase() { LayerId = (bridge1.Owner as DrawObjectBase).LayerId, GroupParam = CopyUtil.DeepCopy(bridge1.Owner.GroupParam), Points = new List <UnitPointBulge>() }; } if (draw12 == null) { draw12 = new MultiSegmentLineBase() { LayerId = (bridge1.Owner as DrawObjectBase).LayerId, GroupParam = CopyUtil.DeepCopy(bridge1.Owner.GroupParam), Points = new List <UnitPointBulge>() }; } if (draw21 == null) { draw21 = new MultiSegmentLineBase() { LayerId = (bridge2.Owner as DrawObjectBase).LayerId, GroupParam = CopyUtil.DeepCopy(bridge2.Owner.GroupParam), Points = new List <UnitPointBulge>() }; } if (draw22 == null) { draw22 = new MultiSegmentLineBase() { LayerId = (bridge2.Owner as DrawObjectBase).LayerId, GroupParam = CopyUtil.DeepCopy(bridge2.Owner.GroupParam), Points = new List <UnitPointBulge>() }; } #region 组合多段线 if (draw11 == draw12 && draw21 == draw22) { if (draw11.Points.Count > 0 && draw11.Points[0] == point11) { draw11.ReverseDirection(); } if (draw21.Points.Count > 0 && draw21.Points[0] != point21) { draw11.ReverseDirection(); } if (draw11.Points.Count > 0) { draw11.Points[draw11.Points.Count - 1].Bulge = double.NaN; } if (draw21.Points.Count > 0) { draw21.Points[draw21.Points.Count - 1].Bulge = double.NaN; } draw11.Points.AddRange(draw21.Points); draw11.IsCloseFigure = true; temps.Remove(draw21); } else if (draw11 == draw12 && draw21 != draw22) { if (draw21.Points.Count > 0 && draw21.Points[0] == point21) { draw21.ReverseDirection(); } if (draw22.Points.Count > 0 && draw22.Points[0] != point22) { draw22.ReverseDirection(); } if (draw11.Points.Count > 0 && draw11.Points[0] != point11) { draw11.ReverseDirection(); } if (draw21.Points.Count > 0) { draw21.Points[draw21.Points.Count - 1].Bulge = double.NaN; } if (draw11.Points.Count > 0) { draw11.Points[draw11.Points.Count - 1].Bulge = double.NaN; } draw21.Points.AddRange(draw11.Points); draw21.Points.AddRange(draw22.Points); temps.Remove(draw11); temps.Remove(draw22); } else if (draw11 != draw12 && draw21 == draw22) { if (draw11.Points.Count > 0 && draw11.Points[0] == point11) { draw11.ReverseDirection(); } if (draw21.Points.Count > 0 && draw21.Points[0] != point21) { draw21.ReverseDirection(); } if (draw12.Points.Count > 0 && draw12.Points[0] != point12) { draw12.ReverseDirection(); } if (draw11.Points.Count > 0) { draw11.Points[draw11.Points.Count - 1].Bulge = double.NaN; } if (draw21.Points.Count > 0) { draw21.Points[draw21.Points.Count - 1].Bulge = double.NaN; } draw11.Points.AddRange(draw21.Points); draw11.Points.AddRange(draw12.Points); temps.Remove(draw21); temps.Remove(draw12); } else if (draw11 == draw21 && draw12 == draw22) { if (draw11.Points.Count > 0 && draw11.Points[0] != point11) { draw11.ReverseDirection(); } if (draw12.Points.Count > 0 && draw12.Points[0] != point12) { draw12.ReverseDirection(); } if (draw11.Points.Count > 0) { draw11.Points[draw11.Points.Count - 1].Bulge = double.NaN; } if (draw12.Points.Count > 0) { draw12.Points[draw12.Points.Count - 1].Bulge = double.NaN; } draw11.IsCloseFigure = true; draw12.IsCloseFigure = true; } else if (draw11 == draw21 && draw12 != draw22) { if (draw11.Points.Count > 0 && draw11.Points[0] != point11) { draw11.ReverseDirection(); } if (draw12.Points.Count > 0 && draw12.Points[0] == point12) { draw12.ReverseDirection(); } if (draw22.Points.Count > 0 && draw22.Points[0] != point22) { draw22.ReverseDirection(); } if (draw11.Points.Count > 0) { draw11.Points[draw11.Points.Count - 1].Bulge = double.NaN; } if (draw12.Points.Count > 0) { draw12.Points[draw12.Points.Count - 1].Bulge = double.NaN; } draw11.IsCloseFigure = true; draw12.Points.AddRange(draw22.Points); temps.Remove(draw22); } else if (draw11 != draw21 && draw12 == draw22) { if (draw12.Points.Count > 0 && draw12.Points[0] != point12) { draw12.ReverseDirection(); } if (draw11.Points.Count > 0 && draw11.Points[0] == point11) { draw11.ReverseDirection(); } if (draw21.Points.Count > 0 && draw21.Points[0] != point21) { draw21.ReverseDirection(); } if (draw12.Points.Count > 0) { draw12.Points[draw12.Points.Count - 1].Bulge = double.NaN; } if (draw11.Points.Count > 0) { draw11.Points[draw11.Points.Count - 1].Bulge = double.NaN; } draw12.IsCloseFigure = true; draw11.Points.AddRange(draw21.Points); temps.Remove(draw21); } else { if (draw11.Points.Count > 0 && draw11.Points[0] == point11) { draw11.ReverseDirection(); } if (draw21.Points.Count > 0 && draw21.Points[0] != point21) { draw21.ReverseDirection(); } if (draw12.Points.Count > 0 && draw12.Points[0] == point12) { draw12.ReverseDirection(); } if (draw22.Points.Count > 0 && draw22.Points[0] != point22) { draw22.ReverseDirection(); } if (draw11.Points.Count > 0) { draw11.Points[draw11.Points.Count - 1].Bulge = double.NaN; } if (draw12.Points.Count > 0) { draw12.Points[draw12.Points.Count - 1].Bulge = double.NaN; } draw11.Points.AddRange(draw21.Points); draw12.Points.AddRange(draw22.Points); temps.Remove(draw21); temps.Remove(draw22); } #endregion } #endregion isChanged = true; temps.ForEach(d => d.Update()); temps.AddRange(oldDraws); return(temps); } return(drawObjects); }