Acessing Selected feature Attributes
Hi everyone,
I'm trying to have the user select a feature or a group of features
then access some of their aattirbutes and modify their selection
accordingly.
So, implementing ITool I got this from the sample to select:
OnMouseDown(LONG button, LONG shift, LONG x, LONG y)
{
IPointPtr pPoint1, pPoint2;
ILayerPtr pLayer;
IFeatureLayerPtr pFLayer;
IDocumentPtr pDoc;
IMapPtr iFocusMap, ipMap1;
IScreenDisplayPtr ipScreen;
IDisplayTransformationPtr ipDispTransform;
IGeometryPtr ipGeometry;
IEnvelopePtr pEnv(CLSID_Envelope), pEnv2(CLSID_Envelope);
double up, down, left, right;
long i=0, LayerCount;
m_ipApp->get_Document(&pDoc);
IMxDocumentPtr pMxDoc(pDoc);
pMxDoc->get_FocusMap(&iFocusMap);
pMxDoc->get_ActiveView(&m_ipActiveView);
m_ipActiveView->get_ScreenDisplay(&ipScreen);
ipScreen->get_DisplayTransformation(&ipDispTransform);
ipDispTransform->ToMapPoint(x, y, &m_point);
m_ipActiveView->HitTestMap(m_point, &iFocusMap);
if(iFocusMap == 0)
{
return E_NOTIMPL;
}
IUnknownPtr ipUnknown;
IUnknownPtr ipUnknown1;
ipUnknown = iFocusMap;
ipUnknown1 = ipMap1;
//Ensure the Map is the FocusMap
if (ipUnknown != ipUnknown1)
{
pMxDoc->get_ActiveView(&m_ipActiveView);
m_ipActiveView->get_FocusMap(&iFocusMap);
m_ipActiveView->PartialRefresh(esriViewGraphics, 0, 0);
}
//Get the focus map
IActiveViewPtr ipActiveView;
m_ipActiveView->get_FocusMap(&iFocusMap);
ipActiveView = iFocusMap;
//Get the point to start the feedback with
ipActiveView->get_ScreenDisplay(&ipScreen);
ipScreen->get_DisplayTransformation(&ipDispTransform);
ipDispTransform->ToMapPoint(x, y, &m_point);
HWND hWnd;
ipScreen->get_hWnd((OLE_HANDLE*) &hWnd);
::SetCapture(hWnd);
m_inUse = true;
return S_OK;
return S_OK;
}
OnMouseMove(LONG button, LONG shift, LONG x, LONG y)
{
IActiveViewPtr ipActiveView;
IScreenDisplayPtr ipScreenDisplay;
IDisplayTransformationPtr ipDispTransform;
ISelectionPtr pSelection;
IMapPtr ipMap;
IPointPtr ipPoint;
IDocumentPtr pDoc;
m_ipApp->get_Document(&pDoc);
IMxDocumentPtr pMxDoc(pDoc);
pMxDoc->get_FocusMap(&ipMap);
ipActiveView = ipMap;
ipActiveView->get_ScreenDisplay(&ipScreenDisplay);
//Start the feedback if this is the first mouse event
if (m_ipEnvelopeFeedback == 0)
{
m_ipEnvelopeFeedback.CreateInstance(CLSID_NewEnvelopeFeedback);
m_ipEnvelopeFeedback->putref_Display(ipScreenDisplay);
m_ipEnvelopeFeedback->Start(m_point);
}
//Move the feedback to the new mouse coordinates
//activeView->get_ScreenDisplay(&ipScreenDisplay);
ipScreenDisplay->get_DisplayTransformation(&ipDispTransform);
ipDispTransform->ToMapPoint(x, y, &ipPoint);
m_ipEnvelopeFeedback->MoveTo(ipPoint);
return S_OK;
}
OnMouseUp(LONG button, LONG shift, LONG x, LONG y)
{
::ReleaseCapture();
//Get the search geometry
long count;
ISelectionPtr pSelection;
IGeometryPtr ipGeometry;
IEnvelopePtr ipEnvelope;
IEnvelopePtr ipEnvelope2;
if (m_ipEnvelopeFeedback == 0)
{
ipGeometry = m_point;
}
else
{
m_ipEnvelopeFeedback->Stop(&ipEnvelope);
ipGeometry = ipEnvelope;
VARIANT_BOOL check = VARIANT_FALSE;
ipGeometry->get_IsEmpty(&check);
if (check == VARIANT_TRUE)
ipGeometry = m_point;
}
//Set the spatial reference of the search geometry to that of the Map
IMapPtr ipMap;
IDocumentPtr pDoc;
m_ipApp->get_Document(&pDoc);
IMxDocumentPtr pMxDoc(pDoc);
pMxDoc->get_FocusMap(&ipMap);
ISpatialReferencePtr ipSpatialReference;
ipMap->get_SpatialReference(&ipSpatialReference);
ipGeometry->putref_SpatialReference(ipSpatialReference);
//Refresh the active view
IActiveViewPtr ipActiveView;
IViewManagerPtr ipViewManager(ipMap);
ipActiveView = ipMap;
ipMap->SelectByShape(ipGeometry, 0, VARIANT_FALSE);
// putting next chunk of code here
ipActiveView->get_Extent(&ipEnvelope2);
ipActiveView->PartialRefresh(esriViewGeoSelection, 0, ipEnvelope2);
}
m_ipEnvelopeFeedback = 0;
m_inUse = false;
return S_OK;
}
First problem I'm have with this is I can only select with rectangles.
That is, clicking directly on a feature does not work. Next, I'm trying
to acess some of the attributes of the selection. A couple of notes:
there are two layers open in my test, I only want to access the
feautres from one of them. Anyway, the second chuck is:
IEnumFeaturePtr ipEnumFeature;
IEnumFeatureSetupPtr pIES(ipEnumFeature);
ipMap->get_FeatureSelection(&pSelection);
pIES->put_AllFields(VARIANT_TRUE);
ipEnumFeature = pSelection;
IFeaturePtr ipFeature;
ipEnumFeature->Reset();
ipEnumFeature->Next(&ipFeature);
IQueryFilterPtr pQFilter(CLSID_QueryFilter);
int i=0;
VARIANT tnodein, tnodeout;
while(i<count)
{
i++;
ipEnumFeature->Next(&ipFeature);
if(ipFeature == NULL) { ::MessageBox(0, "Feature is Null", "Start",
MB_OK);}
ipFeature->get_Value(2, &tnodein);
ipFeature->get_Value(7, &tnodeout);
}
Do I need IEnumFeatureSetup here? That where the error occurs at this
time, but if I don't use that then I get an runtime error at
"ipFeature->get_Value(2, &tnodein);" which puzzles me, since it could
just return NULL.
Any help would be much appreciated.