Distinguished Contributor
Posts: 139
Registered: ‎05-07-2008
Message 1 of 2 (145 Views)

Block made with dynamic blocks

145 Views, 1 Replies
03-18-2013 05:10 PM

I have code which creates a new block by using dynamic blocks.  The dynamic blocks are inserted and their parameters set, then they are added to the new block (see code below).  This is working good, the new block gets created using the dynamic blocks which have all the right parameter settings and dimensions.  The problem is that when this block is inserted, it looks good, but the user cannot snap to any of the geometry within the dynamic blocks.  For example, when measuring, they can't snap to a corner of any dynamic block within the main block.  Does anyone have any idea why this would be?  Am I creating the dynamic blocks wrong?  Or building the new block wrong?  Thanks!


            Document doc = Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            String name = obj.Name;


            using (Transaction Tx = db.TransactionManager.StartTransaction())


                BlockTable blkTbl = Tx.GetObject(db.BlockTableId, OpenMode.ForRead, false, true) as BlockTable;


                BlockTableRecord btr = new BlockTableRecord();

                btr.Name = name;



                ObjectId btrId = blkTbl.Add(btr);

                Tx.AddNewlyCreatedDBObject(btr, true);


                // Add entities

                BlockReference dynBlk1 = InsertDynBlock(obj, 0, 0);

                BlockReference dynBlk2 = InsertDynBlock(obj, 10, 0);


                using (btr)



                    Tx.AddNewlyCreatedDBObject(dynBlk1, true);


                    Tx.AddNewlyCreatedDBObject(dynBlk2, true);







        private static BlockReference InsertDynBlock(Object obj, Double x, Double y)


            Document doc = Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            ObjectId blkid;

            String name= "Drawings/Dyn1.dwg";

            BlockReference bref = null;


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


                bdb.ReadDwgFile(name, System.IO.FileShare.Read, true, "");

                blkid = db.Insert(System.IO.Path.GetFileNameWithoutExtension(name), bdb, true);

                bref = new BlockReference(new Point3d(x, y, 0), blkid);

                Matrix3d mat = Matrix3d.Identity;



                bref.Rotation = 0.0;


                if (bref.IsDynamicBlock)


                    DynamicBlockReferencePropertyCollection dynBrefColl = bref.DynamicBlockReferencePropertyCollection;


                    foreach (DynamicBlockReferenceProperty dynBrefProps in dynBrefColl)


                        if (dynBrefProps.PropertyName.ToUpper() == "HEIGHT")


                            dynBrefProps.Value = Convert.ToDouble(obj.Height);





            return bref;


ADN Support Specialist
Posts: 352
Registered: ‎07-24-2007
Message 2 of 2 (87 Views)

Re: Block made with dynamic blocks

03-25-2013 10:30 AM in reply to: tommam

Quickly looking at this... what happens if you first add the dynblock to the database, then, update the dynamic properties? Also, make sure you call bdb.CloseInput(true) after your call to ReadDWGFile() so that the entire file is read in.

Fenton Webb

Developer Technical Services

Autodesk Developer Network

Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

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