Message 1 of 4
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have a blockreference with some TextString attribute value, if I try to take its position, i am getting the block.Bounds value in between somewhere in the text, but i want its original center without any attribute, I have this code which is working perfectly, but is there any other simple way to do this?
public static Point3d GetBlockGeometricCenterWithoutAttribute(BlockReference originalBlock)
{
// Dictionary to store original attribute values
Dictionary<AttributeReference, string> originalAttributes = new Dictionary<AttributeReference, string>();
// Store the original attribute values
using (Transaction trans = originalBlock.Database.TransactionManager.StartTransaction())
{
foreach (ObjectId attId in originalBlock.AttributeCollection)
{
AttributeReference attrRef = (AttributeReference)trans.GetObject(attId, OpenMode.ForWrite);
// Store the original value
originalAttributes[attrRef] = attrRef.TextString;
// Clear the attribute value
attrRef.TextString = string.Empty;
}
// Commit the transaction (attributes are cleared)
trans.Commit();
}
// Get the geometric center (position)
Point3d center = GetBlockCenter(originalBlock);
// Restore the original attribute values
using (Transaction trans = originalBlock.Database.TransactionManager.StartTransaction())
{
foreach (var attrPair in originalAttributes)
{
AttributeReference attrRef = attrPair.Key;
string originalValue = attrPair.Value;
// Restore the original value
if (!attrRef.IsErased)
{
attrRef.TextString = originalValue;
}
}
// Commit the transaction (attributes are restored)
trans.Commit();
}
originalAttributes.Clear();
return center; // Return the geometric center of the block
}
public static Point3d GetBlockCenter(BlockReference table)
{
Point3d cornerPointMax = table.GeometricExtents.MaxPoint;
Point3d cornerPointMin = table.GeometricExtents.MinPoint;
return new Point3d((cornerPointMax.X + cornerPointMin.X) / 2, (cornerPointMax.Y + cornerPointMin.Y) / 2, 0);
}
Solved! Go to Solution.