Hello
I want to update a block attribute after inserting the block. The block is getting inserted. When I try to obtain Attribute collection I see nothing. I have Pasted the code below. Please advice the solution
Code:
[CommandMethod("BlockAtt")]
public void BlockAtt()
{
//AcadApplication acAPP = new AcadApplication();
ObjectId msId;
string BlockPATH = @"C:\AutoGen\Library\LWP\Rail Reaction\React_CarRail C - CwtR.dwg";
//= new AcadApplication();
Document acD = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acD.Database;
//string BlockPATH = @"C:\AutoGen\Library\LWP\Jamb\BS\CO - E-102 - BS - Plan.dwg";
Point3d insPT = new Point3d(0, 0, 0);
int sFac = 2;
using (DocumentLock acLocDoc = acD.LockDocument())
{
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
ObjectId ObjID;
// Open the Block table for read
BlockTable acBlkTbl = acCurDb.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
// Open Modelspace to add block
BlockTableRecord ms = acBlkTbl[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite) as BlockTableRecord;
using (Database dbInsert = new Database(false, true))
{
dbInsert.ReadDwgFile(BlockPATH, FileShare.Read, true, "");
ObjID = acCurDb.Insert(Path.GetFileNameWithoutExtension(BlockPATH), dbInsert, true);
}
BlockReference BlkRef = new BlockReference(insPT, ObjID);
ms.AppendEntity(BlkRef);
acTrans.AddNewlyCreatedDBObject(BlkRef, true);
Scale3d BlkScale = new Scale3d(sFac, sFac, sFac);
BlkRef.ScaleFactors = BlkScale;
BlkRef.Rotation = (0 * Math.PI / 180);
AcadApplication AcadAPP = (AcadApplication)Application.AcadApplication;
AcadAPP.ZoomExtents();
msId = acBlkTbl[BlockTableRecord.ModelSpace];
//UpdateAttributesInBlock(msId, blockName, attName, AttValue, BlkRef);
acTrans.Commit();
}
UpdateAttributesInBlock(msId, blockName, attName, AttValue);
}
}
public static void UpdateAttributesInBlock(ObjectId btrId, string BlkNm, string attNm, string AttVal)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
//BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
// Not needed, but quicker than aborting
foreach (ObjectId entID in btr)
{
Entity ent = tr.GetObject(entID, OpenMode.ForRead) as Entity;
if (ent != null)
{
BlockReference br = ent as BlockReference;
if (br != null)
{
BlockTableRecord bd = (BlockTableRecord)tr.GetObject(br.BlockTableRecord, OpenMode.ForRead);
if (bd.Name == BlkNm)
{
AttributeCollection attCol = br.AttributeCollection;
foreach (ObjectId arId in attCol)
{
DBObject obj = tr.GetObject(arId, OpenMode.ForRead);
AttributeReference ar = obj as AttributeReference;
if (ar != null)
{
if (ar.Tag == attNm)
{
ar.UpgradeOpen();
ar.TextString = AttVal;
ar.DowngradeOpen();
}
}
}
}
}
}
}
tr.Commit();
}
}