I run into an interesting issue (well, it cost me quite a few hours in my current development project):
A BlockReference object, which is a Block reference generated from a dynamic block definition, can have its IsDynamic property either equal to True, or False. See this code snippet:
{code}
[CommandMethod("IsDyn")]
public static void GetDynamicBlockReference()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
PromptEntityOptions opt = new PromptEntityOptions("\nPick a dynamic block rference:");
opt.SetRejectMessage("\nYou must pick a block!");
opt.AllowNone = false;
opt.AddAllowedClass(typeof(BlockReference), true);
PromptEntityResult res = ed.GetEntity(opt);
if (res.Status != PromptStatus.OK) return;
BlockReference bref=null;
using (Transaction tran = doc.Database.TransactionManager.StartTransaction())
{
bref = tran.GetObject(res.ObjectId, OpenMode.ForRead) as BlockReference;
if (bref.IsDynamicBlock)
{
//This message will show, of course, if you picked dynamic block
MessageBox.Show("Within transaction scope: It is a dynamic block.");
}
else
{
MessageBox.Show("Within transaction scope: It is not a dynamic block.");
}
}
if (bref.IsDynamicBlock)
{
MessageBox.Show("Out of transaction scope: It is a dynamic block.");
}
else
{
//After leaving transaction, this message shows!!!
MessageBox.Show("Out of transaction scope: It is not a dynamic block.");
}
}
{/code}
If you run the code by picking a dynamic block in a drawing, you can see what I am trying to say here.
That is, when you obtain a BlockReference via a ObjectId, the BlockReference is indeed a dynamic while it is still in the scope of transaction. But once you are done with the transaction, you may still want to keep the BlockRefernce object for other purpose. However, as soon as the BlockRefernce leaves the transaction, it is no longer a dynamic block any more.
In my current Acad .NET program, I re-used existing code that retrieve a BlockRefernce from an ObjectId. Then in current program, I'd like use the BlockReference object to do something and eventually update its dynamic properties. But whenever I run the code, its dynamic properties are never updated. Each time I debug, I stoped in my re-used code because it shows clearly it is dynamic block. I eventually found out the BlockReference passed from my re0used code is not a dynamic block once it is out of the transaction, in which the BlockReferebce is generated by the transaction's GetObject() method. Actually, not only IsDynamic property changes from True to False, its DynamicBlockReferencePropertyCollection's Count property also changes from greater than 0 to 0.
I do not see anywhere this issue are mentioned/documented. It is certainly interesting, strange and confusing that an entity's property could change just because of crossing transaction boundary.
I use Acad Map 2009 and VS2008.
Has anyone already known this? Or may I have done something wrong?