AnsweredAssumed Answered

.Net user drawn polyine with rubberBand.TrackNew doesn't snap

Question asked by pwned555 on Apr 29, 2014
Latest reply on May 1, 2014 by pwned555
Hi, I'm very new with writing custom code for ArcMap.  I'm having some issues right now with having the user draw their own line on the screen, I want it to work the same way the line -> straight segment would work normally with it snapping to other lines and points.  The reason I'm doing it with custom code is because I want to calculate and assign a Z value to the points selected by the user if they click on an object with a Z value, otherwise I will give it a value of 0.  So far everything is working okay, except for the fact that they will never click on another object exactly and the line will never be drawn exactly where they want because it doesn't snap at all.

I've created a tool and the OnMouseDown event is as follows
protected override void OnMouseDown(MouseEventArgs arg)         {             IMxDocument mxDocument = ArcMap.Application.Document as IMxDocument;             // Get reference to the current map             IMap map = mxDocument.FocusMap;              IPoint userPoint = null;             if (m_Position != null)                 userPoint = m_Position;             else             {                 IDisplayTransformation dispTransformation = mxDocument.ActiveView.ScreenDisplay.DisplayTransformation;                 userPoint = dispTransformation.ToMapPoint(arg.X, arg.Y);             }              bool isPolylineLayer = false;             IActiveView activeView = mxDocument.ActiveView;             // Get the TOC             IContentsView IContentsView = mxDocument.CurrentContentsView;             //Set the featureLayer             IGeoFeatureLayer featureLayer = null;             // Get the selected layer             System.Object selectedItem = IContentsView.SelectedItem;             if (selectedItem is IGeoFeatureLayer)             {                 featureLayer = selectedItem as IGeoFeatureLayer;                 //Check to make sure that the selected layer is a polyline layer                 if (featureLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline)                 {                     isPolylineLayer = true;                 }             }              //Only preform these actions if user selected a polyline layer             if (isPolylineLayer)             {                 if (activeView != null)                 {                         IScreenDisplay screenDisplay = activeView.ScreenDisplay;                         IRubberBand rubberBand = new RubberLineClass();                         IGeometry geometry = rubberBand.TrackNew(screenDisplay, null);                         //We need to make the geometry zaware to keep from getting an error during AddPolylineToGeoDatabase                         MakeZAware(geometry);                         IPointCollection pointCollection = (IPointCollection)geometry;                          Point pointClass = new Point();                         //Loops through all the points on the polyline and gives it a Z value of 0 or its                         //actual z value if a user clicked a point where that was known                         for (int i = 0; i < pointCollection.PointCount; i++)                         {                             IPoint tempPoint = pointClass.AddZToPoint(pointCollection.get_Point(i));                             pointCollection.UpdatePoint(i, tempPoint);                         }                         IPolyline polyline = (IPolyline)pointCollection;                         AddPolylineToGeoDatabase(featureLayer, polyline, mxDocument);                 }             }         }


This all works alright except that there is no snapping while the geometry is being created.  For the first point selected userPoint holds the snapped value but even this value I don't know how to use as it seems like rubberBand.TrackNew uses the actual point where the user clicked and it doesn't seem possible to give it a different point to use as the start.

I'm wondering what other ways there are to get a polyline from the user, or if there is a way to make the rubberBand.TrackNew snap and use the snapped point for the first value.

Thanks for any help.

Outcomes