using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
using AcRx = Autodesk.AutoCAD.Runtime;
using AcAp = Autodesk.AutoCAD.ApplicationServices;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using AcGe = Autodesk.AutoCAD.Geometry;
using AcEd = Autodesk.AutoCAD.EditorInput;
using AcInt = Autodesk.AutoCAD.Interop;
using AcIntCom = Autodesk.AutoCAD.Interop.Common;
[assembly: CommandClass(typeof(TestArea.MyCommands))]
namespace TestArea
{
public class MyCommands
{
[CommandMethod("TestArea", CommandFlags.Modal | CommandFlags.UsePickSet | CommandFlags.Redraw)]
public void test()
{
DocumentCollection acDocMgr = AcAp.Application.DocumentManager;
Document acDoc = acDocMgr.MdiActiveDocument;
Database acCurDb = acDoc.Database;
Editor ed = acDoc.Editor;
int polies = 0;
double totalArea = 0, totalAreaCOM = 0;
double totalLength = 0;
ObjectId[] ids;
using (DocumentLock docLock = acDoc.LockDocument()) {
using (Transaction tr = acCurDb.TransactionManager.StartTransaction()) {
TypedValue[] filList = new TypedValue[1] { new TypedValue((int)DxfCode.Start, "LWPOLYLINE") };
SelectionFilter filter = new SelectionFilter(filList);
PromptSelectionOptions opts = new PromptSelectionOptions();
opts.MessageForAdding = "Select polylines: ";
PromptSelectionResult res = ed.GetSelection(opts, filter);
if (res.Status != PromptStatus.OK)
return;
SelectionSet selSet = res.Value;
ids = selSet.GetObjectIds();
for (int i = 0; i < ids.Length; i++) {
Polyline pl = tr.GetObject(ids[i], OpenMode.ForRead) as Polyline;
AcIntCom.AcadLWPolyline plCOM = pl.AcadObject as AcIntCom.AcadLWPolyline;
totalArea += pl.Area;
totalAreaCOM += plCOM.Area;
totalLength += pl.Length;
pl.Highlight();
polies++;
}
tr.Commit();
}
}
ed.SetImpliedSelection(ids);
totalLength = Math.Round(totalLength, 4);
totalArea = Math.Round(totalArea, 4);
ed.WriteMessage("\nPolylines - {0}. Total Length = {1}. Total Area = {2}. Total Area COM = {3}.",
polies.ToString(), totalLength.ToString(), totalArea.ToString(), totalAreaCOM.ToString());
}
}
}