How do I move DynamicBlock to a BlockTableRecord,and ensure thatthe result is not an anonymous block?

How do I move DynamicBlock to a BlockTableRecord,and ensure thatthe result is not an anonymous block?

Medithaibet
Contributor Contributor
432 Views
3 Replies
Message 1 of 4

How do I move DynamicBlock to a BlockTableRecord,and ensure thatthe result is not an anonymous block?

Medithaibet
Contributor
Contributor

How do I move DynamicBlock to a BlockTableRecord,and ensure thatthe result is not an anonymous block?
I try deepclone,but show error eAlreadyIrDb,how to transform to BlockTableRecord.thank you eyeryone for here.

 var ent = (Entity)tr.GetObject(item);
 Entity clonedEntity;
 if (ent is BlockReference br && br.IsDynamicBlock)
 {
     //using ObjectIdCollection mapIds = [br.ObjectId];
     //using IdMapping mapping = [];
     //Env.Database.DeepCloneObjects(mapIds, Env.Database.CurrentSpaceId, mapping, false);
     //clonedEntity = (Entity)tr.GetObject(mapping[br.ObjectId].Value, OpenMode.ForWrite);
     var clonedDynamicBlock = (BlockReference)ent.Clone();
     CopyDynamicProperties(br, clonedDynamicBlock, Env.Editor);
     clonedEntity = clonedDynamicBlock;
 }
 else
     clonedEntity = (Entity)ent.Clone();

 btr.AppendEntity(clonedEntity);

 

0 Likes
Accepted solutions (1)
433 Views
3 Replies
Replies (3)
Message 2 of 4

norman.yuan
Mentor
Mentor

It is a bit hard to understand what you want to do: "...move to..." which/what BlockTableRecord? What does CopyDynamicProperies() method do? 

 

If you want to convert a dynamic blockreference (with its dynamic properties are properly set) to a non-anonymous block reference, you may want to try BlockReference.ConvertToStaticBlock() method, which will turn the dyanmic blockreference into a static blockreference.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 4

ActivistInvestor
Mentor
Mentor
Accepted solution

To copy a dynamic block to another owner, you use DeepCloneObjects() and specify the new owner BlockTableRecord as the owner argument. 

 

There is no other way to copy a dynamic block reference (including Clone()), because it requires translation of ObjectIds that can only be done by DeepClone.

0 Likes
Message 4 of 4

Medithaibet
Contributor
Contributor

I solved the problem, the above may not have clearly described the requirements, resulting in ambiguity. The second parameter in the following line of code is btrId, thank you for your answer to give me ideas.


AutoCAD 2019 Developer and ObjectARX Help | Database.DeepCloneObjects Method | Autodesk

 

db.DeepCloneObjects(needAddIds, btrId, mapping, false);

 

0 Likes