最近在学习拓扑运算时,常常需要将拓扑运算的结果的结果显示出来,我们常用的一种方法是将集合对象绘制在ActiveView对象中。基本代码如下:
1 private void button10_Click(object sender, EventArgs e) 2 { 3 4 IFeatureClass featureClass = (layer as IFeatureLayer).FeatureClass; 5 IFeatureCursor featureCursor = featureClass.Search(null, false); 6 IFeature feature = featureCursor.NextFeature(); 7 while (feature != null) 8 { 9 IPolygon polygon = feature.Shape as IPolygon; 10 11 ITopologicalOperator topologicalOP = polygon as ITopologicalOperator; 12 topologicalOP.Simplify(); 13 IGeometry geometry = topologicalOP.Boundary ;//获得一个边界对象 14 15 if (geometry !=null) 16 { 17 if (geometry.GeometryType == esriGeometryType.esriGeometryPolyline ) 18 { 19 //int a=feature.Fields.FindField("NAME"); 20 //string str = feature.get_Value(a) as string; 21 //MessageBox.Show("中断"); 22 ISymbol symbol = GetSimpleLineSymbol(99) as ISymbol;//GetSimpleLineSymbol为一个自定义方法(获得简单线性符号) 23 this.axMapControl1.ActiveView.ScreenDisplay.StartDrawing(this.axMapControl1.ActiveView.ScreenDisplay.hDC, (short)esriScreenCache.esriAllScreenCaches ); 24 25 this.axMapControl1.ActiveView.ScreenDisplay.SetSymbol(symbol); 26 this.axMapControl1.ActiveView.ScreenDisplay.DrawPolyline(geometry); 27 this.axMapControl1.ActiveView.ScreenDisplay.UpdateWindow(); 28 this.axMapControl1.ActiveView.ScreenDisplay.FinishDrawing(); 29 30 31 } 32 33 } 34 //MessageBox.Show("中断"); 35 feature = featureCursor.NextFeature(); 36 // //this.axMapControl1.ActiveView.Refresh(); 37 38 } 39 }
看似正确的代码却得到不到预期的结果,即不能显示出有要素类中每个要素的边界(boundy)对象所组成的的图形。然而在单步调试的过程中却可以得到基本正确的结果。原因何在?
原来在利用ScreenDisplay对象画图的过程中,是开启了另一线程。即画图和程序运行不再一个一个主线程中,由于绘图,和渲染事件处理的较慢,需要等待的时间就较长而在处理的过程中FinishDrawing()事件已经发生,所以我们在map中往往只能“感觉到对象”,但是看不到对象。为了教好的解决改该问题我们可以在DrawPolyline()和FinishDrawing()事件之间添加如下代码:
this.axMapControl1.ActiveView.ScreenDisplay.UpdateWindow();
就可以让代码在此处等待,等确实绘制和渲染完成之后才执行FinishDrawing()事件。这样就可以在activeView中得到所需的图形。