Getting all the fields of a selected Object in Map

1674
4
11-07-2011 10:21 PM
Ariharan
New Contributor
Hi All,
Am developing window based application in dotnet, can i fetch all the records related to a selected feature(probably Polygon) from its dbf? Can anyone help with code? The selection should be a type of single selection at a time, and the layer have thousands of records.

Plz, give me some code sample.
0 Kudos
4 Replies
JamesCrandall
MVP Frequent Contributor
Could you clarify what you want to do exactly?

1. Do you want to get related rows of a selected feature by a RelationshipClass?
2. Do you just want to get attributes of a selected feature?

I am leaning towards #2, but please let us know otherwise.  Here's how you'd access attribute values of selected features:


            pDoc = m_pApp.Document  'setting m_pApp is a seperate question for another thread 
            pMap = pDoc.FocusMap
            Dim pFCursor As IFeatureCursor
            Dim pSelectionSet As ISelectionSet
            Dim pFSelection As IFeatureSelection
            pFSelection = pMap.Layer(0) 'just use the first layer in the TOC for simplicity

            pSelectionSet = pFSelection.SelectionSet
            ''set the pfeaturecursor 
            pFCursor = Nothing
            ''Get a cursor from the selected features
            pSelectionSet.Search(Nothing, False, pFCursor) 

            Dim curTextValue As String
            Dim curIntegerValue As Integer
            Dim curDateValue As DateTime

            Dim pFeat As IFeature
            pFeat = pFCursor.NextFeature
            Do Until pFeat Is Nothing
               curTextValue = pFeat.Value(pFeat.Fields.FindField("YourTextFieldName"))
               curDateValue = pFeat.Value(pFeat.Fields.FindField("YourDateFieldName"))
               curIntegerValue = pFeat.Value(pFeat.Fields.FindField("YourIntegerFieldName"))
               pFeat = pFCursor.NextFeature
            Loop

0 Kudos
Ariharan
New Contributor
Thanks for ur help james. I expected #2 only, but while am using this sample in C#, i cant get the IFeatureSelection from Map.Layer. How can i get the IFeatureSelection from a specified Layer?

I have already filtered the layer and got the layer id. From that particular layer i need to get the Selected features. Once if i got the selected feature, then i can get the attributes from table for that feature. Do you have any samples in C#?
0 Kudos
JamesCrandall
MVP Frequent Contributor
Hi Ari,

No problem.  Sorry, I am not very well versed in C# and wouldn't want to provide poor/bad code examples, so you'll have to convert the VB into C# yourself or hopefully someone else can comment on this thread to help you out.

In any event, here is a Private Function that will accept a string (the layer name you wish to set) and return the IFeatureLayer.  This would go hand-in-hand with the other codebase I provided.



Private Function Get_Lyr(ByVal sLayerName As String) As IFeatureLayer

        Dim i As Integer
        Dim pLayer As IFeatureLayer
        pLayer = Nothing

        pDoc = m_pApp.Document
        pMap = pDoc.FocusMap

        For i = 0 To pMap.LayerCount - 1
            If pMap.Layer(i).Name = sLayerName Then
                pLayer = pMap.Layer(i)
                pLayer.Selectable = False
                Exit For
            End If
        Next

        Return pLayer

End Function



In the original code, just set the pFSelection to this function name and specify the layer name you wish to work on.

            
            Dim pFCursor As IFeatureCursor
            Dim pSelectionSet As ISelectionSet
            Dim pFSelection As IFeatureSelection
            pFSelection = Get_Lyr("YourLayerNameGoesHere") 'use the new Get_Lyr function
            pSelectionSet = pFSelection.SelectionSet
            
            ''set the pfeaturecursor 
            pFCursor = Nothing
            ''Get a cursor from the selected features
            pSelectionSet.Search(Nothing, False, pFCursor) 

            Dim curTextValue As String
            Dim curIntegerValue As Integer
            Dim curDateValue As DateTime

            Dim pFeat As IFeature
            pFeat = pFCursor.NextFeature
            Do Until pFeat Is Nothing
               curTextValue = pFeat.Value(pFeat.Fields.FindField("YourTextFieldName"))
               curDateValue = pFeat.Value(pFeat.Fields.FindField("YourDateFieldName"))
               curIntegerValue = pFeat.Value(pFeat.Fields.FindField("YourIntegerFieldName"))
               pFeat = pFCursor.NextFeature
            Loop
0 Kudos
Ariharan
New Contributor
Hi James,
               Sorry, you misunderstood the question. This layer selection, i already done. But in that selected layer, no features were selected. I got pSelection = MyLayer, but am Getting the pSelectionSet.Count=0. That implies 0 features from MyLayer were selected.


My question is, before getting information from the MyLayer, i need to select some features in MyLayer. How to select a single feature on mouse click in MyLayer?. I think, now u can understand the problem.
0 Kudos