Plot dwg with centered attributes - problem
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
My routine inserts in modelspace different blocks with attributes in it with justification "Center".
After the blocks are inserted and the attributes are filled, it all looks good in Autocad.
Then I want to plot the DWG.
The problem is that the centered attributes in the plot are then justified to "Left" ????
When I run my routine the second time in the same drawing then it goes well.
Why is that? Am I missing something?
It looks like the attributes or blockreference isn't updated well in the database after insert?
Thanks,
Geert
Code:
public static BlockReference InsertBlockReference(Database database, string filename, string blockname, Point3d
insertionpoint, Scale3d scalefactor)
{
using (DocumentLock documentLock = Active.Document.LockDocument())
{
using (Transaction transaction = database.TransactionManager.StartTransaction())
{
ObjectId newBlockID = new ObjectId();
BlockTable blockTable = (BlockTable) database.BlockTableId.GetObject(OpenMode.ForRead); //Modelspace
using (BlockTableRecord blockTableRecord = (BlockTableRecord)transaction.GetObject
(database.CurrentSpaceId, OpenMode.ForWrite))
{
if (blockTable.Has(blockname))
{
newBlockID = blockTable[blockname];
}
else
{
newBlockID = GetBlockIDFromReadDwgFile(database, filename, blockname);
}
BlockReference newBlockReference = new BlockReference(insertionpoint, newBlockID);
newBlockReference.ScaleFactors = scalefactor;
blockTableRecord.AppendEntity(newBlockReference);
transaction.AddNewlyCreatedDBObject(newBlockReference, true);
//Attributes in Block
BlockTableRecord newBlockTableRecord = GetBlockTableRecord(newBlockID, blockname); if (newBlockTableRecord.HasAttributeDefinitions)
{
AddAttributesToNewBlockReference(newBlockTableRecord, newBlockReference);
}
transaction.Commit();
return newBlockReference;
}
}
}
}
private static void AddAttributesToNewBlockReference(BlockTableRecord blocktablerecord, BlockReference
blockreference)
{
using (Transaction transaction = blockreference.BlockId.Database.TransactionManager.StartTransaction())
{
foreach (ObjectId objectId in blocktablerecord)
{
Entity entity = (Entity) transaction.GetObject(objectId,OpenMode.ForRead);
if (entity is AttributeDefinition)
{
AttributeDefinition newAttributeDefinition = (AttributeDefinition)entity;
AttributeReference newAttributeReference = new AttributeReference();
newAttributeReference.SetAttributeFromBlock(newAttributeDefinition, blockreference.BlockTransform); ObjectId attributeReferenceID = blockreference.AttributeCollection.AppendAttribute
(newAttributeReference);
transaction.AddNewlyCreatedDBObject(newAttributeReference, true);
}
}
transaction.Commit();
}
}
public static void UpdateAttributeValues(BlockReference blockreference, Dictionary<string, string> dictionary)
{
using (Transaction transaction = blockreference.BlockId.Database.TransactionManager.StartTransaction())
{
AttributeCollection attributeCollection = blockreference.AttributeCollection;
foreach (ObjectId attributeId in attributeCollection)
{
AttributeReference attributeReference = (AttributeReference)attributeId.GetObject(OpenMode.ForWrite);
if (dictionary.ContainsKey(attributeReference.Tag))
{
attributeReference.TextString = dictionary[attributeReference.Tag];
}
}
transaction.Commit();
}
}

