Code:
Public Function MoveLayerDown(ByVal elementName As String, Optional ByVal LayerID As ObjectId = Nothing) As Boolean
Dim isMoved As Boolean = False ' Get the Object Id for the current Map
Dim currentMapId As ObjectId
If Not FindCurrentMapId(currentMapId) Then
Return isMoved
End If
' Find the object Id of the element to be moved
Dim elementIdTobeMoved As ObjectId
If IsNothing(LayerID) Then
' Suche nach dem Namen
If Not FindElementId(elementIdTobeMoved, elementName) Then
Return isMoved
End If
Else
elementIdTobeMoved = LayerID
isMoved = True
End If
Dim trans As Transaction = Nothing
Try
trans = Utility.TransactionManager.StartTransaction()
Dim currentMap As Map = trans.GetObject(currentMapId, OpenMode.ForWrite)
' Get the position to move the element to
Dim iterator As IEnumerator = currentMap.NewIterator(True, True)
Do While (iterator.MoveNext())
' Bis ans Ende laufen
Loop
' Set the position
Dim pos As IEnumerator = iterator
' Move the element
currentMap.MoveItem(elementIdTobeMoved, pos)
trans.Commit()
trans = Nothing
isMoved = True
Catch e As Autodesk.AutoCAD.Runtime.Exception
Utility.ShowMessage(vbNewLine & "Failed to move element.")
Finally
If Not trans Is Nothing Then
trans.Abort()
trans = Nothing
End If
End Try
Return isMoved
End Function