.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Insert Dymanic Block from file and changeVisibility States

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
BrentBurgess1980
958 Views, 3 Replies

Insert Dymanic Block from file and changeVisibility States

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?

3 REPLIES 3
Message 2 of 4
Hallex
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
Message 3 of 4
BrentBurgess1980
in reply to: Hallex

Thanks Hallex - That worked perfeclty!!

Message 4 of 4
Hallex
in reply to: BrentBurgess1980

You're welcome

Cheers 🙂

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost