Jump to content

New Acad VBA


ChadSnyder

Recommended Posts

I'm new to AutoCAD VBA (not to VB or VBA). I need to know a couple of basic things.

 

I want to (through VBA) select a block and open a form showing it's attributes. I was able to do this through a sample I found but it does not allow me to select the block it just found the first one it came to.

 

Basicly I just want to select the block and open a form, show the attribute info and be able to update the data.

 

Thanks for your help!

Link to comment
Share on other sites

its not exactly what you want but I have just posted some code in reference to another question. This is vary raw but gives an idea of selecting a block. Check out GetAttributes in the help file. You will need to access .TextString rather than .InsertionPoint and dont forget ubound & lbound to gte the correct number of attributes.

 

Public Sub GetSomething()
Dim returnObj As AcadObject
Dim myItem As AcadBlockReference
Dim p As Variant
Dim P1(0 To 2) As Double
Dim varAttributes As Variant
ThisDrawing.Utility.GetEntity returnObj, P1, "select it"
If returnObj.ObjectName = "AcDbBlockReference" Then
   Set myItem = returnObj
   
   varAttributes = myItem.GetAttributes
   p = varAttributes(0).InsertionPoint
   P1(0) = p(0)
   
   End If

End Sub

Link to comment
Share on other sites

its not exactly what you want but I have just posted some code in reference to another question. This is vary raw but gives an idea of selecting a block. Check out GetAttributes in the help file. You will need to access .TextString rather than .InsertionPoint and dont forget ubound & lbound to gte the correct number of attributes.

 

Public Sub GetSomething()
Dim returnObj As AcadObject
Dim myItem As AcadBlockReference
Dim p As Variant
Dim P1(0 To 2) As Double
Dim varAttributes As Variant
ThisDrawing.Utility.GetEntity returnObj, P1, "select it"
If returnObj.ObjectName = "AcDbBlockReference" Then
   Set myItem = returnObj
   
   varAttributes = myItem.GetAttributes
   p = varAttributes(0).InsertionPoint
   P1(0) = p(0)
   
   End If

End Sub

Thanks that worked perfect.

 

Now I need to know how to count the number of attributes in the block so I can do a For Next loop

 

I would think you could do something like myItem.GetAttributes.Count but this did not work.

 

Ideas?

Link to comment
Share on other sites

I'm new to AutoCAD VBA (not to VB or VBA). I need to know a couple of basic things.

 

I want to (through VBA) select a block and open a form showing it's attributes. I was able to do this through a sample I found but it does not allow me to select the block it just found the first one it came to.

 

Basicly I just want to select the block and open a form, show the attribute info and be able to update the data.

 

Thanks for your help!

Attached is example how to solve this task

Change to your needs

On the second question about count attrinbutes

see Ubound property

 

attCount=Ubound(myItem.GetAttributes)-Lbound(myItem.GetAttributes) + 1

not tested though, I wrote it blindly

 

Hth

 

Fatty

 

~'J'~

ChAtt.zip

Link to comment
Share on other sites

Your the man...thanks

 

If you need to count attributes through loop

you could be to use this quicky

But do not forget about this will count all attributes

include constant if there are defined in this block

 

Option Explicit

Sub CountAtts()

    Dim oBlock As AcadBlock, _
        oblkRef As AcadBlockReference, _
        bName As String, _
        oEntity As AcadEntity, _
        varPt As Variant, _
        oObj As AcadObject, _
        oAtt As AcadAttribute, _
        i As Long, _
        iCount As Long


    On Error GoTo Err_Control

    ThisDrawing.Utility.GetEntity oEntity, varPt, " Select block"
    If oEntity Is Nothing Then Exit Sub
    If TypeOf oEntity Is AcadBlockReference Then
         Set oblkRef = oEntity
    End If
    If oblkRef.HasAttributes Then
         bName = oblkRef.Name
         Set oBlock = ThisDrawing.Blocks(bName)
         iCount = 0
         For i = 0 To oBlock.Count - 1
              Set oObj = oBlock.Item(i)
              If TypeOf oObj Is AcadAttribute Then
                   Set oAtt = oObj
                   
                   If oAtt.PromptString <> "" Then
                        Debug.Print "Prompt is :" & oAtt.PromptString
                   Else
                        Debug.Print "Prompt does not defined"
                   End If

              Debug.Print "Tag is :" & oAtt.TagString
              
              If oAtt.TextString <> "" Then
                   Debug.Print "Default value is :" & oAtt.TextString
              Else
                   Debug.Print "Default value does not defited"
              End If
              iCount = iCount + 1
         End If
    Next i
End If

MsgBox "Attribute Count: " & iCount

Err_Control:
If Err Then MsgBox Err.Description
End Sub

 

 

Hth :)

 

Fatty

 

>'J'

Link to comment
Share on other sites

  • 2 years later...
  • 2 years later...

Hi,

I need a code that only you can do

 

I set tag

the value of an attribute

dwg file name that will make

 

ThisDrawig.SaveAs ("attribute value)

 

I'm working on, but I could not do

Is this possible?

 

thanks

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...