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); } } }
private void btnAnalyse_Click(object sender, EventArgs e) { try { ClearRes(); WaitForm.Start("正在分析...", "请稍后"); if (this._rLine == null) { return; } List <DF3DFeatureClass> list = DF3DFeatureClassManager.Instance.GetAllFeatureClass(); if (list == null || list.Count == 0) { return; } IPolyline line = this._rLine.GetFdeGeometry() as IPolyline; ILine l = this._geoFactory.CreateGeometry(gviGeometryType.gviGeometryLine, gviVertexAttribute.gviVertexAttributeZ) as ILine; l.StartPoint = line.StartPoint; l.EndPoint = line.EndPoint; ISpatialFilter filter = new SpatialFilter(); filter.SpatialRel = gviSpatialRel.gviSpatialRelEnvelope; filter.GeometryField = "Geometry"; List <InterPt> listInterPts = new List <InterPt>(); foreach (DF3DFeatureClass dffc in list) { IFeatureClass fc = dffc.GetFeatureClass(); if (fc == null) { continue; } IFeatureLayer fl = dffc.GetFeatureLayer(); if (fl != null) { if (fl.VisibleMask == gviViewportMask.gviViewNone) { continue; } } int indexGeo = fc.GetFields().IndexOf("Geometry"); if (indexGeo == -1) { continue; } filter.Geometry = l; IFdeCursor cursor = null; IRowBuffer row = null; try { cursor = fc.Search(filter, false); while ((row = cursor.NextRow()) != null) { if (!row.IsNull(indexGeo)) { IGeometry geo = row.GetValue(indexGeo) as IGeometry; if (geo.GeometryType == gviGeometryType.gviGeometryModelPoint) { IModelPoint modelPoint = geo as IModelPoint; IModel model = (fc.FeatureDataSet as IResourceManager).GetModel(modelPoint.ModelName); IGeometryConvertor gc = new GeometryConvertor(); IMultiTriMesh triMesh = gc.ModelPointToTriMesh(model, modelPoint, false); if (triMesh != null) { IVector3 v3 = triMesh.LineSegmentIntersect(l); if (v3 != null) { IPoint pttemp = this._geoFactory.CreatePoint(gviVertexAttribute.gviVertexAttributeZ); pttemp.X = v3.X; pttemp.Y = v3.Y; pttemp.Z = v3.Z; double dis = (pttemp.X - line.StartPoint.X) * (pttemp.X - line.StartPoint.X) + (pttemp.Y - line.StartPoint.Y) * (pttemp.Y - line.StartPoint.Y) + (pttemp.Z - line.StartPoint.Z) * (pttemp.Z - line.StartPoint.Z); InterPt ip = new InterPt(); ip.pt = pttemp; ip.dis = dis; listInterPts.Add(ip); } } } } } } catch (Exception ex) { } finally { if (row != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(row); row = null; } if (cursor != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(cursor); cursor = null; } } } if (listInterPts.Count > 0) { this._rLine.VisibleMask = gviViewportMask.gviViewNone; listInterPts.Sort(new CmpInterPt()); IPoint pt = listInterPts[0].pt; IPolyline lineStart = this._geoFactory.CreateGeometry(gviGeometryType.gviGeometryPolyline, gviVertexAttribute.gviVertexAttributeZ) as IPolyline; lineStart.AppendPoint(line.StartPoint); lineStart.AppendPoint(pt); ICurveSymbol csS = new CurveSymbolClass(); csS.Color = 0xff0000ff; csS.Width = -2; IRenderPolyline rLineStart = this._3DControl.ObjectManager.CreateRenderPolyline(lineStart, csS, this._3DControl.ProjectTree.RootID); this._listRes.Add(rLineStart.Guid); IPolyline lineEnd = this._geoFactory.CreateGeometry(gviGeometryType.gviGeometryPolyline, gviVertexAttribute.gviVertexAttributeZ) as IPolyline; lineEnd.AppendPoint(line.EndPoint); lineEnd.AppendPoint(pt); ICurveSymbol csE = new CurveSymbolClass(); csE.Color = 0xffff0000; csE.Width = -2; IRenderPolyline rLineEnd = this._3DControl.ObjectManager.CreateRenderPolyline(lineEnd, csE, this._3DControl.ProjectTree.RootID); this._listRes.Add(rLineEnd.Guid); } else { this._rLine.VisibleMask = gviViewportMask.gviViewNone; ICurveSymbol csS = new CurveSymbolClass(); csS.Color = 0xff0000ff; csS.Width = -2; IRenderPolyline rLine = this._3DControl.ObjectManager.CreateRenderPolyline(line, csS, this._3DControl.ProjectTree.RootID); this._listRes.Add(rLine.Guid); } } catch (Exception ex) { } finally { WaitForm.Stop(); } }
void axRenderControl1_RcMouseClickSelect(IPickResult PickResult, IPoint IntersectPoint, gviModKeyMask Mask, gviMouseSelectMode EventSender) { try { 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; } // 获取polygon IPolygon polygonGC = null; if (rowGC != null) { nPose = rowGC.FieldIndex("Geometry"); IGeometry geo = rowGC.GetValue(nPose) as IGeometry; if (geo.GeometryType == gviGeometryType.gviGeometryPolygon) { polygonGC = geo as IPolygon; } } this.Text = "拾取成功"; //第一个Tab页:ExtrudePolygonToModel if (this.tabControl1.SelectedIndex == 0) { // 1.调接口构造模型 IGeometryConvertor gc = new GeometryConvertor(); gviRoofType rooftype = gviRoofType.gviRoofFlat; switch (this.comboxRoofType.Text) { case "Flat": rooftype = gviRoofType.gviRoofFlat; break; case "Gable": rooftype = gviRoofType.gviRoofGable; break; case "Hip": rooftype = gviRoofType.gviRoofHip; break; } string imgPath = (strMediaPath + @"\dds"); string roof = this.comboBoxRoofTexture.Text; string facade = this.comboBoxFacadeTexture.Text; IModelPoint mp = null; IModel model = null; if (!gc.ExtrudePolygonToModel(polygonGC, int.Parse(this.numFloorNumber.Value.ToString()), double.Parse(this.numFloorHeight.Value.ToString()), double.Parse(this.numSlopeAngle.Value.ToString()), rooftype, facade, roof, out mp, out model)) { MessageBox.Show("拉体块出错!"); return; } //2、将模型及贴图写入osg文件 string modelName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".osg");//输出osg文件路径 IResourceFactory resFactory = new ResourceFactory(); string[] imageNames = model.GetImageNames(); IPropertySet ps = new PropertySet(); for (int i = 0; i < imageNames.Length; i++) { string imgName = imageNames[i]; IImage img = resFactory.CreateImageFromFile(string.Format(@"{0}\{1}", imgPath + @"\facade", imgName)); if (img == null) { img = resFactory.CreateImageFromFile(string.Format(@"{0}\{1}", imgPath + @"\roof", imgName)); } ps.SetProperty(imgName, img); } model.WriteFile(modelName, ps); //3、测试显示模型 mp.ModelName = modelName; IRenderModelPoint rmp = this.axRenderControl1.ObjectManager.CreateRenderModelPoint(mp, null, rootId); rmp.MouseSelectMask = gviViewportMask.gviViewNone; rmp.MaxVisibleDistance = 100000; this.axRenderControl1.Camera.LookAtEnvelope(mp.Envelope);//飞入 mpList.Add(rmp); } //第二个tab页:ExtrudePolygonToTriMesh else { // 1.调接口构造模型 IGeometryConvertor gc = new GeometryConvertor(); ITriMesh tm = gc.ExtrudePolygonToTriMesh(polygonGC, double.Parse(this.numHeight.Value.ToString()), true); if (tm == null) { MessageBox.Show("拉体块出错!"); return; } //2、显示三角面 //---- 渲染样式不是必须的 ----- ISurfaceSymbol surfaceSym = new SurfaceSymbol(); surfaceSym.Color = System.Drawing.Color.Red; surfaceSym.EnableLight = true; ICurveSymbol curveSym = new CurveSymbol(); curveSym.Color = System.Drawing.Color.Yellow; curveSym.Width = 10; surfaceSym.BoundarySymbol = curveSym; //---- ------------------ ----- IRenderTriMesh rmp = this.axRenderControl1.ObjectManager.CreateRenderTriMesh(tm, surfaceSym, rootId); rmp.MouseSelectMask = gviViewportMask.gviViewNone; rmp.MaxVisibleDistance = 100000; this.axRenderControl1.Camera.LookAtEnvelope(rmp.Envelope);//飞入 tmList.Add(rmp); } } } } catch (System.Exception ex) { if (ex.GetType().Name.Equals("UnauthorizedAccessException")) { MessageBox.Show("需要标准runtime授权"); } else { MessageBox.Show(ex.Message); } } }
public void SelectFeaturesFromBaseLyr(IPolyline polyLine) { btn_analyse.Enabled = false; btn_createLine.Enabled = false; this.btnFlyToSourcePoint.Enabled = false; this.btnFlyToTargetPoint.Enabled = false; IFdeCursor cursor = null; try { this.dataGridView1.Rows.Clear(); axRenderControl1.FeatureManager.UnhighlightAll(); IRowBuffer row = null; int index = 0; List <IRowBuffer> list = new List <IRowBuffer>(); this.Text = "开始粗查询"; foreach (IFeatureClass fc in fcMap.Keys) { ISpatialFilter filter = new SpatialFilter(); filter.Geometry = polyline; filter.SpatialRel = gviSpatialRel.gviSpatialRelEnvelope; filter.GeometryField = "Geometry"; cursor = fc.Search(filter, false); while ((row = cursor.NextRow()) != null) { list.Add(row); } this.Text = "开始细查询"; foreach (IRowBuffer r in list) { index++; int geometryIndex = -1; geometryIndex = r.FieldIndex("Geometry"); if (geometryIndex != -1) { IModelPoint modelPoint = r.GetValue(geometryIndex) as IModelPoint; IModel model = (fc.FeatureDataSet as IResourceManager).GetModel(modelPoint.ModelName); IGeometryConvertor gc = new GeometryConvertor(); this.Text = "正在计算第" + index.ToString() + "个IMultiTriMesh是否与线相交"; IMultiTriMesh triMesh = gc.ModelPointToTriMesh(model, modelPoint, false); if (triMesh != null) { ILine l = geoFactory.CreateGeometry(gviGeometryType.gviGeometryLine, gviVertexAttribute.gviVertexAttributeZ) as ILine; l.StartPoint = polyline.StartPoint; l.EndPoint = polyline.EndPoint; IVector3 v3 = triMesh.LineSegmentIntersect(l); if (v3 != null) { string fid = ""; string fName = ""; string groupId = ""; IEnvelope env = null; int fidPos = r.FieldIndex(fc.FidFieldName); axRenderControl1.FeatureManager.HighlightFeature(fc, int.Parse(r.GetValue(fidPos).ToString()), System.Drawing.Color.Red); for (int i = 0; i < r.FieldCount; i++) { string fieldName = r.Fields.Get(i).Name; if (r.Fields.Get(i).Name == "oid") { fid = r.GetValue(i).ToString(); } else if (r.Fields.Get(i).Name == "groupid") { groupId = r.GetValue(i).ToString(); } else if (r.Fields.Get(i).Name == "Name") { fName = r.GetValue(i).ToString(); } else if (r.Fields.Get(i).Name == "Geometry") { IGeometry geometry = r.GetValue(i) as IModelPoint; env = geometry.Envelope; } } RowObject ro = new RowObject() { FID = fid, GroupId = groupId, Name = fName, FeatureClass = fc, Envelop = env }; if (!rowMap.ContainsKey(ro.FID)) { rowMap.Add(ro.FID, ro); } } } } } // end of foreach (IRowBuffer r in list) } // end of foreach (IFeatureClass fc in fcMap.Keys) this.Text = "通视分析完成!"; LoadGridView(); } catch (Exception ex) { if (ex.GetType().Name.Equals("UnauthorizedAccessException")) { MessageBox.Show("需要标准runtime授权"); } else { MessageBox.Show(ex.Message); } } finally { this.btn_analyse.Enabled = true; this.btn_createLine.Enabled = true; this.btnFlyToSourcePoint.Enabled = true; this.btnFlyToTargetPoint.Enabled = true; if (cursor != null) { //Marshal.ReleaseComObject(cursor); cursor = null; } } }