private void btnConstructMultiPolygon_Click(object sender, EventArgs e) { if (gfactory == null) gfactory = new GeometryFactory(); multiPolygon = gfactory.CreateGeometry(gviGeometryType.gviGeometryMultiPolygon, gviVertexAttribute.gviVertexAttributeZ) as IMultiPolygon; if (multiPolygon == null) return; //添加第一个polygon IPolygon polygonFirst = gfactory.CreateGeometry(gviGeometryType.gviGeometryPolygon, gviVertexAttribute.gviVertexAttributeZ) as IPolygon; if (polygonFirst == null) return; /// 外环顺时针:1-4-3-2-1 /// 4-------3 /// | | /// | | /// 1-------2 IRing exteriorRing = polygonFirst.ExteriorRing; point = gfactory.CreatePoint(gviVertexAttribute.gviVertexAttributeZ); point.SetCoords(0, 0, 0, 0, 1); exteriorRing.AppendPoint(point); point.SetCoords(0, 200, 0, 0, 2); exteriorRing.AppendPoint(point); point.SetCoords(100, 200, 0, 0, 3); exteriorRing.AppendPoint(point); point.SetCoords(100, 0, 0, 0, 4); exteriorRing.AppendPoint(point); point.SetCoords(0, 0, 0, 0, 5); exteriorRing.AppendPoint(point); //闭合 /// 内环逆时针:1-2-3-4-1 /// 4-------3 /// | | /// | | /// 1-------2 IRing interiorRing = gfactory.CreateGeometry(gviGeometryType.gviGeometryRing, gviVertexAttribute.gviVertexAttributeZ) as IRing; point.SetCoords(25, 25, 0, 0, 1); interiorRing.AppendPoint(point); point.SetCoords(75, 25, 0, 0, 2); interiorRing.AppendPoint(point); point.SetCoords(75, 75, 0, 0, 3); interiorRing.AppendPoint(point); point.SetCoords(25, 75, 0, 0, 4); interiorRing.AppendPoint(point); point.SetCoords(25, 25, 0, 0, 5); interiorRing.AppendPoint(point); //闭合 polygonFirst.AddInteriorRing(interiorRing); //Clone一个新的内环 IRing pInteriorRingNew = interiorRing.Clone() as IRing; polygonFirst.AddInteriorRing(pInteriorRingNew); multiPolygon.AddPolygon(polygonFirst); //Clone一个新的polygon IPolygon pPolygonNew = polygonFirst.Clone() as IPolygon; multiPolygon.AddPolygon(pPolygonNew); MultiSurface geo = new MultiSurface(); // Geometry属性 geo.Dimension = multiPolygon.Dimension; if (multiPolygon.Envelope != null) { geo.MaxX = multiPolygon.Envelope.MaxX; geo.MaxY = multiPolygon.Envelope.MaxY; geo.MaxZ = multiPolygon.Envelope.MaxZ; geo.MinX = multiPolygon.Envelope.MinX; geo.MinY = multiPolygon.Envelope.MinY; geo.MinZ = multiPolygon.Envelope.MinZ; } geo.GeometryType = multiPolygon.GeometryType; geo.IsEmpty = multiPolygon.IsEmpty; geo.IsValid = multiPolygon.IsValid; geo.VertexAttribute = multiPolygon.VertexAttribute; geo.HasId = multiPolygon.HasId(); geo.HasM = multiPolygon.HasM(); geo.HasZ = multiPolygon.HasZ(); // GeometryCollection属性 geo.GeometryCount = multiPolygon.GeometryCount; geo.IsOverlap = multiPolygon.IsOverlap; // MultiSurface属性 geo.Area = multiPolygon.GetArea(); this.propertyGrid1.SelectedObject = geo; }
void axRenderControl1_RcMouseClickSelect(IPickResult PickResult, IPoint IntersectPoint, gviModKeyMask Mask, gviMouseSelectMode EventSender) { try { this.axRenderControl1.FeatureManager.UnhighlightAll(); if (PickResult != null) { if (PickResult.Type == gviObjectType.gviObjectFeatureLayer) { IFeatureLayerPickResult flpr = PickResult as IFeatureLayerPickResult; int fid = flpr.FeatureId; this.axRenderControl1.FeatureManager.HighlightFeature(_featureClass, fid, System.Drawing.Color.Yellow); ////////////////////////////////////////////////////////////////////////// // // GeometryConvert的代码添加在这里 // ////////////////////////////////////////////////////////////////////////// fidList.Clear(); fidList.Add(fid); IRowBuffer rowGC = _featureClass.GetRow(fidList[0]); int nPose = rowGC.FieldIndex("Geometry"); if (nPose == -1) { MessageBox.Show("不存在Geometry列"); return; } // 获取ModelPoint IModelPoint modelPointGC = null; if (rowGC != null) { nPose = rowGC.FieldIndex("Geometry"); IGeometry geo = rowGC.GetValue(nPose) as IGeometry; if (geo.GeometryType == gviGeometryType.gviGeometryModelPoint) { modelPointGC = geo as IModelPoint; } } this.Text = "拾取成功"; // 获取Model string modelName = modelPointGC.ModelName; IModel modelGC = (_featureClass.FeatureDataSet as IResourceManager).GetModel(modelName); // 1、获取MultiTriMesh IGeometryConvertor gc = new GeometryConvertor(); IMultiTriMesh multiTM = gc.ModelPointToTriMesh(modelGC, modelPointGC, false); this.Text = "ModelToTriMesh完成"; if (this.cbCreateRenderTriMesh.Checked) { // 创建RenderTriMesh在三维上显示 for (int i = 0; i < multiTM.GeometryCount; i++) { ITriMesh tm = multiTM.GetGeometry(i) as ITriMesh; IRenderTriMesh rtm = this.axRenderControl1.ObjectManager.CreateRenderTriMesh(tm, null, rootId); //随机颜色填充TriMesh Random randObj = new Random(i); int aColor = randObj.Next(0, 255); int gColor = randObj.Next(0, 255); int rColor = randObj.Next(0, 255); ISurfaceSymbol ss = new SurfaceSymbol(); ss.Color = System.Drawing.Color.FromArgb(rColor, gColor, aColor); rtm.Symbol = ss; rTMeshList.Add(rtm); } } if (this.cbCreateRenderPolygon.Checked) { // 2、获取投影MultiPolygon IMultiPolygon multiPolygon = gc.ProjectTriMeshToPolygon(multiTM, 1.0); this.Text = "MultiTriMeshToFootprint完成。面积:" + multiPolygon.GetArea(); // 创建RenderPolygon在三维上显示 for (int i = 0; i < multiPolygon.GeometryCount; i++) { IPolygon polygon = multiPolygon.GetGeometry(i) as IPolygon; IRenderPolygon rpolygon = this.axRenderControl1.ObjectManager.CreateRenderPolygon(polygon, null, rootId); rPolygonList.Add(rpolygon); } } if (this.cbCreateRenderPolygon2.Checked) { // 3、获取切割MultiPolygon double heightSpec = 0.0, heightIntersect = 0.0; Double.TryParse(IntersectPoint.Z.ToString(), out heightIntersect); fidList.Clear(); fidList.Add(fid); IEnvelope box = _featureClass.GetFeaturesEnvelope(fidList.ToArray(), "Geometry"); heightSpec = System.Math.Abs(heightIntersect - box.MinZ); // 注意:CutTriMeshToPolygon方法的第三个参数为空间分辨率,应该选择合适值。 // 值过大会导致结果不精确,值过小会导致转换时间过长。使用时应设置大小合适的值来平衡精度和效率问题。 IMultiPolygon multiPolygon2 = gc.CutTriMeshToPolygon(multiTM, heightSpec, 0.5); this.Text = "MultiTriMeshToFootprint2完成。面积:" + multiPolygon2.GetArea(); // 创建RenderPolygon在三维上显示 for (int i = 0; i < multiPolygon2.GeometryCount; i++) { IPolygon polygon2 = multiPolygon2.GetGeometry(i) as IPolygon; IRenderPolygon rpolygon2 = this.axRenderControl1.ObjectManager.CreateRenderPolygon(polygon2, null, rootId); rPolygonList2.Add(rpolygon2); } } if (this.cbCreateRenderMulPoint.Checked) { // 4、获取MultiPoint IMultiPoint multiPoint = gc.MultiTriMeshToMultiPoint(multiTM, 3.0); this.Text = "MultiTriMeshToMultiPoint完成。MultiPoint个数为:" + multiPoint.GeometryCount; //创建RenderPoint在三维上显示 IRenderMultiPoint rpoint = this.axRenderControl1.ObjectManager.CreateRenderMultiPoint(multiPoint, null, rootId); rPointList.Add(rpoint); } } } } catch (System.Exception ex) { if (ex.GetType().Name.Equals("UnauthorizedAccessException")) { MessageBox.Show("需要标准runtime授权"); } else { MessageBox.Show(ex.Message); } } }