.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Problems with Layer Description

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
martinduke2653
1188 Views, 4 Replies

Problems with Layer Description

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.
4 REPLIES 4
Message 2 of 5
Anonymous
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/
Message 3 of 5

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.
Message 4 of 5
GTVic
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.

Message 5 of 5
Ajilal.Vijayan
in reply to: GTVic

Today this helped me!!!!
Thanks GTVic

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost