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?
Solved! Go to Solution.
Solved by Hallex. Go to Solution.
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();
You're welcome
Cheers 🙂
~'J'~