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;
}