Ninjaneer Posted September 26, 2011 Share Posted September 26, 2011 Hi all, I'm trying to access title block attributes from VBA so that I can standarise them across drawing sets. I'm very experienced with Excel VBA but I'm afraid I'm a total noob with AutoCAD. My drawing contains a block called A3BORDER, I can read the items from it and by looking for the right ObjectName I can ignore static text and graphic objects. Here's the code: Sub foo() Set myblk = ThisDrawing.Blocks("*PAPER_SPACE").Item(2) For i = 0 To ThisDrawing.Blocks("A3BORDER").Count - 1 Set myobj = ThisDrawing.Blocks("A3BORDER").Item(i) If myobj.ObjectName = "AcDbAttributeDefinition" Then Debug.Print myobj.StyleName, myobj.TextString End If Next End Sub The problem is I get the attributes from the template not the layout, for example SHEET_NO comes back with value 'XX OF YY' instead of '8 OF 8'. 'XX OF YY' is the default value I see if I edit the title block in the Block Editor. I've tried looking through the items in PAPER_SPACE and one has EffectiveName = "A3BORDER", is that the instance I need? If so how do I get it's items? Thanks Ninja Quote Link to comment Share on other sites More sharing options...
mdbdesign Posted September 26, 2011 Share Posted September 26, 2011 It is good to know about your excel VBA experience. Welcome to the forum Quote Link to comment Share on other sites More sharing options...
Ninjaneer Posted September 27, 2011 Author Share Posted September 27, 2011 Well there's a productive first response. Pardon my ignorance of etiquette on this board but I felt it may be useful to show that I am comfortable with VBA but not the AutoCAD object structure. Ho hum. Quote Link to comment Share on other sites More sharing options...
spiff88 Posted September 28, 2011 Share Posted September 28, 2011 Not sure exactly about your code, but maybe the object you need to be looking for is myobj.ObjectName = "AcDbAttribute" instead of myobj.ObjectName = "AcDbAttributeDefinition" Quote Link to comment Share on other sites More sharing options...
spiff88 Posted September 28, 2011 Share Posted September 28, 2011 Also - have you looked at the myobj.GetAttributes method? Quote Link to comment Share on other sites More sharing options...
Ninjaneer Posted September 29, 2011 Author Share Posted September 29, 2011 Hi Spiff, Thanks but the block has no AcDbAttribute items, just AcDbAttributeDefinition, AcDbText for the fixed test and other graphical items such as lines etc. You've set me on the right path though. I had already tried this ThisDrawing.Blocks("A3BORDER").HasAttributes Which fails so I'd not bothered with GetAttributes. What I had not done is the HasAttributes test on myblk for some reason. That returns True and GetAttributes returns the the text I'm after. The clue should have been that the above code fails to the debug prompt, if it was a valid test it would have returned False. By testing myblk instead I'm effectively this instead doing this (this is not valid code, just illustration) ThisDrawing.Blocks("*PAPER_SPACE").Blocks("A3BORDER").HasAttributes Which obviously is the instance on paper not the one I see in the block editor. Thanks again Spiff88 Ninja Quote Link to comment Share on other sites More sharing options...
BIGAL Posted September 30, 2011 Share Posted September 30, 2011 (edited) There is posts here about using VBA and title block attributes I use them every day so I have posted code will try to find posts for you Couldn't find pre posted code so here it is again may be helpfull Public Sub add_project_number() ' This Updates the project number Dim SS As AcadSelectionSet Dim Count As Integer Dim FilterDXFCode(1) As Integer Dim FilterDXFVal(1) As Variant Dim attribs, newtext As Variant Dim BLOCK_NAME As String 'On Error Resume Next Dim startCH As Double newtext = ThisDrawing.Utility.GetString(True, "Enter new project code : ") FilterDXFCode(0) = 0 FilterDXFVal(0) = "INSERT" FilterDXFCode(1) = 2 FilterDXFVal(1) = "DA1DRTXT" BLOCK_NAME = "DA1DRTXT" Set SS = ThisDrawing.SelectionSets.Add("issued") SS.Select acSelectionSetAll, , , FilterDXFCode, FilterDXFVal For Cntr = 0 To SS.Count - 1 attribs = SS.Item(Cntr).GetAttributes attribs(1).TextString = newtext attribs(1).Update Next Cntr ThisDrawing.SelectionSets.Item("issued").Delete End Sub Edited September 30, 2011 by BIGAL Quote Link to comment Share on other sites More sharing options...
Cherilm Posted October 14, 2011 Share Posted October 14, 2011 I'm not sure if you got your question answered, but the object you might be looking for is not att definition, but Att Value. I'm still trying to learn the methods and such in AutoCAD so I can't be a great deal of help, but I have come across this in other situations. Not sure, but worth a try. 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.