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;
}
}
}

由于时间比较紧,本来想一点点分析详解的,大家可以做参考,也可直接复制,但是最重要的是理解,一个东西理解了才能更好的学习。有什么想法大家可以一起讨论学习。

2019年12月30日20:23:04

C#Winfrom实现Skyline画直线功能的更多相关文章

  1. canvas学习-----画直线

    画布 1.添加canvas标签  可以通过CSS或者JS来设置canvs标签的width,height;Ps: <canvas id="cvs"></canvas ...

  2. MFC画线功能总结

    本文仅用于学习交流,商业用途请支持正版!转载请注明:http://www.cnblogs.com/mxbs/p/6216464.html MFC画线功能要点有二:其一,鼠标按下时记录初始位置为线的起始 ...

  3. MFC消息映射机制以及画线功能实现

    ---此仅供用于学习交流,切勿用于商业用途,转载请注明http://www.cnblogs.com/mxbs/p/6213404.html. 利用VS2010创建一个单文档标准MFC工程,工程名为Dr ...

  4. 《图形学》实验五:改进的Bresenham算法画直线

    开发环境: VC++6.0,OpenGL 实验内容: 使用改进的Bresenham算法画直线. 实验结果: 代码: //中点Bresenham算法生成直线 #include <gl/glut.h ...

  5. 《图形学》实验四:中点Bresenham算法画直线

    开发环境: VC++6.0,OpenGL 实验内容: 使用中点Bresenham算法画直线. 实验结果: 代码: //中点Bresenham算法生成直线 #include <gl/glut.h& ...

  6. 《图形学》实验三:DDA算法画直线

    开发环境: VC++6.0,OpenGL 实验内容: 使用DDA算法画直线. 实验结果: 代码: #include <gl/glut.h> #include <math.h> ...

  7. Bresenham画直线,任意斜率

    function DrawLineBresenham(x1,y1,x2,y2) %sort by x,sure x1<x2. if x1>x2 tmp=x1; x1=x2; x2=tmp; ...

  8. 几种画直线的方法-孙鑫C++笔记

    // HDC画直线 CPoint m_ptOrigin ; void CDrawView::OnLButtonDown(UINT nFlags, CPoint point) { m_ptOrigin ...

  9. 1.1.2-学习Opencv与MFC混合编程之---画图工具 画直线 画圆 画矩形

    源代码地址:http://download.csdn.net/detail/nuptboyzhb/3961685 画图工具 1.     画直线 Ø  增加‘直线’菜单项,建立类向导: Ø  对CXX ...

随机推荐

  1. Linux 服务器作为Nginx web服务器常见优化参数

    内核参数调整cat /etc/sysctl.conf# sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/ ...

  2. 高次arccos积分

    \[\Large\displaystyle \int_0^{1} \frac{\arccos^4 \left(x^2\right)}{\sqrt{1-x^2}}\,\mathrm{d}x\] \(\L ...

  3. C语言与汇编的嵌入式编程:统计字符串中各字符出现的次数

    原始C语言: #include<stdio.h> void main(){ ]; char pipei[] = "abcdefghijklmnopqrstuvwxyz" ...

  4. mysql事务管理及spring声明式事务中主动异常抛出使数据库回滚

    mysql的引擎常用的有两个,一个MyISAM,另一个是InnoDB,mysql默认的为MyISAM,而InnoDB才是支持事务的.所以一般需要修改下,如何修改就不说了. 事务需要依赖数据库,好久没使 ...

  5. vue生命周期中update的具体用法

    在页面上 改变元数据data中数据,并且导致页面重新渲染时,才会进入update周期

  6. NPC脚本界面自定义美化参数说明

    觉得NPC对话界面太单调了 可以自己定义: 在[@main]下面加上 #ACT OPENMERCHANTBIGDLG  参数(WIL文件序号 图片序号 是否可以移动(0,1) 显示位置(0=左上角,1 ...

  7. C语言:计算并输出给定10个数的方差。

    //计算并输出给定10个数的方差. #include<math.h> #include<stdio.h> ]) { double p = 0.0,f=0.0,g=0.0; ; ...

  8. Windows Server 2012 R2 自动映射公共网络驱动器

    1.创建组织单位,在组织单位新建用户或者组 2.新建文件夹(名字无所谓,我这里起名一样方便测试) 3.对文件夹开启共享,设置要共享的用户或者组 4.打开组策略,找到对应的组织单位 5.创建GPO 6. ...

  9. 例题3_4 猜数字游戏的提示(UVa340)

    实现一个经典“猜数字”游戏.给定答案序列和用户猜的序列,统计有多少数字位置正确(A),有多少数字在两个序列都出现过但位置不对(B). 输入包含多组数据.每组输入第一行为序列长度n,第二行是答案序列,接 ...

  10. Django框架之ORM对表结构操作

    ORM的优点:(1)简单,不用自己写SQL语句 (2)开发效率高 ORM的缺点:对于不同的人写的代码,执行效率有差别 ORM的对应关系: 类  ---------->  数据表 对象------ ...