Calculate a Defined Area
 
 
 

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. Use the GetPoint method in a loop to obtain the points from the user.
  2. Create a lightweight polyline from the points provided by the user. Create a new Polyline object. Specify the number of vertices and the points they should be at.
  3. Use the Area property to obtain the area of the newly created polyline.
  4. Dispose of the polyline using its Dispose method.

Calculate the area defined by points entered from the user

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()
  '' 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))
 
  '' 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
 
  '' 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()
{
  // 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));
 
  // 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;
  }
 
  // 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 Code Reference