Exemplo n.º 1
0
        public static ObjectId Create(LineSegment3d L, double H, ObjectId DimStyleId, ObjectId LayerId)
        {
            ObjectId Return = ObjectId.Null;

            #region T
            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;

                var V       = L.Direction;
                var cross_V = V.CrossProduct(Vector3d.ZAxis);

                var CP = CADUtil.GetCenterPoint3d(L);       // center point
                var DP = CADUtil.MoveP(CP, 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 = acDim.Id;

                T.Commit();
            }
            #endregion

            return(Return);
        }
Exemplo n.º 2
0
        public static List <ObjectId> Create(Polyline acPoly, 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 < acPoly.NumberOfVertices; i++)
                {
                    try
                    {
                        var L       = acPoly.GetLineSegmentAt(i);
                        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(acPoly);     // Center point

                            var p = Utils.PointUtil.Move(MP, cross_V);

                            var b = Utils.PointUtil.IsInsidePolyline(acPoly, 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);

                        Return.Add(acDim.Id);

                        acDim.DimensionStyle = DimStyleId;
                        acDim.LayerId        = LayerId;

                        BTR.AppendEntity(acDim);
                        T.AddNewlyCreatedDBObject(acDim, true);
                    }
                    catch
                    {
                    }
                }

                T.Commit();
            }

            return(Return);
        }
Exemplo n.º 3
0
        public void CreateKeyLine()
        {
            bool B = true;

            while (B)
            {
                //#region PRE
                //SelectionSet acSSet = SelectLines();

                //if (acSSet == null)
                //{
                //    B = false;
                //    return;
                //}
                //#endregion

                //#region 폴리라인 선택
                //var selectedPolyLines = new List<Polyline>();
                //var acPolylines = new List<Polyline>();

                //using (Transaction T = AC.DB.TransactionManager.StartTransaction())
                //{
                //    var Lines = from id in acSSet.GetObjectIds()
                //                let acEnt = T.GetObject(id, OpenMode.ForWrite) as Entity
                //                where acEnt is Polyline
                //                let acLine = acEnt as Polyline
                //                select acLine;

                //    if (!Lines.Any())
                //        return;

                //    selectedPolyLines.AddRange(Lines.ToArray());
                //}
                //#endregion

                var Polylines   = select.Objects <Polyline>();
                var acPolylines = new List <Polyline>();

                #region 폴리라인 정리
                Polylines.ForEach(pl =>
                {
                    #region 폴리라인에서 커브 담기
                    var curves = new List <Curve3d>();

                    for (int i = 0; i < pl.NumberOfVertices; i++)
                    {
                        try
                        {
                            var l = pl.GetLineSegmentAt(i).Length;

                            if (l >= Min && l <= Max)
                            {
                                curves.Add(pl.GetLineSegmentAt(i));
                            }
                        }
                        catch (System.Exception)
                        {
                        }
                    }
                    #endregion

                    #region 폴리라인 장축 중심을 읻는 선
                    if (curves.Count == 2)
                    {
                        var cv1 = curves[0];
                        var cv2 = curves[1];

                        var p1 = CADUtil.GetCenterPoint3d(cv1);
                        var p2 = CADUtil.GetCenterPoint3d(cv2);

                        var vec = CADUtil.GetVector(p1, p2);

                        var X = (vec * extend).X;
                        var Y = (vec * extend).Y;

                        var sp = CADUtil.MoveP(p1, -X, -Y, 0);
                        var ep = CADUtil.MoveP(p2, X, Y, 0);

                        var acPolyline = CADUtil.CreatePolyline(sp, ep);

                        acPolylines.Add(acPolyline);
                    }
                    #endregion
                });
                #endregion

                using (DocumentLock DL = AC.Doc.LockDocument())
                {
                    #region 검토 & 연속된 선 연결
                    // 제거할 선들
                    var deletePolylines = new List <Polyline>();

                    acPolylines.ForEach(pl1 =>
                    {
                        var contiPolylines = new List <Polyline>();

                        // 평행하고 같은 선상에 있는 선들
                        var paralPolylines = from pl2 in acPolylines
                                             where !deletePolylines.Contains(pl2)
                                             let line1                         = pl1.GetLineSegmentAt(0)
                                                                     let line2 = pl2.GetLineSegmentAt(0)
                                                                                 where line1.IsParallelTo(line2, new Tolerance(1, 1))
                                                                                 where IsAlmostEqual(line1, line2)
                                                                                 select pl2;

                        // 평행하고 같은 선상에 있는 선들 개수
                        int N = paralPolylines.Count();

                        var paralNearPolylines                                                             = from pl2 in paralPolylines
                                                                         let line1                         = pl1.GetLineSegmentAt(0)
                                                                                                 let line2 = pl2.GetLineSegmentAt(0)
                                                                                                             where CADUtil.getNearDistance(line1, line2) < distance
                                                                                                             select pl2;

                        if (paralNearPolylines.Any())
                        {
                            contiPolylines.AddRange(paralNearPolylines);

                            for (int j = 0; j < N; j++)
                            {
                                contiPolylines.ToList().ForEach(a =>
                                {
                                    var srtPolylines = from pl2 in acPolylines
                                                       where !contiPolylines.Contains(pl2)
                                                       let line1                         = a.GetLineSegmentAt(0)
                                                                               let line2 = pl2.GetLineSegmentAt(0)
                                                                                           where line1.IsParallelTo(line2, new Tolerance(1, 1))
                                                                                           where IsAlmostEqual(line1, line2)
                                                                                           where CADUtil.getNearDistance(line1, line2) < 2 * extend + distance
                                                                                           select pl2;

                                    if (srtPolylines.Any())
                                    {
                                        contiPolylines.AddRange(srtPolylines);
                                    }
                                });
                            }
                        }

                        if (contiPolylines.Any())
                        {
                            var Ps = CADUtil.getFurPoints(contiPolylines.ToList());

                            var acPolyline = CADUtil.CreatePolyline(Ps[0], Ps[1]);

                            deletePolylines.AddRange(contiPolylines);
                        }
                    });

                    #endregion

                    #region 제거
                    using (OpenCloseTransaction T = AC.DB.TransactionManager.StartOpenCloseTransaction())
                    {
                        deletePolylines.ForEach(a =>
                        {
                            var entity = T.GetObject(a.Id, OpenMode.ForWrite) as Entity;

                            if (!entity.IsErased)
                            {
                                entity.Erase(true);
                            }
                        });

                        T.Commit();
                    }
                    #endregion
                }
            }

            AC.Editor.WriteMessage("\n키라인 입력완료 ");
            AC.Editor.PostCommandPrompt();
        }
