Newer
Older
GHFX_REFACTOR / FrmAddInstrument.cs
xiaowei on 25 Nov 2016 9 KB 合并1
using System;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using GeoScene.Data;
using GeoScene.Engine;
using GeoScene.Globe;

namespace Cyberpipe
{
    public partial class FrmAddInstrument : Office2007Form
    {
        public GSOPlane3DControl plane3DControl;
        private GSOGlobeControl globeControl1;
        private GSOFeature feature;
        string modelPath;
        public GSOGeoPoint3D point;
        private GSOLayer modelLayer;
        public FrmAddInstrument(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)
            {
                MessageBox.Show("请选中一个附属物!", "提示");
                return;
            }

            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;
            feature = new GSOFeature();
//            GSOFeatureDataset featDataSet = modelLayer.Dataset as GSOFeatureDataset;
//            if (featDataSet == null) return;
//            feature = featDataSet.CreateFeature();
            string bianhao = "FSW" + Guid.NewGuid() + "TMP";
            feature.SetValue("编号", bianhao);
            feature.Geometry = model;
            modelLayer.AddFeature(feature);
            
            if (MessageBox.Show("是否保存到数据库?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                modelLayer.Save();
            }
            globeControl1.Globe.FlyToFeature(feature, 0, 45, 3);
            globeControl1.Refresh();

            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 + @"\雨篦工井模型\";
            DirectoryInfo theFolder = new DirectoryInfo(path);
            if (theFolder.Exists)
            {
                foreach (FileInfo file in theFolder.GetFiles())
                {
                    if (listBoxSize.Items.Contains(file.Name) == false)
                    {
                        if (Path.GetExtension(file.FullName).ToLower() == ".gcm" || Path.GetExtension(file.FullName).ToLower() == ".3ds")
                        {
                            listBoxSize.Items.Add(file.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();
            } 
        }
        /// <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 + @"\雨篦工井模型\" + listBoxSize.Items[listBoxSize.SelectedIndex].ToString().Trim();
                if (File.Exists(path))
                {
                    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();
            }
        }
    }
}