set attribute with VBA code

Discussion in 'AutoCAD' started by shastu, Jun 25, 2004.

  1. shastu

    shastu Guest

    I need code to set an attribute within a block with a certain tag. It would be setting the value to the same everytime the code is used. Can someone please give me this simple code. I am brand new to VBA within AutoCAD.
     
    shastu, Jun 25, 2004
    #1
  2. shastu

    DougStrong Guest

    This code works for me
    ----------------------------------------------------
    '********Module code***********************
    Option Explicit
    Public Atribts As Variant
    Public ssnew As Object
    '********************************Form code******************
    Private Sub humm()

    On Error Resume Next 'THIS CLEARS ERROR IF NO SELECTIONSET "TBLK"

    'delete the selection set
    ThisDrawing.SelectionSets.Item("TBLK").Delete

    '**********FILTERS FOR SELECT CALL*****************
    Dim intGroupCode(0) As Integer
    Dim varGroupValue(0) As Variant

    intGroupCode(0) = 0
    varGroupValue(0) = "insert"
    '*******END FILTER SETUP******************

    On Error GoTo Err_Control 'define error function

    '*********CREATE SELECTION SET*********************
    Set ssnew = ThisDrawing.SelectionSets.Add("TBLK")
    '*****************************************************


    '********FILL SELECTION SET WITH BLOCKS*****************
    ssnew.Select acSelectionSetAll, , , intGroupCode, varGroupValue


    If ssnew.Count >= 1 Then 'If a block is found

    For r = 0 To ssnew.Count - 1 'Get the block's attributes

    Atribts = ssnew.Item(r).GetAttributes

    For i = LBound(Atribts) To UBound(Atribts)
    MsgBox Atribts(i).TagString '*****Visual feedback
    If UCase(Atribts(i).TagString) = "TAG1" Then

    Atribts(i).TextString = "Ureka!"

    ElseIf UCase(Atribts(i).TagString) = "TAG3" Then

    Atribts(i).TextString = "JOGI!"

    End If

    ssnew.Item(0).Update
    Next i
    Next r


    'delete the selection set
    ThisDrawing.SelectionSets.Item("TBLK").Delete
    Else
    'no attribute block, inform the user
    MsgBox "No Title Block Attributes DETECTED", vbCritical, "Infio"

    'delete the selection set
    ThisDrawing.SelectionSets.Item("TBLK").Delete

    End

    End If

    Exit Sub

    Err_Control:

    'display error number and description
    MsgBox Err.Number & " " & Err.Description

    End

    End Sub
     
    DougStrong, Jul 24, 2004
    #2
  3. shastu

    shastu Guest

    I am getting a compile error telling me that the "r" variable is not defined. What should it be defined as?
     
    shastu, Jul 26, 2004
    #3
  4. shastu

    shastu Guest

    Never mind Doug, I got it working. Now I just need to see if I can make it do what I need it to do. Thanks, and I will let you know if it works for my needs, I just wanted to let you know that I got it to work and you can ignore my previous post. I put the declarations in the wrong area.
     
    shastu, Jul 26, 2004
    #4
  5. shastu

    shastu Guest

    Thank you so much Doug. That is exactly what I was looking for and even simple enough for me to understand. God Bless you!
     
    shastu, Jul 26, 2004
    #5
  6. shastu

    Conan Witzel Guest

    I have a function I set in each project. This way I don't mess with my
    error checking further on in the project. In addition I don't have to do
    any additional checking if I need other selection sets etc

    Sub CheckSelectionSet(Setname As String)
    On Error GoTo exitclean
    Dim sset As AcadSelectionSet
    ThisDrawing.SelectionSets.Item(Setname).Delete
    Set sset = Nothing
    exitclean:

    End Sub

    Hope this helps.

    Conan Witzel
     
    Conan Witzel, Jul 26, 2004
    #6
  7. shastu

    Conan Witzel Guest

    Oh, and I can call the routine again at the end of the code to delete it
    cleanly.

    Thanks,
    Conan
     
    Conan Witzel, Jul 26, 2004
    #7
  8. shastu

    shastu Guest

    Are you still out there Doug? (Or anyone else who wants to take a crack at this) If you remember back to the original question, I just needed you to tell me how to change an attribute value. You so graciously provided the code for it and I added it to the code that I already had. The complete code now lets the user select a directory to work on. It then opens every autocad drawing in that folder individually, changes the attribute value, and then saves and closes the file. It has been working great, but there is one more thing I would like to add to it. If it would so happen that there is a file that does not have the given attribute, right now it bombs the program. Instead I would like it to close the file and record the filename in a text file giving the user a complete list of files that did not have that attribute within that folder. When it is done going through everyfile, I would like it to open the .txt file with the list in it so they know which files were not affected. Does this make sense?
     
    shastu, Aug 6, 2004
    #8
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.