Attribute alignment in the existing block

Attribute alignment in the existing block

Anonymous
Not applicable
959 Views
2 Replies
Message 1 of 3

Attribute alignment in the existing block

Anonymous
Not applicable

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

0 Likes
960 Views
2 Replies
Replies (2)
Message 2 of 3

_gile
Consultant
Consultant

Hi,

 

Sorry but your code doesn't help to understand your request.

 

It add an attribute definition to a layout BlockTableRecord and then add a new attribute reference to all block references in this layout.

 

 

PS: please, when you post some code, use the "Insert code" button: </>



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 3

Anonymous
Not applicable

Adding HostApplicationServices.WorkingDatabase  to my code fixed the issue.

I got the solution from this link https://forums.autodesk.com/t5/net/modify-attributes/m-p/5631971/highlight/true#M44447

 

 This happens when AutoCAD is not expecting to be dealing with a database that is not associated with its current database list, or when AutoCAD wrongly decides that your side database is actually associated with its database list thus keeping a reference to the database. If this happens, you can set the working database yourself, but be *very careful* – make sure you set and reset the working database only where you need to do it, keep the redirection short and sweet and never forget to set it back

 

 

Thanks for your reply _gile. I am new to AutoCAD so I was using various bits of code to make it work.

0 Likes