SkylineGlobe 6.5 如何实现简单多边形的动态绘制 C#示例代码
在Skyline的TEPro软件中,我们可以很容易地绘制出多边形。
那么,在二次开发过程中,该如何绘制一个简单的多边形呢?
通过下面的示例代码,我们可以很容易完成这一项工作。
其中,重点需要了解Geometry对象的定义和使用。
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using TerraExplorerX; 10 11 namespace HelloSkyline 12 { 13 public partial class CTerrainPolygon: Form 14 { 15 SGWorld65 sgworld = null; 16 String pbhander = ""; 17 ITerrainPolygon65 pITPolygon = null; 18 List<double> ListVerticsArray = new List<double>(); 19 20 public CTerrainPolygon() 21 { 22 InitializeComponent(); 23 24 sgworld = new SGWorld65(); 25 sgworld.OnLButtonDown += new _ISGWorld65Events_OnLButtonDownEventHandler(sgworld_OnLButtonDown); 26 sgworld.OnRButtonDown += new _ISGWorld65Events_OnRButtonDownEventHandler(sgworld_OnRButtonDown); 27 } 28 29 bool sgworld_OnRButtonDown(int Flags, int X, int Y) 30 { 31 sgworld.Window.SetInputMode(MouseInputMode.MI_FREE_FLIGHT); 32 pbhander = ""; 33 pITPolygon = null; 34 return true; 35 } 36 37 bool sgworld_OnLButtonDown(int Flags, int X, int Y) 38 { 39 IWorldPointInfo65 pIWPInfo = sgworld.Window.PixelToWorld(X, Y, WorldPointType.WPT_TERRAIN); 40 IPosition65 pIPosition = sgworld.Navigate.GetPosition(AltitudeTypeCode.ATC_ON_TERRAIN); 41 42 if (pbhander == "TerrainPolygon") 43 { 44 if (pITPolygon == null) 45 { 46 ILinearRing cRing = null; 47 double[] cVerticesArray = null; 48 cVerticesArray = new double[] { 49 pIPosition.X, pIPosition.Y, 0, 50 pIWPInfo.Position.X, pIWPInfo.Position.Y, pIWPInfo.Position.Distance, 51 pIWPInfo.Position.X, pIWPInfo.Position.Y, pIWPInfo.Position.Distance, 52 }; 53 cRing = sgworld.Creator.GeometryCreator.CreateLinearRingGeometry(cVerticesArray); 54 55 uint nLineColor = 0xFF00FF00; 56 uint nFillColor = 0x7FFF0000; 57 AltitudeTypeCode eAltitudeTypeCode = AltitudeTypeCode.ATC_ON_TERRAIN; 58 string gid = CreateGroup("AnalysisTEMP"); 59 pITPolygon = sgworld.Creator.CreatePolygon(cRing, nLineColor, nFillColor, eAltitudeTypeCode, gid, "Polygon"); 60 61 IPolygon polygonGeometry = pITPolygon.Geometry as IPolygon; 62 polygonGeometry.StartEdit(); 63 foreach (ILinearRing ring in polygonGeometry.Rings) 64 { 65 double dx = pIWPInfo.Position.X; 66 double dy = pIWPInfo.Position.Y; 67 double dh = pIWPInfo.Position.Distance; 68 ring.Points.AddPoint(dx, dy, dh); 69 ring.Points.DeletePoint(0); 70 } 71 IGeometry editedGeometry = polygonGeometry.EndEdit(); 72 pITPolygon.Geometry = editedGeometry; 73 } 74 else 75 { 76 IPolygon polygonGeometry = pITPolygon.Geometry as IPolygon; 77 polygonGeometry.StartEdit(); 78 foreach (ILinearRing ring in polygonGeometry.Rings) 79 { 80 double dx = pIWPInfo.Position.X; 81 double dy = pIWPInfo.Position.Y; 82 double dh = pIWPInfo.Position.Distance; 83 ring.Points.AddPoint(dx, dy, dh); 84 } 85 IGeometry editedGeometry = polygonGeometry.EndEdit(); 86 pITPolygon.Geometry = editedGeometry; 87 } 88 } 89 return false; 90 } 91 92 private void button1_Click(object sender, EventArgs e) 93 { 94 pbhander = "TerrainPolygon"; 95 sgworld.Window.SetInputMode(MouseInputMode.MI_COM_CLIENT); 96 } 97 98 private void button2_Click(object sender, EventArgs e) 99 { 100 string gid = sgworld.ProjectTree.FindItem("AnalysisTEMP"); 101 if (gid != null && gid != "") 102 { 103 sgworld.ProjectTree.DeleteItem(gid); 104 } 105 } 106 107 private String CreateGroup(String GroupName) 108 { 109 string gid = sgworld.ProjectTree.FindItem(GroupName); 110 if (gid != null && gid != "") 111 { 112 return gid; 113 } 114 else 115 { 116 return sgworld.ProjectTree.CreateLockedGroup(GroupName); 117 } 118 } 119 } 120 }