.NET

Reply
Active Member
yanasdf789
Posts: 10
Registered: ‎03-11-2014
Message 1 of 7 (254 Views)

how to put a block into image?

254 Views, 6 Replies
03-11-2014 09:41 PM
how to put a block in DWG into image with C# code?
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 7 (218 Views)

Re: how to put a block into image?

03-12-2014 11:51 AM in reply to: yanasdf789

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
            }
        }
}

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Member
yanasdf789
Posts: 10
Registered: ‎03-11-2014
Message 3 of 7 (190 Views)

Re: how to put a block into image?

03-13-2014 03:14 AM in reply to: Hallex
hi thank you
what's the " accore.dll"? where can i download it?
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 7 (173 Views)

Re: how to put a block into image?

03-13-2014 09:25 AM in reply to: yanasdf789
if you in A2012 or higher reqiure reference to
accoremgd.dll from acad folder, nothing else
_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Member
yanasdf789
Posts: 10
Registered: ‎03-11-2014
Message 5 of 7 (136 Views)

Re: how to put a block into image?

03-15-2014 10:12 PM in reply to: Hallex
Hi Hallex, thank you for your reply! actually,I wan't to do like this. I choose some entity in dwg ,then i put these entity into a image. with C# Code how can i do it ? is it possible to do?
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 6 of 7 (128 Views)

Re: how to put a block into image?

03-15-2014 11:09 PM in reply to: yanasdf789

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

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Member
yanasdf789
Posts: 10
Registered: ‎03-11-2014
Message 7 of 7 (121 Views)

Re: how to put a block into image?

03-16-2014 05:20 AM in reply to: Hallex
Hi hellax, thank you your reply! i try to run your code, unfortunately I I get the error "ACAD_IMAGE_DICT does not Exist"! what should i do? i try to create a ACAD_IMAGE_DICT DBDictionary ,then run again ,the applicaion collapse !! my code like this DBDictionary IMAGEDict; //************* my add code try { IMAGEDict = (DBDictionary)tr.GetObject(namedDic.GetAt("ACAD_IMAGE_DICT"), OpenMode.ForRead); } catch { IMAGEDict = new DBDictionary(); namedDic.SetAt("ACAD_IMAGE_DICT", IMAGEDict); tr.AddNewlyCreatedDBObject(IMAGEDict, true); } if (!namedDic.Contains("ACAD_IMAGE_DICT")) { ed.WriteMessage("\nACAD_IMAGE_DICT does not Exist"); return; } //*************************** what should i do? i need your help
Announcements
Autodesk Alert | We are currently working on restoring Previous Version License request access in the Autodesk Subscription Center. If you are a subscription customer and would like to request a Previous Version License, please use this link as a temporary workaround. We apologize for the inconvenience and thank you for your patience.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.