Author Topic: Test a block name against muliple names  (Read 4154 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
Test a block name against muliple names
« on: January 05, 2006, 06:51:54 PM »
I know how to do this is lisp, but I'm having problems with my lisp file, so I fiugred this might be a good time to teach myself VBA.  I am seaching the paper space (Layout1) for a block, and it could one of six names.

How would I do this?

Would I set up a variable as a variant and then test the objects?

Thanks for any help in advance.  Just looking for a push.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Glenn R

  • Guest
Re: Test a block name against muliple names
« Reply #1 on: January 05, 2006, 07:09:01 PM »
Here's one way:

Code: [Select]
Public Sub SearchForBlock()
    Dim pEnt As AcadEntity
    Dim pBlkRef As AcadBlockReference
   
    For Each pEnt In ThisDrawing.PaperSpace
        If TypeOf pEnt Is AcadBlockReference Then
            Set pBlkRef = pEnt
            Select Case UCase(pBlkRef.Name)
                Case "NAME1"
                    ' Do something here...
                Case "NAME2"
                    ' Do something here...
                Case "NAME3"
                    ' Do something here...
                Case "NAME4"
                    ' Do something here...
                Case "NAME5"
                    ' Do something here...
                Case "NAME6"
                    ' Do something here...
            End Select
        End If
    Next
End Sub

Cheers,
Glenn.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Test a block name against muliple names
« Reply #2 on: January 05, 2006, 07:14:00 PM »
So you are saying that I have to test it against each possiblity?  If so I can do that because no matter which block name it matches I have to do the same thing.  What you are doing looks like the (cond... funtion in lisp.  Would I be able to write it as an (if or statement?

If Or Obj.Name Is "Title-A" Or Obj.Name Is "Title-B"  etc..... ?

Thanks for the help.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Test a block name against muliple names
« Reply #3 on: January 05, 2006, 07:25:11 PM »
Here is what I tired.  It looks like it likes my If statement, but it said it doesn't like where I set LayoutName.
Code: [Select]
Option Explicit
Private Function GetTitleBlock()
Dim LayoutCol As AcadLayouts
Dim Space As AcadLayout
Dim BlkObj As AcadObject
Dim LayoutName As String
Dim AttVar As Variant

Set LayoutName = "Layout1"
Set Space = ThisDrawing.Layout.Item(LayoutName)
For Each Obj In Space
If Obj.Type Is "AcDbBlockReference" And Obj.Name Is "3M-BORDER-A" Or Obj.Name Is "3M-BORDER-B" Or Obj.Name Is "3M-BORDER-C" Or Obj.Name Is "3M-BORDER-D" Or Obj.Name Is "3M-BORDER-E" Or Obj.Name Is "3M-BORDER-E1" Then
Set AttVar = Obj.GetAttributes
End If

End Function

How can I pass the variable AttVar to another sub function?
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Glenn R

  • Guest
Re: Test a block name against muliple names
« Reply #4 on: January 05, 2006, 07:37:36 PM »
Code: [Select]
Public Sub SearchForBlock()
    Dim pEnt As AcadEntity
    Dim pBlkRef As AcadBlockReference
   
    For Each pEnt In ThisDrawing.PaperSpace
        If TypeOf pEnt Is AcadBlockReference Then
            Set pBlkRef = pEnt
            Select Case UCase(pBlkRef.Name)
                Case "NAME1", "NAME2", "NAME3", "NAME4", "NAME5", "NAME6"
                    ' Do something here...
            End Select
        End If
    Next
End Sub

Cheers,
Glenn.


T.Willey

  • Needs a day job
  • Posts: 5251
Re: Test a block name against muliple names
« Reply #5 on: January 05, 2006, 07:42:25 PM »
Thanks Glenn.  I will have to pick this up tomorrow.  It's time for me to leave.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Re: Test a block name against muliple names
« Reply #6 on: January 05, 2006, 07:44:50 PM »
In VBA you only need to use Set when you are using an Object. Any other Type you just use
var = "Layout1"
or
var = 2.0

to return the result of a function:
Code: [Select]
Private Function GetTitleBlock() As Variant
Dim LayoutCol As AcadLayouts
.....whatever
....more whatever

GetTitleBlock = AttVar
End Function
Then in the other Sub/Function you call it like so:
Code: [Select]
OtherSubAttvar = GetTitleBlock
You could also pass the Layout you want to search like so:
Code: [Select]
OtherSubAttvar = GetTitleBlock("Layout1")

''and change the Function to this, remove the Dim Layout Name from the function
Private Function GetTitleBlock(LayoutName as String) As Variant

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Re: Test a block name against muliple names
« Reply #7 on: January 05, 2006, 07:55:35 PM »
So, using Glenn's example:
Code: [Select]
Public Function SearchForBlock(LayoutName As String) As Variant
    Dim pEnt As AcadEntity
    Dim pBlkRef As AcadBlockReference
    Dim Space As AcadBlock
    Dim AttVar As Variant
   
    Set Space = ThisDrawing.Layouts.Item(LayoutName).Block
    For Each pEnt In Space
        If TypeOf pEnt Is AcadBlockReference Then
            Set pBlkRef = pEnt
            Select Case UCase(pBlkRef.Name)
                Case "TDG", "NAME2", "NAME3", "NAME4", "NAME5", "NAME6"
                    AttVar = pBlkRef.GetAttributes
            End Select
        End If
    Next
    SearchForBlock = AttVar
End Function

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Test a block name against muliple names
« Reply #8 on: January 06, 2006, 11:17:40 AM »
Thanks Glen for the initial code, and thanks Jeff for the explanation.  I think this is going to be a long bumpy road for a while.  This should get me started though.  One more question if I may.
So, using Glenn's example:
Code: [Select]
Public Function SearchForBlock(LayoutName As String) As Variant

Why did you add the "As Variant" at the end here?  I will look in the help also, just thought you could tell me easier than I can find it in the help.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Test a block name against muliple names
« Reply #9 on: January 06, 2006, 11:42:24 AM »
A Function always returns a value to the caller.

The TYPE of the value returned < in this case> is a Variant.

So the local variable < in this case>  SearchForBlock = AttVar expects to be assigned a Variant TYPE value for return.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Test a block name against muliple names
« Reply #10 on: January 06, 2006, 11:53:19 AM »
Okay.  I get it.  Thanks Kerry, that makes a lot of sense.

Off to try and learn some more things.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.