By using the methods provided by the Editor object and the Geometry and Runtime namespaces, you can quickly solve a mathematical problem or locate points in your drawing. Some of the available methods are:
This example calculates a vector between two points and determines the angle from the X-axis.
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
[CommandMethod("AngleFromXAxis")]
public static void AngleFromXAxis()
{
Point2d pt1 = new Point2d(2, 5);
Point2d pt2 = new Point2d(5, 2);
Application.ShowAlertDialog("Angle from XAxis: " +
pt1.GetVectorTo(pt2).Angle.ToString());
}
This example calculates a point based on a base point, an angle and distance.
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
static Point2d PolarPoints(Point2d pPt, double dAng, double dDist)
{
return new Point2d(pPt.X + dDist * Math.Cos(dAng),
pPt.Y + dDist * Math.Sin(dAng));
}
static Point3d PolarPoints(Point3d pPt, double dAng, double dDist)
{
return new Point3d(pPt.X + dDist * Math.Cos(dAng),
pPt.Y + dDist * Math.Sin(dAng),
pPt.Z);
}
[CommandMethod("PolarPoints")]
public static void PolarPoints()
{
Point2d pt1 = PolarPoints(new Point2d(5, 2), 0.785398, 12);
Application.ShowAlertDialog("\nPolarPoint: " +
"\nX = " + pt1.X +
"\nY = " + pt1.Y);
Point3d pt2 = PolarPoints(new Point3d(5, 2, 0), 0.785398, 12);
Application.ShowAlertDialog("\nPolarPoint: " +
"\nX = " + pt2.X +
"\nY = " + pt2.Y +
"\nZ = " + pt2.Z);
}
This example uses the GetDistance method to obtain two points and displays the calculated distance.
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
[CommandMethod("GetDistanceBetweenTwoPoints")]
public static void GetDistanceBetweenTwoPoints()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
PromptDoubleResult pDblRes;
pDblRes = acDoc.Editor.GetDistance("\nPick two points: ");
Application.ShowAlertDialog("\nDistance between points: " +
pDblRes.Value.ToString());
}