1, whole section annotation ctrl+k,ctrl+c eliminate annotation ctrl+k,ctrl+u; 2, the production of menu 3, the production of class-counting related classes named SURVEYTOOLS.CSS code as follows
Using System; Using System.IO; Using System.Windows.Forms; Using System.Reflection; Using ESRI. Arcgis.carto; Using ESRI. Arcgis.display; Using ESRI. Arcgis.geometry; namespace Aotest {///<summary>///Use this object to measure distance, area, and angle on the map///can get the current segment length, total length of the line, polygon area, current segment azimuth, current line segment and the angle of the previous segment/// Use to set the map variable///</summary> public class Surveytools {private IMap m_pmap;//Map Object private ipointcollection m_ppnts; Point set private Inewpolygonfeedback m_ppolygonfeedback; New Polygon Object Private Inewlinefeedback m_plinefeedback; New Line Object private int m_isurveytype; 0-Empty operation 1-measuring distance 2-measuring area 3-measuring angle private bool m_bbusy; Whether the private double m_dtotallength = 0 is being measured; Line total length private double m_dcurrentlength = 0; Current segment length private double M_darea = 0; Polygon Area private Double m_dangle = 0; The angle between the current line segment and the front segment private double m_ddirection = 0; The angle to the north direction, i.e. azimuth///<summary>///Map Object, write only///</summary> public IMap Map {set {M_pmap = value;}}///< Summary>///Determine whether measurements are being measured, read-only///</summary> public bool Issurveying {get {return m_bbusy}}///<summary>///polyline total length, read only///</summary> public double Totallength {get { return m_dtotallength; }///<summary>///the current segment length, read-only///</summary> public double currentlength {get {return m_dcurrentlength;} ///<summary>///Polygon area, read-only///</summary> public double areas {get {return m_darea;}///<SUMMARY&G T The angle between the current segment and the front line, read-only///</summary> public double Angle {get {return m_dangle;}}///<summary>///The orientation angle of the current segment , read-only///</summary> public double Direction {get {return m_ddirection;}}///<summary>///constructor///</su Mmary> public Surveytools () {m_pmap = null; m_ppnts = null; m_ppolygonfeedback = null; m_plinefeedback = null; M_isur Veytype = 0; M_bbusy = false; ///<summary>///start distance measurement, PPNT is the starting point for measurement, the result is///totallength: line total length///currentlength: Current segment length/// Call this method in the MouseDown event of the map///</summary>///<param name= "PPNT" > Measurement start </param> public void LengthsTart (IPoint ppnt) {m_isurveytype = 1; m_plinefeedback = new Newlinefeedbackclass (); m_plinefeedback.display = ((IActiveVi EW) m_pmap). Screendisplay; M_plinefeedback.start (PPNT); M_bbusy = true; m_ppnts= new Polylineclass (); Object EP = System.Reflection.Missing.Value; M_ppnts.addpoint (Ppnt,ref EP, ref EP); ///<summary>///Start area measurement, ppnt as the starting point of the polygon, the measurement result is///area: Polygon Size///Call this method in the MouseDown event of the map///</summary>///<p Aram Name= "PPNT" > Measurement start </param> public void Areastart (IPoint ppnt) {m_isurveytype = 2; m_ppolygonfeedback = new Ne Wpolygonfeedbackclass (); M_ppolygonfeedback.display = ((Iactiveview) m_pmap). Screendisplay; M_ppolygonfeedback.start (PPNT); M_bbusy = true; m_ppnts= new Polygonclass (); Object EP = System.Reflection.Missing.Value; M_ppnts.addpoint (Ppnt,ref EP, ref EP); ///<summary>///begins to measure the angle, the PPNT is the polyline starting point, and the result is///Direction: The direction angle of the current segment, that is, the angle to the north direction///Angle: The angle between the current segment and the Front line section/// Call this method///</summary>///<param name= "Ppnt" > Polyline starting point in the MouseDown event of the map </param> public void Anglestart (IPoint ppnt) {m_isurveytype = 3; m_plinefeedback = new Newlinefeedbackclass (); m_plinefeed Back. Display = ((Iactiveview) m_pmap). Screendisplay; M_plinefeedback.start (PPNT); M_bbusy = true; m_ppnts= new Polylineclass (); Object EP = System.Reflection.Missing.Value; M_ppnts.addpoint (Ppnt,ref EP, ref EP); ///<summary>///Add nodes to a polyline or polygon///Call this method in the MouseDown event of the map, adding the current cursor position as a node///to a polyline or polygon///</summary>///< param name= "PPNT" > inflexion </param> public void Addpoint (IPoint ppnt) {if (M_isurveytype = 1 | | m_isurveytype = = 3) { M_plinefeedback.addpoint (PPNT); else if (M_isurveytype = 2) {m_ppolygonfeedback.addpoint (PPNT);} object EP = System.Reflection.Missing.Value; M_ppnts.addpoint (Ppnt,ref EP, ref EP); ///<summary>///Move the mouse position, dynamically change the position of the last node of the polyline or polygon,///recalculate the measurements, recommend that the MouseMove event of the map be invoked,///and extract the current measurement results for display///</ summary>///<param name= "PPNT" > Endpoint </param> public void MoveTo (IPoint ppnt) {if (M_isurveytype = 1 || M_isurveytype = = 3) {M_plinefeedback.moveto (PPNT); Calculatelength (PPNT); Calculatedirection (PPNT); Calculateangle (PPNT); else if (M_isurveytype = 2) {M_ppolygonfeedback.moveto (PPNT); CalculateArea (PPNT); }///<summary>///ends the current measurement, returning the drawing object drawn on the map when metering is made///recommends that this method MouseDown be called in the DoubleClick event or///(right) event of the map to return the object with a map. Drawshape method painted on the temporary layer of the map///</summary>///<param name= "PPNT" > Endpoint </param>///<returns> map drawing objects </returns> public igeometry surveyend (IPoint ppnt) {igeometry pgeometry =null; if (m_isurveytype = 1 | | m_isurveyt ype = = 3) {m_plinefeedback.addpoint (PPNT); pgeometry = (igeometry) m_plinefeedback.stop (); m_plinefeedback = null;} els E if (M_isurveytype = = 2) {m_ppolygonfeedback.addpoint (PPNT); pgeometry = (igeometry) m_ppolygonfeedback.stop (); m_pPoly Gonfeedback = null; } m_ppnts = null; M_bbusy = false; if (pgeometry!= null) return pgeometry; else return null; ///<summary>///calculates the length of the measured segment, and the results are stored separately:///totallength: Line total length///currentlEngth: Current segment length///</summary>///<param name= "Ppnt" > at the end of the calculation </param> private void Calculatelength ( IPoint ppnt) {try {ipointcollection pPs = new Polylineclass (); double DL = 0; pps.addpointcollection (m_ppnts); Ipolyline Pline; if (Pps.pointcount > 1) {pline = (ipolyline) pPs; DL = Pline.length;} object EP = System.Reflection.Missing.Value; Pps.addpoint (Ppnt,ref EP, ref EP); Pline = (ipolyline) pPs; M_dtotallength = Pline.length; M_dcurrentlength = M_DTOTALLENGTH-DL; catch (System.Exception e) {Console.WriteLine (e.message.tostring ());}} <summary>///Compute the polygon area, the result is stored in the///</summary>///<param name= "PPNT" > The end of the calculation </param> private void CalculateArea (IPoint ppnt) {try {ipointcollection pPs = new Polygonclass (); Pps.addpointcollection (m_ppnts ); Object EP = System.Reflection.Missing.Value; Pps.addpoint (Ppnt,ref EP, ref EP); if (Pps.pointcount > 2) {ipolygon Ppolygon = (ipolygon) pPs; Iarea Parea = (iarea) Ppolygon; M_darea= Parea.area; M_darea = System.Math.Abs (M_darea); The catch (System.Exception e) {Console.WriteLine (e.message.tostring ());}} <summary>///calculates the direction angle of the current line segment, the result is that the direction is///North direction is 0 degrees, clockwise is positive, the range is 0--360 degree///</summary>///<param name= "Ppnt" > End point </param> private void Calculatedirection (IPoint ppnt) {Double dx, dy, da; IPoint P1, p2; P1 = M_ppnts.get_point (m_ppnts.pointcount-1); P2 = ppnt; DX = p2. X-p1. X DY = p2. Y-p1. Y if (dx = = 0) {if (dy >0) {m_ddirection =0} else {m_ddirection = 180;}} else if (dx > 0) {if (dy==0) {M_ddi rection=90; else if (Dy > 0) {da = System.Math.Abs (dx/dy); m_ddirection = System.Math.Atan (DA) * 180/3.14159265;} else if (d Y < 0) {da = System.Math.Abs (dx/dy); m_ddirection = System.Math.Atan (DA) * 180/3.14159265; m_ddirection = 180-m_d Direction; } else {if (dy==0) {m_ddirection=270} else if (dy >= 0) {da = System.Math.Abs (dx/dy); m_ddirection = System.Math . Atan (DA) * 180/3.14159265;M_ddirection = 360-m_ddirection; else {da = System.Math.Abs (dx/dy); m_ddirection = System.Math.Atan (DA) * 180/3.14159265; m_ddirection = 180 + M_ddir ection; }}///<summary>///calculates the angle between the current segment and the front line segment, and the result is stored in the angle///algorithm uses the cosine theorem, the result range is 0--180 degree. </summary>///<param name= "PPNT" > End of calculation </param> private void Calculateangle (IPoint ppnt) {int ICo UNT = M_ppnts.pointcount; if (Icount < 2) return; Double A, AA, B, BB, CC; IPoint Pnt1, Pnt2, Pnt3; try {Pnt1 = PPNT; Pnt2 = M_ppnts.get_point (iCount-1); Pnt3 = M_ppnts.get_point (iCount-2); AA = (pnt1.x-pnt2.x) * (pnt1.x-pnt2.x) + (PNT1.Y-PNT2.Y) * (PNT1.Y-PNT2.Y); A = MATH.SQRT (AA); bb = (pnt3.x-pnt2.x) * (pnt3.x-pnt2.x) + (PNT3.Y-PNT2.Y) * (PNT3.Y-PNT2.Y); b = math.sqrt (BB); CC = (pnt1.x-pnt3.x) * (pnt1.x-pnt3.x) + (PNT1.Y-PNT3.Y) * (PNT1.Y-PNT3.Y); M_dangle = Math.acos (AA+BB-CC)/2/a/b) * 180/3.14159265; catch (Exception e) {Console.WriteLine (e.message.tostring ());}} Declare the IMA in the main window functionP Class A public variable defines an object for a Surveytools class
Public IMap PMap;
Surveytools Ptool; Add in the main window load function
PMap = Axmapcontrol1.map;
Ptool = new Surveytools ();
Ptool.map = Axmapcontrol1.map; Double-click the Length tab on the menu to enter the response code edit area
private void length measure Toolstripmenuitem_click (object sender, EventArgs e) {imapaction = 1; toolstripstatuslabel1.text = "Length measured"; Itool obj = new Controlsmappantoolclass (); Axmapcontrol1.currenttool = obj; Add code to the OnMouseDown event in the map display area Mapcontrol
private void Axmapcontrol1_onmousedown (object sender, Imapcontrolevents2_onmousedownevent e) {object obj; switch (imapa ction) {Case 1: #region//measuring length if (E.button = 1) {if (!ptool.issurveying) Ptool.lengthstart Axmapcontrol1.tomappoint (e.x , e.y)); Else Ptool.addpoint (Axmapcontrol1.tomappoint (e.x, e.y)); } else {if (ptool.issurveying) {obj = new Simplelinesymbolclass (); Axmapcontrol1.drawshape (Ptool.surveyend ( Axmapcontrol1.tomappoint (e.x, e.y)), ref obj); Toolstripstatuslabel1.text = "Distance Measurement"; }} break; #endregion Case 2: #region//Measured area if (E.button = 1) {if (!ptool.issurveying) Ptool.areastart (Axmapcontrol1.tomappoint (e.x , e.y)); Else Ptool.addpoint (Axmapcontrol1.tomappoint (e.x, e.y)); } else {if (ptool.issurveying) {obj = new Simplefillsymbolclass (); Axmapcontrol1.drawshape (Ptool.surveyend ( Axmapcontrol1.tomappoint (e.x, e.y)), ref obj); Toolstripstatuslabel1.text = "Measuring area"; }} break; #endregion
}
Add code to the OnMouseMove event
private void Axmapcontrol1_onmousemove (object sender, Imapcontrolevents2_onmousemoveevent e) { Toolstripstatuslabel2.text = E.x.tostring (); Toolstripstatuslabel3.text = E.y.tostring (); Switch (imapaction) {Case 1: #region//measuring length if (ptool.issurveying) {Ptool.moveto (Axmapcontrol1.tomappoint (e.x, e.y)); too Lstripstatuslabel1.text = "Total length:" + pTool.totalLength.ToString () + "; current segment Length:" + pTool.currentLength.ToString (); } break; #endregion Case 2: #region//measuring area if (ptool.issurveying) {Ptool.moveto (Axmapcontrol1.tomappoint (e.x, e.y)); Toolstripstatuslabel1.text = "Area:" + pTool.Area.ToString (); } break; #endregion
}
}
The clear screen code is
AxMapControl1.ActiveView.PartialRefresh (esriviewdrawphase.esriviewforeground, NULL, NULL);