.NET

.NET

Reply
Mentor
BrentBurgess1980
Posts: 164
Registered: ‎06-16-2008
Message 1 of 4 (351 Views)
Accepted Solution

Insert Dymanic Block from file and changeVisibility States

351 Views, 3 Replies
12-19-2011 06:26 PM

I am trying to insert a Dynamic Block and change it's Visibility State. I can insert the block, and the visibility state changes, however, if I manually try to change it after, it does not change, even though the value has changed. My code executes and returns no errors, so I am assuming that I have done something wrong, or missed something out.

 

 public class MyCommands
        {
        [CommandMethod("MyGroup", "Bolts", "MyCommandLocal", CommandFlags.Modal)]
        public void MyCommand() // This method can have any name
            {
            InsertBlock(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database, "C:/88NutPlan.dwg", new Point3d(0, 0, 0));
            }

        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 == "Visibility1")
                                        {
                                        dynBrefProps.Value = "M36";
                                        }
                                    }
                                }


                            Matrix3d mat = Matrix3d.Identity;
                            bref.TransformBy(mat);
                            bref.Layer = "0";
                            bref.Rotation = Rotation;
                            btr.AppendEntity(bref);
                            tr.AddNewlyCreatedDBObject(bref, true);
                            }
                        }
                    tr.Commit();
                    }
                }
            }
        }

 

If I insert my block manually, I can change the states and it works fine, but the code seems to break the block.

 

Can anyone shed some light on this?

You have to change order of your work with block

See if this working for you :

 using (btr)
{
BlockReference bref = new BlockReference(InsertionPoint, blkid);
Matrix3d mat = Matrix3d.Identity;
bref.TransformBy(mat);
bref.Layer = "0";
bref.Rotation = Rotation;
btr.AppendEntity(bref);
tr.AddNewlyCreatedDBObject(bref, true);
if (bref.IsDynamicBlock)
{
DynamicBlockReferencePropertyCollection dynBrefColl = bref.DynamicBlockReferencePropertyCollection;
foreach (DynamicBlockReferenceProperty dynBrefProps in dynBrefColl)
{
if (dynBrefProps.PropertyName.ToUpper() == "VISIBILITY1")
{
dynBrefProps.Value = "M36";
}
}
}
}
tr.Commit();

 

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 4 (333 Views)

Re: Insert Dymanic Block from file and changeVisibility States

12-20-2011 12:51 AM in reply to: BrentBurgess1980

You have to change order of your work with block

See if this working for you :

            using (btr)
                     {

                         BlockReference bref = new BlockReference(InsertionPoint, blkid);
                
                             Matrix3d mat = Matrix3d.Identity;

                             bref.TransformBy(mat);

                             bref.Layer = "0";

                             bref.Rotation = Rotation;

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

                             if (bref.IsDynamicBlock)
                             {
                                 
                                 DynamicBlockReferencePropertyCollection dynBrefColl = bref.DynamicBlockReferencePropertyCollection;

                                 foreach (DynamicBlockReferenceProperty dynBrefProps in dynBrefColl)
                                 {
                                     if (dynBrefProps.PropertyName.ToUpper() == "VISIBILITY1")
                                     {

                                         dynBrefProps.Value = "M36";
                                     }
                                 }
                             }
 
                     }
                     tr.Commit();

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Mentor
BrentBurgess1980
Posts: 164
Registered: ‎06-16-2008
Message 3 of 4 (307 Views)

Re: Insert Dymanic Block from file and changeVisibility States

12-20-2011 04:00 PM in reply to: Hallex

Thanks Hallex - That worked perfeclty!!

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 4 (298 Views)

Re: Insert Dymanic Block from file and changeVisibility States

12-20-2011 10:46 PM in reply to: BrentBurgess1980

You're welcome

Cheers :smileyhappy:

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.