.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

how to put a block into image?

6 REPLIES 6
Reply
Message 1 of 7
yanasdf789
874 Views, 6 Replies

how to put a block into image?

how to put a block in DWG into image with C# code?
6 REPLIES 6
Message 2 of 7
Hallex
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
Message 3 of 7
yanasdf789
in reply to: Hallex

hi thank you
what's the " accore.dll"? where can i download it?
Message 4 of 7
Hallex
in reply to: yanasdf789

if you in A2012 or higher reqiure reference to
accoremgd.dll from acad folder, nothing else
_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 5 of 7
yanasdf789
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?
Message 6 of 7
Hallex
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
Message 7 of 7
yanasdf789
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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost