Message 1 of 5

Not applicable
10-06-2011
10:16 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I want to add a block with a center aligned attribute to drawing file that is not open in the autocad editor; however, the attribute alignment doesn't display when i open the drawing. But when i open the attribute editor i can verify that the alignment is set properly and then after closing the attribute editor window the alignment actually displays correctly even though no changes were made.
I read a number of other posts with the same or similar issue, but i still have been unable to get mine to display right.
My code is below. What do I need to do to force the attribute alignment to display correctly?
private void centerAttribute() { var path = @"c:\test.dwg"; using (var database = new Database(false, true)) { database.ReadDwgFile(path, FileShare.ReadWrite, false, password: ""); Transaction transaction = null; try { transaction = database.TransactionManager.StartTransaction(); var previousDatabase = HostApplicationServices.WorkingDatabase; HostApplicationServices.WorkingDatabase = database; // add block var block = new BlockTableRecord() { Name = "TestBlock3" }; var blockTable = transaction.GetObject(database.BlockTableId, OpenMode.ForWrite) as BlockTable; var blockId = blockTable.Add(block); transaction.AddNewlyCreatedDBObject(block, true); // add block reference var blockReference = new BlockReference(Point3d.Origin, block.ObjectId); var modelSpace = transaction.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; modelSpace.AppendEntity(blockReference); transaction.AddNewlyCreatedDBObject(blockReference, true); // add attribute var center = new Point3d(1, 1, 0); var attribute = new AttributeReference(center, "Test", "TestAttribute", database.Textstyle); attribute.Justify = AttachmentPoint.BaseCenter; attribute.AlignmentPoint = center; attribute.AdjustAlignment(database); attribute.Position = center; blockReference.AttributeCollection.AppendAttribute(attribute); transaction.AddNewlyCreatedDBObject(attribute, true); database.SaveAs(path, DwgVersion.AC1021); transaction.Commit(); HostApplicationServices.WorkingDatabase = previousDatabase; } catch (Exception exception) { transaction.Abort(); System.Diagnostics.Debug.WriteLine(exception.Message); } } }
Solved! Go to Solution.