How to select corner points via PromptCornerOptions and PromptPointResult?

How to select corner points via PromptCornerOptions and PromptPointResult?

Anonymous
Not applicable
1,599 Views
4 Replies
Message 1 of 5

How to select corner points via PromptCornerOptions and PromptPointResult?

Anonymous
Not applicable
Hello,I just want to select corner points and get the coordinates of all the points,and get all the points sorted by their X coordinate.What I am supposed to do? Can someone give me a demonstration code?Thank you first here.
0 Likes
1,600 Views
4 Replies
Replies (4)
Message 2 of 5

Hallex
Advisor
Advisor

Try this code example, change to suit

 

           #region "Sort points"
        [CommandMethod("SortPoints", CommandFlags.Modal | CommandFlags.UsePickSet | CommandFlags.Redraw)]
        public void GeWindowPointSelection()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            Editor ed = doc.Editor;

            List<Point3d> points = new List<Point3d>();

            Transaction tr = db.TransactionManager.StartTransaction();

            using (tr)
            {
                try
                {
                    PromptPointOptions ppo = new PromptPointOptions("\n\tSpecify a first corner: ");

                    PromptPointResult ppr = ed.GetPoint(ppo);

                    if (ppr.Status != PromptStatus.OK) return;

                    PromptCornerOptions pco = new PromptCornerOptions("\n\tOther corner: ", ppr.Value);

                    PromptPointResult pcr = ed.GetCorner(pco);

                    if (pcr.Status != PromptStatus.OK) return;

                    Point3d pt1 = ppr.Value;

                    Point3d pt2 = pcr.Value;

                    if (pt1.X == pt2.X || pt1.Y == pt2.Y)
                    {
                        ed.WriteMessage("\nInvalid point specification");

                        return;
                    }
                    TypedValue[] tv = new TypedValue[] { new TypedValue((int)DxfCode.Start, "POINT") };

                    SelectionFilter flt = new SelectionFilter(tv);

                    PromptSelectionResult res;

                    res = ed.SelectWindow(pt1, pt2, flt);

                    if (res.Status != PromptStatus.OK)

                        return;

                    SelectionSet sset = res.Value;

                    if (sset.Count == 0)
                        return;
                   BlockTableRecord btr= (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

                   

                    foreach (SelectedObject obj in sset)
                    {
 
                        DBObject dbobj = obj.ObjectId.GetObject(OpenMode.ForRead);

                        DBPoint ptobj = dbobj as DBPoint;

                        if (ptobj != null)
                        {                        
                            Point3d pt = ptobj.Position;

                            points.Add(pt);

                        }
                    }

                    points.Sort(sortByX);

                    int n = 0;

                    foreach (Point3d dp in points)
                    {
                        n += 1;

                        DBText txt = new DBText();

                        txt.SetDatabaseDefaults();

                        txt.Position = dp ;

                        txt.TextString = n.ToString();

                        btr.AppendEntity(txt);

                        tr.AddNewlyCreatedDBObject(txt, true);

                    }

                        tr.Commit();

                    ed.Regen();

                }
                catch (System.Exception ex)
                {
                    ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
                }

            }

        }

        //http://www.theswamp.org/index.php?topic=32761.msg382406
       static  public int sortByX(Point3d a, Point3d b)
        {
            if (a.X == b.X)
                return a.Y.CompareTo(b.Y);
            return a.X.CompareTo(b.X);
        }

        #endregion

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes
Message 3 of 5

Anonymous
Not applicable

Well,I could not thank you more.Very nice to meet you here.And your help is great.Have a nice day! Smiley Very Happy

0 Likes
Message 4 of 5

Hallex
Advisor
Advisor

I'm glad  too

Cheers

Smiley Very Happy

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes
Message 5 of 5

Anonymous
Not applicable
Spoiler
 

Hello,Sorry to disturb you again.But I get a problem.I am  a VB programmer.I am not good at programming at all.I rewrite your SortByX method like this :

    Public Function SortByX(ByVal a As Point3d, ByVal b As Point3d) As Integer
        If (a.X = b.X) Then
            Return a.Y.CompareTo(b.Y)
        End If

        Return a.X.CompareTo(b.X)
    End Function

But how to call this method in VB code is just different form the way in C#.When I type"Points.Sort(SortByX)",Two errors occur.They tips that I have not assign arguments to a and b in the SortByX method.Did I rewrite the SortByX mehtod right in VB,or how should I rewrite it and call it in VB?

0 Likes