1. 程式人生 > 程式設計 >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 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,dXCoord,};
      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 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;
  }
 }
}

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。