如何在事件处理程序中取回ObjectID?
我想对用户修改的对象做一些事情,但我就是不能让它在我的事件处理程序中工作,而且在我看来,问题是ObjectID不正确。在第二个代码片段中,我做了一些测试,一切都如我所愿。如何正确访问ObjectID?预先感谢。
public void acPolyMod(object senderObj, EventArgs evtArgs)
{
Tables tables = HostMapApplicationServices.Application.ActiveProject.ODTables;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Entity ent = senderObj as Entity;
ObjectId id = ent.ObjectId;
Autodesk.Gis.Map.ObjectData.Table table = tables;
Records records = table.GetObjectTableRecords(0, id, Autodesk.Gis.Map.Constants.OpenMode.OpenForWrite, true);
if (records.Count == 0)
{
AddODRecord(tables, m_tableName, id, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
else
{
UpdateOD(m_tableName, id, "Date", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
}
我的测试代码:
public void asd()
{
Tables tables = HostMapApplicationServices.Application.ActiveProject.ODTables;
Editor AcadEditor = Application.DocumentManager.MdiActiveDocument.Editor;
AcadEditor.WriteMessage("\n Please select an entity to add a record: ");
PromptSelectionOptions options = new PromptSelectionOptions();
options.SingleOnly = true;
options.SinglePickInSpace = true;
PromptSelectionResult result = AcadEditor.GetSelection(options);
ObjectId[] ids = result.Value.GetObjectIds();
AddODRecord(tables, m_tableName, ids, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
UpdateOD(m_tableName, ids, "Date", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
**** Hidden Message ***** 您可以共享AddODRecord和UpdateOD这两个函数吗? 是的,当然。我做了更多的测试,问题似乎是我试图在事件hendler中运行这个。我这样想是因为<pre>Entity ent = senderObj as Entity;
ObjectId id = ent.ObjectId;
和
ObjectId[] ids = result.Value.GetObjectIds();
ids
转换为字符串时,会吐出相同的ObjectID号
public bool AddODRecord(Tables tables, string tableName, ObjectId id, string value)
{
try
{
Autodesk.Gis.Map.ObjectData.Table table = tables;
// Create and initialize an record
Record tblRcd = Record.Create();
table.InitRecord(tblRcd);
MapValue val = tblRcd; // String type
val.Assign(value);
table.AddRecord(tblRcd, id);
return true;
}
catch (MapException)
{
return false;
}
}
public void UpdateOD(string TableName, ObjectId objectID, string columnName, string value)
{
Autodesk.Gis.Map.ObjectData.Tables tables = Autodesk.Gis.Map.HostMapApplicationServices.Application.ActiveProject.ODTables;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
using (Records records = tables.GetObjectRecords(0, objectID, Autodesk.Gis.Map.Constants.OpenMode.OpenForWrite, false))
{
if (records.Count == 0)
{
ed.WriteMessage("\nData attachment failed.");
return;
}
foreach (Record Recd in records)
{
Autodesk.Gis.Map.ObjectData.Table tbl = tables;
if (tbl.Name.CompareTo(TableName) == 0)
{
for (int i = 0; i < Recd.Count; i++)
{
FieldDefinitions tblDefs = tbl.FieldDefinitions;
FieldDefinition column = null;
column = tblDefs;
if (column.Name.CompareTo(columnName) == 0)
{
Recd.Assign(value);
records.UpdateRecord(Recd);
break;
}
}
}
}
}
谢谢!是的,当编辑在事件处理程序之外完成时,您应该对实体进行修改。
页:
[1]