TheSwamp
Code Red => VB(A) => Topic started by: hardwired 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"
-
You could do something like this:
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.
-
or search with a selection set
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
-
or search with a selection set
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.
-
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... ;-)
-
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..
-
aaah, sorry, i was typing that as the the last 2 posts came in, lol
-
Hi,
So using Hendie's code:
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..
-
What i mean is, i have this code afterwards:
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?
-
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 ...
-
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
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
-
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.
-
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..
-
I do something like this. There are probably better and/or more elegant ways...
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
-
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
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
-
Hi,
Once again, thanks to both of you for your input. I used Hendie's code in the end and it works just fine for me..