using Autodesk..ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
[CommandMethod("CalculateDefinedArea")]
public static void CalculateDefinedArea()
{
// 提示用户输入 5 个点 Prompt the user for 5 points
Document acDoc = Application.DocumentManager.MdiActiveDocument;
PromptPointResult pPtRes;
Point2dCollection colPt = new Point2dCollection();
PromptPointOptions pPtOpts = new PromptPointOptions("");
// 提示输入第一个点 Prompt for the first point
pPtOpts.Message = "\nSpecify first point: ";
pPtRes = acDoc.Editor.GetPoint(pPtOpts);
colPt.Add(new Point2d(pPtRes.Value.X, pPtRes.Value.Y));
// 如果用户按了 ESC 键或取消了命令就退出 Exit if the user presses ESC or cancels the command
if (pPtRes.Status == PromptStatus.Cancel) return;
int nCounter = 1;
while (nCounter <= 4)
{
// 提示指定下一个点 Prompt for the next points
switch(nCounter)
{
case 1:
pPtOpts.Message = "\nSpecify second point: ";
break;
case 2:
pPtOpts.Message = "\nSpecify third point: ";
break;
case 3:
pPtOpts.Message = "\nSpecify fourth point: ";
break;
case 4:
pPtOpts.Message = "\nSpecify fifth point: ";
break;
}
// 使用前一个点作为基点 Use the previous point as the base point
pPtOpts.UseBasePoint = true;
pPtOpts.BasePoint = pPtRes.Value;
//问题在这里,UseBasePoint 和BasePoint 属性指的是什么?请指教。
pPtRes = acDoc.Editor.GetPoint(pPtOpts);
colPt.Add(new Point2d(pPtRes.Value.X, pPtRes.Value.Y));
if (pPtRes.Status == PromptStatus.Cancel) return;
// 递增计数器 Increment the counter
nCounter = nCounter + 1;
}
// 用5个点创建多段线 Create a polyline with 5 points
using (Polyline acPoly = new Polyline())
{
acPoly.AddVertexAt(0, colPt[0], 0, 0, 0);
acPoly.AddVertexAt(1, colPt[1], 0, 0, 0);
acPoly.AddVertexAt(2, colPt[2], 0, 0, 0);
acPoly.AddVertexAt(3, colPt[3], 0, 0, 0);
acPoly.AddVertexAt(4, colPt[4], 0, 0, 0);
// 闭合多段线 Close the polyline
acPoly.Closed = true;
// 查询多段线的面积 Query the area of the polyline
Application.ShowAlertDialog("Area of polyline: " +
acPoly.Area.ToString());
// 销毁多段线 Dispose of the polyline
}
}
[/ol]