前言:
成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供寧蒗網(wǎng)站建設(shè)、寧蒗做網(wǎng)站、寧蒗網(wǎng)站設(shè)計(jì)、寧蒗網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、寧蒗企業(yè)網(wǎng)站模板建站服務(wù),10多年寧蒗做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
這里記錄了我在學(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í)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
名稱欄目:C#Winfrom實(shí)現(xiàn)Skyline畫直線功能的示例代碼
文章分享:http://www.chinadenli.net/article44/ispcee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、網(wǎng)站維護(hù)、虛擬主機(jī)、App設(shè)計(jì)、網(wǎng)站排名、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)