Finding intersection point of a line and block entities

Finding intersection point of a line and block entities

a.kouchakzadeh
Advocate Advocate
1,692 Views
5 Replies
Message 1 of 6

Finding intersection point of a line and block entities

a.kouchakzadeh
Advocate
Advocate

Hello world

 

Im trying to find the intersection of lines and blocks, but I need the accurate position. I found out the the intersectwith method returns the line and bounding box intersection point. my block is a circle and I need the exact position. by the way, its not just one circle, its a couple of circles with the same center but different radiuses.

here is the code:

Dim oblk As AcadBlock
Dim oBlk1 As AcadBlock
Dim oBlkRef As AcadBlockReference
Dim oBlkRef1 As AcadBlockReference
Dim oEnt As AcadEntity
Dim oEnt1 As AcadEntity
Dim SS As AcadSelectionSet


'*******

Dim BlockEnt As AcadEntity

For Each lineobj In LineSSet
   For Each blockrefobj In BlockSSet

   Set SS = BlockSSet
   For Each oBlkRef In SS

Set oblk = ThisDrawing.Blocks(oBlkRef.Name)
For Each oEnt In oblk
If TypeOf oEnt Is AcadBlockReference Then
Set oBlkRef1 = oEnt
Set oBlk1 = ThisDrawing.Blocks(oBlkRef1.Name)
For Each oEnt1 In oBlk1
With oEnt1
If Not ThisDrawing.Layers(.Layer).Lock Then
.Layer = "0"
.color = acByLayer
End If
End With
Next oEnt1
Else

 


intpoints = lineobj.IntersectWith(oEnt, acExtendNone) '////here is the problem
MsgBox intpoints(0) & " " & intpoints(1)

End If
Next oEnt
Next oBlkRef

 

 

there is an intersection, but I get the subscript out of range error for the message box.

where is the problem?

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

norman.yuan
Mentor
Mentor
Accepted solution

Firstly, the code you showed, except for the last 2 lines in the inner "For Each...Next" looks irrelevant to your question, you really need to show the code that matters to the question. 

 

Then, the oEnt used in IntersectWith() method is a BlockReference WITHIN a Block Definition, not as your question asked, a Circle. 

 

Third, you try to detect a line (or anything, for that matter) in ModelSpace (or PaperSpace) intersects with an BLOCK DEFINITION! Thus, there can NEVER be a intersection between this two things.

 

Fourth, the IntersectWith() method does not detects one or more intersecting points, it returns vbEmpty. That is why the code MsgBox ....gets you "Subscript out of range..." error. You need to test the returned Variant to see if it is empty or not.

 

Now, comes to how to find intersecting point of a line with a BLOCKREFERENCE. As you have already known, when directly call IntersectWith(), you get points on the block reference's bounding box, because block reference is a reference of a block definition (imagine it as a picture of the block definition, a single sheet). To find intersecting with element entities in a block, you are in right track, sort of: you can loop through each entity in the block definition to test intersecting; HOWEVER, you need to transform each element entity in the block definition to the position of the block reference and test intersecting there. 

 

With VBA, an easy solution would be:

1. Call AcadBlockReference.Explode() method, then you get a set of entities being exactly the same ones in the block definition, but transformed the the position of the block reference;

2. test your line's intersecting with each of the entities from explosion. You have to be able to identify which type of entity/entities is/are expected to be intersecting, right?

3. After intersecting test, you can erase all the entities from the explosion. Note: the original block reference is not affected by the explosion.

 

Hope this helps.

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 6

a.kouchakzadeh
Advocate
Advocate

Special thanks to you Mr. Yuan.

yes indeed it was a great explanation. but Im confusing between a block definition and a block reference.

is there any way I can loop through a block reference entities without exploding it?

the block consists of many circles. some of the blocks have a line, some don't. 

this image shows the block which I want to find the intersection point of the yellow line with it.

Capture.JPG

 

if you have some free time, can you help me with the code? Im still confusing between block References and block definitions.

 

0 Likes
Message 4 of 6

a.kouchakzadeh
Advocate
Advocate

I found out some thing. when I explode a blockreference, does it keep the original block and add the block elements on it?

because when I delete elements, I can still see the original block under the exploded block.

 

0 Likes
Message 5 of 6

Ed__Jobe
Mentor
Mentor
Accepted solution

Blocks consist of two parts, a definition and an insertion or reference to the definition. When you create a block, the insertion point you pick becomes the block definition's 0,0,0 of the block's coordinate system. A BlockReference does not contain all the Block's entities. It just stores the insertion point, scale and rotation. Then AutoCAD draws the block starting at the insertion point.

 

When you use the VBA Explode method, it copies the block definition's entities at the insertion point, transforming the entities from the block's coordinate system to the current coordinate system. It does not erase the original BlockReference object.

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 6 of 6

a.kouchakzadeh
Advocate
Advocate

Thanks Ed, I exploded my block successfully and looped through the entities to find the intersection point and i deleted the entities after i got the points.

0 Likes