Tools
Home Products Services VBA for ADT

Tools

The Code

Findfile

 

Function findfile(ByVal fn As String, Optional doProject As Boolean) As String
    Dim pths, fName As String, i As Long
    On Error Resume Next
    Err.Clear
   
    pths = Application.Preferences.Files.SupportPath
    pths = Split(pths, ";", , 1)
   
    ReDim Preserve pths(UBound(pths) + 1)
    pths(UBound(pths)) = Application.Path
   
    fn = Trim(fn)
    If Mid(fn, 2, 1) = ":" Then fn = Mid(fn, 3)
    While Left(fn, 1) = "\"
        fn = Mid(fn, 2)
    Wend
    fn = Trim(fn)
   
    For i = 0 To UBound(pths)
        fName = pths(i)
        If Right(fName, 1) <> "\" Then
            fName = fName & "\" & fn
        Else
            fName = fName & fn
        End If
       
        If Dir(fName) <> "" Then
            findfile = fName
            Exit For
        End If
    Next
       
    fName = ThisDrawing.GetVariable("projectname")
   
    pths = Empty
   
    If doProject = True And findfile = "" And fName <> "" Then
        pths = Application.Preferences.Files.GetProjectFilePath(fName)
        If Not (IsEmpty(pths)) Then
            pths = Split(pths, ";", , 1)
           
            For i = 0 To UBound(pths)
                fName = pths(i)
                If Right(fName, 1) <> "\" Then
                    fName = fName & "\" & fn
                Else
                    fName = fName & fn
                End If
               
                If Dir(fName) <> "" Then
                    findfile = fName
                    Exit For
                End If
            Next
           
        End If
    End If
   
End Function

AutoCAD Distance

 

Function Distance(Point1, Point2) As Double 

    Dim dist As Double, i As Long
 

    On Error Resume Next
    For i = LBound(Point1) To UBound(Point1)
        dist = dist + ((Point1(i) - Point2(i)) ^ 2)
        If Err Then Exit For
    Next
 

    Distance = Sqr(dist) 

End Function

Get Centroid of an Object

 

Function getcentroid(obj As AcadEntity) As Variant
    Dim ll, ur
    obj.GetBoundingBox ll, ur
    getcentroid = ThisDrawing.Utility.PolarPoint(ll, _
        ThisDrawing.Utility.AngleFromXAxis(ll, ur), Distance(ll, ur) / 2#)
End Function

Check to See if a Value Exists in an Array

 

Function isInArray(arr As Variant, val) As Long
    isInArray = -1
    If IsArray(arr) Then
        Dim i As Long
        For i = 0 To UBound(arr)
            If arr(i) = val Then
                isInArray = i
                Exit Function
            End If
        Next
    End If
End Function