TheSwamp

Code Red => .NET => Topic started by: emrahctrk on November 26, 2020, 09:38:10 AM

Title: Coordinates of a Picked point on the screen
Post by: emrahctrk 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
Title: Re: Coordinates of a Picked point on the screen
Post by: gile 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.         }
Title: Re: Coordinates of a Picked point on the screen
Post by: emrahctrk on November 27, 2020, 06:21:53 AM
thank you. It is useful