Implementing snapping in engine tools)

Source: Internet
Author: User
Tags ipoint

In the engine tool (itool:

 

In The onclick event processing function:

First, you need to obtain a layer as a reference for snapping,

Ifeaturelayer targetlayer

 

Then declare an imovepointfeedback as the display of the captured point when the mouse moves:

Optional m_pmoveptfeed = new movepointfeedback (); <br/> mfeedback = (idisplayfeedback) m_pmoveptfeed; <br/> isimplemarkersymbolclass (); <br/> irgbcolor prgbcolor = new rgbcolorclass (); <br/> prgbcolor. red = 0; <br/> prgbcolor. green = 0; <br/> prgbcolor. blue = 0; <br/> simplemarkersymbol. color = prgbcolor; <br/> simplemarkersymbol. size = 3; <br/> simplemarkersymbol. style = ESRI. arcGIS. display. esrisimplemarkerstyle. esrismssquare; <br/> isymbol symbol = simplemarkersymbol as isymbol; <br/> symbol. ROP2 = esrirasteropcode. esriropnotxorpen; <br/> // symbol. ROP2 = esrirasteropcode .; <br/> m_pmoveptfeed.symbol = (isymbol) simplemarkersymbol;

 

Then start Feedback Display (tmppoint refers to the start point, but it does not affect much. If you do not want the source point to be on the screen, you can take a point outside the screen ):

M_pmoveptfeed.display = mmapcontrol. activeview. screendisplay; <br/> m_pmoveptfeed.start (tmppoint, tmppoint );

 

In the onmousemove event:

Ipoint ppoint2 = NULL; <br/> ipoint Ppoint = pmap. activeview. screendisplay. displaytransformation. tomappoint (x, y); <br/> ppoint2 = snapping (Ppoint. x, Ppoint. y, targetlayer, pmap, 10); <br/> If (ppoint2 = NULL) <br/> ppoint2 = Ppoint; <br/> (imovepointfeedback) mfeedback ). moveTo (ppoint2 );

 

Among them, the snapping function is the most important lookup function. Its function is to find the closest point (that is, the point to be captured) on the target layer based on the input coordinate, and return the point, if no value is found, null is returned. The last parameter indicates the number of pixels in the map control to locate the capturing point in the surrounding area.

Public ipoint snapping (Double X, Double Y, ifeaturelayer ifeaturelyr, imapcontrol3 axmapcontrol1, double snappingdis) <br/>{< br/> ipoint ihitpoint = NULL; <br/> IMAP = axmapcontrol1.map; <br/> iactiveview iview = axmapcontrol1.activeview; <br/> ifeatureclass ifclss = ifeaturelyr. featureclass; <br/> ipoint point = new pointclass (); <br/> point. putcoords (x, y); <br/> double length = convertpixelstomapunits (axmapcontrol1.activeview, snappingdis); <br/> itopologicaloperator ptopo = point as itopologicaloperator; <br/> igeometry pgeometry = ptopo. buffer (length ). envelope as igeometry; <br/> ispatialfilter spatialfilter = new spatialfilterclass (); <br/> spatialfilter. geometryfield = ifeaturelyr. featureclass. shapefieldname; <br/> spatialfilter. spatialrel = esrispatialrelenum. esrispatialrelintersects; <br/> spatialfilter. geometry = pgeometry; <br/> ifeaturecursor cursor = ifclss. search (spatialfilter, false); <br/> ifeature if = cursor. nextfeature (); <br/> If (if = NULL) return NULL; <br/> ipoint ihitpt = new ESRI. arcGIS. geometry. point (); <br/> ihittest = if. shape as ihittest; <br/> double hitdist = 0; <br/> int partindex = 0; <br/> int vertexindex = 0; <br/> bool bvertexhit = false; <br/> // tolerance in pixels for line hits <br/> double Tol = convertpixelstomapunits (iview, snappingdis); <br/> If (ihittest. hittest (point, Tol, esrigeometryhitparttype. esrigeometrypartboundary, <br/> ihitpt, ref hitdist, ref partindex, ref vertexindex, ref bvertexhit) <br/>{< br/> ihitpoint = ihitpt; <br/>}< br/> // axmapcontrol1.activeview. refresh (); <br/> return ihitpoint; <br/>}< br/> Public double convertpixelstomapunits (iactiveview pactiveview, pixdouble elunits) <br/>{< br/> double realworlddisplayextent; <br/> int pixelextent; <br/> double sizeofonepixel; <br/> pixelextent = pactiveview. screendisplay. displaytransformation. get_deviceframe (). right-pactiveview. screendisplay. displaytransformation. get_deviceframe (). left; <br/> realworlddisplayextent = pactiveview. screendisplay. displaytransformation. visiblebounds. width; <br/> sizeofonepixel = realworlddisplayextent/pixelextent; <br/> return pixelunits * sizeofonepixel; <br/>}

 

In this case, the mouse can capture objects on the target layer in real time. If you need to obtain the capturing point of the current position, you can call the following in the corresponding event (such as onmousedown or ondbclick:

Ipoint Ppoint = (imovepointfeedback) mfeedback). Stop ();

At this time, real-time capturing will stop. If you need to start capturing again, you can call these statements later:

// Start snap again <br/> ipoint tmppoint = new pointclass (); <br/> tmppoint. putcoords (pmap. extent. xmin-1, pmap. extent. ymin-1); <br/> imovepointfeedback m_pmoveptfeed = (imovepointfeedback) mfeedback; <br/> m_pmoveptfeed.display = pmap. activeview. screendisplay; <br/> m_pmoveptfeed.start (tmppoint, tmppoint );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.