這篇文章給大家分享的是有關(guān)如何使用C#Winfrom實(shí)現(xiàn)Skyline畫直線功能的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

1、畫線的邏輯:
讓我回到TerraExplorer Pro這個軟件中嘗試畫一條線,從每一步操作去發(fā)現(xiàn),到底發(fā)生了什么?1.鼠標(biāo)左鍵在3D窗口中選擇一個點(diǎn)(確定第一個點(diǎn)的位置)。2.挪動鼠標(biāo),在第二個點(diǎn)單擊鼠標(biāo)左鍵(確定第二個點(diǎn)的位置)。3.按住鼠標(biāo)左鍵不放,在3D窗口中挪動地球,松開后發(fā)現(xiàn)沒有畫出線,這時左鍵單擊下一個點(diǎn)又畫了一個線。(左鍵選中拖拽不畫線)4.右鍵單擊取消最后一個點(diǎn),將上一個點(diǎn)定為線最后的終點(diǎn)(刪除最后一個點(diǎn)位,將倒數(shù)第二個點(diǎn)定為線的終點(diǎn))
嘗試自己去畫一條線很重要,在畫完之后上面這些話你會多少理解一些。
2、畫線的代碼
下面是需要綁定的事件,這個代碼有個小Bug等待你自己去發(fā)現(xiàn)
sgworld.OnRButtonUp += Sgworld_OnRButtonUp;//綁定鼠標(biāo)右擊抬起事件 sgworld.OnLButtonUp += Sgworld_OnLButtonUp;//綁定鼠標(biāo)左擊抬起事件 sgworld.OnLButtonDown += Sgworld_OnLButtonDown;//綁定鼠標(biāo)左擊按下事件 sgworld.OnFrame += Sgworld_OnFrame;//綁定實(shí)時渲染事件
using System;using System.Windows.Forms;using TerraExplorerX;//引用Skyline的名稱空間namespace Skyline畫線{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } //全局變量 SGWorld701 sgworld; bool Drawline = false; double centerX = 0; double centerY = 0; ITerrainPolyline701 polyline = null; //畫直線按鈕 按鈕的Name為 Drawaline private void Drawaline_Click(object sender, EventArgs e) { Drawline = true; } //窗體加載 private void Form1_Load(object sender, EventArgs e) { sgworld = new SGWorld701(); sgworld.Project.Open("工程路徑"); sgworld.OnRButtonUp += Sgworld_OnRButtonUp;//綁定鼠標(biāo)右擊抬起事件 sgworld.OnLButtonUp += Sgworld_OnLButtonUp;//綁定鼠標(biāo)左擊抬起事件 sgworld.OnLButtonDown += Sgworld_OnLButtonDown;//綁定鼠標(biāo)左擊按下事件 sgworld.OnFrame += Sgworld_OnFrame;//綁定實(shí)時渲染事件 } //鼠標(biāo)左擊按下事件 獲取屏幕中心點(diǎn)位置 private bool Sgworld_OnLButtonDown(int Flags, int X, int Y) { IWorldPointInfo701 centerOfWorld1 = sgworld.Window.CenterPixelToWorld(WorldPointType.WPT_DEFAULT); centerX = centerOfWorld1.Position.X; centerY = centerOfWorld1.Position.Y; return false; } //實(shí)時渲染事件 private void Sgworld_OnFrame() { IMouseInfo701 mouse1= sgworld.Window.GetMouseInfo(); IWorldPointInfo701 worldPointInfo = sgworld.Window.PixelToWorld(mouse1.X, mouse1.Y); if (worldPointInfo != null) { IPosition701 pos = worldPointInfo.Position; if (polyline!=null) { polyline.Geometry.StartEdit(); ((ILineString)polyline.Geometry).Points.DeletePoint( ((ILineString)polyline.Geometry).Points.Count - 1 ); ((ILineString)polyline.Geometry).Points.AddPoint( worldPointInfo.Position.X, worldPointInfo.Position.Y, worldPointInfo.Position.Altitude ); polyline.Geometry.EndEdit(); } } } //鼠標(biāo)右擊彈起事件 private bool Sgworld_OnLButtonUp(int Flags, int X, int Y) { IWorldPointInfo701 centerOfWorld2 = sgworld.Window.CenterPixelToWorld(WorldPointType.WPT_DEFAULT); double centerPointDistance = sgworld.CoordServices.GetDistance(centerOfWorld2.Position.X, centerOfWorld2.Position.Y, centerX, centerY); //判斷如果鼠標(biāo)單擊畫線按鈕后執(zhí)行下面 if (Drawline == true) { IWorldPointInfo701 ipWorldInfor = sgworld.Window.PixelToWorld(X, Y); if (polyline == null) { double dXCoord = ipWorldInfor.Position.X; double dYCoord = ipWorldInfor.Position.Y; double[] array = new double[] { }; array = new double[] { dXCoord, dYCoord, 0, dXCoord, dYCoord, 0, }; ILineString lr = sgworld.Creator.GeometryCreator.CreateLineStringGeometry(array); polyline = sgworld.Creator.CreatePolyline(lr, 0xffffff, AltitudeTypeCode.ATC_TERRAIN_ABSOLUTE, "", ""); } else { if (centerPointDistance==0) { ILineString new_lr = polyline.Geometry as ILineString; new_lr.StartEdit(); new_lr.Points.AddPoint(ipWorldInfor.Position.X, ipWorldInfor.Position.Y, ipWorldInfor.Position.Altitude); new_lr.EndEdit(); } } } return false; } //鼠標(biāo)右擊事件結(jié)束畫線,并刪除最后一個點(diǎn) private bool Sgworld_OnRButtonUp(int Flags, int X, int Y) { if (polyline != null) { polyline.Geometry.StartEdit(); ((ILineString)polyline.Geometry).Points.DeletePoint( ((ILineString)polyline.Geometry).Points.Count - 1 ); polyline.Geometry.EndEdit(); } Drawline = false; polyline = null; return true; } }}
由于時間比較緊,本來想一點(diǎn)點(diǎn)分析詳解的,大家可以做參考,也可直接復(fù)制,但是最重要的是理解,一個東西理解了才能更好的學(xué)習(xí)。有什么想法大家可以一起討論學(xué)習(xí)。
感謝各位的閱讀!關(guān)于“如何使用C#Winfrom實(shí)現(xiàn)Skyline畫直線功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
文章名稱:如何使用C#Winfrom實(shí)現(xiàn)Skyline畫直線功能-創(chuàng)新互聯(lián)
鏈接URL:http://chinadenli.net/article10/ecddo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司、云服務(wù)器、網(wǎng)站維護(hù)、軟件開發(fā)、服務(wù)器托管、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容