01 July 2008

Creating a layer

This article builds off of the last as it uses the LayerExists function to see if the layer we want to make already exists.

  Public Function LayerCreate(ByRef oDb As Database, _

                              ByRef oTransMan As DatabaseServices.TransactionManager, _

                              ByVal strName As String) As Boolean

    ' Open a transaction so we can modify the drawing

    Dim oTrans As Transaction = oTransMan.StartTransaction()

 

    ' Set our return value to nothing

    Dim retVal As Boolean = False

 

    Try

      ' Check to see if the layer layer exists

      If LayerExists(oDb, oTrans, strName) = False Then

        ' The layer doesn't exist so make a new one

        Dim oLayer As LayerTableRecord = New LayerTableRecord

 

        ' Set the layer's name

        oLayer.Name = strName

 

        ' Open up the layer table (for writing)

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

                                                                  OpenMode.ForWrite, _

                                                                  False), _

                                              LayerTable)

 

        ' Add the new layer to the layer table

        oLayerTable.Add(oLayer)

 

        ' Add the line object to the drawing database

        oTrans.AddNewlyCreatedDBObject(oLayer, True)

 

        ' Close the open transaction (very important)

        oTrans.Commit()

      End If

 

      retVal = True

    Catch ex As Runtime.Exception

      MsgBox(ex.Message, MsgBoxStyle.Information, "Error in LayerCreate")

    Finally

      ' Clean up the transaction object (very important)

      oTrans.Dispose()

    End Try

 

    Return retVal

  End Function


Keep in mind that this is a VERY simple layer routine. If this is all you use, all your layers will be white, continuous and have the default width. I'll leave it up to you to add in the extra features. Hint: do it before the oLayerTable.Add(oLayer) line.

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.