By Adam Nagy
The API Help contains a sample that lets you prompt the user to select a Face. This is helped by the option of specifying the object types we allow:
If you need more granularity when deciding which faces should be selectable then you can just handle the OnPreSelect event of the SelectEvents object. There you can check e.g. if the Face is planar or not, and in which direction its Normal is pointing. If e.g. it's pointing away from the user then you could make it not selectable:
Public Sub TestSelection() ' Create a new clsSelect object. Dim oSelect As New clsSelect ' Call the pick method of the clsSelect object and set ' the filter to pick any face. Dim oFaces As ObjectsEnumerator Set oFaces = oSelect.Pick(kPartFaceFilter) ' Check to make sure an object was selected. Dim oSelSet As SelectSet Set oSelSet = ThisApplication.ActiveDocument.SelectSet For Each oFace In oFaces oSelSet.Select oFace Next End Sub
'************************************************************* ' The declarations and functions below need to be copied into ' a class module whose name is "clsSelect". The name can be ' changed but you'll need to change the declaration in the ' calling function "TestSelection" to use the new name. ' Declare the event objects Private WithEvents oInteractEvents As InteractionEvents Private WithEvents oSelectEvents As SelectEvents ' Declare a flag that's used to determine when selection stops. Private bStillSelecting As Boolean Public Function Pick(filter As SelectionFilterEnum) As ObjectsEnumerator ' Initialize flag. bStillSelecting = True ' Create an InteractionEvents object. Set oInteractEvents = ThisApplication.CommandManager.CreateInteractionEvents ' Ensure interaction is enabled. oInteractEvents.InteractionDisabled = False ' Set a reference to the select events. Set oSelectEvents = oInteractEvents.SelectEvents oSelectEvents.WindowSelectEnabled = True ' Set the filter using the value passed in. oSelectEvents.AddSelectionFilter filter ' Start the InteractionEvents object. oInteractEvents.Start ' Loop until a selection is made. Do While bStillSelecting ThisApplication.UserInterfaceManager.DoEvents Loop ' Get the selected item. If more than one thing was selected, ' just get the first item and ignore the rest. Dim oSelectedEnts As ObjectsEnumerator Set oSelectedEnts = oSelectEvents.SelectedEntities If oSelectedEnts.Count > 0 Then Set Pick = oSelectedEnts Else Set Pick = Nothing End If ' Stop the InteractionEvents object. oInteractEvents.Stop ' Clean up. Set oSelectEvents = Nothing Set oInteractEvents = Nothing End Function Private Sub oInteractEvents_OnTerminate() ' Set the flag to indicate we're done. bStillSelecting = False End Sub Private Sub oSelectEvents_OnPreSelect( _ PreSelectEntity As Object, DoHighlight As Boolean, _ MorePreSelectEntities As ObjectCollection, _ ByVal SelectionDevice As SelectionDeviceEnum, _ ByVal ModelPosition As Point, _ ByVal ViewPosition As Point2d, ByVal View As View) If Not TypeOf PreSelectEntity Is Face Then Exit Sub Dim oFace As Face Set oFace = PreSelectEntity If Not TypeOf oFace.Geometry Is Plane Then Exit Sub Dim oPlane As Plane Set oPlane = oFace.Geometry Dim oViewDir As Vector Set oViewDir = View.Camera.Eye.VectorTo(View.Camera.Target) If oFace.IsParamReversed Then oViewDir.ScaleBy -1 End If ' The direction the user is looking into and the face's ' normal the same then the dot product is positive, so ' we won't highlight the face If oPlane.Normal.DotProduct(oViewDir.AsUnitVector()) > 0 Then ' Without this all 6 faces of the box would be selected DoHighlight = False End If End Sub Private Sub oSelectEvents_OnSelect( _ ByVal JustSelectedEntities As ObjectsEnumerator, _ ByVal SelectionDevice As SelectionDeviceEnum, _ ByVal ModelPosition As Point, ByVal ViewPosition As Point2d, ByVal View As View) ' Set the flag to indicate we're done. bStillSelecting = False End Sub
Only the 3 user facing faces are selected.