Message 1 of 6
Dynamic Block Becomes Static After Insertion
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I am trying to write a program to replace the old block with the new block. both blocks are dynamic. It seems the new blockreference becomes static after insertion
Is there any simpler way to insert a dynamic block which already exists in the drawing?
Here is my code :
[CommandMethod("RP")]
public void cmdRP()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
var optEnt1 = new PromptEntityOptions("Please Select Entity to be replaced : ");
var optEnt2 = new PromptEntityOptions("Please Select Enttity to Replace the orginal Entity : ");
optEnt1.SetRejectMessage("Only Support Block Reference ");
optEnt2.SetRejectMessage("Only Support Block Reference ");
optEnt1.AddAllowedClass(typeof(BlockReference), true);
optEnt2.AddAllowedClass(typeof(BlockReference), true);
var blkOld = ed.GetEntity(optEnt1);
if (blkOld.Status != PromptStatus.OK) return;
var blkNew = ed.GetEntity(optEnt2);
if (blkNew.Status != PromptStatus.OK) return;
using (Transaction trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
{
try
{
var blkRefOld = trans.GetObject(blkOld.ObjectId, OpenMode.ForRead) as BlockReference;
var blkRefNew = trans.GetObject(blkNew.ObjectId, OpenMode.ForRead) as BlockReference;
var blkMat = blkRefOld.BlockTransform;
var newBlkRef = new BlockReference(new Point3d(0, 0, 0), blkRefNew.BlockTableRecord);
newBlkRef.SetDatabaseDefaults();
newBlkRef.TransformBy(blkMat);
var bt = trans.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead) as BlockTable;
var ms = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
ms.AppendEntity(newBlkRef);
trans.AddNewlyCreatedDBObject(newBlkRef, true);
RXClass rxclass = RXObject.GetClass(typeof(AttributeDefinition));
var btr = trans.GetObject(blkRefOld.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
var collection = newBlkRef.AttributeCollection;
if (btr.HasAttributeDefinitions)
{
foreach (ObjectId objId in btr)
{
if (objId.ObjectClass.IsDerivedFrom(rxclass))
{
var attDef = trans.GetObject(objId, OpenMode.ForRead) as AttributeDefinition;
if (!attDef.Constant)
{
var attRef = new AttributeReference();
attRef.SetAttributeFromBlock(attDef,blkMat);
collection.AppendAttribute(attRef);
trans.AddNewlyCreatedDBObject(attRef, true);
}
}
}
}
bgFunction.SyncDynamicBlockProps(blkRefOld.ObjectId, newBlkRef.ObjectId, trans);
blkRefOld.UpgradeOpen();
blkRefOld.Erase();
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage("Error : MAIN Method.");
}
finally
{
trans.Commit();
}
}
}