Exemplo n.º 4
0
        public void CreateKeyLine2()
        {
            var selectedPolyLines = new List <Polyline>();
            var acPolylines       = new List <Polyline>();

            #region 폴리라인 선택
            using (Transaction T = AC.DB.TransactionManager.StartTransaction())
            {
                try
                {
                    SelectionSet acSSet = SelectLines();

                    var Lines = from id in acSSet.GetObjectIds()
                                let acEnt = T.GetObject(id, OpenMode.ForWrite) as Entity
                                            where acEnt is Polyline
                                            let acLine = acEnt as Polyline
                                                         select acLine;

                    if (!Lines.Any())
                    {
                        return;
                    }

                    selectedPolyLines.AddRange(Lines.ToArray());
                }
                catch (System.Exception ex)
                {
                    AC.Editor.WriteMessage(Convert.ToString(ex));
                }
            }
            #endregion

            using (DocumentLock DL = AC.Doc.LockDocument())
            {
                #region 입력
                selectedPolyLines.ForEach(pl =>
                {
                    #region 폴리라인에서 커브 담기
                    var curves = new List <Curve3d>();

                    for (int i = 0; i < pl.NumberOfVertices; i++)
                    {
                        try
                        {
                            var l = pl.GetLineSegmentAt(i).Length;

                            if (l >= Min && l <= Max)
                            {
                                curves.Add(pl.GetLineSegmentAt(i));
                            }
                        }
                        catch (System.Exception)
                        {
                        }
                    }
                    #endregion

                    if (curves.Count == 2)
                    {
                        var cv1 = curves[0];
                        var cv2 = curves[1];

                        var p1 = CADUtil.GetCenterPoint3d(cv1);
                        var p2 = CADUtil.GetCenterPoint3d(cv2);

                        var vec = CADUtil.GetVector(p1, p2);

                        var X = (vec * extend).X;
                        var Y = (vec * extend).Y;

                        var sp = CADUtil.MoveP(p1, -X, -Y, 0);
                        var ep = CADUtil.MoveP(p2, X, Y, 0);

                        var acPolyline = CADUtil.CreatePolyline(sp, ep);

                        acPolylines.Add(acPolyline);
                    }
                });
                #endregion

                #region 검토 & 연속된 선 연결
                // 제거할 선들
                var deletePolylines = new List <Polyline>();

                acPolylines.ForEach(pl1 =>
                {
                    var direction = pl1.GetLineSegmentAt(0).Direction;
                    direction     = direction.X < 0 ? -direction : direction;
                    var Ang       = Math.Round(Math.Atan2(direction.Y, direction.X) / Math.PI * 180) % 180;
                    var A         = Math.Abs(Ang) == 90 || Ang == 0 ? 0 : Math.Tan(Ang / 180 * Math.PI);

                    //MessageBox.Show(Ang + "\n" + A.ToString());

                    // 같은 2차 그래프에 존재하는 선들
                    var parallelPolylines = from pl2 in acPolylines
                                            where !deletePolylines.Contains(pl2)
                                            let line1                         = pl1.GetLineSegmentAt(0)
                                                                    let line2 = pl2.GetLineSegmentAt(0)
                                                                                where line1.IsParallelTo(line2, new Tolerance(1, 1))
                                                                                //let b1 = line1.StartPoint.Y - A * line1.StartPoint.X
                                                                                //let b2 = line2.StartPoint.Y - A * line2.StartPoint.X
                                                                                //let sp1 = line1.StartPoint
                                                                                //let sp2 = line2.StartPoint
                                                                                //where (Ang == 0 && IsAlmostEqual(sp1.Y, sp2.Y)) ||
                                                                                //      (Math.Abs(Ang) == 90 && IsAlmostEqual(sp1.X, sp2.X)) ||
                                                                                //      (Ang != 0 && Math.Abs(Ang) != 90 && IsAlmostEqual(line1,line2))
                                                                                where IsAlmostEqual(line1, line2)
                                                                                select pl2;

                    if (parallelPolylines.Any())
                    {
                        var Ps = CADUtil.getFurPoints(parallelPolylines.ToList());

                        var acPolyline = CADUtil.CreatePolyline(Ps[0], Ps[1]);

                        deletePolylines.AddRange(parallelPolylines);
                    }
                });

                #endregion

                #region 제거
                using (OpenCloseTransaction T = AC.DB.TransactionManager.StartOpenCloseTransaction())
                {
                    deletePolylines.ForEach(a =>
                    {
                        var entity = T.GetObject(a.Id, OpenMode.ForWrite) as Entity;

                        if (!entity.IsErased)
                        {
                            entity.Erase(true);
                        }
                    });

                    T.Commit();
                }
                #endregion
            }

            AC.Editor.WriteMessage("\n키라인 입력완료 ");
            AC.Editor.PostCommandPrompt();
        }
