Author Topic: Checking if block exists..  (Read 5830 times)

0 Members and 1 Guest are viewing this topic.

hardwired

  • Guest
Checking if block exists..
« on: March 10, 2008, 05:37:43 AM »
Hi,

How can I check if a certain block exists or is inserted in a drawing, which could be on any layout or in modelspace, without cycling through layouts to check?

Is it using ThisDrawing.Blocks.Item("") ? I want to check by name really, a block called "Fixing_Chart"

Chuck Gabriel

  • Guest
Re: Checking if block exists..
« Reply #1 on: March 10, 2008, 07:07:20 AM »
You could do something like this:

Code: [Select]
Function BlockExists(ByRef blockName as String) as Boolean
  Dim block as AcadBlock
  On Error Resume Next
  Set block = ThisDrawing.Blocks(blockName)
  If Err = 0 then
    BlockExists = True
  Else
    BlockExists = False
  End If
  Err.Clear
  On Error Goto 0
  Set block = Nothing
End Function

I'm not generally a fan of inline error handling, but sometimes exceptions are warranted.
« Last Edit: March 10, 2008, 07:19:58 AM by Chuck Gabriel »

hendie

  • Guest
Re: Checking if block exists..
« Reply #2 on: March 10, 2008, 07:38:25 AM »
or search with a selection set

Code: [Select]
Set sset1 = ThisDrawing.SelectionSets.Add("SSbks")
        Dim FilterType(0 To 1) As Integer
        Dim FilterData(0 To 1) As Variant
        FilterType(0) = 0: FilterData(0) = "INSERT"
        FilterType(1) = 2: FilterData(1) = "MyBlockName"
    sset1.Select acSelectionSetAll, , , FilterType, FilterData
then
check the selection set count
if sset1.count =0 then the block doesn't exist

Chuck Gabriel

  • Guest
Re: Checking if block exists..
« Reply #3 on: March 10, 2008, 08:00:33 AM »
or search with a selection set

Code: [Select]
Set sset1 = ThisDrawing.SelectionSets.Add("SSbks")
        Dim FilterType(0 To 1) As Integer
        Dim FilterData(0 To 1) As Variant
        FilterType(0) = 0: FilterData(0) = "INSERT"
        FilterType(1) = 2: FilterData(1) = "MyBlockName"
    sset1.Select acSelectionSetAll, , , FilterType, FilterData
then
check the selection set count
if sset1.count =0 then the block doesn't exist

Oops.  I guess I didn't read carefully enough.  Mine will only tell you whether the block definition exists in the block table.

rkmcswain

  • Swamp Rat
  • Posts: 978
Re: Checking if block exists..
« Reply #4 on: March 10, 2008, 08:02:47 AM »
Quote from: Chuck Gabriel
Oops.  I guess I didn't read carefully enough.  Mine will only tell you whether the block definition exists in the block table.

The original request asks to "check if a certain block exists" (which yours does) -or- "is inserted in a drawing", (which hendie's does) - so I think the OP is covered... ;-)

hardwired

  • Guest
Re: Checking if block exists..
« Reply #5 on: March 10, 2008, 08:16:18 AM »
Hi,

Thanks for the input both of you..

Ok, with either of these, checking if the block exists, how can i therefore check the attribute values. Now i know that i can only check them if a blockreference has been inserted, so i know i can check now that a block exists in the block collections, but what i need to check is if one has been inserted, but without cycling through all the layouts, i want it to be almost instant so when the userform loads, it checks for inserted blocks and displays the attribute values on the form..

I can do the attribute check and populate the form, its just checking for an inserted block without checking every layout that i don't know..

hardwired

  • Guest
Re: Checking if block exists..
« Reply #6 on: March 10, 2008, 08:17:07 AM »
aaah, sorry, i was typing that as the the last 2 posts came in, lol

hardwired

  • Guest
Re: Checking if block exists..
« Reply #7 on: March 10, 2008, 09:46:10 AM »
Hi,

So using Hendie's code:

Code: [Select]
Set sset1 = ThisDrawing.SelectionSets.Add("SSbks")
        Dim FilterType(0 To 1) As Integer
        Dim FilterData(0 To 1) As Variant
        FilterType(0) = 0: FilterData(0) = "INSERT"
        FilterType(1) = 2: FilterData(1) = "MyBlockName"
sset1.Select acSelectionSetAll, , , FilterType, FilterData


......how do i access the block, what state is the object in the selection set? Is it an AcadEntity, or an AcadObject or a AcadBlockReference or it stored in the selection set as something else. I want to now access the the attributes from here but what do i need to put in order to check that its the right type of object / what is the variable name of the item in the selection set?

...I hope you get me, lol..

hardwired

  • Guest
Re: Checking if block exists..
« Reply #8 on: March 10, 2008, 09:51:03 AM »
What i mean is, i have this code afterwards:

Code: [Select]
        attribX = BlockX.GetAttributes
               
    For countz = LBound(attribX) To UBound(attribX)
        Select Case attribX(countz).TagString
            Case "FIX1"
                fx1descTXT.text = attribX(countz).TextString
            Case "FIX2"
                fx2descTXT.text = attribX(countz).TextString
            Case "FIX3"
                fx3descTXT.text = attribX(countz).TextString
            Case "FIX4"
                fx4descTXT.text = attribX(countz).TextString
            Case "FIX5"
                fx5descTXT.text = attribX(countz).TextString
            Case "FIX6"
                fx6descTXT.text = attribX(countz).TextString
            Case "FIX7"
                fx7descTXT.text = attribX(countz).TextString
            Case "FIX8"
                fx8descTXT.text = attribX(countz).TextString
            Case "FIX9"
                fx9descTXT.text = attribX(countz).TextString
            Case "FIX10"
                fx10descTXT.text = attribX(countz).TextString
            End Select
    Next 'End countx HasAttributes check loop..


......so how do i tie that in with the selection set code? I have dimensioned BlockX as AcadBlockReference but is this right to whats in the ss?

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Checking if block exists..
« Reply #9 on: March 10, 2008, 10:02:10 AM »
Well, the idea got me thinking, and this is something I could use on a regular basis .. well not the attribute part, but the finding out if the block exists .. oddly enough it can also be used to count the number of insertions of a specific block ... so here it is for all posterity ...

Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Checking if block exists..
« Reply #10 on: March 10, 2008, 10:10:38 AM »
Paul ... let me see if I understand you correctly ...

a) you want to find out if a block is inserted in a drawing
b) you want to populate a set of text boxes if it is

