Select to view content in your preferred language

Trying to assign already selected features to ISelctionSet2

575
1
03-19-2013 12:02 PM
CraigGraham
Occasional Contributor
Good Afternoon,
I am trying to loop through a set of selected features for a specific layer.  The key issue I have is that the user will have already made a selection (with any of the out-of-the-box tools) when they click my button.

My code needs to make sure that key attributes are identical for all features selected in a certain layer, the rest of the selected features on the map are neither here nor there. 

All of the examples I find of how to use a count property on any of the objects that refer to a selection are solutions where the selection set is not made yet, and ISelectionSet is being used to 1) create the selection, then 2) return info about the selection.

Everything I've done in the past assumes that the user is using a custom tool to drive the selection process, and even then they are always selecting a single polygon.

So with all that background, here are the relatively straight forward questions:

  1. 1 Is it possible to assign features from one layer (that are already selected) to the selection set interface, not to make a selection, but to simply view properties about the selection

  2. 2  Assuming the answer to 1 is no; would I be able to setup some sort of query, or filter, or queryfilter to populate iSelectionSet2 with features from a specific layer that are currently selected? (esriSelectionResultAnd)

  3. 3 Assuming the answer to 2 is yes; would anyone have an example of how that hand off would look like? 

I could have sworn I've used something as straight forward as set pSelSet = OBJECT.SelectedFeatures in the past, but I can't seem to find it, so maybe I'm dreaming. 

While typing this I think I may have found a way to achieve #1 from above using the iFeatureSelection interface.  I'm going to post for future reference, and test it

    Dim pFSel As IFeatureSelection, pSelSet As ISelectionSet2, pFLayer As IFeatureLayer2

'(snipped a bunch of code that sets up pFLayer)

' *** Got this series of QI from an ESRI example, not sure how it will work out but will report back ***
    Set pFSel = pFLayer
    Set pSelSet = pFSel.SelectionSet
' *** End of ESRI Example technique ***

' What I really needed to do all along...
    iSelectedFeatureCount = pSelSet.Count
    If iSelectedFeatureCount < 1 Then 


I'll go test now, but if anyone has any ideas in the meantime please feel free to chime in.

Cheers,
Craig
0 Kudos
1 Reply
AlexanderGray
Honored Contributor
I think we have a terminology hurdle here so I will try my best to answer.  I am not sure the word assigning is correct in this context. 
From a featurelayer class instance or object, you can cast (.net terminology) or query interface (COM terminology) to any interface that the class implements, including IFeatureSelection.  The IFeatureSelection property returns an ISelectionSet.  The ISelectionSet return is an instance of either RelQueryTableSelectionSet, SelectionSet or TMSSelectSet, most likely a SelectionSet.  No matter, since all three implement both ISelectionSet and ISelectionSet2, so you can cast (or QI) from one to the other.  The ISelectionSet2 has the update method that is extra that returns a cursor you can use to loop through all the features and edit them.  A search cursor is sufficient just to read the features. 

You can also get at the features through their objectid (on the featureclass that you can get through the IFeatureLayer), the IDs property returns an array of the objectids of the selected features.
So in VBA it would look like this
dim fSel as IfeatureSelection
set fSel = featLayer
dim selSet2 as ISelectionSet2
set selSet2 = dSel.SelectionSet
if selSet2.Count > 1 then
dim upCur as ICursor
selSet2.Update(Nothing, false, upCur)
dim rw as IRow
set rw = upCur.NextRow
etc.
0 Kudos