计算已定义的面积
 
 

If the area you want to calculate is based on user specified points, you might consider creating an in memory object such as a lightweight polyline, and then query of the area of the object before discarding it. The following steps explain how you might accomplish this:

如果想计算用户输入的点定义的图形的面积,可以考虑在内存中创建一个对象,像优化轻量多段线,然后在它删除前查询它的面积。下面是怎么样实现这个功能的步骤的说明:

  1. 在一个循环中使用 GetPoint 方法获取用户指定的点。
  2. 根据用户提供的点创建优化多段线。创建新的多段线对象,然后指定顶点数和点。
  3. 使用 Area 属性获取新创建的多段线的面积。
  4. 使用 Dispose 方法删除多段线。

计算用户输入的点定义的面积

This example prompts the user to enter five points. A polyline is then created out of the points entered. The polyline is closed, and the area of the polyline is displayed in a message box. Since the polyline is not added to a block, it needs to be disposed before the command ends.

本例提示用户输入五个点,然后根据输入的点创建多段线。该多段线是闭合的,所形成的面积显示在消息框中。因为多段线不需要添加到块中,所以在命令结束前需要删除它。

VB.NET

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime
 
<CommandMethod("CalculateDefinedArea")> _
Public Sub CalculateDefinedArea()
  '' 提示用户输入 5 个点    Prompt the user for 5 points
  Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
 
  Dim pPtRes As PromptPointResult
  Dim colPt As Point2dCollection = New Point2dCollection
  Dim pPtOpts As PromptPointOptions = New PromptPointOptions("")
 
  '' 提示输入第一个点   Prompt for the first point
  pPtOpts.Message = vbLf & "Specify 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 Then Exit Sub
 
  Dim nCounter As Integer = 1
 
  While (nCounter <= 4)
      '' 提示指定下一个点   Prompt for the next points
      Select Case nCounter
          Case 1
              pPtOpts.Message = vbLf & "Specify second point: "
          Case 2
              pPtOpts.Message = vbLf & "Specify third point: "
          Case 3
              pPtOpts.Message = vbLf & "Specify fourth point: "
          Case 4
              pPtOpts.Message = vbLf & "Specify fifth point: "
      End Select
 
      '' 使用前一个点作为基点   Use the previous point as the base point
      pPtOpts.UseBasePoint = True
      pPtOpts.BasePoint = pPtRes.Value
 
      pPtRes = acDoc.Editor.GetPoint(pPtOpts)
      colPt.Add(New Point2d(pPtRes.Value.X, pPtRes.Value.Y))
 
      If pPtRes.Status = PromptStatus.Cancel Then Exit Sub
 
      '' 递增计数器    Increment the counter
      nCounter = nCounter + 1
  End While
 
  '' 用5个点创建多段线   Create a polyline with 5 points
  Using acPoly As Polyline = 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
  End Using
End Sub

C#

using Autodesk.AutoCAD.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;
 
      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
  }
}
VBA/ActiveX 代码参考