Author Topic: Coordinates of a Picked point on the screen  (Read 523 times)

0 Members and 1 Guest are viewing this topic.

emrahctrk

  • Mosquito
  • Posts: 3
Coordinates of a Picked point on the screen
« on: November 26, 2020, 09:38:10 AM »
hi, can you tell me how can I get coordinate of a point picked on the screen? I want to write X and Y coordinates on the screen where I will pick.   Do you have any c# codes for it. I am new in C# so I am still learning.
Thank you all

gile

  • Water Moccasin
  • Posts: 2270
  • Marseille, France
Re: Coordinates of a Picked point on the screen
« Reply #1 on: November 26, 2020, 01:57:27 PM »
Hi,

Here's a simple and commented example:
Code - C#: [Select]
  1.         [CommandMethod("PRINTCOORDS")]
  2.         public static void PrintCoordinates()
  3.         {
  4.             Document doc = Application.DocumentManager.MdiActiveDocument;
  5.             Database db = doc.Database;
  6.             Editor ed = doc.Editor;
  7.  
  8.             // prompt the user to pick a point
  9.             PromptPointResult ppr = ed.GetPoint("\nPick a point: ");
  10.  
  11.             // return if the user cancelled
  12.             if (ppr.Status != PromptStatus.OK)
  13.                 return;
  14.  
  15.             Point3d pickedPoint = ppr.Value;
  16.  
  17.             // start a transaction to add a text to the current space
  18.             using (Transaction tr = db.TransactionManager.StartTransaction())
  19.             {
  20.                 // open the current space BlockTableRecord for write
  21.                 BlockTableRecord curSpace =
  22.                     (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  23.  
  24.                 // create a new DBText instance and set its properties
  25.                 DBText text = new DBText()
  26.                 {
  27.                     Position = pickedPoint,
  28.                     TextString = $"{pickedPoint.X:0.00}, {pickedPoint.Y:0.00}"
  29.                 };
  30.  
  31.                 // transform the text to UCS
  32.                 text.TransformBy(ed.CurrentUserCoordinateSystem);
  33.  
  34.                 // append the text to current space
  35.                 curSpace.AppendEntity(text);
  36.  
  37.                 // add the text to the transaction
  38.                 tr.AddNewlyCreatedDBObject(text, true);
  39.  
  40.                 // commit the changes
  41.                 tr.Commit();
  42.             }
  43.         }
Speaking English as a French Frog

emrahctrk

  • Mosquito
  • Posts: 3
Re: Coordinates of a Picked point on the screen
« Reply #2 on: November 27, 2020, 06:21:53 AM »
thank you. It is useful