/*ClassLibrary code*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
namespace ClassLibrary1
{
public class Class1
{
[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;
acPoly.Length.ToString();
// Query the area of the polyline
Application.ShowAlertDialog("Area of polyline: " +
acPoly.Area.ToString());
// Dispose of the polyline
}
}
[CommandMethod("CalulatePerimeter")]
public static void CalculateDefinedPerimeter()
{
// 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;
acPoly.Length.ToString();
// Query the area of the polyline
Application.ShowAlertDialog("Length of polyline:-" + acPoly.Length.ToString());
// Dispose of the polyline
}
}
}
}
I'm able to calculate Area, iPerimeter from this.ButI can't get exact vertices for marking to get the proper measurements. In addition i would like to have the output to be displayed in a pop-up text window like below
x= 66 x'=66
y=60 y'=60
Area= 3960
It is better to have an option to get this parameter in an Excel file too.
Tool : Cadsofttool
Language : C#
Framwork : 3.5
.Net : 2012
Also another issue, I couldn't open any AutoCAD dwg file with this program.so i need a code that must be compatible to above program to open DWG file in this Cadsofttool.Or give me a solution to implement the above-mentioned function in CAD editor.