Copy Dynamic Block Reference within a drawing

Copy Dynamic Block Reference within a drawing

ilovejingle
Enthusiast Enthusiast
1,558 Views
3 Replies
Message 1 of 4

Copy Dynamic Block Reference within a drawing

ilovejingle
Enthusiast
Enthusiast

I think this probably been asked quite a number of times, but I still cannot figure it out.

I have a drawing with some dynamic blocks, I need to copy them as they are in the code. 

If I use getTransformedCopy method, then It becomes anoymous. I want to copy it and keep everything the same .How should I make a copy of it while maintaining the ability to stretch the handles in AutoCAD.

 

0 Likes
1,559 Views
3 Replies
Replies (3)
Message 2 of 4

norman.yuan
Mentor
Mentor

I assume you do know that a dynamic block reference is, in most cases, when its dynamic property is changed from its original value, a reference to an anonymous block definition, which is generated by AutoCAD automatically.

 

Creating a copy of dynamic block reference by calling Entity.GetTransformedCopy() is bad thing to do, because the copying is simply to a shallow copying (Entity.Clone()  along with Entity.TransformBy()). This the quote from .NET API documentation (pay attention to lines in blue):

 

<QUOTE>

Function implementation in derived classes 

The default Entity implementation of this function should be adequate for most derived entity types. However, derived entity classes that wish to support non-uniform scaling or non-orthogonal transformations will need to override this method with their own implementation. 

This function must create a copy of the entity (using memory that has been dynamically allocated), apply the transformation matrix transform to the copy and then return the transformed copy. 

Determining what constitutes a valid transformation matrix and whether to do a shallow clone (that is, the entity's clone() method), a deepclone (if the entity owns other objects), or no clone at all (that is, make this function a no-op), is up to the implementer. 

 

Default implementation 

If the entity is uniformly scaled and orthogonal, Entity.GetTransformedCopy will call the entity's clone() method to create a clone of the entity, then call Entity.TransformBy() on the clone, and then return the transformed clone.</QUOTE>

 

Each dynamic block reference has undocumented ExtensionDictionary to hold information on how it is related to the original dynamic block definition in spite being the reference of an anonymous block definition. When a shallow copying (Entity.Clone()) happens, its ExtensionDictionary data would not be created with the cloned block reference, thus, its "dynamic" capability is lost, and just becomes a regular reference of an anonymous block definition.

 

I am not sure what you really want to do, but creating dynamic block reference by GetTransformedCopy() probably is not something you want to or should do.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 4

ilovejingle
Enthusiast
Enthusiast

Thank you for your explaination, now I think the question is "How to do a proper deep clone of the entity with all the extension dictionaries attached ? " 

I found a similar questions here. 

https://forums.autodesk.com/t5/net/entity-deepclone-vs-database-deepcloneobjects/td-p/7525517

Database.DeepCloneObjects()

This method seems solved the problem.

 

 

 

0 Likes
Message 4 of 4

amitnkukanur
Collaborator
Collaborator

Try copy constructor implementation

Senior Software Engineer
0 Likes