This crosses many classes so I will try and show the relevant code. This is where the attribute sync is called
//Legacy titleblock was found
if (oldTbList != null)
{
using (Transaction acTrans = acDb.TransactionManager.StartTransaction())
{
BlockTable acBlkTbl = (BlockTable)acTrans.GetObject(acDb.BlockTableId, OpenMode.ForRead);
BlockTableRecord acBlkTblRec = (BlockTableRecord)acTrans.GetObject(acBlkTbl[oldTbList[0].Name], OpenMode.ForRead, false);
acBlkTblRec.SynchronizeAttributes();
acTrans.Commit();
}
_TbId = ReplaceTB(oldTbList, TbId, replaceLayout);
}
I committed the transaction here. If I don't commit then I get the old Attribute Collection later. On line 100 in the below code is where I get eWasErased.
private ObjectId ReplaceTB(List<TitleBlkLegacyProps> oldTbList, ObjectId tbId, bool delLayout = false)
{
string[] blkName = { newTBName };
//Import the new Title Block
SysLog.WriteLineToLog(DateTime.Now.ToString("yyyy/MM/dd") + " " + DateTime.Now.ToString("hh:mm:ss tt - ") + "Importing replacement title block");
AcDb.ImportMultiBlocks(blkName, TBFullPath);
ObjectId blkRecId = ObjectId.Null;
ObjectId acBlkRefInsertId = ObjectId.Null;
using (Transaction acTrans = AcDb.TransactionManager.StartTransaction())
{
BlockTable acTblItm = (BlockTable)acTrans.GetObject(AcDb.BlockTableId, OpenMode.ForRead);
//Get the Title block definition (BlockRecord)
acBlkTblRec = (BlockTableRecord)acTrans.GetObject(acTblItm[newTBName], OpenMode.ForRead);
blkRecId = acBlkTblRec.Id;
//If the title block definition was found
if (blkRecId != ObjectId.Null)
{
using (BlockReference acBlkRefInsert = new BlockReference(new Point3d(0, 0, 0), blkRecId))
{
acBlkRefInsert.Layer = tbLayer;
BlockTableRecord acSpace;
if (delLayout == true)
{
//TODO get from DB
//Create New Layout, get the ID and block table record
AcLayout acLayout = new AcLayout("Add", "MRD-Print");
ObjectId acLayOID = acLayout.LayoutIdNew;
AcLayoutBtr = acLayout.LayoutBtrId;
//Get the Existing Paperspace Viewport
acLayout.GetVport(AcLayoutNameOld);
AcVprt = acLayout.ViewportOld;
//Create New viewport in New layout
new AcVprt(AcDb);
}
acSpace = (BlockTableRecord)acTrans.GetObject(AcLayoutBtr.ObjectId, OpenMode.ForWrite);
acSpace.AppendEntity(acBlkRefInsert);
acTrans.AddNewlyCreatedDBObject(acBlkRefInsert, true);
acBlkRefInsertId = acBlkRefInsert.Id;
foreach (ObjectId id in acBlkTblRec)
{
DBObject obj = id.GetObject(OpenMode.ForRead);
AttributeDefinition attDef = obj as AttributeDefinition;
if ((attDef != null) && (!attDef.Constant))
{
using (AttributeReference attRef = new AttributeReference())
{
attRef.SetAttributeFromBlock(attDef, acBlkRefInsert.BlockTransform);
attRef.TextString = "";
acBlkRefInsert.AttributeCollection.AppendAttribute(attRef);
acTrans.AddNewlyCreatedDBObject(attRef, true);
}
}
}
}
BlockReference acBlkRefOld = (BlockReference)acTrans.GetObject(tbId, OpenMode.ForWrite);
BlockReference acBlkRefNew = (BlockReference)acTrans.GetObject(acBlkRefInsertId, OpenMode.ForWrite);
Dictionary<string, string> acBlkRefAttDict = acBlkRefOld.GetAttToDict(acBlkTblRec);
AttributeCollection acAttColOld = acBlkRefOld.AttributeCollection;
AttributeCollection acAttColNew = acBlkRefNew.AttributeCollection;
// TODO Get From DB create external clean up format RunScript???
string[] exclude = { "", " ", " ", ".", "..", "...", "-", "_" };
string replaceChar = ".";
string tbSaveAsTag = "DWGNO";
for (int i = 0; i <= oldTbList.Count - 1; i++)
{
string[] attValue = new string[1];
string[] attTag = new string[1];
string[] attCombine = new string[1];
if (oldTbList[i].AttOldIndex.Contains(";"))
{
attValue = oldTbList[i].AttOldIndex.Split(';');
attTag = oldTbList[i].AttOldTag.Split(';');
attCombine = oldTbList[i].CombineChar.Split(';');
}
else
{
attValue[0] = oldTbList[i].AttOldIndex;
attTag[0] = oldTbList[i].AttOldTag;
attCombine[0] = oldTbList[i].CombineChar;
}
for (int j = 0; j <= attValue.Length - 1; j++)
{
int attOld = Convert.ToInt32(attValue[j]);
AttributeReference attRefOld = (AttributeReference)acTrans.GetObject(acAttColOld[attOld], OpenMode.ForWrite);
if (attRefOld.Tag.Equals(attTag[j], StringComparison.OrdinalIgnoreCase))
{
int attNew = Convert.ToInt32(oldTbList[i].AttNewIndex);
AttributeReference attRefNew = (AttributeReference)acTrans.GetObject(acAttColNew[attNew], OpenMode.ForWrite);
foreach (string excludeStr in exclude)
{
if (attRefOld.TextString.Equals(excludeStr, StringComparison.OrdinalIgnoreCase))
{
attRefOld.TextString = "";
}
}
attRefNew.TextString = attRefNew.TextString + attCombine[j] + attRefOld.TextString;
}
}
}
acBlkRefOld.Erase();
new AcLayout("DeleteContains", acLayNameOld: "Layer");
if (delLayout == true)
{
new AcLayout("Delete", acLayNameOld: AcLayoutNameOld);
}
foreach (ObjectId attId in acAttColNew)
{
AttributeReference attRef = (AttributeReference)acTrans.GetObject(attId, OpenMode.ForWrite);
foreach (string excludeStr in exclude)
{
if (attRef.TextString.Equals(excludeStr, StringComparison.OrdinalIgnoreCase))
{
attRef.TextString = replaceChar;
}
}
if (attRef.Tag.Equals(tbSaveAsTag, StringComparison.OrdinalIgnoreCase))
{
FnClnUp fnClnUp = new FnClnUp(attRef.TextString);
attRef.TextString = fnClnUp.Filename;
}
}
}
else
{
// TODO if the block wasnt found
}
acTrans.Commit();
}
tbId = acBlkRefInsertId;
return acBlkRefInsertId;
}