C# Winfrom實(shí)現(xiàn)Skyline畫直線功能的示例代碼
前言:
這里記錄了我在學(xué)習(xí)Skyline二次開(kāi)發(fā)中所遇到的問(wèn)題,適合剛接觸Skyline二次開(kāi)發(fā)的同學(xué)查看使用,從邏輯到代碼逐一詳解,但是還是重在理解,希望對(duì)你有所幫助。
1、畫線的邏輯:
讓我回到TerraExplorer Pro這個(gè)軟件中嘗試畫一條線,從每一步操作去發(fā)現(xiàn),到底發(fā)生了什么?
1.鼠標(biāo)左鍵在3D窗口中選擇一個(gè)點(diǎn)(確定第一個(gè)點(diǎn)的位置)。
2.挪動(dòng)鼠標(biāo),在第二個(gè)點(diǎn)單擊鼠標(biāo)左鍵(確定第二個(gè)點(diǎn)的位置)。
3.按住鼠標(biāo)左鍵不放,在3D窗口中挪動(dòng)地球,松開(kāi)后發(fā)現(xiàn)沒(méi)有畫出線,這時(shí)左鍵單擊下一個(gè)點(diǎn)又畫了一個(gè)線。(左鍵選中拖拽不畫線)
4.右鍵單擊取消最后一個(gè)點(diǎn),將上一個(gè)點(diǎn)定為線最后的終點(diǎn)(刪除最后一個(gè)點(diǎn)位,將倒數(shù)第二個(gè)點(diǎn)定為線的終點(diǎn))
嘗試自己去畫一條線很重要,在畫完之后上面這些話你會(huì)多少理解一些。
2、畫線的代碼
下面是需要綁定的事件,這個(gè)代碼有個(gè)小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í)時(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í)時(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í)時(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é)束畫線,并刪除最后一個(gè)點(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; } } }
由于時(shí)間比較緊,本來(lái)想一點(diǎn)點(diǎn)分析詳解的,大家可以做參考,也可直接復(fù)制,但是最重要的是理解,一個(gè)東西理解了才能更好的學(xué)習(xí)。有什么想法大家可以一起討論學(xué)習(xí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- c# 播放聲音的四種方法
- C#實(shí)現(xiàn)用于操作wav聲音文件的類實(shí)例
- 教你如何用C#制作文字轉(zhuǎn)換成聲音程序
- C#實(shí)現(xiàn)通過(guò)winmm.dll控制聲音播放的方法
- C# winform中窗口關(guān)閉按鈕的隱藏與禁用詳解
- C# WinForm-Timer控件的使用
- C#用Topshelf創(chuàng)建Windows服務(wù)的步驟分享
- C# Winform中如何繪制動(dòng)畫示例詳解
- C# Winform調(diào)用百度接口實(shí)現(xiàn)人臉識(shí)別教程(附源碼)
- C# Winform程序?qū)崿F(xiàn)防止多開(kāi)的方法總結(jié)【親測(cè)】
- C#調(diào)用Win32的API函數(shù)--User32.dll
- c# 通過(guò)WinAPI播放PCM聲音
相關(guān)文章
c#中的浮點(diǎn)型轉(zhuǎn)整形的舍取 四舍五入和銀行家舍入實(shí)現(xiàn)代碼
c#中的浮點(diǎn)型轉(zhuǎn)整形的舍取 四舍五入和銀行家舍入實(shí)現(xiàn)代碼,學(xué)習(xí)c#的朋友可以參考下2012-03-03C#實(shí)現(xiàn)連接SQL Server2012數(shù)據(jù)庫(kù)并執(zhí)行SQL語(yǔ)句的方法
這篇文章主要介紹了C#實(shí)現(xiàn)連接SQL Server2012數(shù)據(jù)庫(kù)并執(zhí)行SQL語(yǔ)句的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了C#連接SQL Server2012數(shù)據(jù)庫(kù)并執(zhí)行查詢、插入等操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-10-10C#實(shí)現(xiàn)將Email地址轉(zhuǎn)成圖片顯示的方法
這篇文章主要介紹了C#實(shí)現(xiàn)將Email地址轉(zhuǎn)成圖片顯示的方法,涉及C#操作圖片的相關(guān)技巧,需要的朋友可以參考下2015-06-06C#實(shí)現(xiàn)在控制臺(tái)輸出當(dāng)前系統(tǒng)時(shí)間的方法
這篇文章主要介紹了C#實(shí)現(xiàn)在控制臺(tái)輸出當(dāng)前系統(tǒng)時(shí)間的方法,涉及C#時(shí)間函數(shù)DateTime.Now的使用方法,需要的朋友可以參考下2015-04-04C#WinFrom導(dǎo)出Excel過(guò)程解析
這篇文章主要介紹了C#WinFrom導(dǎo)出Excel過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11