Exemplo n.º 5
0
        /// <summary>
        /// 폴리라인의 중심점 구하기
        /// </summary>
        /// <param name="acPolyline"></param>
        /// <returns></returns>
        private Point3d GetVisualCenterPoint(Polyline acPolyline)
        {
            #region 폴리라인 내부 점 받기
            var min_P = acPolyline.Bounds.Value.MinPoint;
            var max_P = acPolyline.Bounds.Value.MaxPoint;

            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 = CADUtil.MoveP(min_P, dis_X * j, dis_Y * i);

                    if (IsInsideInPolyline(acPolyline, p))
                    {
                        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

            #region 폴리라인의 중심점
            var X = 0.0;
            var Y = 0.0;

            Points.ForEach(p =>
            {
                X += (p.X) / Points.Count;
                Y += (p.Y) / Points.Count;
            });

            //return new Point3d(X, Y, 0);
            #endregion

            #region 점, 문자
            //using (Transaction T = acDb.TransactionManager.StartTransaction())
            //{
            //    BlockTable BT = T.GetObject(acDb.BlockTableId, OpenMode.ForRead) as BlockTable;
            //    BlockTableRecord BTR = T.GetObject(BT[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

            //    DBPoint p1 = new DBPoint(min_P);
            //    DBPoint p2 = new DBPoint(max_P);
            //    DBPoint p3 = new DBPoint(new Point3d(X, Y, 0));
            //    DBPoint p4 = new DBPoint(cp);

            //    //BTR.AppendEntity(p1);
            //    //BTR.AppendEntity(p2);
            //    //BTR.AppendEntity(p3);
            //    BTR.AppendEntity(p4);

            //    //T.AddNewlyCreatedDBObject(p1, true);
            //    //T.AddNewlyCreatedDBObject(p2, true);
            //    //T.AddNewlyCreatedDBObject(p3, true);
            //    T.AddNewlyCreatedDBObject(p4, true);

            //    for (int i = 0; i < Points.Count; i++)
            //    {
            //        //using (DBPoint acPoint = new DBPoint(Points[i]))
            //        //{
            //        //    BTR.AppendEntity(acPoint);
            //        //    T.AddNewlyCreatedDBObject(acPoint, true);
            //        //}
            //    }
            //    acDb.Pdmode = 34;
            //    acDb.Pdsize = 150;

            //    T.Commit();
            //}
            //return new Point3d();
            #endregion
        }