/// <summary> /// 폴리라인의 중심점 구하기 /// </summary> /// <param name="acPolyline"></param> /// <returns></returns> public static Point3d GetCenterP(Polyline acPolyline) { #region 폴리라인 내부 점 받기 // 폴리라인 내부에 n간격으로 점을 찍음 var min_P = acPolyline.Bounds.Value.MinPoint; var max_P = acPolyline.Bounds.Value.MaxPoint; //CAD.CreateCircle(min_P, 50); //CAD.CreateCircle(max_P, 50); int n = 20; // 정밀도 double dis_X = (max_P.X - min_P.X) / n; double dis_Y = (max_P.Y - min_P.Y) / n; var Points = new List <Point3d>(); for (int i = 0; i < n + 1; i++) { for (int j = 0; j < n + 1; j++) { var p = PointUtil.Move(min_P, dis_X * j, dis_Y * i); if (IsInsidePolyline(acPolyline, p, false)) { //CAD.CreateCircle(p, 20); Points.Add(p); } } } #endregion #region 무게중심 // 폴리라인 내부의 점들끼리의 거리의 합이 가장 큰 점이 가장 가운데에 위치해 있다 double value = double.MaxValue; Point3d cp = new Point3d(); Points.ForEach(p1 => { double val = 0; Points.ForEach(p2 => { if (!p1.IsEqualTo(p2)) { val += p1.DistanceTo(p2) * p1.DistanceTo(p2); } }); if (value > val) { value = val; cp = p1; } }); return(cp); #endregion }
public static List <ObjectId> Create(List <Point3d> points, double H, bool updown, ObjectId DimStyleId, ObjectId LayerId) { List <ObjectId> Return = new List <ObjectId>(); using (Transaction T = AC.DB.TransactionManager.StartTransaction()) { var BT = T.GetObject(AC.DB.BlockTableId, OpenMode.ForRead) as BlockTable; var BTR = T.GetObject(BT[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; for (int i = 0; i < points.Count - 1; i++) { try { var L = new LineSegment3d(points[i], points[i + 1]); var V = L.Direction; var cross_V = V.CrossProduct(Vector3d.ZAxis); var MP = Utils.PointUtil.GetCenterP(L); // Middle point #region 폴리라인 내부에 입력되어야하는 경우 // updown이 true일 경우 폴리라인 외부, false인 경우 폴리라인 내부 if (!updown) { var CP = Utils.PointUtil.GetCenterP(points); // Center point var p = Utils.PointUtil.Move(MP, cross_V); var P = new Polyline(); for (int j = 0; j < points.Count; j++) { P.AddVertexAt(j, PointUtil.To2D(points[j]), 0, 0, 0); } var b = Utils.PointUtil.IsInsidePolyline(P, p, false); if (!b) { cross_V = -cross_V; } } #endregion var DP = CADUtil.MoveP(MP, cross_V, H); // dimension point var acDim = new AlignedDimension(L.StartPoint, L.EndPoint, DP, string.Empty, DimStyleId); acDim.DimensionStyle = DimStyleId; acDim.LayerId = LayerId; BTR.AppendEntity(acDim); T.AddNewlyCreatedDBObject(acDim, true); Return.Add(acDim.Id); } catch { } } T.Commit(); } return(Return); }