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

Create layer programmatically

11 REPLIES 11
Reply
Message 1 of 12
fmarcelino
798 Views, 11 Replies

Create layer programmatically

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
11 REPLIES 11
Message 2 of 12
wesbird
in reply to: fmarcelino

add myT.AddNewlyCreatedDBObject before Commit.
Windows 10 64 bit, AutoCAD (ACA, Map) 2023
Message 3 of 12
fmarcelino
in reply to: fmarcelino

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
Message 4 of 12
wesbird
in reply to: fmarcelino

myT.AddNewlyCreatedDBObject(x, True) 'LayerTableRecord
Windows 10 64 bit, AutoCAD (ACA, Map) 2023
Message 5 of 12
fmarcelino
in reply to: fmarcelino

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
Message 6 of 12
cgay
in reply to: fmarcelino

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.
Message 7 of 12
Dave Jameson
in reply to: fmarcelino

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
Message 8 of 12
Anonymous
in reply to: fmarcelino

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
Message 9 of 12
parcea89
in reply to: fmarcelino

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)
)
Message 10 of 12
Dave Jameson
in reply to: fmarcelino

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
Message 11 of 12
Dave Jameson
in reply to: fmarcelino

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
Message 12 of 12
mgsell
in reply to: fmarcelino

Filipe can you tell me what refernce need to be checked to use the autodesk.autocad.databseservices ? thanks

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