Hi,
I already could figured out how to detect a command execution:
m_doc = Application.DocumentManager.MdiActiveDocument
Private Shared Sub m_doc_CommandWillStart(ByVal sender As Object, ByVal e As Autodesk.AutoCAD.ApplicationServices.CommandEventArgs) Handles m_doc.CommandWillStart
Dim _frm As New frmChangeLayer
_frm.ShowDialog()
End Sub
'-----------------------------------------------------------------
Public Class frmChangeLayer
Inherits System.Windows.Forms.Form
'Initializes the combobox with a list of all layers existing
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
Application.DocumentManager.MdiActiveDocument.LockDocument(DocumentLockMode.AutoWrite, Nothing, Nothing, True)
Dim curdb As Database = Application.DocumentManager.MdiActiveDocument.Database
Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = curdb.TransactionManager
Dim myT As Transaction = tm.StartTransaction()
Try
Dim lt As LayerTable = CType(tm.GetObject(curdb.LayerTableId, OpenMode.ForRead), LayerTable)
For Each objId As ObjectId In lt
ComboBox1.Items.Add(CType(tm.GetObject(objId, OpenMode.ForRead), LayerTableRecord).Name)
Next
myT.Commit()
Catch ex As Exception
myT.Abort()
MessageBox.Show("Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
myT.Dispose()
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = db.TransactionManager
Dim myT As Transaction = tm.StartTransaction()
Dim ExistingLayerId As ObjectId
Dim elt As LayerTable = CType(tm.GetObject(db.LayerTableId, OpenMode.ForWrite, False), LayerTable)
ExistingLayerId = elt.Item(ComboBox1.SelectedItem)
Dim curLay As ObjectId = db.Clayer
db.Clayer = ExistingLayerId
' Do something
db.Clayer = curLay
myT.Commit()
myT.Dispose()
Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Close()
End Sub
End Class
------------------------------------------------------------
This should work but what happends is, when I show my form (frmChangeLayer) the LayerManager is blocked. I think that's why my algorithm doesn't change current layer.
Does anyone knows a way to do this? Change the current layer before a command execution programmatically?
Regards,
Filipe Marcelino