Hello wizards of the swamp!
I have a problem when I try to convert a Dynamic Block into a Static Block through C#, the problem is that some of the blocks I convert end up with the wrong rotation.
I have a DWG that my company works in, and here I run a custom command called "STATIFY". This command removes some Dynamic Blocks my company prefers to work with and insert new Static Blocks from an external DWG given to us by our customer. Because of specifications given by the customer, the DWG we produce needs to contain the Static Blocks provided by the customer. This leaves the built-in ConvertToStaticBlock method useless as when you use the ConvertToStaticBlock method, your "new" converted block's name will be the anonymous name of the dynamic block. I.E a converted block gets the name *UXX while I want the name to be CUSTOMER_BLOCK_TYPE_YY.
So my STATIFY command works pretty alright, for some blocks it even works perfectly but that isn't good enough. So in short I pass the Dynamic Blocks' block reference values to the Static Blocks' block reference values.
...
NewBlockRef.Rotation = OldBlockRef.Rotation
NewBlockRef.Scaling = OldBlockRef.Scaling
...
At this point I get into trouble since when I set the rotational value it doesn't always work right. At first I thought it was because of the flip states, but after closer inspection I think there is a problem with the Dynamic Blocks in the DWG I have to work with. For instance I have noticed that a 90 degree rotation works in different ways on different blocks even though it shouldn't:
I suspect this might be caused by an user error, but I think I will be a pretty unpopular man if I tell people they have to redo all their DWGs and also I think I am close to a solution that will work:
//This line fixes the rotation
NewBlockRef.BlockTableRecord = OldBlockRef.BlockTableRecord
//This next line fixes my problem too
NewBlockRef.BlockTableRecord = OldBlockRef.AnonymousBlockTableRecord
//The following line does NOT fix the rotation
NewBlockRef.BlockTableRecord = OldBlockRef.DynamicBlockTableRecord
Unfortunately these two fixes leads to my new Static Blocks getting the names of the anonymous blocks (*UXX and not Customer_Block_YY). When I don't set:
NewBlockRef.BlockTableRecord = OldBlockRef.BlockTableRecord
all my new blocks get the names they are supposed to btw. Had I been able to change the name from *UXX to CUSTOMER_BLOCK_TYPE_YY I think my solution would have been complete, but this does not work because I get a DuplicateNameError. I am also unable to change the name to CUSTOMER_BLOCK_TYPE_YY*UXX as this leads to InvalidInputError. I suspect that these errors are caused by my lack of knowledge of AutoCAD and what BlockReference and BlockTableRecord really are, and also something tells me that the statement
NewBlockRef.BlockTableRecord = OldBlockRef.BlockTableRecord
is problematic. Despite this, my brain tells me that I have to be onto something here. There has to be some data in the
OldBlockRef.BlockTableRecord or
OldBlockRef.AnonymousBlockTableRecord that will make the rotation of my NewBlock correct? What data could this be and how would I proceed to extract it? For instance I tried to check if there was a Transformational Matrix I could extract from the OldBlockRef, but this didn't work either.
I am not at my workstation right now so I don't have access to the code, but if anyone wants to give this a go I can post some code when I get back to work. I do have to take some precautions with what code I post as this is a work project though, but if I am able to get my code to work with help from The Swamp I will obviously write up a working example I can post so others might learn from my mistakes.