No need to iterate through every entity in the drawing

No need to iterate through every entity in the drawing

Anonymous
Not applicable
408 Views
2 Replies
Message 1 of 3

No need to iterate through every entity in the drawing

Anonymous
Not applicable
Hi,

The For Each loop in CodeOne_Click works fine. I use CodeTwo_Click to not iterate through every entity in the drawing. But the For Each loop falls back into an error when not only blocks are in a drawing. Where do I get wrong?

Thx

Private Const Selectedblock = "TREE"

Private Sub CodeOne_Click()
Dim oEntity As AcadEntity
Dim oBlkRef As AcadBlockReference

For Each oEntity In ThisDrawing.ModelSpace
If TypeOf oEntity Is AcadBlockReference Then
Set oBlkRef = oEntity
If oBlkRef.EffectiveName = Selectedblock Then
oBlkRef.Visible = True
End If
End If
Next oEntity

End Sub


Private Sub CodeTwo_Click()
Dim oBlkRef As AcadBlockReference

For Each oBlkRef In ThisDrawing.ModelSpace
If oBlkRef.EffectiveName = Selectedblock Then
Call getBlockReference(oBlkRef.Handle)
oBlkRef.Visible = True
End If
Next oBlkRef

End Sub


Private Sub getBlockReference(ByVal sHandle As String)
Dim oBlkRef As AcadBlockReference

On Error GoTo 0 ' turn on the default error handler
Set oBlkRef = ThisDrawing.HandleToObject(sHandle)
End Sub
0 Likes
409 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable
Hi bas,

As you can see below, Autodesk have monumentally stuffed up the
formatting of your code making it nearly impossible to read.

For future posts enclose the code in markers like this:


Private Const Selectedblock = "TREE"
Private Sub CodeOne_Click()
Dim oEntity As AcadEntity
Dim oBlkRef As AcadBlockReference
For Each oEntity In ThisDrawing.ModelSpace
If TypeOf oEntity Is AcadBlockReference Then
Set oBlkRef = oEntity
If oBlkRef.EffectiveName = Selectedblock Then
oBlkRef.Visible = True
End If
End If
Next oEntity
End Sub

Private Sub CodeTwo_Click()
Dim oBlkRef As AcadBlockReference
For Each oBlkRef In ThisDrawing.ModelSpace
If oBlkRef.EffectiveName = Selectedblock Then
Call getBlockReference(oBlkRef.Handle)
oBlkRef.Visible = True
End If
Next oBlkRef
End Sub

Private Sub getBlockReference(ByVal sHandle As String)
Dim oBlkRef As AcadBlockReference
On Error GoTo 0 ' turn on the default error handler
Set oBlkRef = ThisDrawing.HandleToObject(sHandle)
End Sub


That way they will retain their formatting for viewers using newsgroup
readers and will only lose the indenting for those using a web browser.

You second set of code won't work as the "For Each" process deals with
everything in model space and where something is returned which doesn't
have an ".EffectiveName" property, you will get an error.

The best way to handle your requirement is to create a SelectionSet of
Block references and then run the "For Each" on it.

Look in the help files sample code to see how to do it. or search these
NGs where suitable code segments have been posted dozens of times.

Regards,


Laurie Comerford


bas.hendriks@inbo.com wrote:
> Hi, The For Each loop in CodeOne_Click works fine. I use CodeTwo_Click
> to not iterate through every entity in the drawing. But the For Each
> loop falls back into an error when not only blocks are in a drawing.
> Where do I get wrong? Thx Private Const Selectedblock = "TREE" Private
> Sub CodeOne_Click() Dim oEntity As AcadEntity Dim oBlkRef As
> AcadBlockReference For Each oEntity In ThisDrawing.ModelSpace If TypeOf
> oEntity Is AcadBlockReference Then Set oBlkRef = oEntity If
> oBlkRef.EffectiveName = Selectedblock Then oBlkRef.Visible = True End If
> End If Next oEntity End Sub Private Sub CodeTwo_Click() Dim oBlkRef As
> AcadBlockReference For Each oBlkRef In ThisDrawing.ModelSpace If
> oBlkRef.EffectiveName = Selectedblock Then Call
> getBlockReference(oBlkRef.Handle) oBlkRef.Visible = True End If Next
> oBlkRef End Sub Private Sub getBlockReference(ByVal sHandle As String)
> Dim oBlkRef As AcadBlockReference On Error GoTo 0 ' turn on the default
> error handler Set oBlkRef = ThisDrawing.HandleToObject(sHandle) End Sub
0 Likes
Message 3 of 3

Anonymous
Not applicable
Thx!
0 Likes