.NET

Reply
Mentor
BrentBurgess1980
Posts: 162
Registered: ‎06-16-2008
Message 1 of 5 (319 Views)
Accepted Solution

Dynamic Block Block Tables

319 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
gasty1001
Posts: 546
Registered: ‎04-11-2010
Message 2 of 5 (312 Views)

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
BrentBurgess1980
Posts: 162
Registered: ‎06-16-2008
Message 3 of 5 (309 Views)

Re: Dynamic Block Block Tables

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

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

Mentor
BrentBurgess1980
Posts: 162
Registered: ‎06-16-2008
Message 4 of 5 (306 Views)

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
gasty1001
Posts: 546
Registered: ‎04-11-2010
Message 5 of 5 (302 Views)

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
Need installation help?

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