Reputation:
I am trying to insert a blockReference (in my case a legend) into a specific layout amongst other layouts.
Here it's the code that I used:
BlockTable blockTable = transaction.GetObject(database.BlockTableId, OpenMode.ForWrite) as BlockTable;
BlockTableRecord blkTbRecPaper = transaction.GetObject(blockTable[BlockTableRecord.PaperSpace], OpenMode.ForWrite) as BlockTableRecord;
blkTbRecPaper.AppendEntity(blockReference);
My problem is that it only works when the last selected layout is the one that I am looking for. Is there a way to directly specify the wanted layout instead addin the blockReference into the blockTableREcord of the paper space ?
Upvotes: 2
Views: 2717
Reputation: 8584
You need to open the layout dictionary in order to get all layouts on the drawing. Then you can open the respective BlockTableRecord and insert the new block reference.
using (Transaction tr = db.TransactionManager.StartTransaction())
{
DBDictionary layoutDic
= tr.GetObject(
db.LayoutDictionaryId,
OpenMode.ForRead,
false
) as DBDictionary;
foreach (DBDictionaryEntry entry in layoutDic)
{
ObjectId layoutId = entry.Value;
Layout layout
= tr.GetObject(
layoutId,
OpenMode.ForRead
) as Layout;
ed.WriteMessage(
String.Format(
"{0}--> {1}",
Environment.NewLine,
layout.LayoutName
)
);
}
tr.Commit();
}
See a better sample at http://adndevblog.typepad.com/autocad/2012/05/listing-the-layout-names.html
Upvotes: 2