.NET

Reply
Mentor
166 Posts
9 Kudos
Registered: ‎06-16-2008
Post 1 of 5
Accepted Solution

Dynamic Block Block Tables

331 Views, 4 Replies
08-09-2012 05:21 PM

I have a dynamic block of a steel beam, with a block table, by which I can change the size etc.

 

public static void InsertBlock(Database db, string FileName, Point3d InsertionPoint, double Rotation = 0)
            {
            ObjectId blkid = ObjectId.Null;
            using (Database bdb = new Database(false, true))
                {
                bdb.ReadDwgFile(FileName, System.IO.FileShare.Read, true, "");
                blkid = db.Insert(System.IO.Path.GetFileNameWithoutExtension(FileName), bdb, true);

                using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);

                    BlockTableRecord btr = default(BlockTableRecord);
                    btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);


                    using (btr)
                        {
                        BlockReference bref = new BlockReference(InsertionPoint, blkid);
                        using (bref)
                            {
                            if (bref.IsDynamicBlock)
                                {
                                DynamicBlockReferencePropertyCollection dynBrefColl = bref.DynamicBlockReferencePropertyCollection;
                                foreach (DynamicBlockReferenceProperty dynBrefProps in dynBrefColl)
                                    {
                                    if (dynBrefProps.PropertyName == "Name")
                                        {
                                        dynBrefProps.Value = "150UB18.0";
                                        }
                                    }
                                }

                            Matrix3d mat = Matrix3d.Identity;
                            bref.TransformBy(mat);
                            bref.Layer = "0";
                            bref.Rotation = Rotation;

                            btr.AppendEntity(bref);
                            tr.AddNewlyCreatedDBObject(bref, true);
                            
                            }
                        }
                    tr.Commit();
                    }
                }
            }

 Once the beam is placed from this code, I can change the size from the list, but there is no physical change.

 

If I comment out,

dynBrefProps.Value = "150UB18.0";

  the size will change when I select it. The 150UB18.0 is in the list when you call GetAllowedValues for the Name.

 

Does anyone know how to successfully set a value? I have looked at

GetAllowedValues().SetValue(object value, int index)  but not too sure how this one works.

Hi,

 

I think you need to append the blockreference and add it to the transaction *BEFORE* to access and edit the dynamic properties, after that you need to call UpdateAnonymousBlocks() and commit the tranasaction.

 

Gaston Nunez

 

Distinguished Mentor
582 Posts
91 Kudos
Registered: ‎04-11-2010
Post 2 of 5

Re: Dynamic Block Block Tables

08-09-2012 07:19 PM in reply to: BrentBurgess1980

Hi,

 

I think you need to append the blockreference and add it to the transaction *BEFORE* to access and edit the dynamic properties, after that you need to call UpdateAnonymousBlocks() and commit the tranasaction.

 

Gaston Nunez

 

Mentor
166 Posts
9 Kudos
Registered: ‎06-16-2008
Post 3 of 5

Re: Dynamic Block Block Tables

08-09-2012 07:30 PM in reply to: gasty1001

Gaston - You are a champ!!! that worked!!

Mentor
166 Posts
9 Kudos
Registered: ‎06-16-2008
Post 4 of 5

Re: Dynamic Block Block Tables

08-09-2012 07:33 PM in reply to: gasty1001

Here is the snippet. I replaced the original Using with this one.

 

using (btr)
                        {
                        bref = new BlockReference(InsertionPoint, blockID);
                        
                        btr.AppendEntity(bref);
  
                        using (bref)
                            {
                            if (bref.IsDynamicBlock)
                                {
                                DynamicBlockReferencePropertyCollection dynBrefColl = bref.DynamicBlockReferencePropertyCollection;
                                foreach (DynamicBlockReferenceProperty dynBrefProps in dynBrefColl)
                                    {
                                    if (dynBrefProps.PropertyName == "Name")
                                        {
                                        dynBrefProps.Value = "150UB18.0";
                                        }
                                    }
                                }
                            
                            Matrix3d mat = Matrix3d.Identity;
                            bref.TransformBy(mat);
                            bref.Layer = "0";
                            bref.Rotation = Rotation;

                            tr.AddNewlyCreatedDBObject(bref, true);
                            }
                        }

 

Distinguished Mentor
582 Posts
91 Kudos
Registered: ‎04-11-2010
Post 5 of 5

Re: Dynamic Block Block Tables

08-09-2012 08:03 PM in reply to: BrentBurgess1980

Hi Brent,

 

Glad to help you.

 

Gaston Nunez

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!