.NET

Reply
Contributor
martinduke2653
Posts: 19
Registered: ‎12-04-2005
Message 1 of 5 (585 Views)
Accepted Solution

Problems with Layer Description

585 Views, 4 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.

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.

*tp
Message 2 of 5 (585 Views)

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/
Contributor
martinduke2653
Posts: 19
Registered: ‎12-04-2005
Message 3 of 5 (585 Views)

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.
*Expert Elite*
GTVic
Posts: 917
Registered: ‎09-07-2004
Message 4 of 5 (318 Views)

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.

Valued Mentor
Ajilal.Vijayan
Posts: 447
Registered: ‎01-12-2008
Message 5 of 5 (152 Views)

Re: Problems with Layer Description

02-01-2014 08:51 PM in reply to: GTVic
Today this helped me!!!!
Thanks GTVic
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!