autocad

Find Intersect Between Two Block Objects

This code will create two blocks that have a light weight polyline of a triangle and a trapezoid. We want the two object to have a specific gap between them without intersecting. WE CAN DO THIS! No Calculus required. We do this by exploding the block and then offsetting the polyline a specific distance with method Offset. It then determines if there is an intersection between the two offset objects by using the IntersectWith method . If it does find an intersection, it will iterate and move the second object until there is no longer and intersection. Block 1 is a triangle and block two is a trapezoid. From the image below you can see there are two intersection points.

It is important that the block is first exploded before using the intersect method, or else the method will find an intersection of the bounding box and not the actual object as shown in the 3rd image below.

Before Moving Block 2
After Moving Block 2
After moving the block If the block is not exploded. Blue and Red Shows the Bounding Boxes
Option Explicit
Sub findIntersect()
    Dim acadApp As AcadApplication
    Dim acadDoc As AcadDocument
    Dim basepoint1(0 To 2) As Double
    Dim basepoint2(0 To 2) As Double
    Dim basePoint2move(0 To 2) As Double
    Dim basepointblock(0 To 2) As Double
    Dim intersect1 As Variant
    Dim intersect2 As Variant
    Dim intPoints As Variant
    Dim iterateDist As Double
    Dim objblock1 As AcadBlock
    Dim objblock2 As AcadBlock
    Dim objBlockRef As AcadBlockReference
    Dim objss As AcadSelectionSet
    Dim offsetDist As Double
    Dim plineObjLW As AcadLWPolyline
    Dim ProceedNext As Long
    Dim shapeName1 As String
    Dim shapeName2 As String
    Dim vertices() As Double
    'get access to autocad application
    Set acadApp = GetObject(, "AutoCAD.Application")
    Set acadDoc = acadApp.Documents.Add
    'set some variables
    offsetDist = 0.1
    iterateDist = 0.01
    shapeName1 = "triangle"
    shapeName2 = "rectangle"
    'define the two blocks
    basepointblock(0) = 0: basepointblock(1) = 0: basepointblock(2) = 0
    Set objblock1 = acadDoc.Blocks.Add(basepointblock, shapeName1)
    Set objblock2 = acadDoc.Blocks.Add(basepointblock, shapeName2)
    'add the triangle to the block
    ReDim vertices(5)
    vertices(0) = 0: vertices(1) = 0
    vertices(2) = 5: vertices(3) = 0
    vertices(4) = 3: vertices(5) = 5
    Set plineObjLW = objblock1.AddLightWeightPolyline(vertices)
    plineObjLW.Closed = True
    'add the trapizoid to the block
    ReDim vertices(7)
    vertices(0) = 0: vertices(1) = 0
    vertices(2) = 2: vertices(3) = 0
    vertices(4) = 3: vertices(5) = 2
    vertices(6) = -1: vertices(7) = 2
    Set plineObjLW = objblock2.AddLightWeightPolyline(vertices)
    plineObjLW.Closed = True
    'insert the block 1 into model space, then explode and offset
    basepoint1(0) = 0
    basepoint1(1) = 0
    basepoint1(2) = 0
    Set objBlockRef = acadDoc.ModelSpace.InsertBlock(basepoint1, shapeName1, 1, 1, 1, 0)
    intersect1 = objBlockRef.Explode
    On Error Resume Next
    intersect1 = intersect1(0).Offset(offsetDist)
    If Err.Number <> 0 Then
        intersect1 = intersect1(1).Offset(offsetDist)
        Err.Clear
    End If
    On Error GoTo 0
    'insert the block 2 into model space, then explode and offset
    basepoint2(0) = 5.1
    basepoint2(1) = 0
    basepoint2(2) = 0
    Set objBlockRef = acadDoc.ModelSpace.InsertBlock(basepoint2, shapeName2, 1, 1, 1, 0)
    intersect2 = objBlockRef.Explode
    On Error Resume Next
    intersect2 = intersect2(0).Offset(offsetDist)
    If Err.Number <> 0 Then
        intersect2 = intersect2(1).Offset(offsetDist)
        Err.Clear
    End If
    On Error GoTo 0
    'move block 2 until it no longer intersects block 1
    Do Until ProceedNext = 1
        On Error Resume Next
        intPoints = intersect1(0).IntersectWith(intersect2(0), acExtendNone)
        On Error GoTo 0
        If VarType(intPoints) <> vbEmpty And IsArray(intPoints) = True Then
            If UBound(intPoints) > -1 Then
                basePoint2move(0) = basepoint2(0) + iterateDist
                intersect2(0).Move basepoint2, basePoint2move
                basepoint2(0) = basepoint2(0) + iterateDist
            Else
                ProceedNext = 1
            End If
        Else
            ProceedNext = 1
        End If
    Loop
    'erase all objects
    Set objss = acadDoc.SelectionSets.Add("ToErase")
    objss.Select acSelectionSetAll
    objss.Erase
    objss.Delete
    're insert the blocks into model space
    Set objBlockRef = acadDoc.ModelSpace.InsertBlock(basepoint1, shapeName1, 1, 1, 1, 0)
    Set objBlockRef = acadDoc.ModelSpace.InsertBlock(basepoint2, shapeName2, 1, 1, 1, 0)
End Sub

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s