• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Contributor
    Posts: 19
    Registered: ‎12-04-2005

    Problems with Layer Description

    371 Views, 3 Replies
    01-09-2007 06:03 PM
    Hello All,

    I am using AutoCAD 2006 and Visual Studio 2005 with VB.Net. I have a subroutine to create a layer, it works fine except that for some reason the description field does not work. Whatever value I pass in on description it fails to be applied to the layer, here is my code:

    Public Sub MakeLayer(ByVal LayerName As String, ByVal Colour As Int16, _
    ByVal LineType As String, Optional ByVal Description As String = "")
    Dim layerId As ObjectId 'the return value for this function
    Dim db As Database = HostApplicationServices.WorkingDatabase
    Dim trans As Transaction = db.TransactionManager.StartTransaction()
    'Get the layer table first...
    Try
    Dim lt As LayerTable = trans.GetObject(db.LayerTableId, OpenMode.ForWrite)
    'Check if Layer exists...
    If lt.Has(LayerName) Then
    layerId = lt.Item(LayerName)
    Else
    'If not, create the layer here.
    Dim ltr As LayerTableRecord = New LayerTableRecord()
    ltr.Name = LayerName
    ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, Colour)
    If LineType <> "Continuous" Then
    ltr.LinetypeObjectId = CheckForLineType(LineType)
    End If
    ltr.Description = Description
    layerId = lt.Add(ltr)
    trans.AddNewlyCreatedDBObject(ltr, True)
    End If
    trans.Commit()
    Catch aex As Autodesk.AutoCAD.Runtime.Exception
    MsgBox("AutoCAD Exception (MAKELAYER): " & aex.Message, MsgBoxStyle.Exclamation)
    Catch ex As System.Exception
    MsgBox("System Exception: " & ex.Message, MsgBoxStyle.Exclamation)
    Finally
    trans.Dispose()
    End Try
    End Sub

    Can anybody please explain what is going on here, thanks in advance for any advice you can offer.


    Kind Regards,

    Martin.
    Please use plain text.
    *tp

    Re: Problems with Layer Description

    01-10-2007 07:21 AM in reply to: martinduke2653
    Try this instead.

    Public Sub MakeLayer(ByVal LayerName As String, ByVal Colour As Int16,
    _
    ByVal LineType As String, Optional ByVal Description As String = "")
    Dim layerId As ObjectId 'the return value for this function
    Dim db As Database = HostApplicationServices.WorkingDatabase
    Dim trans As Transaction = db.TransactionManager.StartTransaction()
    'Get the layer table first...
    Try
    Dim ltr As LayerTableRecord = New LayerTableRecord()
    Dim lt As LayerTable = trans.GetObject(db.LayerTableId,
    OpenMode.ForWrite)
    'Check if Layer exists...
    If lt.Has(LayerName) Then
    layerId = lt.Item(LayerName)
    Else
    'If not, create the layer here.
    ltr.Name = LayerName
    ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, Colour)
    'If LineType <> "Continuous" Then
    ' ltr.LinetypeObjectId = CheckForLineType(LineType)
    'End If
    'ltr.Description = Description
    layerId = lt.Add(ltr)
    trans.AddNewlyCreatedDBObject(ltr, True)
    End If
    trans.Commit()
    LayerDescription(LayerName, Description)
    Catch aex As Autodesk.AutoCAD.Runtime.Exception
    MsgBox("AutoCAD Exception (MAKELAYER): " & aex.Message,
    MsgBoxStyle.Exclamation)
    Catch ex As System.Exception
    MsgBox("System Exception: " & ex.Message,
    MsgBoxStyle.Exclamation)
    Finally
    trans.Dispose()
    End Try
    End Sub

    Public Sub LayerDescription(ByVal layerName As String, ByVal
    Description
    As String)
    Using db As AcDb.Database = HostApplicationServices.WorkingDatabase
    Using tr As AcDb.Transaction =
    db.TransactionManager.StartTransaction
    Try
    Dim tbl As AcDb.LayerTable = _
    CType(tr.GetObject(db.LayerTableId,
    AcDb.OpenMode.ForRead,
    False), AcDb.LayerTable)
    If tbl.Has(layerName) Then
    Dim ltr As AcDb.LayerTableRecord =
    tr.GetObject(tbl.Item(layerName), AcDb.OpenMode.ForWrite, False)
    ltr.Description = Description
    End If
    tr.Commit()
    Catch ex As Exception
    tr.Abort()
    End Try
    End Using
    End Using
    End Sub

    tp




    escreveu na mensagem news:5446186@discussion.autodesk.com...
    Hello All,

    I am using AutoCAD 2006 and Visual Studio 2005 with VB.Net. I have a
    subroutine to create a layer, it works fine except that for some reason
    the
    description field does not work. Whatever value I pass in on description
    it
    fails to be applied to the layer, here is my code:

    Public Sub MakeLayer(ByVal LayerName As String, ByVal Colour As Int16,
    _
    ByVal LineType As String, Optional ByVal Description As String = "")
    Dim layerId As ObjectId 'the return value for this function
    Dim db As Database = HostApplicationServices.WorkingDatabase
    Dim trans As Transaction =
    db.TransactionManager.StartTransaction()
    'Get the layer table first...
    Try
    Dim lt As LayerTable = trans.GetObject(db.LayerTableId,
    OpenMode.ForWrite)
    'Check if Layer exists...
    If lt.Has(LayerName) Then
    layerId = lt.Item(LayerName)
    Else
    'If not, create the layer here.
    Dim ltr As LayerTableRecord = New LayerTableRecord()
    ltr.Name = LayerName
    ltr.Color = Color.FromColorIndex(ColorMethod.ByAci,
    Colour)
    If LineType <> "Continuous" Then
    ltr.LinetypeObjectId = CheckForLineType(LineType)
    End If
    ltr.Description = Description
    layerId = lt.Add(ltr)
    trans.AddNewlyCreatedDBObject(ltr, True)
    End If
    trans.Commit()
    Catch aex As Autodesk.AutoCAD.Runtime.Exception
    MsgBox("AutoCAD Exception (MAKELAYER): " & aex.Message,
    MsgBoxStyle.Exclamation)
    Catch ex As System.Exception
    MsgBox("System Exception: " & ex.Message,
    MsgBoxStyle.Exclamation)
    Finally
    trans.Dispose()
    End Try
    End Sub

    Can anybody please explain what is going on here, thanks in advance for
    any
    advice you can offer.


    Kind Regards,

    Martin.



    I'm protected by SpamBrave
    http://www.spambrave.com/
    Please use plain text.
    Contributor
    Posts: 19
    Registered: ‎12-04-2005

    Re: Problems with Layer Description

    01-10-2007 03:15 PM in reply to: martinduke2653
    Hello,

    Thank you for that, it works a treat, it seems to be a bit of a bug in the API to me that you cannot set the value when you first create the layer.


    Regards,

    Martin.
    Please use plain text.
    Distinguished Mentor
    Posts: 839
    Registered: ‎09-07-2004

    Re: Problems with Layer Description

    02-08-2012 06:47 PM in reply to: martinduke2653

    This is old but I thought I would update it. You can avoid the separate subroutine if you set the description one step later.

     

    So instead of:

    LayerObj.Description = Description

    LayerTable.Add(LayerObj)

     

    Just flip the order of those two lines:

    LayerTable.Add(LayerObj)

    LayerObj.Description = Description

     

    I believe this is because the description is not really part of the layer, it is attached to the layer table or xdata or something. Therefore the description cannot be set until the layer is part of the layer table. Another side effect of this is that if you use the -EXPORTTOAUTOCAD command you will lose all of your layer descriptions.

    Please use plain text.