溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使用C#Winfrom實現Skyline畫直線功能

發布時間:2021-09-28 11:17:40 來源:億速云 閱讀:182 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關如何使用C#Winfrom實現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;  } }}

由于時間比較緊,本來想一點點分析詳解的,大家可以做參考,也可直接復制,但是最重要的是理解,一個東西理解了才能更好的學習。有什么想法大家可以一起討論學習。

感謝各位的閱讀!關于“如何使用C#Winfrom實現Skyline畫直線功能”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女