zoukankan      html  css  js  c++  java
  • C#Winfrom实现Skyline画直线功能

    C#Winfrom实现Skyline画直线功能

    前言:

    这里记录了我在学习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

  • 相关阅读:
    【JZOJ4928】【NOIP2017提高组模拟12.18】A
    【JZOJ4922】【NOIP2017提高组模拟12.17】环
    【JZOJ4923】【NOIP2017提高组模拟12.17】巧克力狂欢
    【JZOJ4924】【NOIP2017提高组模拟12.17】向再见说再见
    【JZOJ4919】【NOIP2017提高组模拟12.10】神炎皇
    【JZOJ4920】【NOIP2017提高组模拟12.10】降雷皇
    【JZOJ4921】【NOIP2017提高组模拟12.10】幻魔皇
    【罗宾欺诈者】回环符文——回文树(回文自动机)
    【怪物】KMP畸形变种——扩展KMP
    【51NOD1304】字符串的相似度
  • 原文地址:https://www.cnblogs.com/alannever/p/12199590.html
Copyright © 2011-2022 走看看