Below code shows the procedure to generate the preview icons for programmatically created blocks. Code uses AutoCAD built in command “BLOCKICON” to create the icon.
[CommandMethod("BlkPreview", CommandFlags.Session)]
static public void BlkPreview()
{
Document doc =
Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction Tx =
db.TransactionManager.StartTransaction())
{
BlockTable table =
Tx.GetObject(db.BlockTableId,
OpenMode.ForRead) as BlockTable;
//"Test" - block whose preview icon need to Generate.
BlockTableRecord blk =
Tx.GetObject(table["Test"],
OpenMode.ForRead) as BlockTableRecord;
object app = Application.AcadApplication;
object ActiveDocument =
app.GetType().InvokeMember(
"ActiveDocument", BindingFlags.GetProperty,
null, app, null
);
object[] dataArry = new object[1];
dataArry[0] = "_.BLOCKICON " + blk.Name + "n";
ActiveDocument.GetType().InvokeMember("SendCommand",
System.Reflection.BindingFlags.InvokeMethod,
null, ActiveDocument, dataArry);
Tx.Commit();
}
}

Leave a Reply