Setting the reference point in the selection

Setting the reference point in the selection

k005
Advisor Advisor
2,363 Views
17 Replies
Message 1 of 18

Setting the reference point in the selection

k005
Advisor
Advisor

 

Hello everyone !

 

var selection = ed.GetSelection(); the coordinates of the point I clicked for the first time,

var pointResult = ed.GetPoint("\n\nReference Point >"); -----> how can I point write this part automatically?

 

sample :

 

Point1 and Point2 = ed.GetSelection

 

ed.GetPoint(Point1)

 

Thanks in advance to the helper.

 

 

 

 

 var selection = ed.GetSelection();
            Application.SetSystemVariable("OSMODE", 32);
            if (selection.Status == PromptStatus.OK)
            {
                var pointResult = ed.GetPoint("\n\nReferans Noktası >");
                if (pointResult.Status == PromptStatus.OK)

 

 

 

0 Likes
Accepted solutions (1)
2,364 Views
17 Replies
Replies (17)
Message 2 of 18

k005
Advisor
Advisor

 

 (setq p3 (mapcar '/ (mapcar '+ p1 p2) '(2. 2. 2.)))

 

 

var pointResult = ed.GetPoint("\n\nReferans Noktası >");

var pointResult = ed.GetPoint(P3);

as in this lisp example. from the selection window (from p1 and p2)

 

How can I obtain and use a third point (p3) on the same axis?

0 Likes
Message 3 of 18

essam-salah
Advisor
Advisor

@k005 wrote:

 

Hello everyone !

 

var selection = ed.GetSelection(); the coordinates of the point I clicked for the first time,

var pointResult = ed.GetPoint("\n\nReference Point >"); -----> how can I point write this part automatically?

 

sample :

 

Point1 and Point2 = ed.GetSelection

 

ed.GetPoint(Point1)

 


hi @k005 

your question still not clear, if i you want to select a point then use it as ref point here is the an approcch:

 

[CommandMethod("Test_1")]
        public void SelectionWithRefPoint()
        {
            #region curr doc
            var adoc = Application.DocumentManager.MdiActiveDocument;
            var adb = adoc.Database;
            var editor = adoc.Editor;
            var cdoc = cApp.CivilApplication.ActiveDocument;
            #endregion

            var selResult = editor.GetSelection();
            Application.SetSystemVariable("OSMODE", 32);

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

            DBPoint refPoint = null;
            bool isRefPointFound = false;
            using (var tr = adb.TransactionManager.StartTransaction())
            {
                // searching for the first point found in selection
                foreach (var ObjId in selResult.Value.GetObjectIds())
                {
                    if (ObjId.GetObject(OpenMode.ForRead) is DBPoint dbPoint)
                    {
                        refPoint = dbPoint;
                        isRefPointFound = true;
                        break; // a point found
                    }
                }
                if (isRefPointFound == false)
                {
                    editor.WriteMessage("\n --- No Point Selected ---- \n");
                    return; 
                }

                // prompt for point
                var ppo = new PromptPointOptions("\n------- select a point --------") 
                {
                    UseBasePoint = true,
                    BasePoint = refPoint.Position,
                };
                var pointResult = editor.GetPoint(ppo);
                if (pointResult.Status != PromptStatus.OK)
                    return;

                // TO DO
                // ...
            }
        }

 

0 Likes
Message 4 of 18

k005
Advisor
Advisor

Hi @essam-salah 

 

 

* I made a little change on the subject. We need to get P3 (middle point) not P1 (first point).

 

It should look like the result here. that is, when the selection window is made ( P1 and P2 )

getting point P3 on the same axis into a variable.

 

 

 

(defun c:Test ()
  
  (princ "Use a window or cross-window type of selection to")
  (if (setq s (ssget "_:S"))
    (progn
      (and (setq x (ssnamex s))
	   (setq p1 (cadar (cdddr (assoc (last (car x)) x))))
	   (setq p2 (cadar (cdr (assoc (last (car x)) x))))
	   (setq p3 (mapcar '/ (mapcar '+ p1 p2) '(2. 2. 2.)))
	   (command "_point" "_non" p3))
      ))
  (princ p3)
  )

 

 

 

var pointResult = ed.GetPoint(P3); 

or otherwise. but for P3, I will not enter a reference point. we will get this during the selection window.

 

0 Likes
Message 5 of 18

_gile
Consultant
Consultant

Hi,

Point3d GetMidPoint(Point3d p1, Point3d p2) =>
    p1 + (p2 - p1) * 0.5;


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 18

k005
Advisor
Advisor
Point3d GetMidPoint(Point3d p1, Point3d p2) =>
    p1 + (p2 - p1) * 0.5;

 

OK. How do I get the contents of p1 and p2?

more precisely, how do I get it from getselection?

0 Likes
Message 7 of 18

_gile
Consultant
Consultant

Getting the points used during a selection (as with ssnamex LISP function) is not reliable because the user may have made several window, crossing, point selections during the same prompt. Besides, the .NET API does not expose anything about this subject neither in the PromptSelectionResult type, nor in the Selection type.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 8 of 18

k005
Advisor
Advisor

Yes . I understand. But I will suffice, albeit only as an approximate point. so this P3 point is not a very functional point...

 

so if we can translate the lisp code to c#, i think i can achieve my goal...

0 Likes
Message 9 of 18

_gile
Consultant
Consultant

@k005  a écrit :

if we can translate the lisp code to c#, i think i can achieve my goal...


No we cannot translate the LISP code. As I said upper, there's no equivalent API to the ssnamex LISP function.

 

What you could do is aggregate the geometric extents of all the selected entity and compute the middle of the resulting extents.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 10 of 18

k005
Advisor
Advisor

...What you could do is aggregate the geometric extents of all the selected entity and compute the middle of the resulting extents....

 

 

No, that would be too long. Understood.

In this case, another method is required or I have to mark the reference point on the screen with the mouse. 😞

 

Thank you.

 

 

0 Likes
Message 11 of 18

_gile
Consultant
Consultant

@k005  a écrit :

No, that would be too long. Understood.


Are you sure? Did you try it?

 

An example:

        [CommandMethod("TEST")]
        public static void Test()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            var selection = ed.GetSelection();
            if (selection.Status != PromptStatus.OK)
                return;
            using (var tr = db.TransactionManager.StartTransaction())
            {
                var extents = new Extents3d();
                foreach (var id in selection.Value.GetObjectIds())
                {
                    var entity = (Entity)tr.GetObject(id, OpenMode.ForRead);
                    extents.AddExtents(entity.GeometricExtents);
                }
                var point = new DBPoint(
                    extents.MinPoint + (extents.MaxPoint - extents.MinPoint) * 0.5);
                point.ColorIndex = 30;
                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                curSpace.AppendEntity(point);
                tr.AddNewlyCreatedDBObject(point, true);
                tr.Commit();
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 12 of 18

cuongtk2
Advocate
Advocate

Extents3d of entities is not pickpoint region

 

Document doc = AcadApp.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;

            using (DocumentLock lk = doc.LockDocument())
            {

                PromptSelectionOptions options = new PromptSelectionOptions();
                // options.SingleOnly = true;
                PromptSelectionResult selp = ed.GetSelection(options);
                if (selp.Status != PromptStatus.OK)
                    return;
                SelectionSet ss = selp.Value;

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


                foreach (SelectedObject item in ss)
                {
                    if (item.SelectionMethod == SelectionMethod.PickPoint)
                    {
                        PickPointSelectedObject ppso = item as PickPointSelectedObject;
                        PickPointDescriptor ppd = ppso.PickPoint;
                        var pt = ppd.PointOnLine;
                        ls.Add(pt);
                        // ls.Count = 1 as ed.GetEntity
                    }

                    if (item.SelectionMethod == SelectionMethod.Crossing ||
                        item.SelectionMethod == SelectionMethod.Window
                        )
                    {
                        CrossingOrWindowSelectedObject cowso = item as CrossingOrWindowSelectedObject;
                        PickPointDescriptor[] ppd1 = cowso.GetPickPoints();
                        foreach (PickPointDescriptor item1 in ppd1)
                        {
                            ls.Add(item1.PointOnLine);
                            //ls.Count = 4 as vertices of rectang
                        }

                    }

                }



            }

 

 

Message 13 of 18

_gile
Consultant
Consultant

Thank you very much @cuongtk2 .

You show me something I never saw before.

Anyway, it looks like you also have to iterate through all the selected entities as for computing the extents of all selected entities.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 14 of 18

_gile
Consultant
Consultant

So, here's an example with picked points as shown by @cuongtk2 .

        [CommandMethod("TEST2")]
        public static void Test2()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            var selection = ed.GetSelection();
            if (selection.Status != PromptStatus.OK)
                return;

            var extents = new Extents3d();
            foreach (SelectedObject item in selection.Value)
            {
                switch (item.SelectionMethod)
                {
                    case SelectionMethod.PickPoint:
                        var ppso = (PickPointSelectedObject)item;
                        extents.AddPoint(ppso.PickPoint.PointOnLine);
                        break;
                    case SelectionMethod.Window:
                    case SelectionMethod.Crossing:
                        var cowso = (CrossingOrWindowSelectedObject)item;
                        foreach (var ppd in cowso.GetPickPoints())
                        {
                            extents.AddPoint(ppd.PointOnLine);
                        }
                        break;
                    case SelectionMethod.Fence:
                        var fso = (FenceSelectedObject)item; 
                        foreach (var ppd in fso.GetIntersectionPoints())
                        {
                            extents.AddPoint(ppd.PointOnLine);
                        }
                        break;
                    default:
                        break;
                }
            }
            using (var tr = db.TransactionManager.StartTransaction())
            {
                var point = new DBPoint(
                    extents.MinPoint + (extents.MaxPoint - extents.MinPoint) * 0.5);
                point.ColorIndex = 30;
                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                curSpace.AppendEntity(point);
                tr.AddNewlyCreatedDBObject(point, true);
                tr.Commit();
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 15 of 18

k005
Advisor
Advisor

thank you for the codes. Yes this is happening.

I ask you; converting the resulting DBpoint to point2d.

the reason is; It's because I can't add it to the codes.

 

this part using ;

 

 point.Value.TransformBy(ed.CurrentUserCoordinateSystem),
0 Likes
Message 16 of 18

_gile
Consultant
Consultant
Accepted solution

@k005  a écrit :

I ask you; converting the resulting DBpoint to point2d.


The DBPoint is only used here to show the concrete result of the computation. Its coordinates (the Point3d passed as argument to the DBPoint constructor) are the result of the expression:

extents.MinPoint + (extents.MaxPoint - extents.MinPoint) * 0.5


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 17 of 18

k005
Advisor
Advisor

@_gile 

 

 point.Value.TransformBy(ed.CurrentUserCoordinateSystem),

 

point.Value

 

If so, how do I use it in this section?

0 Likes
Message 18 of 18

k005
Advisor
Advisor

@_gile 

 

point.Position.TransformBy(ed.CurrentUserCoordinateSystem),

 

In this way, my question was solved.

 

First of all, many thanks to you and the other helpful friends.

 

 

0 Likes