I am trying to insert block with attributes from external drawing. When I perform the same operation using insert command block is inserter correctly and I can access attributes using enhanced attribute editor. When I perform the same task using .net code attributes are not in attribute collection of a newly inserted block. I can still see them by accessing BlockTableRecord but behavior inside the drawing is different. Any idea what am I missing? Here is the code:
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
PromptPointOptions prPoint = new PromptPointOptions("Enter Attachment Point:");
PromptPointOptions prPoint2 = new PromptPointOptions("Enter BOM Tag Location:");
PromptPointResult prRes = ed.GetPoint(prPoint);
prPoint2.BasePoint = prRes.Value;
prPoint2.UseBasePoint = true;
PromptPointResult prRes2 = ed.GetPoint(prPoint2);
if (prRes.Status == PromptStatus.OK && prRes2.Status == PromptStatus.OK)
{
using (Transaction trans = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
Database newDb = new Database(false, true);
newDb.ReadDwgFile(@"d:\Projects\RandD\ACAD\BOM_R.dwg", FileShare.Read, false, string.Empty);
ObjectId newBlockID = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database.Insert(BlockTableRecord.ModelSpace, "My Block", newDb, true);
BlockReference br = new BlockReference(prRes2.Value, newBlockID);
br.ScaleFactors = new Scale3d(5, 5, 5);
btr.AppendEntity(br);
trans.AddNewlyCreatedDBObject(br, true);
Leader ld = new Leader();
ld.AppendVertex(prRes.Value);
ld.AppendVertex(prRes2.Value);
btr.AppendEntity(ld);
trans.AddNewlyCreatedDBObject(ld, true);
trans.Commit();
br.Dispose();
ld.Dispose();
btr.Dispose();
bt.Dispose();
trans.Dispose();
}
}
And to access them:
string spacing = " ";
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
using (Transaction trans = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
foreach (ObjectId obj in btr)
{
DBObject brO = trans.GetObject(obj, OpenMode.ForRead);
ed.WriteMessage("\n" + spacing + brO.GetType());
if (brO.GetType().ToString() == "Autodesk.AutoCAD.DatabaseServices.BlockReference")
{
BlockReference br = (BlockReference)brO;
//br.AttributeCollection is empty
BlockTableRecord inBtr = (BlockTableRecord)trans.GetObject(br.BlockTableRecord, OpenMode.ForRead);
ed.WriteMessage("\n " + spacing + br.Name);
foreach (ObjectId inObj in inBtr)
{
DBObject inBrO = trans.GetObject(inObj, OpenMode.ForWrite);
ed.WriteMessage("\n " + spacing + inBrO.GetType());
if (inBrO.GetType().ToString() == "Autodesk.AutoCAD.DatabaseServices.AttributeDefinition")
{
ed.WriteMessage("\n " + spacing + ((AttributeDefinition)inBrO).Tag);
((AttributeDefinition)inBrO).TextString = "Test";
}
}
}
}
trans.Dispose();
}