Check if 2 block definition geometries are identical

Check if 2 block definition geometries are identical

Mario-Villada
Advocate Advocate
1,551 Views
5 Replies
Message 1 of 6

Check if 2 block definition geometries are identical

Mario-Villada
Advocate
Advocate

Hi All,

 

I have some AutoCAD drawings that were exported from Tekla. all visible parts are exported as individual blocks with different names and inserted at 0,0,0. However, many of them have the same geometry. For instance a top view of a column looks the same across the drawings but each column is a separate block with a different name.

 

My goal is to compare each block definition to each other, find out which ones have identical geometry, replace them all with a single block definition and purge the drawings. This for the purpose of reducing the file size as at the moment is too big.

 

Do you know any way to achieve this by code?

So far I think of comparing each boundary box and number of entities of each block definition. but sometimes the geometry is rotated 180 degrees so I might get wrong results. Any Ideas?

 

Thanks in advance.

 

0 Likes
Accepted solutions (2)
1,552 Views
5 Replies
Replies (5)
Message 2 of 6

Ed__Jobe
Mentor
Mentor

I don't have many ideas yet. The first one would be to write each one to file and check the file size. This is assuming that similar blocks wouldn't have identical file sizes. You could manually wblock a few samples and check their file size in Explorer to see if this would work.

 

You might have a few more comparison options in .NET though. If you want to actually compare nested objects, you could first test each block definition's Count property. Obviously, identical blocks would have the same number of entities.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 3 of 6

Mario-Villada
Advocate
Advocate
Thanks Ed, write each block is a good idea. I did think about comparing the count property. I can also compare the bounding box which identical blocks should have the same one. However, with this 2 checks there is a chance that it will pick some blocks with equal count and bounding box but are rotated 180 degrees.
I would also need to find a way to check the orientation of these blocks. I am also exploring the .Net avenue with C#. I was thinking somehow to get the collection of entities in 2 blocks and comparing them using Collection1.equals(Collection2) method, but I cannot figure it out how to get such collections. Thanks again for your help.
0 Likes
Message 4 of 6

Ed__Jobe
Mentor
Mentor
Accepted solution

The Block.Item is not a true collection, more of an array. You can iterate it like so:

 

 

Dim i As Integer
Dim ent As AcadEntity
Dim ents As New Collection
For i = 0 to block.Count -1
  Set ent = block.Item(i)
  'do something with ent
  ents.Add ent
Next i

 

 

Now you can For..Each the Collection object. This didn't really gain you very much, since you were iterating the Block already.

 

However, take note that a VBA Collection object does not have a method to compare contents, like the Equals method. That's only in .NET, which was the method I had in mind in my first post. This and this might help. They show that you can't simply say "Collection1.equals(Collection2)". You need to actually compare the contents. I found this archived post that claims to have a class that can compare Collections.

 

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 5 of 6

Norman_Yuan
Mentor
Mentor
Accepted solution

Well, since your question is to determine whether 2 block definition have the same geometric entities, you simply need to compare each element entities in the block definition by their geometry, besides comparing the common block definition properties first: such as Count/Origin/Layer/Color/LineWeight/LineType...

 

Depending the type of the element entity, comparing the geometries would be different, some are quite easy (Line, Circle...) some might be harder (Polyline, Spline...). For example, for line, you simply compare 2 lines' Start/EndPoint, or Circle, you simply compare Center and Radius, for LwPolyline, you compare each Vertex and the bulge at the vertex...

 

Something like:

 

Dim ent1 As AcadEntity
Dim ent2 As AcadEntity
Dim theSame As Boolean

For Each ent1 in BlockDefinition1 
  theSame=Ture
  For Each ent2 in BlockDefinition2
    If IsGeometryTheSame(ent1, ent2) Then
      theSame=False
      Exit For
    End If
  Next
  If Not theSame Then Exit For
Next

Private Function IsGeometryTheSame(ent1As AcadEntity, ent2 As AcadEntity) As Boolean
  Dim theSame As Boolean
  If TypeOf ent1 Is AcadLine Then
     theSame=CompareLines(ent1, ent2)
  ElseIf TypeOf ent1 Is AcadCircel Then
    theSame=CompareCircle(ent1, ent2)
  Else If TypeOf ent1 Is AcadArc Then
    ...
  EnseIf..... Then
     .... ...
  EnseIf..... Then  
    ... ...
  End If
  IsGeometryTheSame=theSame
End Function

Private Function CompareLines(ent1 As AcadEntity, ent2 As AcadEntity) As Boolean
  If Not (TypeOf ent2 Is AcadLine) Then
    CompareLines = False
    Exit Function
  End If
  Dim line1 As AcadLine
  Dim line2 As AcadLine
  Set line1=ent1
  Set line2=ent2
  '' Compare startpoints and endpoints of the 2 lines here
  CompareLines=[result]
End Function

 

Well, there could be quite some code, depending how many different types of entities you expect to come across in the block definition. If the block definitions have nested blocks, you would do it recursively (hoping that is not the case of yours). The the code logic itself is rather straightforward: if you want to distinguish entities by its geometry, you need to compare the geometric information. 

 

Since you also post the same question in the .NET forum, I'd say, the logic of comparing geometries would be the same, except for the code using Acad .NET API to reach the block definition and entities in it.

 

Norman Yuan

Drive CAD With Code

EESignature

Message 6 of 6

Mario-Villada
Advocate
Advocate

This is awesome!. thank you so much. you pretty much did all the hard work for me. I will adapt your code, To be honest I will not need to compare 100% of the the entities in the block definition. if I first check the count property and bounding box are the same, I will be happy for instance to check only all lines are the same and ignore any other type of entities. I was hoping to find an easier solution but think think this is it.

0 Likes