TheSwamp
Code Red => .NET => Topic started by: tomipac on July 10, 2021, 03:50:48 AM
-
I would like to do a couple of things with objects that the user modifies, but i just cant get it to work inside my event handler and it seems to me that the problem is with an incorrect ObjectID. In the second code snippet, which i made to test things, everything works as i would like it to. How do i access the ObjectID properly?
Thanks in advance. :-)
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[m_tableName];
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"));
}
}
My test code:
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[0], DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
UpdateOD(m_tableName, ids[0], "Date", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
-
Can you share the 2 functions AddODRecord & UpdateOD?
-
Yes, of course. I was doing a little bit more testing, and it seems like the problem is that im trying to run this inside the event hendler. Im thinking this becouse
Entity ent = senderObj as Entity;
ObjectId id = ent.ObjectId;
and ObjectId[] ids = result.Value.GetObjectIds();
ids[0]
when converted to string spits out the same ObjectID number.
public bool AddODRecord(Tables tables, string tableName, ObjectId id, string value)
{
try
{
Autodesk.Gis.Map.ObjectData.Table table = tables[tableName];
// Create and initialize an record
Record tblRcd = Record.Create();
table.InitRecord(tblRcd);
MapValue val = tblRcd[0]; // 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[Recd.TableName];
if (tbl.Name.CompareTo(TableName) == 0)
{
for (int i = 0; i < Recd.Count; i++)
{
FieldDefinitions tblDefs = tbl.FieldDefinitions;
FieldDefinition column = null;
column = tblDefs[i];
if (column.Name.CompareTo(columnName) == 0)
{
Recd[i].Assign(value);
records.UpdateRecord(Recd);
break;
}
}
}
}
}
-
Thanks! And yes, you should be making modifications to entities when edits have been completed outside of the event handler.