Area プロパティを使用すると、円弧、円、楕円、ライトウェイト ポリライン、ポリライン、リージョン、ハッチング、平面で閉じられたスプライン、および曲線の基点タイプから派生したその他の図形の領域を見つけることができます。
複数のオブジェクトの合計面積を計算する必要がある場合、一連のリージョンに Boolean メソッドを使用して、希望のエリアを表す単一のリージョンを取得すれば、合計を求められます。この単一リージョンから、Area プロパティを使用してその面積を取得できます。算出される面積は、クエリーするオブジェクトのタイプによって異なります。
計算する面積がユーザ指定の点に基づいている場合、ライトウェイト ポリラインのようなメモリ内オブジェクトを作成し、破棄する前にそのオブジェクトの面積をクエリーします。次の手順では、この実行方法を説明します。
この例ではユーザに 5 つの点を入力するようプロンプト表示します。そうすると、入力された点からポリラインが作成されます。ポリラインは閉じられており、ポリラインの面積がメッセージ ボックスに表示されます。ポリラインはブロックに追加されていないので、コマンドが終了する前に破棄する必要があります。
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
}
}