Newer
Older
EMS_REFACTOR / FrmAddPipeFitting .cs
nn-203 on 26 Jul 2017 10 KB first commit
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using GeoScene.Data;
using GeoScene.Engine;
using GeoScene.Globe;

namespace Cyberpipe
{
    public partial class FrmAddPipeFitting : Office2007Form
    {
        public GSOPlane3DControl plane3DControl;
        private GSOGlobeControl globeControl1;
        private GSOFeature feature;
        string modelPath;
        public GSOGeoPoint3D point;
        private GSOLayer modelLayer;
        public FrmAddPipeFitting(GSOGlobeControl ctl, GSOLayer layer)
        {
            InitializeComponent();
            globeControl1 = ctl;
            point = new GSOGeoPoint3D(ctl.Globe.CameraState.Longitude, ctl.Globe.CameraState.Latitude, 0);
            modelLayer = layer;
            plane3DControl = new GSOPlane3DControl();
            panel1.Controls.Add(plane3DControl);
            plane3DControl.Dock = DockStyle.Fill;
        }
        /// <summary>
        /// 添加按钮事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (feature != null)
            {
                if (markerFeature != null)
                {
                    markerFeature.Delete();
                }
                
                GSOPoint3d pt = new GSOPoint3d();
                pt.X = point.X;
                pt.Y = point.Y;
                pt.Z = point.Z;
                GSOGeoModel model = new GSOGeoModel();
                model.FilePath = modelPath;
                model.Position = pt;
                model.AltitudeMode = EnumAltitudeMode.Absolute;
                GSOFeatureDataset featDataSet = modelLayer.Dataset as GSOFeatureDataset;
                if (featDataSet == null) return;
                feature = featDataSet.CreateFeature();
                feature.Geometry = model;
                GSOPoint2d xyzPoint = Utility.Latlon_2_XYZ(pt.X, pt.Y);
                string bianhao = "TZGD"+Guid.NewGuid() + "TMP";
                feature.SetValue("编号", bianhao);
                feature.SetValue("X坐标", xyzPoint.X);
                feature.SetValue("Y坐标", xyzPoint.Y);
                modelLayer.AddFeature(feature);
                if (MessageBox.Show("是否保存到数据库?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    modelLayer.Save();
                }
                globeControl1.Globe.FlyToFeature(feature, 0, 45, 3);
                globeControl1.Refresh();
            }
            else
            {
                MessageBox.Show("请选中一个附属物及其规格!","提示");
                return;
            }
            Close();     
        }
        /// <summary>
        /// 关闭按钮事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
        /// <summary>
        /// 窗体初始化事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmAddPipeFitting_Load(object sender, EventArgs e)
        {
            if (globeControl1 != null)
            {
                globeControl1.MouseClick += globeControl1_MouseClick;
            }
            if (point != null)
            {
                textBoxXLon.Text = point.X.ToString();
                textBoxXLat.Text = point.Y.ToString();

                markerFeature = new GSOFeature();
                GSOGeoMarker marker = new GSOGeoMarker();
                GSOMarkerStyle3D style = new GSOMarkerStyle3D();
                style.IconPath = Application.StartupPath + "/Resource/CrossIcon.png";
                marker.Style = style;
                marker.SetPosition(point.X, point.Y, point.Z);
                marker.AltitudeMode = EnumAltitudeMode.ClampToGround;
                markerFeature.Name = "目标点";
                markerFeature.CustomID = 001;
                markerFeature.Geometry = marker;
                globeControl1.Globe.MemoryLayer.AddFeature(markerFeature);
            }
            string path = Application.StartupPath + @"\管道配件gcm\";
            DirectoryInfo theFolder = new DirectoryInfo(path);
            if (theFolder.Exists)
            {
                foreach (DirectoryInfo NextFolder in theFolder.GetDirectories())
                {
                    listBoxType.Items.Add(NextFolder.Name);
                }
            }
            else
            {
                MessageBox.Show("文件夹【" +path+ "】不存在!","提示");
            }
        }
        GSOFeature markerFeature;
        /// <summary>
        /// 球单击事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void globeControl1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                GSOPoint3d p = globeControl1.Globe.ScreenToScene(e.X, e.Y);
                textBoxXLon.Text = p.X.ToString();
                textBoxXLat.Text = p.Y.ToString();
                point.X = p.X;
                point.Y = p.Y;
                if (markerFeature != null && markerFeature.Geometry != null)
                {
                    GSOGeoMarker marker = markerFeature.Geometry as GSOGeoMarker;
                    marker.SetPosition(point.X, point.Y, point.Z);
                }
                else
                {
                    markerFeature = new GSOFeature();
                    GSOGeoMarker marker = new GSOGeoMarker();
                    GSOMarkerStyle3D style = new GSOMarkerStyle3D();
                    style.IconPath = Application.StartupPath + "/Resource/CrossIcon.png";
                    marker.Style = style;
                    marker.SetPosition(point.X, point.Y, point.Z);
                    marker.AltitudeMode = EnumAltitudeMode.ClampToGround;
                    markerFeature.Name = "目标点";
                    markerFeature.CustomID = 001;
                    markerFeature.Geometry = marker;
                    globeControl1.Globe.MemoryLayer.AddFeature(markerFeature);
                }
                globeControl1.Refresh();
            } 
        }

        List<string> list = new List<string>();
        /// <summary>
        /// listbox选中项改变事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBoxType_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBoxSize.Items.Clear();
            list.Clear();
            string path = Application.StartupPath + @"\管道配件gcm\" + listBoxType.SelectedItem + @"\";
            DirectoryInfo theFolder = new DirectoryInfo(path);
            foreach (FileInfo nextFile in theFolder.GetFiles())
            {
                if (nextFile.Name.IndexOf("3DS") > -1 || nextFile.Name.IndexOf("gcm")>-1)
                {
                    listBoxSize.Items.Add(nextFile.Name.Substring(0,nextFile.Name.IndexOf(".")));
                    list.Add(nextFile.Name);
                }
            }
        }
        /// <summary>
        /// listbox选中项改变事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBoxSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxSize.SelectedIndex != -1)
            {
                string path = Application.StartupPath + @"\\管道配件gcm\" + listBoxType.SelectedItem + "\\" + list[listBoxSize.SelectedIndex];
                GSOGeoModel model = new GSOGeoModel();
                model.FilePath = path;
                modelPath = path;
                model.SetPosition(0 ,0, 0);
                feature = new GSOFeature();
                feature.Geometry = model;
                feature.Geometry.LatLonCoord = false;
                feature.HighLight = false;
                plane3DControl.Plane3DScene.RemoveAllFeature();
                plane3DControl.Plane3DScene.AddFeature(feature);
            }
        }
        /// <summary>
        /// 经度文本框内容改变事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBoxXLon_TextChanged(object sender, EventArgs e)
        {
            if (textBoxXLon.Text.Trim() != "")
            {
                string longitude = textBoxXLon.Text.Trim();
                double dLon = 0.0;
                if (!double.TryParse(longitude, out dLon))
                {
                    textBoxXLon.Text = "";
                }
                else
                {
                    if (dLon > 180)
                    {
                        textBoxXLon.Text = 180 + "";
                        point.X = 180;
                    }
                    else if (dLon < -180)
                    {
                        textBoxXLon.Text = -180 + "";
                        point.X = -180;
                    }
                }
            }
        }
        /// <summary>
        /// 纬度文本框内容改变事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBoxXLat_TextChanged(object sender, EventArgs e)
        {
            if (textBoxXLat.Text.Trim() != "")
            {
                string latitude = textBoxXLat.Text.Trim();
                double dLat = 0.0;
                if (!double.TryParse(latitude, out dLat))
                {
                    textBoxXLat.Text = "";
                }
                else
                {
                    if (dLat > 90)
                    {
                        textBoxXLat.Text = 90 + "";
                        point.Y = 90;
                    }
                    else if (dLat < -90)
                    {
                        textBoxXLat.Text = -90 + "";
                        point.Y = -90;
                    }
                }
            }
        }
        /// <summary>
        /// 窗体关闭事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmAddPipeFitting_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (globeControl1 != null)
            {
                globeControl1.MouseClick -= globeControl1_MouseClick;
            }           
            if (markerFeature != null)
            {
                markerFeature.Delete();
                globeControl1.Refresh();
            }
        }
    }
}