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

Create a layer and set as current using VB.NET

8 REPLIES 8
Reply
Message 1 of 9
usch555
4268 Views, 8 Replies

Create a layer and set as current using VB.NET

Can anyone advice me a solution for the following scenario:

User enters a layer name in a textbox. I need to pass this layer name to my command method, create a layer and set the layer as current programmatically.

Here is the snippet of source code in VB.NET:

Dim myLayerTable As DatabaseServices.LayerTable
Dim myLayer As New DatabaseServices.LayerTableRecord

myLayerTable = myDB.LayerTableId.GetObject(DatabaseServices.OpenMode.ForWrite)
------------------------------------------------------
myLayer.Name = "Test Layer"
---------------------------------------------------------
myLayerTable.Add(myLayer)

I need to set myLayer.Name to whatever user enters in the textbox . I also need to set the newly created layer as current layer

I know how to do it in object oriented programming. But looks like I ran out of ideas here.

Thanks in advance for your advice.
8 REPLIES 8
Message 2 of 9
Anonymous
in reply to: usch555

This may be what you're looking for:
To set the current layer, set the Clayer property of the database to the
ObjectId of a LayerTableRecord.

Greetings.

wrote in message news:5859412@discussion.autodesk.com...
Can anyone advice me a solution for the following scenario:

User enters a layer name in a textbox. I need to pass this layer name to my
command method, create a layer and set the layer as current
programmatically.

Here is the snippet of source code in VB.NET:

Dim myLayerTable As DatabaseServices.LayerTable
Dim myLayer As New DatabaseServices.LayerTableRecord

myLayerTable =
myDB.LayerTableId.GetObject(DatabaseServices.OpenMode.ForWrite)
------------------------------------------------------
myLayer.Name = "Test Layer"
---------------------------------------------------------
myLayerTable.Add(myLayer)

I need to set myLayer.Name to whatever user enters in the textbox . I also
need to set the newly created layer as current layer

I know how to do it in object oriented programming. But looks like I ran out
of ideas here.

Thanks in advance for your advice.
Message 3 of 9
ReneRam
in reply to: usch555

Sub SetMyLayerCurrent()
Dim myDWG As ApplicationServices.Document
Dim myDB As DatabaseServices.Database
Dim myTransMan As DatabaseServices.TransactionManager
Dim myTrans As DatabaseServices.Transaction

myDWG = ApplicationServices.Application.DocumentManager.MdiActiveDocument
myDB = myDWG.Database
myTransMan = myDWG.TransactionManager
myTrans = myTransMan.StartTransaction

Dim myLT As DatabaseServices.LayerTable
Dim myLayer As New DatabaseServices.LayerTableRecord
Dim myLayerId As ObjectId

Dim LayerName As String = "Test Layer"

' Verifica se il Layer Esiste
Try
' If Layer Exist recover LayerID
myLT = CType(myTransMan.GetObject(myDB.LayerTableId, OpenMode.ForRead, True, True), LayerTable)
myLayerId = myLT.Item(LayerName)
' If Deleted => Recover Layer
If myLayerId.IsErased Then
myLT.UpgradeOpen()
myLT.Item(LayerName).GetObject(OpenMode.ForWrite, True, True).Erase(False)
End If

Catch ex As Exception
' Layer Doesn't Exist: Create it
myLT = myDB.LayerTableId.GetObject(DatabaseServices.OpenMode.ForWrite, True, True)
myLayer.Name = LayerName
myLT.Add(myLayer)
' Add Layer to DB
myTrans.AddNewlyCreatedDBObject(myLayer, True)
' Recover LayerID of newly created Layer
myLT = CType(myTransMan.GetObject(myDB.LayerTableId, OpenMode.ForRead, False), LayerTable)
myLayerId = myLT.Item(LayerName)
End Try

' Set Layer as Current
myDB.Clayer = myLayerId

myTrans.Commit()
myTrans.Dispose()
myTransMan.Dispose()
End Sub ' Set as Current Layer
Message 4 of 9
usch555
in reply to: usch555

Thanks for the code. I appreciate it very much.

I need to set the LayerName to whatever user enters in the textbox.

Dim LayerName As String = "Test Layer" 'Textbox1.Text

Could you please tell me how to pass the text parameter as LayerName to my command method.

Thanks once again.
Message 5 of 9
usch555
in reply to: usch555

