Community
You can use something like this code:
public class BlockUtils { //ads_queueexpr [DllImport("accore.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ads_queueexpr")] extern static private int ads_queueexpr(byte[] command); //_____________________________________________________// //used block named "Door" //Path to save bitmap: "c:\\Test\\Door.bmp" //Change all to your suit [CommandMethod("dic")] public void AddBlockImageToDwg() { Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; Database db = doc.Database; Transaction tr = doc.TransactionManager.StartTransaction(); try { using (DocumentLock doclock = doc.LockDocument()) { using (tr) { DBDictionary namedDic = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead); if (!namedDic.Contains("ACAD_IMAGE_DICT") ) { ed.WriteMessage("\nACAD_IMAGE_DICT does not Exist"); return; } ObjectId dictId = RasterImageDef.GetImageDictionary(db); // string recBase = "Door#"; if (dictId == ObjectId.Null) { // Image dictionary doesn't exist, create new dictId = RasterImageDef.CreateImageDictionary(db); } // Open the image dictionary DBDictionary dict = (DBDictionary)tr.GetObject(dictId, OpenMode.ForRead); // Get a record name for our raster image definition if (dict.Contains("Door")) { ed.WriteMessage("\nDoor RasterImage Definition Exists"); } BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead); BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); if (!bt.Has("Door")) { ed.WriteMessage("\nDoor does not Exist"); return; } BlockTableRecord blkDef = (BlockTableRecord)tr.GetObject(bt["Door"], OpenMode.ForRead); UnicodeEncoding uEncode = new UnicodeEncoding(); // create block preview icon ads_queueexpr(uEncode.GetBytes("(COMMAND \"_.BLOCKICON\" \"Door\" )\n")); try { System.Drawing.Bitmap blkImg = blkDef.PreviewIcon; blkImg.Save("c:\\Test\\Door.bmp"); } catch { throw new System.Exception("Impossible to save block preview icon!"); } ObjectId defId = ObjectId.Null; RasterImageDef rid = new RasterImageDef(); rid.SourceFileName = "c:\\Test\\Door.png"; // Load it rid.Load(); dict.UpgradeOpen(); defId = dict.SetAt("Door", rid); // Let the transaction know tr.AddNewlyCreatedDBObject(rid, true); // Create the raster image that references the definition defId = dict.GetAt("Door"); RasterImage ri = new RasterImage(); //ri.Name = "Door";//optional ri.ImageDefId = defId; ri.ShowImage = true; Matrix3d ucs = ed.CurrentUserCoordinateSystem; Point3d pt = Point3d.Origin; Vector3d xAxis = new Vector3d(1, 0, 0); Vector3d yAxis = new Vector3d(0, 1, 0); ri.Orientation = new CoordinateSystem3d( pt.TransformBy(ucs), xAxis.TransformBy(ucs), yAxis.TransformBy(ucs) ); ri.SetClipBoundaryToWholeImage(); btr.AppendEntity(ri); tr.AddNewlyCreatedDBObject(ri, true); // Create a reactor between the RasterImage and the // RasterImageDef to avoid the "unreferenced" // warning in the XRef palette RasterImage.EnableReactors(true); ri.AssociateRasterDef(rid); tr.Commit(); } } } catch (Autodesk.AutoCAD.Runtime.Exception ex) { Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.Message ); } finally { // empty code block } } }
You can create a block of this entity, and then use the same code,
unfortunately I do not have time for this job, try it yourself
Can't find what you're looking for? Ask the community or share your knowledge.