Author Topic: Does Layer Exist?  (Read 8018 times)

0 Members and 1 Guest are viewing this topic.


  • Guest
Re: Does Layer Exist?
« Reply #15 on: October 13, 2010, 11:07:09 AM »
Hey Eric,

Props to that man, i deleted the function and just added the addlayer code in my sub and it works..

Thanks to you and to Bryco and Arizona of course for your help..

Good skills..


  • Guest
Re: Does Layer Exist?
« Reply #16 on: January 03, 2011, 01:50:36 PM »
Not vba code, but

Autocad has a method of checking if a layer exists and if so, make it current...

Use the Autocad layer command:

(command "._layer" "_M" "layername" "")

The "m" (make) command creates the layer if it does not exist.


  • Seagull
  • Posts: 12954
  • I like my water diluted.
Re: Does Layer Exist?
« Reply #17 on: January 03, 2011, 01:59:22 PM »
That doesn't check if the layer exists.... that simply makes a new layer.

Look into TBLSEARCH for checking if a layer exists.
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @


  • Guest
Re: Does Layer Exist?
« Reply #18 on: January 12, 2011, 04:21:20 PM »
I think it does what the OP wanted:

"I'm trying to check if a layer exists, and if it does, use it for a block insert and if it doesn't, create it and use it.."


  • Guest
Re: Does Layer Exist?
« Reply #19 on: March 24, 2011, 08:54:08 AM »
Here is my Block adder it uses the error code from layer not found traps the error and then adds the layer before continuing. Using Error Handlers is always a good idea even if it only has a case else that gives a msgbox or debug.print.

Code: [Select]
Public Function AddBlocks(varInsPnt As Variant, strLayerName As String, _
                     strBlock As String, Optional strAttValue As String = "") As String
Dim objBlkRef As AcadBlockReference
Dim objAttRef As AcadAttributeReference
Dim intLoop As Integer
Dim varAtts As Variant
Dim strHandle As String
On Error GoTo Err_Control
 Set objBlkRef = ThisDrawing.ModelSpace.InsertBlock(varInsPnt, strBlock, 1, 1, 1, 0)
 strHandle = objBlkRef.Handle
 objBlkRef.Layer = strLayerName
 If strAttValue <> "" Then
      If objBlkRef.HasAttributes Then
           varAtts = objBlkRef.GetAttributes
           For intLoop = LBound(varAtts) To UBound(varAtts)
                Set objAttRef = varAtts(intLoop)
                objAttRef.TextString = strAttValue
           Next intLoop
      End If
 End If
 AddBlocks = strHandle
 Exit Function

 Select Case Err.Number
      Case -2145386476    'Layer Name not Found
           'Skip Layer Change or Create Layer
           ThisDrawing.Layers.Add (strLayerName)
      Case Else
           'LogError Err.Number, Err.Description & "; " & strAttValue & ", " & strBucket, "AddBlocks", "modACAD.bas"
           'MsgBox Err.Number & ": " & Err.Description
           Resume Exit_Here
 End Select
End Function

Daniel Eiszele

  • Newt
  • Posts: 85
Re: Does Layer Exist?
« Reply #20 on: March 25, 2011, 09:20:15 PM »
Even though you have a workable answer you may find that these snippets would fix your previous attempts.
Code: [Select]
'THis is the name of the layer we are testing for existence
LayerName = "LSC-REVISIONS_" & revnumCOMBO.Text

'Function to tell if a layer exists or not
Private Function DoesLayerExist(LayerName as String) As Boolean

Dim objLayer As AcadLayer

DoesLayerExist = False

For Each objLayer In ThisDrawing.Layers
If UCase(objLayer.Name) = UCase(LayerName) Then
DoesLayerExist = True
RMlayer = objLayer
Exit Function
End If
Next objLayer

End Function

'This is the test part of the code
If DoesLayerExist(LayerName) = False Then
RMlayer = ThisDrawing.Layers.Add(LayerName)
  RMlayer.Plottable = False
End If

'Now Set the block Layer
RMblock.Layer = LayerName

The main problem I found with the previous attempts were you were giving a function a return type and then telling it the return type was something else.  Ie "acadlayer" but setting it to "True". 

Also you can put the function anywhere within the same module and vb will find it!