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.
public void StoreBlockData(ObjectId blockId, double diameter, string type, ObjectId ownerId)
{
using (Transaction tr = blockId.Database.TransactionManager.StartTransaction())
{
DBObject obj = tr.GetObject(blockId, OpenMode.ForWrite);
DBDictionary extDict;
if (obj.ExtensionDictionary.IsNull)
{
obj.CreateExtensionDictionary();
extDict = (DBDictionary)tr.GetObject(obj.ExtensionDictionary, OpenMode.ForWrite);
}
else
{
extDict = (DBDictionary)tr.GetObject(obj.ExtensionDictionary, OpenMode.ForWrite);
}
// Store Diameter
Xrecord diameterXRec
= new Xrecord
{ Data
= new ResultBuffer
(new TypedValue
((int)DxfCode
.Real, diameter
)) }; extDict.SetAt("Diameter", diameterXRec);
tr.AddNewlyCreatedDBObject(diameterXRec, true);
// Store Type
Xrecord typeXRec
= new Xrecord
{ Data
= new ResultBuffer
(new TypedValue
((int)DxfCode
.Text, type
)) }; extDict.SetAt("Type", typeXRec);
tr.AddNewlyCreatedDBObject(typeXRec, true);
// Store Owner
Xrecord ownerXRec
= new Xrecord
{ Data
= new ResultBuffer
(new TypedValue
((int)DxfCode
.Text, ownerId
.ToString())) }; extDict.SetAt("Owner", ownerXRec);
tr.AddNewlyCreatedDBObject(ownerXRec, true);
tr.Commit();
}
}
// Get XRecord
public Dictionary<string, object> GetBlockData(ObjectId blockId)
{
Dictionary
<string,
object> blockData
= new Dictionary
<string,
object>();
using (Transaction tr = blockId.Database.TransactionManager.StartTransaction())
{
DBObject obj = tr.GetObject(blockId, OpenMode.ForRead);
if (obj.ExtensionDictionary.IsNull)
return blockData; // return empty dictionary if no data
DBDictionary extDict = (DBDictionary)tr.GetObject(obj.ExtensionDictionary, OpenMode.ForRead);
// Retrieve the Diameter, Type and Owner XRecords and add them to the dictionary
foreach (string key
in new[] { "Diameter",
"Type",
"Owner" }) {
if (extDict.Contains(key))
{
Xrecord xRec = (Xrecord)tr.GetObject(extDict.GetAt(key), OpenMode.ForRead);
TypedValue value = xRec.Data.AsArray()[0];
if (key == "Owner")
{
Handle handle
= new Handle
(Convert
.ToInt64((string)value.Value)); blockData[key] = blockId.Database.GetObjectId(false, handle, 0);
}
else
blockData[key] = value.Value;
}
}
tr.Commit();
}
return blockData;
}
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 ]