.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Duplicate Object Id in nested blocks

1 REPLY 1
Reply
Message 1 of 2
vkpunique
138 Views, 1 Reply

Duplicate Object Id in nested blocks

Hello, So I am trying to print coordinates of "Ref Point" block from selected nested blocks.

In my attach drawing we have Test2 Nested block, which contain two sub block Test. Test block is rectangle with 4 Ref Point Block. Just check out attach drawing and this will make more sense.

 

So I am trying to loop through all objects in this nested block and Print Ref Point block coordinates with their object Id but I am getting same Object ID for multiple Block Reference. I don't know reason for this. 

for this block i have 8 ref point block , but i am getting only 4 sets of Object ID even through i am printing 8 set of coordinates.

Here is my sample code. Keep in mind that i am using few extension method to simplify my code but code should be easy to understand.

public static Dictionary<Handle, Point3d> GetRefPointBlockPositions(this BlockReference blockReference)
        {

            var blockReferences = new Dictionary<Handle, Point3d>();
            var blockDefination=blockReference.GetBlock();
            foreach (ObjectId objectId in blockDefination)
            {
                if (objectId.IsValidObjectType(typeof(BlockReference)))
                {
                    var subBlockRef = objectId.GetObject<BlockReference>();
                    var subBlockDef = subBlockRef.GetBlock();
                    if (subBlockDef.Name == "Ref Point")
                    {
                        if (!blockReferences.ContainsKey(subBlockRef.Handle))
                        { 
                            var point=new Point3d(subBlockRef.Position.X+ blockReference.Position.X, subBlockRef.Position.Y + blockReference.Position.Y, subBlockRef.Position.Z + blockReference.Position.Z);
                            blockReferences.Add(subBlockRef.Handle, point);
                        }
                    }

                    if (subBlockDef.IsSuperBlock())
                    {
                        var subBlocksDictionary = subBlockRef.GetRefPointBlockPositions();
                        foreach (var item in subBlocksDictionary)
                        {
                            if (!blockReferences.ContainsKey(item.Key))
                            {
                                blockReferences.Add(item.Key, item.Value);
                            }
                        }
                    }
                }
            }
            return blockReferences;
        }

 

1 REPLY 1
Message 2 of 2
norman.yuan
in reply to: vkpunique

While the block definition "Test2" contains 2 nested block (named "Test") references, each has 4 "Ref Point" block reference (thus, 4 points), it is so obvious that because the 2 "Test" block references are the references to the SAME block definition, thus, there is only ONE SET of entities.

 

In your case, if you want to get a group of points keyed by entity's ID/Handle, you would use a nested Dictionary to store the points that is keyed by the block reference Id/Handle:

 

Dictionary<[Handle of the "Test" block reference], Dictionary<[Handle of "point" block reference], Point3d>>

 

NOTE, you use the block reference's ID/HANDLE, not entity's in the block definition, because you have MULTIPLE block references of the SAME block definition.

 

Hope this would help you rewrite your code logic to get 2 sets of 4 points in the block reference of "Test2".

 

 

Norman Yuan

Drive CAD With Code

EESignature

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report