ChadSnyder Posted November 3, 2006 Share Posted November 3, 2006 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! Quote Link to comment Share on other sites More sharing options...
dbroada Posted November 3, 2006 Share Posted November 3, 2006 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 Quote Link to comment Share on other sites More sharing options...
ChadSnyder Posted November 3, 2006 Author Share Posted November 3, 2006 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? Quote Link to comment Share on other sites More sharing options...
fixo Posted November 3, 2006 Share Posted November 3, 2006 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 Quote Link to comment Share on other sites More sharing options...
dbroada Posted November 3, 2006 Share Posted November 3, 2006 I'm almost out the door (!) but something like for lbound(varAttributes) to ubound(varAttributes) check the help - it is in there. Quote Link to comment Share on other sites More sharing options...
ChadSnyder Posted November 3, 2006 Author Share Posted November 3, 2006 Your the man...thanks Quote Link to comment Share on other sites More sharing options...
fixo Posted November 3, 2006 Share Posted November 3, 2006 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' Quote Link to comment Share on other sites More sharing options...
muck Posted July 22, 2009 Share Posted July 22, 2009 With the above code I get "AutoCAD Main Window" not open. Why is that? Thank you, Quote Link to comment Share on other sites More sharing options...
metozade Posted January 1, 2012 Share Posted January 1, 2012 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.