Jump to content

Finding a title block name and referring to it in VBA AutoCad


flamelitface

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

Flamelitface

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.

Link to comment
Share on other sites

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)
   x.Delete
   myItem.Name = BlockName
   BlockName = ""
   End If
Next myItem
ThisDrawing.SelectionSets.Item("Blocks").Delete
ThisDrawing.Regen acAllViewports
End Sub

Link to comment
Share on other sites

Cheers dbroada

 

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.

Link to comment
Share on other sites

  • 4 weeks later...

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.

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...