How to work with the snap environment |
SummaryThe snap Environment manages snap agents and snap tolerance and is responsible for attempting to s The NAP vertices and points using the SNAP environment settings. This topic includes code examples and methods to best manage snapping programmatically.
In this topic
- Working with the SNAP environment
- SNAP agents
- Snapping in Z-dimension
- Snap Point method
working with the snap environmentThe ArcGIS Engine Editor ' s snap Environment (ienginesnapenvironment) controls each snap agent (ienginesnapagent) with hit type settings, and snapping tolerance. The hits type of each feature snap agent is managed through the Ienginefeaturesnapagent interface. All settings can is manually modified or verified on the Snapping Settings dialog box. See the following-screen shot:the ArcGIS Engine editor ' s SNAP environment also controls the snap tolerance. If required, you can provide a interface to allow the end of user to manage the snap tolerance. See the following code example to set up Basic SNAP environment settings and turn on SNAP tips:[C #
]
Publicvoid Snapenvirsettings (Iengineeditor editor) {Get the snap environment from the editor. Ienginesnapenvironment snapenvironment = Editoras ienginesnapenvironment; //ensure there is a snap agent turned on the snap environment. if (snapenvironment.snapagentcount = 0) {System.Windows.Forms.MessageBox.Show ( "You need-turn on at least one snapping agent!!!"); return;} //code to display the snap tolerance in a message box. double tolerance = snapenvironment.snaptolerance; System.Windows.Forms.MessageBox.Show (convert.tostring (tolerance)); //set the snap tolerance. Snapenvironment.snaptoleranceunits = Esrienginesnaptoleranceunits.esrienginesnaptolerancemapunits; Snapenvironment.snaptolerance = 15; //turn on snap tips. ((IEngineEditProperties2) editor). Snaptips = true;}
[vb.net]
PublicSub Snapenvirsettings (ByVal EditorAs Iengineeditor)' Get the snap environment from the editor.Dim snapenvironmentAs Ienginesnapenvironment = EditorAs Ienginesnapenvironment' Ensure there is a snap agent turned on the snap environment.if snapenvironment.snapagentcount = 0 then System.Windows.Forms.MessageBox.Show ( "need to turn on at least one snapping agent!!!") returnendif ' Code to display the snap tolerance in a message box. dim tolerance asdouble = snapenvironment.snaptolerance System.Windows.Forms.MessageBox.Show (convert.tostring (tolerance)) ' Set the snap tolerance. Snapenvironment.snaptoleranceunits = Esrienginesnaptoleranceunits.esrienginesnaptolerancemapunits Snapenvironment.snaptolerance = ' Turn on snap tips. ((IEngineEditProperties2) editor). Snaptips = trueendsub
This method checks the snap environment for selected snap agents. Feature SNAP Agents is checked if the hitparttype was not equal to Esrigeometrypartnone. The other SNAP agents is only registered if they is checked; Therefore, if the number of SNAP agents is greater than the number of feature snap agents, a snap agent (sketch snap agent ) is checked. See the following code example:[C #
]
PrivateBOOL Checkisanysnapagentselected (ienginesnapenvironment snapenvironment) {int snapagentcount = Snapenvironment.snapagentcount;int checkedfeaturesnapagentcount = 0;int featuresnapagentcount = 0;Loop through all registered SNAP agents in the snap environment. Count feature SNAP Agents,//checked feature snap agents, and Nonfeature snap agents. for (int i = 0; i < Snapagentcount; i++) {Ienginesnapagent currentsnapagent = sn Apenvironment.get_snapagent (i); if (currentsnapagent is ienginefeaturesnapagent) {IEngineFeatureSnapAgent Featuresnapagent = currentsnapagent as ienginefeaturesnapagent; featuresnapagentcount++; //determine if the feature snap agent is checked. if (featuresnapagent.hittype! = Esrigeometryhitparttype.esrigeometrypartnone) { checkedfeaturesnapagentcount++; }}} if (Checkedfeaturesnapagentcount > 0 | | snapagentcount > Featuresnapagentcount) {returntrue;} else {returnfalse;}
[vb.net]
PrivateFunction checkisanysnapagentselected (ByVal snapenvironmentAs Ienginesnapenvironment)AsBooleanDim SnapagentcountAsInteger = Snapenvironment.snapagentcountDim CheckedfeaturesnapagentcountAsInteger = 0Dim FeaturesnapagentcountAsInteger = 0' Loop through all registered SNAP agents in the snap environment. Count feature SNAP Agents,' Checked feature snap agents, and Nonfeature snap agents.Dim IAsIntegerFor i = 0To SnapAgentCount-1Step i + 1Dim currentsnapagentAs Ienginesnapagent = Snapenvironment.get_snapagent (i)IfTypeOf currentsnapagentIs IenginefeaturesnapagentThenDim featuresnapagentAs Ienginefeaturesnapagent = Currentsnapagentas ienginefeaturesnapagent featuresnapagentcount = featuresnapagentcount + 1 if featuresnapagent.hittype <> Esrigeometryhitparttype.esrigeometrypartnone then checkedfeaturesnapagentcount = checkedfeaturesnapagentcount + 1 EndifendifnextIf Checkedfeaturesnapagentcount > 0 or snapagentcount > Featuresnapagentcount thenreturntrueElse returnfalseendIf endfunction
SNAP AgentsSNAP agents implement the Ienginesnapagent interface and, when registered as a component category, is inserted into the E SRI Snap Agents component category. However, the Feature snap agent (Ienginefeaturesnapagent) is a more detailed class of SNAP agents, and each feature class H As a feature snap agent instantiated when the Snap environment window is first opened, if it hasn ' t already been created P Rogrammatically. See the following code example showing what to create a feature snap agent programmatically:[C #
]
Publicvoid Addnewsnapagent () {Iengineeditor editor =New Engineeditorclass (); Iengineeditlayers editlayers = EditorAs Iengineeditlayers; Ienginesnapenvironment snapenvironment = Editoras ienginesnapenvironment; //check that the user is editing; otherwise, there'll be no snap agent loaded. if (Editlayers.targetlayer = null) {System.Windows.Forms.MessageBox.Show ( span class= "str" > "start an edit session"); return;} //clear all existing snap agents. Snapenvironment.clearsnapagents (); //create a feature snap agent. Ienginefeaturesnapagent featuresnapagent = new enginefeaturesnap (); Ifeatureclass layerfeatureclass = EditLayers.TargetLayer.FeatureClass; Featuresnapagent.featureclass = Layerfeatureclass; Featuresnapagent.hittype = esrigeometryhitparttype.esrigeometrypartboundary; //activate only the snap agent for the target layer. Snapenvironment.addsnapagent (featuresnapagent);}
[vb.net]
PublicSub addnewsnapagent ()Dim EditorAs Iengineeditor =New Engineeditorclass ()Dim editlayersAs Iengineeditlayers = EditorAs IengineeditlayersDim snapenvironmentAs Ienginesnapenvironment = EditorAs Ienginesnapenvironment' Check that the user is editing; Otherwise, there'll be no snap agent loaded.If Editlayers.targetlayerIsnothingthen System.Windows.Forms.MessageBox.Show ( " Please start an edit session ") returnendif ' Clear all existing snap agents. Snapenvironment.clearsnapagents () ' Create A Feature Snap agent. dim featuresnapagent as ienginefeaturesnapagent = New Enginefeaturesnap () dim layerfeatureclass as Ifeatureclass = EditLayers.TargetLayer.FeatureClass Featuresnapagent.featureclass = Layerfeatureclass Featuresnapagent.hittype = Esrigeometryhitparttype.esrigeometrypartboundary Activate only the snap agent for the target layer. Snapenvironment.addsnapagent (featuresnapagent) endSub
Programmatically changing the snap environment parameters does not require opening the snap window to change the settings. In ArcGIS Engine, the Snap window reflects the snap environment settings while it is open. All ienginesnapagents, such as the Edit sketch vertices snap agent, is visible on the snap window even if they has been programmatically removed; This allows the end user to turn them on and off as needed.
snapping in Z-dimensionArcGIS Engine does not currently support snapping in z-dimension.
Snap Point MethodThe Snappoint method from the Ienginesnapenvironment interface, a IPoint is passed to the method and used to find The closest feature to snap to. Using Snappoint calls the Ienginesnapagent.snap method to and call each SNAP agent in priority order until it finds one T Hat returns True. This results in new coordinates that is then assigned to the original point. The order in which snap agents is added to the snap environment determines the priority of SNAP agents. This "order is" reflected in the Snap environment window and can changed using the window.
See Also:Iengineeditproperties2.snaptips Property
How to work with the snap environment