How to insert a dwg block using Brx

I am building some automation that require inserting blocks from a library (with path defined already in the support file).
After a thorough research on this forum I have found similar code that require inserting block or xrefs , I did modify the code to suit my needs but it's not inserting the block or nothing its happening. Can someone please help me on this ?[code]PASTE CODE HERE[/code]
Here's my code below :

        public static ObjectIdCollection InsertBlock(string blockName)

        {           

            ObjectIdCollection ids = new ObjectIdCollection();

            Database Database = HostApplicationServices.WorkingDatabase;

            _AcDb.TransactionManager manager = Database.TransactionManager;

            using (Transaction trans = manager.StartTransaction())

            {

                BlockTable blockTable = (BlockTable)trans.GetObject(Database.BlockTableId, OpenMode.ForRead);

                ObjectId id;

                BlockTableRecord rec;

                    using (Database tempDb = new Database(false, true))

                    {

                        tempDb.ReadDwgFile(blockName, FileShare.Read, true, string.Empty);

                        id = Database.Insert(blockName, tempDb, false);                    

                        rec = trans.GetObject(id, OpenMode.ForWrite) as BlockTableRecord;                 

                    }

                    Point3d insPt = new Point3d(0, 0, 0);               

                    BlockReference blockReference = new BlockReference(insPt, blockTable[blockName]);              

                    ids.Add(rec.AppendEntity(blockReference));

                    trans.AddNewlyCreatedDBObject(blockReference, true);

                    trans.Commit();

            }

return ids;

        }




















Comments

  •    couldn't get the insert code thing to work, anyway have a look

       public static class Commands
        {
            [CommandMethod("test")]
            static public void test()
            {
                string blk = "C:/Temp/blk.dwg";
                InsertBlock(blk,"blk");
            }

            public static ObjectIdCollection InsertBlock(string blockFullPath, string blockName)
            {

                ObjectIdCollection ids = new ObjectIdCollection();
                Database database = HostApplicationServices.WorkingDatabase;
                AcDb.TransactionManager manager = database.TransactionManager;

                using (Transaction trans = manager.StartTransaction())
                {
                    ObjectId id;
                    BlockTable blockTable = (BlockTable)trans.GetObject(database.BlockTableId, OpenMode.ForRead);

                    using (Database tempDb = new Database(false, true))
                    {
                        tempDb.ReadDwgFile(blockFullPath, FileOpenMode.OpenForReadAndWriteNoShare, true, null);
                        id = database.Insert(blockName, tempDb, false);
                    }
                    BlockTableRecord rec = trans.GetObject(database.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                    Point3d insPt = new Point3d(0, 0, 0);
                    BlockReference blockReference = new BlockReference(insPt, id);
                    ids.Add(rec.AppendEntity(blockReference));
                    trans.AddNewlyCreatedDBObject(blockReference, true);
                    trans.Commit();
                }
                return ids;
            }
        }

  •  Thanks a lot Daniel, really appreciate it.
    It works and that was quick.
This discussion has been closed.