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. python的爬虫小入门

    爬虫的相关操作 1.爬文本内容 # coding=gbk import requests ##声明相关库 import re response=requests.get('http://duanziw ...

  2. Catalyst 6500/6000 Switches ARP or CAM Table

    译:https://www.cisco.com/c/en/us/support/docs/switches/catalyst-6500-series-switches/71079-arp-cam-ta ...

  3. 【PAT甲级】1069 The Black Hole of Numbers (20 分)

    题意: 输入一个四位的正整数N,输出每位数字降序排序后的四位数字减去升序排序后的四位数字等于的四位数字,如果数字全部相同或者结果为6174(黑洞循环数字)则停止. trick: 这道题一反常态的输入的 ...

  4. BZOJ5319/LOJ2551「JSOI2018」列队

    问题描述 作为一名大学生,九条可怜在去年参加了她人生中的最后一次军训. 军训中的一个重要项目是练习列队,为了训练学生,教官给每一个学生分配了一个休息位置.每次训练开始前,所有学生都在各自的休息位置休息 ...

  5. canvas的beginPath和closePath

    https://www.cnblogs.com/xuehaoyue/p/6549682.html https://segmentfault.com/a/1190000010330319 https:/ ...

  6. Unable to create a debugging engine.

    用QT Creator调试的时候报如下错误: Unable to create a debugging engine. QT里面打开Tools -> Options -> Kits 发现D ...

  7. spark实验(二)--eclipse安装scala环境(2)

    此次在eclipse中的安装参考这篇博客https://blog.csdn.net/lzxlfly/article/details/80728772 Help->Eclipse Marketpl ...

  8. logback.xml设置mogodb日志打印控制台

    <logger name="org.springframework.data.mongodb.core" level="DEBUG"/>

  9. bootstrap与vue,react的区别

    链接(与Vue区别):https://www.php.cn/faq/423095.html 链接(BootStrap, React, Vue的比较):https://www.jianshu.com/p ...

  10. xadmin 后台管理

    xadmin后台管理 安装:luffy虚拟环境下 >: pip install https://codeload.github.com/sshwsfc/xadmin/zip/django2 注册 ...