/// <summary> /// Update the window with the current snap environment and snap tip settings /// </summary> private void RefreshDisplay() { snapTolerance.Text = snapEnvironment.SnapTolerance.ToString(); snapTolUnits.SelectedIndex = (int)snapEnvironment.SnapToleranceUnits; snapTips.Checked = ((IEngineEditProperties2)editor).SnapTips; //remove all feature snap agents from the data grid view snapAgents.Rows.Clear(); //display feature snap and snap agents that are active for (int i = 0; i < snapEnvironment.SnapAgentCount; i++) { try { IEngineSnapAgent snapAgent = snapEnvironment.get_SnapAgent(i); IEngineFeatureSnapAgent ftrSnapAgent = snapAgent as IEngineFeatureSnapAgent; if (ftrSnapAgent != null) { //for feature snap agents add a row to the data view grid esriGeometryHitPartType hitType = ftrSnapAgent.HitType; bool vertex, edge, end; vertex = (hitType & esriGeometryHitPartType.esriGeometryPartVertex) == esriGeometryHitPartType.esriGeometryPartVertex; edge = (hitType & esriGeometryHitPartType.esriGeometryPartBoundary) == esriGeometryHitPartType.esriGeometryPartBoundary; end = (hitType & esriGeometryHitPartType.esriGeometryPartEndpoint) == esriGeometryHitPartType.esriGeometryPartEndpoint; string vertexString = vertex ? "vertex" : " "; string edgeString = edge ? " edge" : " "; string endString = end ? " end " : " "; string hitTypes = vertexString + edgeString + endString; object[] rowData = { snapAgent.Name.ToString(), ftrSnapAgent.FeatureClass.AliasName, hitTypes }; snapAgents.Rows.Add(rowData); } else { //add the active edit sketch snap agents object[] rowData = { snapAgent.Name.ToString(), "<not applicable>", "<not applicable>" }; snapAgents.Rows.Add(rowData); } } catch (Exception) { } } }
/// <summary> /// Turns off feature snap agents and turns off edit sketch snap agents by removing them /// </summary> /// <remarks> /// Using Clear or Remove feature snap agents can be deactivated so that the user can't enable them. /// Here we simply turn off all the agents, which will allow the user to turn them on. /// </remarks> private void TurnOffAgents() { for (int i = snapEnvironment.SnapAgentCount - 1; i >= 0; i--) { IEngineSnapAgent snapAgent = snapEnvironment.get_SnapAgent(i); IEngineFeatureSnapAgent ftrSnapAgent = snapAgent as IEngineFeatureSnapAgent; if (ftrSnapAgent != null) { //turn off the feature snap agent ftrSnapAgent.HitType = esriGeometryHitPartType.esriGeometryPartNone; } else { //there is no way to turn snap agents off, they must be removed snapEnvironment.RemoveSnapAgent(i); } } //refresh this window RefreshDisplay(); }
public void OnMouseDown(int button, int shift, int x, int y) { this._tool.OnMouseDown(button, shift, x, y); IEngineSnapEnvironment engineEditor = Editor.UniqueInstance.EngineEditor as IEngineSnapEnvironment; if (engineEditor.SnapAgentCount < 1) { Editor.UniqueInstance.OperationStack.Undo(); MessageBox.Show("没有设置捕捉图层!"); } else { IFeatureLayer layer = EditTask.UnderLayers[0] as IFeatureLayer; IFeatureClass featureClass = layer.FeatureClass; IEngineFeatureSnapAgent agent = engineEditor.get_SnapAgent(0) as IEngineFeatureSnapAgent; IDataset dataset = featureClass as IDataset; IDataset dataset2 = agent.FeatureClass as IDataset; if (!dataset.Name.Equals(dataset2.Name)) { Editor.UniqueInstance.OperationStack.Undo(); MessageBox.Show("没有将征占点图层设置为捕捉图层!"); } else { IEngineEditSketch sketch = Editor.UniqueInstance.EngineEditor as IEngineEditSketch; int index = featureClass.FindField("XMBH"); IClone lastPoint = sketch.LastPoint as IClone; IPoint point = lastPoint.Clone() as IPoint; IGeoDataset dataset3 = featureClass as IGeoDataset; if (point.SpatialReference.Name != dataset3.SpatialReference.Name) { point.Project(dataset3.SpatialReference); } IGeometry geometry = (point as ITopologicalOperator).Buffer(0.0002); ISpatialFilter filter = new SpatialFilterClass { Geometry = geometry, SpatialRel = esriSpatialRelEnum.esriSpatialRelEnvelopeIntersects, SubFields = "XMBH", GeometryField = featureClass.ShapeFieldName }; IFeatureCursor o = featureClass.Search(filter, false); IFeature feature = o.NextFeature(); Marshal.ReleaseComObject(o); o = null; if (feature == null) { Editor.UniqueInstance.OperationStack.Undo(); MessageBox.Show("必须捕捉征占点!"); } else if (!feature.HasOID || feature.Shape.IsEmpty) { Editor.UniqueInstance.OperationStack.Undo(); MessageBox.Show("捕捉的征占点没有OID或图形有问题!"); } else { IPolyline polyline = sketch.Geometry as IPolyline; if (this.ps == 0) { int num2; int num3; this._firstValue = feature.get_Value(index); IPoint mapPoint = (feature.Shape as IClone).Clone() as IPoint; mapPoint.Project(polyline.SpatialReference); this._hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.FromMapPoint(mapPoint, out num2, out num3); polyline.FromPoint = mapPoint; polyline.ToPoint = mapPoint; } else if (this.ps >= 1) { object obj2 = feature.get_Value(index); if (!this._firstValue.Equals(obj2)) { Editor.UniqueInstance.OperationStack.Undo(); MessageBox.Show("必须捕捉项目编号相同的征占点!"); return; } IPoint point3 = (feature.Shape as IClone).Clone() as IPoint; point3.Project(polyline.SpatialReference); polyline.ToPoint = point3; } sketch.Geometry = polyline; this.ps++; } } } }
private void SnapPoint() { IEngineSnapEnvironment engineEditor = Editor.UniqueInstance.EngineEditor as IEngineSnapEnvironment; IEngineEditSketch sketch = Editor.UniqueInstance.EngineEditor as IEngineEditSketch; IPointCollection points = sketch.Geometry as IPointCollection; bool flag = false; if (sketch.GeometryType == esriGeometryType.esriGeometryPolygon) { flag = true; } if ((points.PointCount != 1) && (!flag || (points.PointCount != 2))) { IEngineFeatureSnapAgent agent2 = engineEditor.get_SnapAgent(0) as IEngineFeatureSnapAgent; esriSpatialRelEnum esriSpatialRelIntersects = esriSpatialRelEnum.esriSpatialRelIntersects; if (agent2.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon) { esriSpatialRelIntersects = esriSpatialRelEnum.esriSpatialRelTouches; } IPoint lastPoint = sketch.LastPoint; List <IFeature> pFeatures = new List <IFeature>(); ISpatialFilter filter = new SpatialFilterClass { Geometry = lastPoint, SpatialRel = esriSpatialRelIntersects, SubFields = agent2.FeatureClass.OIDFieldName + "," + agent2.FeatureClass.ShapeFieldName, GeometryField = agent2.FeatureClass.ShapeFieldName }; IFeatureCursor o = agent2.FeatureClass.Search(filter, false); IFeature item = o.NextFeature(); while (item != null) { if (item.HasOID && !item.Shape.IsEmpty) { pFeatures.Add(item); item = o.NextFeature(); } } Marshal.ReleaseComObject(o); if (pFeatures.Count >= 1) { IPoint queryPoint = null; if (flag) { queryPoint = points.get_Point(points.PointCount - 3); } else { queryPoint = points.get_Point(points.PointCount - 2); } List <IFeature> list2 = new List <IFeature>(); filter = new SpatialFilterClass { Geometry = queryPoint, SpatialRel = esriSpatialRelIntersects, SubFields = agent2.FeatureClass.OIDFieldName + "," + agent2.FeatureClass.ShapeFieldName, GeometryField = agent2.FeatureClass.ShapeFieldName }; o = null; o = agent2.FeatureClass.Search(filter, false); item = o.NextFeature(); while (item != null) { if (item.HasOID && !item.Shape.IsEmpty) { list2.Add(item); item = o.NextFeature(); } } Marshal.ReleaseComObject(o); if (list2.Count >= 1) { IList <IFeature> list3 = new List <IFeature>(); for (int i = 0; i < list2.Count; i++) { if (this.ContainFeature(pFeatures, list2[i])) { list3.Add(list2[i]); } } IFeature feature2 = null; if (list3.Count == 1) { feature2 = list3[0]; } else { if (list3.Count == 0) { return; } if (list3.Count == pFeatures.Count) { IFeatureLayer pLayer = Editor.UniqueInstance.SnapLayers[0]; SnapExFeatures features = new SnapExFeatures(pFeatures, this._hookHelper, pLayer); features.ShowDialog(); if (features.Index == -1) { return; } feature2 = pFeatures[features.Index]; features = null; } } double hitDistance = -1.0; int hitSegmentIndex = -1; int hitPartIndex = -1; bool bRightSide = false; IGeometry geometry = this.ProjectGeometry(feature2.Shape); IHitTest test = geometry as IHitTest; if (test.HitTest(lastPoint, engineEditor.SnapTolerance, esriGeometryHitPartType.esriGeometryPartVertex, null, ref hitDistance, ref hitPartIndex, ref hitSegmentIndex, ref bRightSide)) { int num5 = -1; double num6 = -1.0; int num7 = -1; int num8 = -1; bool flag4 = false; int part = sketch.Part; IHitTest test2 = geometry as IHitTest; if (test2.HitTest(queryPoint, engineEditor.SnapTolerance, esriGeometryHitPartType.esriGeometryPartVertex, null, ref num6, ref num8, ref num7, ref flag4)) { IPolyline polyline3; if (flag) { num5 = points.PointCount - 2; } else { num5 = points.PointCount - 1; } IGeometryCollection geometrys = geometry as IGeometryCollection; IPointCollection points2 = geometrys.get_Geometry(hitPartIndex) as IPointCollection; IPolyline polyline = new PolylineClass(); IPolyline polyline2 = new PolylineClass(); IPointCollection points3 = polyline as IPointCollection; IPointCollection points4 = polyline2 as IPointCollection; object before = Missing.Value; object after = Missing.Value; IPoint inPoint = null; bool flag6 = false; if (num7 > hitSegmentIndex) { int num9 = hitSegmentIndex; hitSegmentIndex = num7; num7 = num9; flag6 = true; } int num10 = 0; if (flag) { for (num10 = num7; num10 <= hitSegmentIndex; num10++) { inPoint = points2.get_Point(num10); points3.AddPoint(inPoint, ref before, ref after); } int num11 = points2.PointCount - 1; for (num10 = hitSegmentIndex; num10 < num11; num10++) { inPoint = points2.get_Point(num10); points4.AddPoint(inPoint, ref before, ref after); } for (num10 = 0; num10 <= num7; num10++) { inPoint = points2.get_Point(num10); points4.AddPoint(inPoint, ref before, ref after); } if (polyline.Length <= polyline2.Length) { polyline3 = polyline; } else { polyline3 = polyline2; flag6 = true; } } else { num10 = num7; while (num10 <= hitSegmentIndex) { inPoint = points2.get_Point(num10); points3.AddPoint(inPoint, ref before, ref after); num10++; } polyline3 = polyline; } IPointCollection points5 = polyline3 as IPointCollection; int index = num5; if (flag6) { for (num10 = points5.PointCount - 2; num10 > 0; num10--) { IPoint newPoints = (points5.get_Point(num10) as IClone).Clone() as IPoint; points.InsertPoints(index, 1, ref newPoints); index++; } } else { for (num10 = 1; num10 < (points5.PointCount - 1); num10++) { IPoint point7 = (points5.get_Point(num10) as IClone).Clone() as IPoint; points.InsertPoints(index, 1, ref point7); index++; } } } } } } } }
private void GetSnapInfo(ref LayerSnapInfo LSInfo) { int num; int geometryHitType; IFeatureClass featureClass = LSInfo.Layer.FeatureClass; if (this.m_pSnapEnvironment != null) { IFeatureSnapAgent agent = null; for (num = 0; num < this.m_pSnapEnvironment.SnapAgentCount; num++) { agent = this.m_pSnapEnvironment.get_SnapAgent(num) as IFeatureSnapAgent; if ((agent != null) && (featureClass == agent.FeatureClass)) { if (agent is IVerticalSnapAgent) { LSInfo.bVerticalSnap = true; } else { geometryHitType = agent.GeometryHitType; if ((geometryHitType & 4) != 0) { LSInfo.bSnapBoundary = true; } if ((geometryHitType & 32) != 0) { } if ((geometryHitType & 16) != 0) { LSInfo.bSnapEndPoint = true; } if ((geometryHitType & 8) != 0) { } if ((geometryHitType & 1) != 0) { LSInfo.bSnapVertex = true; } } } } } else { IEngineFeatureSnapAgent agent2 = null; for (num = 0; num < this.m_pEngineSnapEnvironment.SnapAgentCount; num++) { agent2 = this.m_pEngineSnapEnvironment.get_SnapAgent(num) as IEngineFeatureSnapAgent; if ((agent2 != null) && (featureClass == agent2.FeatureClass)) { if (agent2 is IVerticalSnapAgent) { LSInfo.bVerticalSnap = true; } else { geometryHitType = (int)agent2.HitType; if ((geometryHitType & 4) != 0) { LSInfo.bSnapBoundary = true; } if ((geometryHitType & 32) != 0) { } if ((geometryHitType & 16) != 0) { LSInfo.bSnapEndPoint = true; } if ((geometryHitType & 8) != 0) { } if ((geometryHitType & 1) != 0) { LSInfo.bSnapVertex = true; } } } } } }