欢迎加我的QQ群:193522571,一起来讨论、交流!

AutoCAD.Net/C#.Net QQ群:193522571 点选多段线时取得当前多段线的两端点

 

using Autodesk.AutoCAD.ApplicationServices;
    using Autodesk.AutoCAD.DatabaseServices;
    using Autodesk.AutoCAD.EditorInput;
    using Autodesk.AutoCAD.Runtime;
    using Autodesk.AutoCAD.Geometry;
    using Autodesk.AutoCAD.GraphicsInterface;
    using Autodesk.AutoCAD.Colors;
    //_____________________________________//

        [CommandMethod("colseg")]
        public void ColoredSegment()
        {

            Document doc = Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            Editor ed = doc.Editor;

            PromptEntityOptions peo = new PromptEntityOptions("\nSelect Segment of Polyline:");

            peo.SetRejectMessage("\nMust be a Polyline!");

            peo.AddAllowedClass(typeof(Autodesk.AutoCAD.Databa​seServices.Polyline), false);

            PromptEntityResult per = ed.GetEntity(peo);

            if (per.Status != PromptStatus.OK)

                return;

            Transaction tr = db.TransactionManager.StartTransaction();

            Autodesk.AutoCAD.DatabaseServices.Polyline pl = new Autodesk.AutoCAD.DatabaseServices.Polyline();

            IntegerCollection ic = new IntegerCollection();

            using (tr)
            {
                Autodesk.AutoCAD.DatabaseServices.Polyline pline = tr.GetObject(per.ObjectId, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Polyline;

                if (pline != null)
                {
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(pline.OwnerId, OpenMode.ForWrite);

                    double bulg = 0;

                    Point3d picked = pline.GetClosestPointTo((Point3d)per.PickedPoint, false);

                    double par = pline.GetParameterAtPoint(picked);

                    int m = (int)par;

                    Point2dCollection verts = new Point2dCollection(2);

                    SegmentType stype = pline.GetSegmentType(m);

                    if (stype == SegmentType.Arc)
                    {
                        CircularArc2d arc = pline.GetArcSegment2dAt(m);

                        bulg = pline.GetBulgeAt(m);

                        verts.Add(arc.StartPoint);

                        verts.Add(arc.EndPoint);
                    }
                    else if (stype == SegmentType.Line)
                    {
                        LineSegment2d ln = pline.GetLineSegment2dAt(m);

                        verts.Add(ln.StartPoint);

                        verts.Add(ln.EndPoint);

                        bulg = 0;
                    }

                    pl.AddVertexAt(0, verts[0], bulg, 0, 0);

                    pl.AddVertexAt(1, verts[1], 0, 0, 0);

                    pl.ColorIndex = 121;

                    ObjectIdCollection ids = new ObjectIdCollection();

                    btr.AppendEntity(pl);

                    tr.AddNewlyCreatedDBObject(pl, true);

                    verts = new Point2dCollection();

                    if (pl != null)
                    {
                        TransientManager tm = TransientManager.CurrentTransientManager;
                        tm.AddTransient(pl, TransientDrawingMode.Highlight, 128, ic);

                    }
                }

                if (pl != null)
                {
                    pl.Erase();

                    pl.Dispose();// optional, might be removed
                }

                tr.Commit();
                // pause for user input only
                string key = ed.GetString("\nPress any key: ").StringResult;

                TransientManager.CurrentTransientManager.EraseTran​sient(pline, ic);

            }

        }

 

 

 

posted @ 2014-04-08 22:00  swtool  阅读(1873)  评论(1编辑  收藏  举报
欢迎加我的QQ群:193522571,一起来讨论、交流!