Layer object behavior

Layer object behavior

Anonymous
Not applicable
279 Views
4 Replies
Message 1 of 5

Layer object behavior

Anonymous
Not applicable
In the code snippet below, I am wondering why I have to set the objLayer
object to Nothing before I can create the second and third layers, when I
take "Set objLayer = Nothing" out the code never changes the objLayer
variable besides the first time. Shouldn't the "Set objLayer =
ThisDrawing..." lines just overwrite without having to first clear the
variable or is this expected behavior? I am using VBA 6 w/ Acad2002.
Thanks:

On Error Resume Next
Set objLayer = ThisDrawing.Layers("S-GRID")

If objLayer Is Nothing Then
Set objLayer = ThisDrawing.Layers.Add("S-GRID")
If objLayer Is Nothing Then
MsgBox "Unable to Add S-GRID Layer " & vbCr & Err.Description
End If

On Error Resume Next
Set objLinetype = ThisDrawing.Linetypes("Center2")

If objLinetype Is Nothing Then
ThisDrawing.Linetypes.Load "Center2", "C:\Program Files\AutoCAD
2002\Support\acad.lin"
If Err Then
MsgBox "Error loading CENTER2 linetype" & vbCr &
Err.Description
End If
Set objLinetype = ThisDrawing.Linetypes("Center2")
End If

objLayer.Color = 9
objLayer.Linetype = objLinetype
End If

Set objLayer = Nothing
Set objLayer = ThisDrawing.Layers("S-GRID-IDEN")
Debug.Print Err.Description ' take this out later
If objLayer Is Nothing Then
Set objLayer = ThisDrawing.Layers.Add("S-GRID-IDEN")
If objLayer Is Nothing Then
MsgBox "Unable to Add S-GRID-IDEN Layer " & Err.Description
End If
objLayer.Color = acGreen
End If

Set objLayer = Nothing
Set objLayer = ThisDrawing.Layers("S-GRID-IDEN-TEXT")
Debug.Print Err.Description ' take this out later too
If objLayer Is Nothing Then
Set objLayer = ThisDrawing.Layers.Add("S-GRID-IDEN-TEXT")
If objLayer Is Nothing Then
MsgBox "Unable to Add S-GRID-IDEN-TEXT Layer " & Err.Description
End If
objLayer.Color = acWhite
End If


--
matthew g.
0 Likes
280 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable
Matthew,

Here's your answer.

Joe
--

Dim objLayer As AcadLayer

On Error Resume Next
'load linetype upfront
ThisDrawing.Linetypes.Load "Center2", "C:\Program Files\AutoCAD 2002\Support\acad.lin"

If Err.Description <> "Duplicate record name" Then
MsgBox "Unknown error loading linetype", vbCritical, "Error loading linetype"

Else
Err.Clear
End If

Set objLayer = ThisDrawing.Layers.Add("S-GRID")

With objLayer
.Color = 9
If Err = 0 Then
.Linetype = "Center2" 'Center2 loaded

Else
.Linetype = "Continuous" 'no, default to standard linetype
End If
End With

Set objLayer = ThisDrawing.Layers.Add("S-GRID-IDEN")

With objLayer
.Color = acGreen
End With

Set objLayer = ThisDrawing.Layers.Add("S-GRID-IDEN-TEXT")

With objLayer
.Color = acWhite
End With
0 Likes
Message 3 of 5

Anonymous
Not applicable
> Matthew,
> Here's your answer.
>

Thanks Joe. So the difference was in loading the linetype up front or in
the way that you handled the error trapping? Perhaps both? And more
importantly, what is the logic behind that?


--
matthew g.
0 Likes
Message 4 of 5

Anonymous
Not applicable
> Shouldn't the "Set objLayer = ThisDrawing..." lines just overwrite without
having to
> first clear the variable or is this expected behavior?

It would overwrite if the right hand side (ThisDrawing...) did not produce
an error. When the named layer does not exist, an eror is produced when
evaluating the RHS, and error handling skips down to the next line without
resetting objLayer. I think you were expecting
ThisDrawing.Layers("NonExistentLayer") to return Nothing... it doesn't, it
just returns an error.

Joe's code gets around this by Joe's knowing that if you .Add a layer that
already exists, it returns the existing layer. I didn't know VBA would do
this, and it seems like a useful feature... but to me, intuitively, it seems
most "proper" for VBA to throw an error if you try to Add something that
already exists. I'm not complaining, I just wouldn't have guessed it would
work like that.

James
0 Likes
Message 5 of 5

Anonymous
Not applicable
> It would overwrite if the right hand side (ThisDrawing...) did not produce
> an error.
(snip)


Thank you extremely for the explanation, it makes a lot more sense now.


--
matthew g.
0 Likes