C#Winfrom實現Skyline畫直線功能
前言:
這里記錄了我在學習Skyline二次開發中所遇到的問題,適合剛接觸Skyline二次開發的同學查看使用,從邏輯到代碼逐一詳解,但是還是重在理解,希望對你有所幫助。
1、畫線的邏輯:
讓我回到TerraExplorer Pro這個軟件中嘗試畫一條線,從每一步操作去發現,到底發生了什么?
1.鼠標左鍵在3D窗口中選擇一個點(確定第一個點的位置)。
2.挪動鼠標,在第二個點單擊鼠標左鍵(確定第二個點的位置)。
3.按住鼠標左鍵不放,在3D窗口中挪動地球,松開后發現沒有畫出線,這時左鍵單擊下一個點又畫了一個線。(左鍵選中拖拽不畫線)
4.右鍵單擊取消最后一個點,將上一個點定為線最后的終點(刪除最后一個點位,將倒數第二個點定為線的終點)
嘗試自己去畫一條線很重要,在畫完之后上面這些話你會多少理解一些。
2、畫線的代碼
下面是需要綁定的事件,這個代碼有個小Bug等待你自己去發現
sgworld.OnRButtonUp += Sgworld_OnRButtonUp;//綁定鼠標右擊抬起事件
sgworld.OnLButtonUp += Sgworld_OnLButtonUp;//綁定鼠標左擊抬起事件
sgworld.OnLButtonDown += Sgworld_OnLButtonDown;//綁定鼠標左擊按下事件
sgworld.OnFrame += Sgworld_OnFrame;//綁定實時渲染事件
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;//綁定鼠標右擊抬起事件
sgworld.OnLButtonUp += Sgworld_OnLButtonUp;//綁定鼠標左擊抬起事件
sgworld.OnLButtonDown += Sgworld_OnLButtonDown;//綁定鼠標左擊按下事件
sgworld.OnFrame += Sgworld_OnFrame;//綁定實時渲染事件
}
//鼠標左擊按下事件 獲取屏幕中心點位置
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;
}
//實時渲染事件
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();
}
}
}
//鼠標右擊彈起事件
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);
//判斷如果鼠標單擊畫線按鈕后執行下面
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;
}
//鼠標右擊事件結束畫線,並刪除最后一個點
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;
}
}
}