Attribute alignment in the existing block

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I am trying to run an Add ins in AutoCAD which modifies attribute text. The Text always aligns to the right, after the program is run.
I want it to to set to the basecenter position. It is very similar to this issue discussed here (https://forums.autodesk.com/t5/net/attribute-alignment/td-p/3182590) but the solution is not working in my case.
Please help me with the solution , here is my code:
private static void SetBlockAttributeValue(Autodesk.AutoCAD.DatabaseServices.Database acDb, string layout, string blockName, string attributeName, string attributeValue)
{
var center = new Point3d(1, 1, 0);
using (Transaction trans = acDb.TransactionManager.StartTransaction())
{
// HostApplicationServices.WorkingDatabase = acDb;
DBDictionary dbDictionary = (DBDictionary)trans.GetObject(acDb.LayoutDictionaryId, OpenMode.ForRead);
foreach (DictionaryEntry de in dbDictionary)
{
if (string.Compare(de.Key.ToString(), layout, true) == 0)
{
Layout layout1 = (Layout)trans.GetObject((ObjectId)de.Value, OpenMode.ForWrite);
BlockTable blockTable;
blockTable = trans.GetObject(acDb.BlockTableId, OpenMode.ForRead) as BlockTable;
// BlockTable blockTable = (BlockTable)trans.GetObject(acDb.BlockTableId, OpenMode.ForRead);
BlockTableRecord blockTableRecord = (BlockTableRecord)trans.GetObject(layout1.BlockTableRecordId, OpenMode.ForWrite);
using (AttributeDefinition acAttDef = new AttributeDefinition())
{
// acAttDef.Position = new Point3d(12, 4, 0);
acAttDef.TextString = attributeValue; //attributeValue;
acAttDef.Height = 1;
acAttDef.Justify = AttachmentPoint.BaseCenter;
blockTableRecord.AppendEntity(acAttDef);
blockTable.UpgradeOpen();
blockTable.Add(blockTableRecord);
foreach (ObjectId entityId in blockTableRecord)
{
Entity entity = (Entity)trans.GetObject(entityId, OpenMode.ForRead);
if (entity.GetType() == typeof(BlockReference))
{
BlockReference blockReference = (BlockReference)entity;
AttributeCollection collection = blockReference.AttributeCollection;
foreach (ObjectId id in collection)
{
DBObject dbObject = trans.GetObject(id, OpenMode.ForWrite);
AttributeReference reference = (AttributeReference)dbObject;
if (reference != null)
{
if (string.Compare(reference.Tag, attributeName, true) == 0)
{
reference.UpgradeOpen();
// reference.TextString = attributeValue;
reference.SetAttributeFromBlock(acAttDef, blockReference.BlockTransform);
// reference.Position = acAttDef.Position.TransformBy(blockReference.BlockTransform);
reference.Justify = AttachmentPoint.BaseCenter;
reference.AlignmentPoint = center;
reference.AdjustAlignment(acDb);
// reference.TextString = attributeValue;
reference.TextString = acAttDef.TextString;
blockReference.AttributeCollection.AppendAttribute(reference);
}
}
}
}
}
}
}
}
trans.Commit();
//HostApplicationServices.WorkingDatabase = acDb;
}
}