.NET

Reply
Valued Contributor
fmarcelino
Posts: 61
Registered: ‎10-25-2005
Message 1 of 12 (245 Views)

Create layer programmatically

245 Views, 11 Replies
10-26-2005 09:40 AM
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
Distinguished Contributor
wesbird
Posts: 359
Registered: ‎12-05-2003
Message 2 of 12 (245 Views)

Re: Create layer programmatically

10-26-2005 10:09 AM in reply to: fmarcelino
add myT.AddNewlyCreatedDBObject before Commit.
Vault Pro 2014, Windows 7 64 bit, AutoCAD (ACA, Map) 2014
Valued Contributor
fmarcelino
Posts: 61
Registered: ‎10-25-2005
Message 3 of 12 (245 Views)

Re: Create layer programmatically

10-26-2005 10:29 AM 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
Distinguished Contributor
wesbird
Posts: 359
Registered: ‎12-05-2003
Message 4 of 12 (245 Views)

Re: Create layer programmatically

10-26-2005 01:09 PM in reply to: fmarcelino
myT.AddNewlyCreatedDBObject(x, True) 'LayerTableRecord
Vault Pro 2014, Windows 7 64 bit, AutoCAD (ACA, Map) 2014
Valued Contributor
fmarcelino
Posts: 61
Registered: ‎10-25-2005
Message 5 of 12 (245 Views)

Re: Create layer programmatically

10-27-2005 02:28 AM 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
Distinguished Contributor
cgay
Posts: 125
Registered: ‎08-02-2005
Message 6 of 12 (245 Views)

Re: Create layer programmatically

10-28-2005 01:40 PM 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.
Contributor
Dave Jameson
Posts: 12
Registered: ‎09-16-2005
Message 7 of 12 (245 Views)

Re: Create layer programmatically

05-09-2006 04:51 AM 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
Active Member
ManfredM
Posts: 10
Registered: ‎04-26-2006
Message 8 of 12 (245 Views)

Re: Create layer programmatically

05-10-2006 08:53 AM 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
Contributor
parcea89
Posts: 23
Registered: ‎04-25-2006
Message 9 of 12 (245 Views)

Re: Create layer programmatically

05-10-2006 10:52 PM 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)
)
Contributor
Dave Jameson
Posts: 12
Registered: ‎09-16-2005
Message 10 of 12 (245 Views)

Re: Create layer programmatically

05-12-2006 12:00 AM 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
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.