By Virupaksha Aithal
Below code shows the procedure to run the boundary command through command line. Note, code uses ActiveX API SendCommand to run the command. To avoid the different interop’s issue, code uses late binding technique. Command is run in session mode, so that issued boundary command is run synchronously.
Also, refer blog on identifying the boundary using API here
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);
}

Leave a Reply to Jason HuffineCancel reply