VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

VBA find or search

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
ofcarvajall
375 Views, 3 Replies

VBA find or search

Hello,

 

I need a method in Autocad VBA like the Autocad linecommand "find". The goal is to have a list (I am not sure if a dictinionary) with the results of this command.

 

For example:

I want to identify all elements that are related to the word "Motor".

 

I appreciate any help.

 

Regards,

Tags (3)
Labels (3)
3 REPLIES 3
Message 2 of 4
Ray-Sync
in reply to: ofcarvajall

Hi, Look at this:

Sub FindText(keyword As String)
    Dim doc As Object
    Set doc = ThisDrawing

    Dim result() As String
    Dim i As Integer
    i = 0

    ' Iterate through all entities in the drawing
    Dim obj As Object
    For Each obj In doc.ModelSpace
        If obj.ObjectName = "AcDbText" Or obj.ObjectName = "AcDbMText" Or obj.ObjectName = "AcDbAttributeReference" Then
            ' Check if the text contains the keyword
            If InStr(1, obj.TextString, keyword, vbTextCompare) > 0 Then
                ' Add the text to the result array
                ReDim Preserve result(i)
                result(i) = obj.TextString
                i = i + 1
            End If
        End If
    Next obj

    ' Check if any results were found
    If i > 0 Then
        ' Display the results
        MsgBox "Elements related to '" & keyword & "':" & vbCrLf & Join(result, vbCrLf)
    Else
        MsgBox "No elements related to '" & keyword & "' found."
    End If
End Sub
jefferson
Message 3 of 4
norman.yuan
in reply to: ofcarvajall

A bit of correction: while the code does the work correctly by looping through ModelSpace (or PaperSpace), but it would not find text values of AcadAttributeReferences, because they are not directly owned/contained in the ModelSpace/PaperSpace, rather they are inside AcadBlockReferences. So if the OP does indeed expect text values of block attributes are also searched, the code should be updated like:

 

    Dim obj As Object
    Dim blk As AcadBlockReference
    Dim atts As Variant
    Dim n As Integer
    For Each obj In doc.ModelSpace
        If obj.ObjectName = "AcDbText" Or obj.ObjectName = "AcDbMText" Then
            ' Check if the text contains the keyword
            If InStr(1, obj.TextString, keyword, vbTextCompare) > 0 Then
                ' Add the text to the result array
                ReDim Preserve result(i)
                result(i) = obj.TextString
                i = i + 1
            End If
        ElseIf TypeOf obj Is AcadBlockReference Then
            Set blk=obj
            If blk.HasAttributes Then
                atts = blk.GetAttributes
                For n=0 to UBound(atts)
                    If InStr(1, atts(n).TextString, keyword, vbTextCompare) > 0 Then
                       ' Add the text to the result array
                        ReDim Preserve result(i)
                        result(i) = obj.TextString
                        i = i + 1
                     End If
                Next
            End If
        End If
    Next obj

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 4 of 4
ofcarvajall
in reply to: Ray-Sync

Thanks!!!

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

Post to forums  

Forma Design Contest


AutoCAD Beta