Generating preview icons for programmatically created blocks

By Virupaksha Aithal

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();
    }
}

Comments

One response to “Generating preview icons for programmatically created blocks”

  1. Thanks for sharing

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading