01 July 2008

Find an existing layer

Before I get to creating new layers, I thought I'd spend a moment on checking if a layer already exists. After all, we can't make a layer if it's already there.

  Public Function LayerExists(ByRef oDb As Database, _

                              ByRef oTrans As Transaction, _

                              ByVal strName As String) As Boolean

    ' Create a layer object

    Dim oLayer As LayerTableRecord

 

    ' Set our return value to nothing

    Dim retValue As Boolean = False

 

    Try

      ' Open up the layer table (for reading)

      Dim oLayerTable As LayerTable = CType(oTrans.GetObject(oDb.LayerTableId, _

                                                            OpenMode.ForRead, _

                                                            False), _

                                            LayerTable)

 

      ' Loop through each object in the layer table

      For Each objId As ObjectId In oLayerTable

        ' Get the layertablerecord object for the current id

        oLayer = CType(oTrans.GetObject(objId, _

                                        OpenMode.ForWrite), _

                      LayerTableRecord)

 

        ' Compare the layer name to the strName variable and

        ' make sure we're not checking erased layers

        If oLayer.Name.ToUpper.Equals(strName.ToUpper) And _

          Not oLayer.IsErased Then

          ' Found the layer

          retValue = True

          Exit For

        End If

      Next

    Catch ex As System.Exception

      MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error in LayerExists")

    End Try

 

    Return retValue

  End Function

No comments:

Disclaimer

All materials on this site are provided "as is" and without any warranty. Any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall the authors be liable to any party for any direct, indirect, incidental, special, exemplary, or consequential damages arising in any way out of the use or misuse of this site.