Create layer programmatically

Create layer programmatically

Anonymous
Not applicable
1,328 Views
11 Replies
Message 1 of 12

Create layer programmatically

Anonymous
Not applicable
Hi,

i'm trying to create a layer programmatically with the following code:

< CommandMethod("addlayer") > _
Public Function AddLayer()
Dim curdb As Database
Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager
Dim myT As Transaction

curdb = Application.DocumentManager.MdiActiveDocument.Database
tm = curdb.TransactionManager
myT = tm.StartTransaction()

Dim lt As LayerTable = CType(tm.GetObject(curdb.LayerTableId, OpenMode.ForWrite), LayerTable)
Dim x As New LayerTableRecord
x.Name = "Test1"

x.IsOff = chkON.Checked 'Checkbox
x.IsFrozen = chkFrozen.Checked 'Checkbox
x.IsLocked = chkLocked.Checked 'Checkbox

'Color RED
x.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(255, 0, 0)

'Lineweight
x.LineWeight = LineWeight.LineWeight120

x.IsPlottable = chkPlottable.Checked 'Checkbox

x.Description = "Layer test1"
lt.Add(x)

myT.Commit()

End Function

This code actually inserts the layer, but first, the property 'DESCRIPTION' appears empty. Second, after I run this code the layer is inserted but, if I access the layer manager just to check if the layer were actually inserted and then click cancel, the insert layer gets removed!! I don't understand why. Maybe because the document database isn't updated automatically...I don't know.

Thanks in advance for your attention, I hope you can help me.

Regards,
Filipe Marcelino
0 Likes
1,329 Views
11 Replies
Replies (11)
Message 2 of 12

wesbird
Collaborator
Collaborator
add myT.AddNewlyCreatedDBObject before Commit.
Windows 10 64 bit, AutoCAD (ACA, Map) 2023
0 Likes
Message 3 of 12

Anonymous
Not applicable
like this?

finally
myT.AddNewlyCreatedDBObject(x, True) 'LayerTableRecord
myT.Commit()
end try

or

finally
myT.AddNewlyCreatedDBObject(lt, True) 'LayerTable
myT.Commit()
end try



Regards,
Filipe Marcelino
0 Likes
Message 4 of 12

wesbird
Collaborator
Collaborator
myT.AddNewlyCreatedDBObject(x, True) 'LayerTableRecord
Windows 10 64 bit, AutoCAD (ACA, Map) 2023
0 Likes
Message 5 of 12

Anonymous
Not applicable
Thanks weslleywang, it worked. My command ended like this:


' Define command 'addlayer'
< CommandMethod("addlayer") > _
Public Function AddLayer()
Dim curdb As Database
Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager
Dim myT As Transaction
curdb = Application.DocumentManager.MdiActiveDocument.Database
tm = curdb.TransactionManager
myT = tm.StartTransaction()
Dim lt As LayerTable = CType(tm.GetObject(curdb.LayerTableId, OpenMode.ForWrite), LayerTable)
Dim x As New LayerTableRecord
x.Name = "Test Layer"
x.IsOff = True
x.IsFrozen = True
x.IsLocked = True
'Color RED
x.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(255, 0, 0)
x.LineWeight = LineWeight.LineWeight120
x.IsPlottable = False
x.Description = "Layer insert sucessfully"
lt.Add(x)
myT.AddNewlyCreatedDBObject(x, True)
myT.Commit()
End Function


Regards,
Filipe Marcelino null
0 Likes
Message 6 of 12

cgay
Enthusiast
Enthusiast
First, I think your "Function" should be "Shared Sub".
Second, you probably should wrap your transaction in an error handler.
Third, you should only "Commit" the transaction if there were no errors, otherwise, you should "Abort" the transaction.

Attached is your code with error handling.
0 Likes
Message 7 of 12

Anonymous
Not applicable
Hi.
This code is great, but could somebody please show me how to add a linetype to this layer!!!
I am using vb.net 2003 and autocad 2006.

yours in anticipation
Dave
0 Likes
Message 8 of 12

Anonymous
Not applicable
First search the curdb.LinetypeTableId.
Search for the linetype you want in Linetypetable and fetch the LinetypeID in the variable MyLinetypeObjectID

Finally add code in the part with x ...:
.LinetypeObjectId = MyLinetypeObjectID
0 Likes
Message 9 of 12

parcea89
Contributor
Contributor
I hope this can help.....
Auto Layer Loading, just replace FUNCTIONNAME to your desire descipline like "Archi, Civil or Struct.

;;;=== Make Layer =================================
(DEFUN C:FUNTIONNAME ()
(COMMAND "LAYER" "MakeLayer" " "LeyerName" "Color" "Red" "" "LineType" "center" ""
"SET" "0" "")
(PRINC)
(PROMPT "\nFuntionName LAYERS LOADED......")
(PRINC)
)

**************************************************************
HERE ARE THE SAMPLE

(DEFUN C:ARCHI ()
(COMMAND "LAYER"
"M" " "Wall" "C" "Green" "" "LT" "Continuous" ""
"M" " "Window" "C" "11" "" "LT" "Hidden" ""
"SET" "0" "")
(PRINC)
(PROMPT "\nARCHITECTURAL LAYERS LOADED......")
(PRINC)
)
0 Likes
Message 10 of 12

Anonymous
Not applicable
Thanks Manfred
I had the x.LinetypeObjectId = MyLinetypeObjectID part but i am strugeling with the search of the linetype table I have:-
Dim MyLinetypeObjectID as LinetypeId = curdb.LinetypeTableId.
but can't see the way forward.
All help much appreciated.
Regards
Dave
0 Likes
Message 11 of 12

Anonymous
Not applicable
Thanks 'parcea'
I have a lisp version already but here is something you could add to yours to place a description in your layer definition.
;;;*Syntax (ldescription name desc)
(DEFUN ldescription(LAYERNAME LAYERDESC)
(setq myLayer (vla-Add (vla-Get-Layers (vla-Get-ActiveDocument
(vlax-Get-Acad-Object))) LAYERNAME))
(vla-Put-Description myLayer LAYERDESC)
)
Regards
Dave
0 Likes
Message 12 of 12

Anonymous
Not applicable
Filipe can you tell me what refernce need to be checked to use the autodesk.autocad.databseservices ? thanks
0 Likes