namespace PlinesArea
{
public class PlineArea
{
public Dictionary<string, List<ObjectId>> GetObjects()
{
Document acDoc = AcAp.Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
BlockTable acBlTbl = (BlockTable)acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead);
BlockTableRecord acBlTblRec = (BlockTableRecord)acTrans.GetObject(acBlTbl[BlockTableRecord.ModelSpace], OpenMode.ForRead);
Dictionary<string, List<ObjectId>> dict = new Dictionary<string, List<ObjectId>>();
foreach (ObjectId item in acBlTblRec)
{
if (item.IsValid && !item.IsErased)
if (!dict.Keys.Contains(item.ObjectClass.Name))
dict.Add(item.ObjectClass.Name, new List<ObjectId>() { item });
else
dict[item.ObjectClass.Name].Add(item);
}
return dict;
}
}
[CommandMethod("PlinesAreaToEd")]
public void Main()
{
var doc = AcApp.DocumentManager.MdiActiveDocument;
var ed = doc.Editor;
Database db = doc.Database;
try
{
Dictionary<string, List<ObjectId>> dict = GetObjects();
var pso = new PromptSelectionOptions();
pso.MessageForAdding = "\nSelect polylines: ";
var filList = new TypedValue[1] { new TypedValue((int)DxfCode.Start, "*POLYLINE") };
SelectionFilter sf = new SelectionFilter(filList);
PromptSelectionResult psr = ed.GetSelection(pso, sf);
if (psr.Status == PromptStatus.OK)
{
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
var selSet = psr.Value;
var ids = selSet.GetObjectIds();
int i = 1;
foreach (ObjectId objId in ids)
{
var pl = tr.GetObject(objId, OpenMode.ForRead) as Polyline;
if (pl.Closed)
using (AcDb.MPolygon mp = new AcDb.MPolygon())
{
try
{
foreach (ObjectId item in dict["AcDbText"])
{
using (Transaction t = db.TransactionManager.StartTransaction())
{
DBText text = (DBText)t.GetObject(item, OpenMode.ForWrite);
DBPoint acPoint = new DBPoint(new Point3d(Math.Round(text.Position.X), Math.Round(text.Position.Y), Math.Round(text.Position.Z)));
mp.AppendLoopFromBoundary(pl, true, AcGe.Tolerance.Global.EqualPoint);
/*if (mp.IsPointOnLoopBoundary(acPoint.Position, 0, AcGe.Tolerance.Global.EqualPoint))
{
ed.WriteMessage("\n: " + text.TextString + ". Площадь: " + (pl.Area / 1000000).ToString() + " Периметр:" + (pl.Length / 1000).ToString());
}*/
if (mp.IsPointInsideMPolygon(acPoint.Position, AcGe.Tolerance.Global.EqualPoint).Count > 0)
{
ed.WriteMessage("\n" + text.TextString + ". Площадь: " + (pl.Area / 1000000).ToString() + " Периметр:" + (pl.Length / 1000).ToString());
}
else
{
}
}
}
}
catch { }
}
tr.Commit();
}
}
}
}
catch (System.Exception ex)
{
ed.WriteMessage("\n" + ex.Message);
}
}
}
}