static ObjectIdCollection collection = new ObjectIdCollection();
static string commandName = "";
static bool IsCommandActive()
{
String str = (String)Application.GetSystemVariable("CMDNAMES");
if (String.Compare(commandName, str, true) != 0)
{
return true;
}
return false;
}
[CommandMethod("BoundaryCommandLine", CommandFlags.Session)]
static public void BoundaryCommandLine()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
PromptPointOptions options =
new PromptPointOptions("Pick a point");
PromptPointResult result = ed.GetPoint(options);
if (result.Status != PromptStatus.OK)
return;
double x = result.Value.X;
double y = result.Value.Y;
string strPt = x.ToString() + "," + y.ToString();
//put the database
Database db = doc.Database;
collection.Clear();
commandName = (String)Application.GetSystemVariable("CMDNAMES");
db.ObjectAppended +=
new ObjectEventHandler(Database_ObjectAppended);
//run the boundary command using ActiveX send command.
object[] dataArry = new object[1];
dataArry[0] = "-boundary " + strPt + " ";
doc.AcadDocument.GetType().InvokeMember("SendCommand",
BindingFlags.InvokeMethod,
null, doc.AcadDocument, dataArry);
if (IsCommandActive() == true)
{
dataArry[0] = "Yes ";
doc.AcadDocument.GetType().InvokeMember(
"SendCommand",
BindingFlags.InvokeMethod,
null, doc.AcadDocument, dataArry
);
}
db.ObjectAppended -=
new ObjectEventHandler(Database_ObjectAppended);
using (DocumentLock lock1 = doc.LockDocument())
{
using (Transaction ta =
db.TransactionManager.StartTransaction())
{
foreach (ObjectId id in collection)
{
if (id.IsErased == true)
{
continue;
}
DBObject obj = ta.GetObject(id,
OpenMode.ForRead);
if (obj is
Autodesk.AutoCAD.DatabaseServices.Polyline)
{
//
Autodesk.AutoCAD.DatabaseServices.Polyline pl
= (Autodesk.AutoCAD.DatabaseServices.Polyline)obj;
ed.WriteMessage("area is " +
pl.Area.ToString() + "\n");
}
else if (obj is
Autodesk.AutoCAD.DatabaseServices.Region)
{
Autodesk.AutoCAD.DatabaseServices.Region rg =
(Autodesk.AutoCAD.DatabaseServices.Region)obj;
ed.WriteMessage("area is "
+ rg.Area.ToString() + "\n");
}
}
ta.Commit();
}
}
}
static void Database_ObjectAppended(object sender, ObjectEventArgs e)
{
collection.Add(e.DBObject.ObjectId);
}