C#Winfrom实现Skyline画直线功能
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;
}
}
}