Hi all,
I have the following (simplified) code to create a layer:
<Autodesk.AutoCAD.Runtime.CommandMethod("blabla", CommandFlags.Modal)> Public Sub blabla() Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument Dim db As Database = doc.Database Using tr As Transaction = db.TransactionManager.StartTransaction() 'Dim lt As LayerTable = trx.GetObject(db.LayerTableId, OpenMode.ForRead) Dim lt As LayerTable = DirectCast(tr.GetObject(db.LayerTableId, OpenMode.ForRead), LayerTable) Dim layerName As String = "hello" Dim ltr As New LayerTableRecord() ltr.Name = layerName ltr.Description = "bye" lt.UpgradeOpen() lt.Add(ltr) tr.AddNewlyCreatedDBObject(ltr, True) tr.Commit() End Using End Sub
The layer is created, but the layerdescription is not.
This piece of code has always worked in AutoCAD 2016 and the versions before, but now it looks like it is not working anymore on AutoCAD 2017 and 2018.
Is it something I'm doing wrong? Or has something changed in the latest AutoCAD releases?
Solved! Go to Solution.
Solved by Keith.Brown. Go to Solution.
Add your layer first to the transaction and then add the description before committing the transaction.
<Autodesk.AutoCAD.Runtime.CommandMethod("blabla", CommandFlags.Modal)> Public Sub blabla() Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument Dim db As Database = doc.Database Using tr As Transaction = db.TransactionManager.StartTransaction() 'Dim lt As LayerTable = trx.GetObject(db.LayerTableId, OpenMode.ForRead) Dim lt As LayerTable = DirectCast(tr.GetObject(db.LayerTableId, OpenMode.ForRead), LayerTable) Dim layerName As String = "hello" Dim ltr As New LayerTableRecord() ltr.Name = layerName lt.UpgradeOpen() lt.Add(ltr) tr.AddNewlyCreatedDBObject(ltr, True) ltr.Description = "bye" tr.Commit() End Using End Sub