C # winfrom implementation of skyline drawing line function example code

Time:2021-5-9

preface:

Here is a record of the problems I encountered in learning the secondary development of skyline. It is suitable for students who have just come into contact with the secondary development of skyline to view and use it. I will explain the logic and code one by one, but still focus on understanding. I hope it will help you.

1. The logic of drawing lines:

Let me go back to terraexplorer pro and try to draw a line to find out what happened from every step?
1. Click the left mouse button to select a point in the 3D window (determine the position of the first point).
2. Move the mouse and click the left mouse button at the second point (determine the position of the second point).
3. Hold down the left mouse button and move the earth in the 3D window. When you release it, you find that there is no line drawn. At this time, click the next point and draw another line( Left click to select (drag without drawing line)
4. Right click to cancel the last point and set the last point as the end point of the line (delete the last point and set the penultimate point as the end point of the line)

It’s important to try to draw a line by yourself. After drawing, you will understand some of the above words.

2. Line drawing code

Here are the events that need to be bound. This code has a small bug waiting for you to find

sgworld.OnRButtonUp += Sgworld_ OnRButtonUp;// Binding right click up event
 sgworld.OnLButtonUp += Sgworld_ OnLButtonUp;// Bind left mouse click up event
 sgworld.OnLButtonDown += Sgworld_ OnLButtonDown;// Bind left click press event
 sgworld.OnFrame += Sgworld_ OnFrame;// Binding real time rendering events
using System;
using System.Windows.Forms;
using TerraExplorerX;// A namespace that references skyline

Space skyline
{
 public partial class Form1 : Form
 {
  public Form1()
  {
   InitializeComponent();
  }
  //Global variables
  SGWorld701 sgworld;
  bool Drawline = false;
  double centerX = 0;
  double centerY = 0;
  ITerrainPolyline701 polyline = null;

  //The name of the draw line button is draw line
  private void Drawaline_Click(object sender, EventArgs e)
  {
   Drawline = true;
  }
  //Form loading
  private void Form1_Load(object sender, EventArgs e)
  {
   sgworld = new SGWorld701();
   Sgworld. Project. Open ("project path");
   sgworld.OnRButtonUp += Sgworld_ OnRButtonUp;// Binding right click up event
   sgworld.OnLButtonUp += Sgworld_ OnLButtonUp;// Bind left mouse click up event
   sgworld.OnLButtonDown += Sgworld_ OnLButtonDown;// Bind left click press event
   sgworld.OnFrame += Sgworld_ OnFrame;// Binding real time rendering events
  }
  
  //Click the left mouse button to get the position of the center point on the screen
  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;
  }
  //Real time rendering events 
  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();

    }
   }
  }


  //Right click bounce event 
  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);
   //Judge if the mouse click the draw line button to execute the following steps
   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;
  }
  //Right click the event to finish drawing a line and delete the last point
  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;
  }
 }
}

Due to the tight time, I wanted to analyze and explain it in detail. You can make reference or copy it directly, but the most important thing is to understand. Only when you understand something, can you learn it better. If you have any ideas, you can discuss them together.

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.

Recommended Today

Looking for frustration 1.0

I believe you have a basic understanding of trust in yesterday’s article. Today we will give a complete introduction to trust. Why choose rust It’s a language that gives everyone the ability to build reliable and efficient software. You can’t write unsafe code here (unsafe block is not in the scope of discussion). Most of […]