Author Topic: Events in AutoCAD  (Read 2069 times)

0 Members and 1 Guest are viewing this topic.

Augusto

  • Newt
  • Posts: 75
Events in AutoCAD
« on: June 05, 2023, 09:07:18 PM »
Hello Everyone,

Hope you're all doing well.

I'm here once again in need of your expertise and guidance. I have a question related to events in AutoCAD.

Is there any event related to the database or object that allows me to identify a selected object, specifically a BlockReference in my case?

For some modification commands, I plan to transfer the selection from the block to the polyline. I've embedded information pertaining to the selection object in the code below.
Code - C#: [Select]
  1. public void StoreBlockData(ObjectId blockId, double diameter, string type, ObjectId ownerId)
  2. {
  3.     using (Transaction tr = blockId.Database.TransactionManager.StartTransaction())
  4.     {
  5.         DBObject obj = tr.GetObject(blockId, OpenMode.ForWrite);
  6.  
  7.         DBDictionary extDict;
  8.         if (obj.ExtensionDictionary.IsNull)
  9.         {
  10.             obj.CreateExtensionDictionary();
  11.             extDict = (DBDictionary)tr.GetObject(obj.ExtensionDictionary, OpenMode.ForWrite);
  12.         }
  13.         else
  14.         {
  15.             extDict = (DBDictionary)tr.GetObject(obj.ExtensionDictionary, OpenMode.ForWrite);
  16.         }
  17.  
  18.         // Store Diameter
  19.         Xrecord diameterXRec = new Xrecord { Data = new ResultBuffer(new TypedValue((int)DxfCode.Real, diameter)) };
  20.         extDict.SetAt("Diameter", diameterXRec);
  21.         tr.AddNewlyCreatedDBObject(diameterXRec, true);
  22.  
  23.         // Store Type
  24.         Xrecord typeXRec = new Xrecord { Data = new ResultBuffer(new TypedValue((int)DxfCode.Text, type)) };
  25.         extDict.SetAt("Type", typeXRec);
  26.         tr.AddNewlyCreatedDBObject(typeXRec, true);
  27.  
  28.         // Store Owner
  29.         Xrecord ownerXRec = new Xrecord { Data = new ResultBuffer(new TypedValue((int)DxfCode.Text, ownerId.ToString())) };
  30.         extDict.SetAt("Owner", ownerXRec);
  31.         tr.AddNewlyCreatedDBObject(ownerXRec, true);
  32.  
  33.         tr.Commit();
  34.     }
  35. }
  36.  
  37. // Get XRecord
  38.         public Dictionary<string, object> GetBlockData(ObjectId blockId)
  39.         {
  40.             Dictionary<string, object> blockData = new Dictionary<string, object>();
  41.  
  42.             using (Transaction tr = blockId.Database.TransactionManager.StartTransaction())
  43.             {
  44.                 DBObject obj = tr.GetObject(blockId, OpenMode.ForRead);
  45.  
  46.                 if (obj.ExtensionDictionary.IsNull)
  47.                     return blockData;  // return empty dictionary if no data
  48.  
  49.                 DBDictionary extDict = (DBDictionary)tr.GetObject(obj.ExtensionDictionary, OpenMode.ForRead);
  50.  
  51.                 // Retrieve the Diameter, Type and Owner XRecords and add them to the dictionary
  52.                 foreach (string key in new[] { "Diameter", "Type", "Owner" })
  53.                 {
  54.                     if (extDict.Contains(key))
  55.                     {
  56.                         Xrecord xRec = (Xrecord)tr.GetObject(extDict.GetAt(key), OpenMode.ForRead);
  57.                         TypedValue value = xRec.Data.AsArray()[0];
  58.  
  59.                         if (key == "Owner")
  60.                         {
  61.                             Handle handle = new Handle(Convert.ToInt64((string)value.Value));
  62.                             blockData[key] = blockId.Database.GetObjectId(false, handle, 0);
  63.                         }
  64.                         else
  65.                             blockData[key] = value.Value;
  66.                     }
  67.                 }
  68.  
  69.                 tr.Commit();
  70.             }
  71.  
  72.             return blockData;
  73.         }
  74.  
  75.  

I'm sure your suggestions and insights will be of great assistance, as they have always been.
Thanks a lot in advance.

Best Regards,
Luís Augusto.

mod:kdub:to [ code=csharp ]
« Last Edit: June 05, 2023, 11:46:46 PM by kdub_nz »

Augusto

  • Newt
  • Posts: 75
Re: Events in AutoCAD
« Reply #1 on: June 05, 2023, 09:17:14 PM »
Dictionary<string, object> blockData = GetBlockData(blockId);
ObjectId owner = (ObjectId)blockData["Owner"];