Could you please let me know which assembly you are referring to for ObjectID in the following line of code:

Dim myLayerId As ObjectId

Thanks
Message 6 of 9
caddie75
in reply to: usch555

Hi,

ObjectID (just like LayerTable and LayerTableRecord) can be found in the namespace: Autodesk.AutoCAD.DatabaseServices
Assembly: acdbmgd.Dll

(Look for it in the Object Browser in VS)

As reply to you're other post: Call a Form (with the textbox on it) in you're commandMethod, and retrieve the value when the form is closed. (you can expose a public property in the form Class like: LayerName, and retrieve it after the form was closed, do check if the form was cancelled)

A. Caddie
Message 7 of 9
usch555
in reply to: usch555

thanks for your help.
Message 8 of 9
ReneRam
in reply to: usch555

Here's part of my module

the namespace for the myLayerId is:

"Autodesk.AutoCAD.DatabaseServices.ObjectId"


Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput

Sub SetMyLayer(ByVal LayerName As String, _
Optional ByVal LayOff As Boolean = False, _
Optional ByVal LayFrozen As Boolean = False, _
Optional ByVal LayLocked As Boolean = False, _
Optional ByVal ColorByACI As Integer = 7, _
Optional ByVal LayLType As String = "Continuous", _
Optional ByVal LayLWeight As DatabaseServices.LineWeight = _
DatabaseServices.LineWeight.ByLineWeightDefault, _
Optional ByVal LayIsPlottable As Boolean = True, _
Optional ByVal LayDescription As String = "", _
Optional ByVal SetCurrent As Boolean = True)
Dim myDWG As ApplicationServices.Document
Dim myDB As DatabaseServices.Database
Dim myTransMan As DatabaseServices.TransactionManager
Dim myTrans As DatabaseServices.Transaction

myDWG = ApplicationServices.Application.DocumentManager.MdiActiveDocument
myDB = myDWG.Database
myTransMan = myDWG.TransactionManager
myTrans = myTransMan.StartTransaction

Dim myLT As DatabaseServices.LayerTable
Dim myLayer As New DatabaseServices.LayerTableRecord
Dim myLayerId As ObjectId

' Verifica se il Layer Esiste
Try
' Se il Layer Esiste => Recupera il suo ID
myLT = CType(myTransMan.GetObject(myDB.LayerTableId, OpenMode.ForRead, True, True), LayerTable)
myLayerId = myLT.Item(LayerName)
' Se è stato cancellato => Recupera il Layer
If myLayerId.IsErased Then
myLT.UpgradeOpen()
myLT.Item(LayerName).GetObject(OpenMode.ForWrite, True, True).Erase(False)
End If

Catch ex As Exception
' Il Layer NON Esiste: Bisogna crearlo con tutte le sue caratteristiche
myLT = myDB.LayerTableId.GetObject(DatabaseServices.OpenMode.ForWrite, True, True)
myLayer.Name = LayerName
myLT.Add(myLayer)
'Aggiungere il Layer al Database del Disegno
myTrans.AddNewlyCreatedDBObject(myLayer, True)
' Recupera l'ID del Layer appena creato
myLT = CType(myTransMan.GetObject(myDB.LayerTableId, OpenMode.ForRead, False), LayerTable)
myLayerId = myLT.Item(LayerName)
End Try

' Impostazioni del Layer
Try
myLayer = myTransMan.GetObject(myLayerId, OpenMode.ForWrite, True, True)
Catch ex As Exception
End Try
myLayer.Color = Colors.Color.FromColorIndex(Colors.ColorMethod.ByAci, ColorByACI)
myLayer.LineWeight = LayLWeight
myLayer.IsOff = LayOff
myLayer.IsFrozen = LayFrozen
myLayer.IsLocked = LayLocked
myLayer.IsPlottable = LayIsPlottable
myLayer.LinetypeObjectId = GetLineTypeId(LayLType)
myLayer.Description = LayDescription


' Verifica se deve essere impostato come Layer Corrente
If SetCurrent Then myDB.Clayer = myLayerId
myTrans.Commit()
myTrans.Dispose()
myTransMan.Dispose()
End Sub ' Reset Layer

Hope it helps
René
Message 9 of 9
usch555
in reply to: usch555

rene,

Thanks for the code and your help. It really helped me a lot.

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