View Full Version : Finding a title block name and referring to it in VBA AutoCad
26th Aug 2010, 10:42 am
Hello, I am using autocad2005 and I'm running a script which is almost in completion. In the script I select a block by clicking it, however, I already know the blocks name and I would like to use that so that the mouse doesn't have to be used at all.
26th Aug 2010, 11:17 am
are you running a script or VBA and what are you doing with the block once selected?
If VBA you can create a selection set in VBA refering to the block's name. I'm not sure you can directly access a block by name in a script but I think you can create a selection set in LISP and call the block through that.
Usually if I am after a title block in a script I access it through its insertion point which is always 0,0 if the drawing was done to our standards.
26th Aug 2010, 11:33 am
I am running a LISP routine that activates a VBA macro. Within the vba macro, it selects a block (by selecting it with the mouse at the moment) and then it renames attributes accordingly.
I've seen scripts that seem to run through all entities til it finds the right one such as this one here: http://www.visiblevisual.com/index.php/AutoCad-VB/VBA/delete-blocks-by-name.html
But it seems to fail at this line:
If oBkRef.EffectiveName = BlockName Then
I don't think it recognises .effectivename as VBA syntax...
26th Aug 2010, 09:06 pm
You can not post hyperlinks yet, that's why your post was moderated -and please keep in your mind this rule for your next 8 posts.
26th Aug 2010, 10:33 pm
Okey dokey, any responses to my original problem will still be greatly appreciated!
31st Aug 2010, 09:03 am
this may not be appropriate for your routine or the best method anyway but if I have to find a particular block I create a selection set and step through. The following was the first of my routines that I found...
Public Sub UpdateBorder()
'%<\AcVar Filename \f "%fn2">%
Dim myEntity As AcadEntity
Dim mySelSet As AcadSelectionSet
Dim TempName As String
Dim gpCode(0 To 1) As Integer
Dim dataValue(0 To 1) As Variant
Dim groupCode As Variant
Dim dataCode As Variant
Dim myAttrib As Variant
Dim myCurrentAtt As Variant
Dim BlockName As String
Dim P1(0 To 2) As Double
Dim BlockPos As Variant
Dim x As AcadBlockReference
Dim objScale As Single
Dim myItem As Variant
Dim i As Integer
P1(0) = 0: P1(1) = 0: P1(2) = 0
BlockPos = P1
objScale = 1
gpCode(0) = 0 'item
gpCode(1) = 2 '?
groupCode = gpCode
dataValue(0) = "INSERT" 'item type
dataValue(1) = "STLA?-E" '?
dataCode = dataValue
Set mySelSet = ThisDrawing.SelectionSets.Add("Blocks")
mySelSet.Select acSelectionSetAll, , , groupCode, dataCode
For Each myItem In mySelSet
If UCase(Left$(myItem.Name, 5)) = "STLA1" Then BlockName = "P:\Design_Office\E343\Blanks\E343S-A1.dwg"
If UCase(Left$(myItem.Name, 5)) = "STLA2" Then BlockName = "P:\Design_Office\E343\Blanks\E343S-A2.dwg"
If UCase(Left$(myItem.Name, 5)) = "STLA3" Then BlockName = "P:\Design_Office\E343\Blanks\E343S-A3.dwg"
If BlockName <> "" Then
Set x = ThisDrawing.ModelSpace.InsertBlock(BlockPos, BlockName, objScale, objScale, objScale, 0)
myItem.Name = BlockName
BlockName = ""
31st Aug 2010, 09:55 am
I ended up solving my problem, the If oBkRef.EffectiveName = BlockName Then works fine in autocad 2007 so I figure that some vba doesn't work in old versions of autocad. I've written my script now and it picks out a title block by name, and then changes attributes accordingly. I'll post the script later for those searching for a solution to the same problem.
22nd Sep 2010, 02:31 pm
the "Effectivename" parameter I believe was added once support for dynamic blocks was added
23rd Sep 2010, 03:18 am
Just a hint you can globaly change block attributes no matter where they are within a dwg MS or PS to stop updating numerous blocks you can add a second condition that it must be block name and a unique attribute value. Eg Title block but sheet 3 only
If you want to run as a script you would need to either hard code block name, or ask or pick to retrieve name. I have done both, hard coded preliminary dwg updates to become issued v's 1 block update at a time. You can do extra stuff I pick the unique attribute value as either another block's attribute or as text so indpendendant of drafty method of labelling.
Powered by vBulletin™ Version 4.1.2 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.