In this article, let’s see how to collect two corners from the AutoCAD screen.
[CommandMethod("CollectCornersTest", CommandFlags.Modal)]
public static void CollectCornersTest_Method()
{
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
try
{
Point3d pt1, pt2;
CollectCorners(out pt1, out pt2);
ed.WriteMessage("\nCorner #1: {0}\nCorner #2: {1}", pt1, pt2);
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.Message);
}
}
public static void CollectCorners(out Point3d corner1, out Point3d corner2)
{
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
PromptPointResult prPntRes1;
PromptPointOptions prPntOpt1 = new PromptPointOptions("\nSpecify the first corner");
prPntOpt1.AllowArbitraryInput = true;
prPntOpt1.AllowNone = false;
prPntOpt1.LimitsChecked = false;
do
{
prPntRes1 = ed.GetPoint(prPntOpt1);
} while (prPntRes1.Status != PromptStatus.OK && prPntRes1.Status != PromptStatus.Cancel);
if (prPntRes1.Status == PromptStatus.Cancel)
throw new System.Exception("*Cancel*");
corner1 = prPntRes1.Value;
PromptPointResult prPntRes2;
PromptPointOptions prPntOpt2 = new PromptPointOptions("\nSpecify the opposite corner");
prPntOpt2.AllowArbitraryInput = true;
prPntOpt2.AllowNone = false;
prPntOpt2.LimitsChecked = false;
prPntOpt2.BasePoint = prPntRes1.Value;
prPntOpt2.UseBasePoint = true;
//prPntOpt2.UseDashedLine = true;
do
{
prPntRes2 = ed.GetCorner(prPntOpt2);
} while (prPntRes2.Status != PromptStatus.OK && prPntRes2.Status != PromptStatus.Cancel);
if (prPntRes2.Status == PromptStatus.Cancel)
throw new System.Exception("*Cancel*");
corner2 = prPntRes2.Value;
}
The operation itself is simple but the above code takes care of quite some matters such as error input, user escaping out, accepting command line input, using base point, nicely exception handling, and the use of the ‘out’ argument, which is better in this case than the ‘ref’ one (the ‘ByRef’ in VB.NET) as we talked about earlier.
Here is some sample output in the command line window.
Command: CollectCornersTest
Specify the first corner:
Specify the opposite corner:
Corner #1: (281.805406823063,-278.466407626779,0)
Corner #2: (573.511660592189,-116.459408050606,0)
Please note that the coordinates of the collected corners are in UCS instead of WCS, so if readers would like to create a Polyline from them, for example, please do not forget to do the transformation, as demonstrated many times before.
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides project wizards in C#, VB.NET and CLI/Managed C++, and various item wizards such as Event Handlers, Command/LispFunction Definers, and Entity/Draw Jiggers in both C# and VB.NET, to help program AutoCAD addins.
Posted by: |