I have new in ArcGis Dev. I am trying to create a tool. In the tool when cliked on the feature it will tell you about the XY point of the feature and the name of the feature. I am getting the XY. When I am trying to get the feature Name like if I click on Structure it will show the message "NE.Structure".
Apart structure, all the features I am getting null exception("System.NUllReferenceException occured in the dll but not handled by user code") and 2ndly what I tried is selecting the feature which I think is changing the map state. Can anyone please help. Here is what I tried
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
// TODO: Add MapControl.OnMouseDown implementation
screenPoint = new ESRI.ArcGIS.Geometry.Point();
screenPoint.X = X;
screenPoint.Y = Y;
IScreenDisplay screenDisplay = (m_application.Document as IMxDocument).ActiveView.ScreenDisplay;
mapPoint = screenDisplay.DisplayTransformation.ToMapPoint(X,Y);
IMxDocument pMxDoc = (IMxDocument)m_application.Document;
IMap pMap = (IMap)pMxDoc.FocusMap;
mapPoint.SpatialReference = pMap.SpatialReference;
IEnvelope env = new EnvelopeClass() as IEnvelope;
// env.PutCoords(mapPoint.X, mapPoint.Y, mapPoint.X, mapPoint.Y);
ITopologicalOperator pTopOp = mapPoint as ITopologicalOperator;
ISpatialFilter pSpatFlt = new SpatialFilterClass();
pSpatFlt.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
pSpatFlt.Geometry = pTopOp.Buffer(10);
ESRI.ArcGIS.esriSystem.UID pFeatLayerUID = new ESRI.ArcGIS.esriSystem.UIDClass();
pFeatLayerUID.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}"; /* refer http:// help.arcgis.com/EN/sdk/10.0/ArcObjects_NET/componenthelp/index.html#/Layers_Property/001200000m9t000000/ */
IEnumLayer pEnumLayer = pMap.get_Layers(pFeatLayerUID, true);
ILayer pThisLayer = pEnumLayer.Next();
//IGeometry selectionGeometry = (mapPoint as ITopologicalOperator).Buffer(100); // make a buffer
pMap.SelectByShape(mapPoint, (m_application as IMxApplication).SelectionEnvironment, false);
IFeatureLayer pThisFeatLayer = pThisLayer as IFeatureLayer;
IEnumFeature enumFeatures = (IEnumFeature)pMap.FeatureSelection;
IFeature thisFeature = enumFeatures.Next();
do
{
IGeometry thisGeom = thisFeature.ShapeCopy;
IFeatureCursor pFtCur = pThisFeatLayer.Search(pSpatFlt,true);
IFeature pFt = pFtCur.NextFeature();
IFeatureClass fC = (IFeatureClass)pFt.Class;
MessageBox.Show(fC.AliasName.ToString());
thisFeature = enumFeatures.Next();
} while (thisFeature != null);
MessageBox.Show("X position is " + mapPoint.X.ToString() + "Y position is :" + mapPoint.Y.ToString());
}
}
The exception I am getting is like :Error message
This above error is coming in this line # IFeatureClass fC = (IFeatureClass)pFt.Class;
You asked me what I mean by changing the map state. In case user already selected any feature or an area, in that if my tool is trying to select the clicked feature, I am afraid it would deselect the user selection.
-
1Welcome to GIS SE! As a new user please take the tour to learn about our focused Q&A format. You have tagged arcpy in your question, yet you appear to have included arcobjects (C#?) code and no python code in your question. What error messages are you getting? Can you please edit your question to include any error messages in full. Also what do you mean by "I think is changing the map state"?Midavalo– Midavalo ♦2017年02月14日 20:08:25 +00:00Commented Feb 14, 2017 at 20:08
-
Hi Midavalo, I have edited my questions with the questions you asked and added a image of the excetion.royan– royan2017年02月14日 20:22:32 +00:00Commented Feb 14, 2017 at 20:22
-
Please include errors as text rather than pictures so that they are available to future searches by anyone who encounters the same symptom.PolyGeo– PolyGeo ♦2017年02月14日 20:26:40 +00:00Commented Feb 14, 2017 at 20:26
-
Added the exception in the body in text formatroyan– royan2017年02月14日 20:31:59 +00:00Commented Feb 14, 2017 at 20:31
-
What line is the exception occurring on? Are you sure you are getting a feature back from pFtCur.NextFeature() or is it null?danielm– danielm2017年02月14日 20:42:37 +00:00Commented Feb 14, 2017 at 20:42
1 Answer 1
The issue is that you are not getting any features back from this line:
IFeature pFt = pFtCur.NextFeature();
The biggest issue is that you have a few things going on here and they are used together in a way that doesn't work. On one hand you are selecting features on the map and then looping through those features. On the other hand you are executing a spatial query on existing layers.
So I would pick one of the following methods depending on your needs.
The SelectByShape method:
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
screenPoint = new ESRI.ArcGIS.Geometry.Point();
screenPoint.X = X;
screenPoint.Y = Y;
IScreenDisplay screenDisplay = (m_application.Document as IMxDocument).ActiveView.ScreenDisplay;
mapPoint = screenDisplay.DisplayTransformation.ToMapPoint(X,Y);
IMxDocument pMxDoc = (IMxDocument)m_application.Document;
IMap pMap = (IMap)pMxDoc.FocusMap;
mapPoint.SpatialReference = pMap.SpatialReference;
//IGeometry selectionGeometry = (mapPoint as ITopologicalOperator).Buffer(100); // make a buffer
pMap.SelectByShape(mapPoint, (m_application as IMxApplication).SelectionEnvironment, false);
IEnumFeature enumFeatures = (IEnumFeature)pMap.FeatureSelection;
IFeature thisFeature;
while((thisFeature = enumFeatures.Next()) != null)
{
IFeatureClass fC = (IFeatureClass)thisFeature.Class;
MessageBox.Show(fC.AliasName.ToString());
thisFeature = enumFeatures.Next();
}
MessageBox.Show("X position is " + mapPoint.X.ToString() + "Y position is :" + mapPoint.Y.ToString());
}
The layer loop method:
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
// TODO: Add MapControl.OnMouseDown implementation
screenPoint = new ESRI.ArcGIS.Geometry.Point();
screenPoint.X = X;
screenPoint.Y = Y;
IScreenDisplay screenDisplay = (m_application.Document as IMxDocument).ActiveView.ScreenDisplay;
mapPoint = screenDisplay.DisplayTransformation.ToMapPoint(X,Y);
IMxDocument pMxDoc = (IMxDocument)m_application.Document;
IMap pMap = (IMap)pMxDoc.FocusMap;
mapPoint.SpatialReference = pMap.SpatialReference;
ITopologicalOperator pTopOp = mapPoint as ITopologicalOperator;
ISpatialFilter pSpatFlt = new SpatialFilterClass();
pSpatFlt.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
pSpatFlt.Geometry = pTopOp.Buffer(10);
ESRI.ArcGIS.esriSystem.UID pFeatLayerUID = new ESRI.ArcGIS.esriSystem.UIDClass();
pFeatLayerUID.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}"; /* refer http:// help.arcgis.com/EN/sdk/10.0/ArcObjects_NET/componenthelp/index.html#/Layers_Property/001200000m9t000000/ */
IEnumLayer pEnumLayer = pMap.get_Layers(pFeatLayerUID, true);
ILayer pThisLayer;
while((pThisLayer = pEnumLayer.Next()) != null)
{
IFeatureCursor pFtCur = pThisFeatLayer.Search(pSpatFlt,true);
IFeature pFt = pFtCur.NextFeature();
if(pFt != null)
{
IFeatureClass fC = (IFeatureClass)pFt.Class;
MessageBox.Show(fC.AliasName.ToString());
break;
}
}
MessageBox.Show("X position is " + mapPoint.X.ToString() + "Y position is :" + mapPoint.Y.ToString());
}
I'm not able to test these at the moment, but it should get you started.
-
hey danielm, First of all thanks for sharing the analysis with code. I understood the place where I got the exception. I used the second layer loop method with small modification that is declaring the pThisFeatLayer.royan– royan2017年02月15日 08:00:51 +00:00Commented Feb 15, 2017 at 8:00