Code:
<Autodesk.AutoCAD.Runtime.CommandMethod("CADde_getSelectionFilteredToHandle")> _
Public Shared Sub CADde_getSelectionFilteredToHandle()
Dim tAcadDoc As ApplicationServices.Document = ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim tAcadDocED As EditorInput.Editor = tAcadDoc.Editor Try
'zuerst selektieren wir (nur fürs Beispiel) die Elemente, die bei der
' nächsten Selection 'erlaubt' sein sollen
Dim tPromptAllowed As EditorInput.PromptSelectionOptions = New EditorInput.PromptSelectionOptions
tPromptAllowed.MessageForAdding = "Erlaubte Elemente wählen: "
Dim tSelResAllowed As EditorInput.PromptSelectionResult = tAcadDocED.GetSelection(tPromptAllowed)
If (tSelResAllowed.Status = EditorInput.PromptStatus.OK) AndAlso (tSelResAllowed.Value.Count > 0) Then
'dann speichern wir die 'erlaubten/wählbaren' Elemente
pAllowedObjectIDs = tSelResAllowed.Value.GetObjectIds
'und jetzt die Testselektion, um zu sehen, ob tatsächlich auf obige Elemente gefiltert wird
Try
AddHandler tAcadDocED.SelectionAdded, AddressOf SelectionAddedHandler
Dim tSelRes As EditorInput.PromptSelectionResult = tAcadDocED.GetSelection()
If (tSelRes.Status = EditorInput.PromptStatus.OK) AndAlso (tSelRes.Value.Count > 0) Then
Debug.Print(tSelRes.Value.Count.ToString & " Objekte selektiert")
End If
Catch ex2 As Exception
Call MsgBox("Unbekannter Fehler aufgetreten" & vbNewLine & ex2.Message)
Finally
RemoveHandler tAcadDocED.SelectionAdded, AddressOf SelectionAddedHandler
End Try
Else
Debug.Print("nix gewählt")
End If
Catch ex As Exception
Call MsgBox("Unbekannter Fehler aufgetreten" & vbNewLine & ex.Message)
End Try
End Sub
Private Shared pAllowedObjectIDs() As DatabaseServices.ObjectId = Nothing
Private Shared Sub SelectionAddedHandler(ByVal sender As Object, ByVal e As EditorInput.SelectionAddedEventArgs)
If (pAllowedObjectIDs IsNot Nothing) AndAlso (pAllowedObjectIDs.Count > 0) AndAlso (e.AddedObjects.Count > 0) Then
Dim tSelectedObjectIDs() As DatabaseServices.ObjectId = e.AddedObjects.GetObjectIds
For i As Integer = tSelectedObjectIDs.GetUpperBound(0) To tSelectedObjectIDs.GetLowerBound(0) Step -1
If Not pAllowedObjectIDs.Contains(tSelectedObjectIDs(i)) Then
'dieses Element steht nicht in der Liste der zugelassenen Objekte ==> also tschüss
e.Remove(i)
End If
Next
End If
End Sub