Author Topic: How do I get ObjectID back in an event handler?  (Read 1396 times)

0 Members and 1 Guest are viewing this topic.

tomipac

  • Mosquito
  • Posts: 3
How do I get ObjectID back in an event handler?
« 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. :-)

Code: [Select]
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:

Code: [Select]
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"));
           
        }

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Re: How do I get ObjectID back in an event handler?
« Reply #1 on: July 10, 2021, 12:00:37 PM »
Can you share the 2 functions AddODRecord & UpdateOD?

tomipac

  • Mosquito
  • Posts: 3
Re: How do I get ObjectID back in an event handler?
« Reply #2 on: July 10, 2021, 12:57:51 PM »
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
Code: [Select]
Entity ent = senderObj as Entity;
            ObjectId id = ent.ObjectId;
and
Code: [Select]
ObjectId[] ids = result.Value.GetObjectIds();
ids[0]
when converted to string spits out the same ObjectID number.

Code: [Select]
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;
            }
        }
Code: [Select]
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;
                            }
                        }
                    }
                }
            }
       

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Re: How do I get ObjectID back in an event handler?
« Reply #3 on: July 10, 2021, 03:18:10 PM »
Thanks! And yes, you should be making modifications to entities when edits have been completed outside of the event handler.