If that is the case, using the code provided by Hendie, sset1 is the selection set holding the AcadBlockReference(s). Keep in mind that unless you are absolutely certain there will only be 1 instance of this block per drawing, you will have multiple blocks in that selection set.
Lets presume you only have 1 block in the drawing in sset1

Code: [Select]
If sset1.Count > 0 Then
   Set BlockX = sset1.Item(0)
   If BlockX.HasAttributes Then
      attribX = BlockX.GetAttributes
       For countz = LBound(attribX) To UBound(attribX)
           Select Case attribX(countz).TagString
               Case "FIX1"
                   fx1descTXT.text = attribX(countz).TextString
               Case "FIX2"
                   fx2descTXT.text = attribX(countz).TextString
               Case "FIX3"
                   fx3descTXT.text = attribX(countz).TextString
               Case "FIX4"
                   fx4descTXT.text = attribX(countz).TextString
               Case "FIX5"
                   fx5descTXT.text = attribX(countz).TextString
               Case "FIX6"
                   fx6descTXT.text = attribX(countz).TextString
               Case "FIX7"
                   fx7descTXT.text = attribX(countz).TextString
               Case "FIX8"
                   fx8descTXT.text = attribX(countz).TextString
               Case "FIX9"
                   fx9descTXT.text = attribX(countz).TextString
               Case "FIX10"
                   fx10descTXT.text = attribX(countz).TextString
               End Select
       Next 'End countx HasAttributes check loop..
   End If
End If

I hope that clears things up
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Atook

  • Swamp Rat
  • Posts: 1027
  • AKA Tim
Re: Checking if block exists..
« Reply #11 on: March 10, 2008, 11:02:47 AM »
And if there's more than one in a drawing you'll need to put the set BlockX and attribute code in a for-next loop.

hardwired

  • Guest
Re: Checking if block exists..
« Reply #12 on: March 10, 2008, 01:34:11 PM »
Cheers everybody, got it working like a dream now. Using combination of Hendie's and Keith's code, it works how i need it but there's always one thing with selection sets that i fall over on and can never remember how to code for it....

.....When i run the code once its fine, but a second time and it flags up that the selection set already exists....Now what is everybody's best way and easiest way to check if the set exists first..

I haven't used selection sets much at all and the last one was ages back so can't remember exactly what i used but, i'm sure i just used sset1.Delete but i can't get that to work, or maybe i have it in the wrong place..

rkmcswain

  • Swamp Rat
  • Posts: 978
Re: Checking if block exists..
« Reply #13 on: March 10, 2008, 02:07:16 PM »
I do something like this. There are probably better and/or more elegant ways...

Code: [Select]
On Error Resume Next
Dim ss As AcadSelectionSet
Set ss = ThisDrawing.SelectionSets.Item("abcd")
If Err.Number <> 0 Then
    Set ss = ThisDrawing.SelectionSets.Add("abcd")
    Err.Clear
Else
    ss.Clear
End If

hendie

  • Guest
Re: Checking if block exists..
« Reply #14 on: March 10, 2008, 04:15:08 PM »
I can't remember where I got this, it may have been from that other site with RR a while ago... or it may have been somewhere else

Code: [Select]
Dim Mycollection As AcadSelectionSets
Dim SSet1 As AcadSelectionSet

' first check if any ss exist and if they do, delete them
        Set Mycollection = ThisDrawing.SelectionSets
             For Each SSet1 In Mycollection
                 If SSet1.Name = "MySS" Then
                     ThisDrawing.SelectionSets.Item("MySS").Delete
                     Exit For
                 End If
             Next

I normally place this just before the part where I create the new selection set