C# Winfrom實(shí)現(xiàn)Skyline畫(huà)直線功能的示例代碼
前言:
這里記錄了我在學(xué)習(xí)Skyline二次開(kāi)發(fā)中所遇到的問(wèn)題,適合剛接觸Skyline二次開(kāi)發(fā)的同學(xué)查看使用,從邏輯到代碼逐一詳解,但是還是重在理解,希望對(duì)你有所幫助。

1、畫(huà)線的邏輯:
讓我回到TerraExplorer Pro這個(gè)軟件中嘗試畫(huà)一條線,從每一步操作去發(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)有畫(huà)出線,這時(shí)左鍵單擊下一個(gè)點(diǎn)又畫(huà)了一個(gè)線。(左鍵選中拖拽不畫(huà)線)
4.右鍵單擊取消最后一個(gè)點(diǎn),將上一個(gè)點(diǎn)定為線最后的終點(diǎn)(刪除最后一個(gè)點(diǎn)位,將倒數(shù)第二個(gè)點(diǎn)定為線的終點(diǎn))
嘗試自己去畫(huà)一條線很重要,在畫(huà)完之后上面這些話你會(huì)多少理解一些。
2、畫(huà)線的代碼
下面是需要綁定的事件,這個(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畫(huà)線
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//全局變量
SGWorld701 sgworld;
bool Drawline = false;
double centerX = 0;
double centerY = 0;
ITerrainPolyline701 polyline = null;
//畫(huà)直線按鈕 按鈕的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)單擊畫(huà)線按鈕后執(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é)束畫(huà)線,并刪除最后一個(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)畫(huà)示例詳解
- 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)文章
Unity實(shí)現(xiàn)攻擊范圍檢測(cè)并繪制檢測(cè)區(qū)域
這篇文章主要介紹了Unity實(shí)現(xiàn)攻擊范圍檢測(cè)并繪制檢測(cè)區(qū)域,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
C#實(shí)現(xiàn)程序開(kāi)機(jī)啟動(dòng)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)程序開(kāi)機(jī)啟動(dòng)的方法,涉及C#針對(duì)應(yīng)用程序及注冊(cè)表的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
C#中將DataTable轉(zhuǎn)化成List<T>的方法解析
大家應(yīng)該都知道在.net項(xiàng)目中使用到DataTable和List<T>集合的地方較多,有的時(shí)候需要將DataTable轉(zhuǎn)化成List<T>,那么改如何轉(zhuǎn)化呢?下面通過(guò)這篇文章來(lái)一起學(xué)習(xí)下吧,本文中給出了詳細(xì)的示例代碼,相信對(duì)大家的理解和學(xué)習(xí)具有一定的參考借鑒價(jià)值。2016-12-12
C#線程開(kāi)發(fā)之System.Thread類詳解
本文詳細(xì)講解了C#線程開(kāi)發(fā)之System.Thread類,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05

