Newer
Older
GHFX_REFACTOR / FrmAddSinMark.cs
xiaowei on 28 Nov 2016 21 KB 增加标识器管理
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using GeoScene.Globe;
using GeoScene.Data;
using GeoScene.Engine;
using System.Runtime.InteropServices;
using System.Data.OracleClient;

namespace Cyberpipe
{
    public partial class FrmAddSinMark : Form
    {
        public GSOPlane3DControl plane3DControl;
        private GSOGlobeControl globeControl1;
        private GSOFeature feature;
        GSOFeature markerFeature = null;
        public GSOGeoPoint3D point;
        List<string> bhList = new List<string>();
        string modelPath;
        private GSOLayer modelLayer;
        string modelObj="TRO7红色.gcm";
        double xValue;
        double yValue;
        List<string> list = new List<string>();

        public FrmAddSinMark(GSOGlobeControl ctl)
        {
            InitializeComponent();     
            this.globeControl1 = ctl; 
            modelLayer =  globeControl1.Globe.Layers.GetLayerByCaption("标识器");
            this.point = new GSOGeoPoint3D(ctl.Globe.CameraState.Longitude, ctl.Globe.CameraState.Latitude, 0);

            plane3DControl = new GSOPlane3DControl();
            panel1.Controls.Add(plane3DControl);
            plane3DControl.Dock = DockStyle.Fill;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox3.Text == "")
                {
                    MessageBox.Show("请填写标识器编号!", "提示");
                    return;
                }
                else
                {
                    string sql = "select count(*) from 标识器 t where t.编号='" + textBox3.Text.ToString().Trim() + "'";
                    int count = int.Parse(OracleUtils.ExecuteScalar(OracleUtils.ConnectionString, CommandType.Text, sql).ToString());
                    if (count > 0)
                    {
                        MessageBox.Show("该标识器编号已存在!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }

                if (textBox1.Text == "")
                {
                    MessageBox.Show("请填写标识器ID号!", "警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);

                    return;
                }

                if (textBox4.Text == "")
                {
                    MessageBox.Show("请填写标识对象编号!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                    return;
                }

                if (textBox5.Text == "")
                {
                    MessageBox.Show("请填写标识对象类型!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                    return;
                }

                if (textBox6.Text == "")
                {
                    MessageBox.Show("请填写标识对象名称!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (comboBox2.Text == "")
                {
                    MessageBox.Show("请填写标识所属道路!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }


                string mark_ID = textBox3.Text;
                string mark_Mac = textBox1.Text;
                string markObjCode = textBox4.Text;
                string markObjType = textBox5.Text;
                string markObjName = textBox6.Text;
                string belong = textBox9.Text;
                string bulidTime = dateTimePicker1.Value.ToString();
                string Street = comboBox2.Text;
                string design = textBox10.Text;
                string designPer = textBox11.Text;
                string buildCom = textBox12.Text;
                string buildPer = textBox13.Text;
                string monitor = textBox14.Text;
                string monitorPer = textBox15.Text;
                string mon = textBox16.Text;
                double deep = Convert.ToDouble(textBoxX4.Text);
                string MarkType = comboBox1.Text;
                double elevation = Convert.ToDouble(textBoxX1.Text);
                xValue = Convert.ToDouble(textBox7.Text);
                yValue = Convert.ToDouble(textBox2.Text);
                string useState = comboBox3.Text;
                string modelPath = "管道配件gcm\\电子标识器\\" + modelObj;
                string modelPath2 = Application.StartupPath + @"\\管道配件gcm\电子标识器\" + modelObj;


                if (feature != null)
                {
                    if (markerFeature != null)
                    {
                        markerFeature.Delete();
                    }

                    int ID = GeoScene.Data.GSOProjectManager.AddProject("+proj=tmerc +lat_0=0 +lon_0=120 +k=1 +x_0=499885 +y_0=6 +a=6378140 +b=6356755.288157528 +units=m +no_defs");
                    GeoScene.Data.GSOPoint2d pt2d = new GeoScene.Data.GSOPoint2d(yValue, xValue);
                    GeoScene.Data.GSOPoint2d result = GeoScene.Data.GSOProjectManager.Inverse(pt2d, ID);

                    GSOPoint3d pt = new GSOPoint3d();
                    pt.X = point.X;
                    pt.Y = point.Y;
                    pt.Z = point.Z;

                    GSOPoint3d pt2 = new GSOPoint3d();
                    pt2.X = result.X;
                    pt2.Y = result.Y;
                    pt2.Z = -0.4;

                    GSOGeoModel model = new GSOGeoModel();
                    model.FilePath = modelPath2;
                    model.Position = pt2;
                    //model.AltitudeMode = EnumAltitudeMode.Absolute;
                    model.AltitudeMode = EnumAltitudeMode.RelativeToGround;

                    feature = new GSOFeature();
                    feature.Geometry = model;
                    modelLayer.Editable = true;
                    //feature.

                    GSOFieldDefn defn = new GSOFieldDefn();
                    defn.Name = "编号";
                    defn.Type = EnumFieldType.Text;
                    defn.Width = 100;
                    feature.AddField(defn);

                    feature.SetFieldValue("编号", mark_ID);

                    defn.Name = "标识器ID号";
                    defn.Type = EnumFieldType.Text;
                    defn.Width = 100;
                    feature.AddField(defn);
                    feature.SetFieldValue("标识器ID号", mark_Mac);

                    defn.Name = "标识器型号";
                    defn.Type = EnumFieldType.Text;
                    defn.Width = 100;
                    feature.AddField(defn);
                    feature.SetFieldValue("标识器型号", MarkType);

                    defn.Name = "地面高程";
                    defn.Type = EnumFieldType.Float;
                    feature.AddField(defn);
                    elevation = Math.Round(elevation, 3);
                    feature.SetFieldValue("地面高程", elevation);

                    defn.Name = "X坐标";
                    defn.Type = EnumFieldType.Float;
                    feature.AddField(defn);
                    xValue = Math.Round(xValue, 3);
                    feature.SetFieldValue("X坐标", xValue);

                    defn.Name = "Y坐标";
                    defn.Type = EnumFieldType.Float;
                    feature.AddField(defn);
                    yValue = Math.Round(yValue, 3);
                    feature.SetFieldValue("Y坐标", yValue);

                    defn.Name = "标识器埋深";
                    defn.Type = EnumFieldType.Float;
                    feature.AddField(defn);
                    deep = Convert.ToDouble(deep.ToString("0.000"));
                    deep = Math.Round(deep, 3);
                    feature.SetFieldValue("标识器埋深", deep);

                    defn.Name = "对象编号";
                    defn.Type = EnumFieldType.Text;
                    defn.Width = 100;
                    feature.AddField(defn);
                    feature.SetFieldValue("对象编号", markObjCode);

                    defn.Name = "对象类型";
                    defn.Type = EnumFieldType.Text;
                    defn.Width = 100;
                    feature.AddField(defn);
                    feature.SetFieldValue("对象类型", markObjType);

                    defn.Name = "对象名称";
                    defn.Type = EnumFieldType.Text;
                    defn.Width = 100;
                    feature.AddField(defn);
                    feature.SetFieldValue("对象名称", markObjName);

                    defn.Name = "埋设时间";
                    defn.Type = EnumFieldType.Text;
                    defn.Width = 100;
                    feature.AddField(defn);
                    feature.SetFieldValue("埋设时间", bulidTime);

                    defn.Name = "所属道路";
                    defn.Type = EnumFieldType.Text;
                    defn.Width = 100;
                    feature.AddField(defn);
                    feature.SetFieldValue("所属道路", comboBox2.Text);

                    defn.Name = "使用状态";
                    defn.Type = EnumFieldType.Text;
                    defn.Width = 100;
                    feature.AddField(defn);
                    feature.SetFieldValue("使用状态", useState);

                    defn.Name = "权属单位";
                    defn.Type = EnumFieldType.Text;
                    defn.Width = 100;
                    feature.AddField(defn);
                    feature.SetFieldValue("权属单位", belong);

                    defn.Name = "设计单位";
                    defn.Type = EnumFieldType.Text;
                    defn.Width = 100;
                    feature.AddField(defn);
                    feature.SetFieldValue("设计单位", design);

                    defn.Name = "设计人姓名";
                    defn.Type = EnumFieldType.Text;
                    defn.Width = 100;
                    feature.AddField(defn);
                    feature.SetFieldValue("设计人姓名", designPer);

                    defn.Name = "施工单位";
                    defn.Type = EnumFieldType.Text;
                    defn.Width = 100;
                    feature.AddField(defn);
                    feature.SetFieldValue("施工单位", buildCom);

                    defn.Name = "施工责任人";
                    defn.Type = EnumFieldType.Text;
                    defn.Width = 100;
                    feature.AddField(defn);
                    feature.SetFieldValue("施工责任人", buildPer);

                    defn.Name = "施工监理";
                    defn.Type = EnumFieldType.Text;
                    defn.Width = 100;
                    feature.AddField(defn);
                    feature.SetFieldValue("施工监理", monitor);

                    defn.Name = "监理责任人";
                    defn.Type = EnumFieldType.Text;
                    defn.Width = 100;
                    feature.AddField(defn);
                    feature.SetFieldValue("监理责任人", monitorPer);

                    defn.Name = "模型路径";
                    defn.Type = EnumFieldType.Text;
                    defn.Width = 100;
                    feature.AddField(defn);
                    feature.SetFieldValue("模型路径", modelPath);

                    defn.Name = "备注";
                    defn.Type = EnumFieldType.Text;
                    defn.Width = 100;
                    feature.AddField(defn);
                    feature.SetFieldValue("备注", mon);
                    modelLayer.AddFeature(feature);

                    globeControl1.Globe.JumpToFeature(feature, 2.5);
                    globeControl1.Refresh();
                    modelLayer.Save();
                    modelLayer.Editable = false;
                    MessageBox.Show("标识器添加成功!", "提示");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("请指定模型规格!", "提示");
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("标识器添加失败:" + ex.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        
        private void listBoxSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxSize.SelectedIndex != -1)
            {
                string path = Application.StartupPath + @"\\管道配件gcm\\电子标识器\\" + list[listBoxSize.SelectedIndex];
                modelObj=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;
                this.plane3DControl.Plane3DScene.RemoveAllFeature();
                this.plane3DControl.Plane3DScene.AddFeature(feature);
            }
        }

        private void FrmAddSinMark_Load(object sender, EventArgs e)
        {
            if (globeControl1 != null)
            {
                globeControl1.MouseClick += new MouseEventHandler(globeControl1_MouseClick);
            }
            this.dateTimePicker1.CustomFormat = "yyyy-MM-dd HH:mm:ss";
            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)
            { 
                MessageBox.Show("文件夹【" + path + "】不存在!", "提示");
            }

            listBoxSize.Items.Clear();
            list.Clear();
            string path1 = Application.StartupPath + @"\管道配件gcm\" + "电子标识器" + @"\";
            DirectoryInfo theFolder1 = new DirectoryInfo(path1);
            foreach (FileInfo nextFile in theFolder1.GetFiles())
            {
                if (nextFile.Name.IndexOf("3DS") > -1 || nextFile.Name.IndexOf("gcm") > -1)
                {
                    this.listBoxSize.Items.Add(nextFile.Name.Substring(0, nextFile.Name.IndexOf(".")));
                    list.Add(nextFile.Name);
                }
            }
            comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
            comboBox1.Items.Add("BIRMM-EM14");
            comboBox1.Items.Add("BIRMM-EM30");
            comboBox1.Text = "BIRMM-EM14";

            comboBox3.DropDownStyle = ComboBoxStyle.DropDown;
            comboBox3.Items.Add("使用中");
            comboBox3.Items.Add("废弃");
            comboBox3.Text = "使用中";

            comboBox2.DropDownStyle = ComboBoxStyle.DropDown;
            foreach (Location l in Utility.locationList)
                comboBox2.Items.Add(l.NAME);

            textBox9.Text = "管委会";
            textBox10.Text = "北京无线电计量测试研究所";
            textBoxX1.Text = "0";
            textBoxX4.Text = "0.4";
        }

        private  void globeControl1_MouseClick(object sender, MouseEventArgs e)
        {
            try
            { 
                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 = Path.GetDirectoryName(Application.ExecutablePath) + "/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();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("运行错误:" + ex.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //经度
        private void textBoxXLon_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (textBoxXLon.Text.Trim() != "" && textBoxXLat.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;
                        }
                    }
                    int ID = GeoScene.Data.GSOProjectManager.AddProject("+proj=tmerc +lat_0=0 +lon_0=120 +k=1 +x_0=499885 +y_0=6 +a=6378140 +b=6356755.288157528 +units=m +no_defs");
                    double x = Convert.ToDouble(textBoxXLon.Text);
                    double y = 0;
                    GeoScene.Data.GSOPoint2d pt2d = new GeoScene.Data.GSOPoint2d(x, y);
                    GeoScene.Data.GSOPoint2d result = GeoScene.Data.GSOProjectManager.Forward(pt2d, ID);
                    textBox7.Text = result.Y.ToString();
                    //textBoxX3.Text = result.Y.ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("X坐标转换失败:" + ex.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        //纬度
        private void textBoxXLat_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (textBoxXLat.Text.Trim() != "" && textBoxXLon.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;
                        }
                    }
                    int ID = GeoScene.Data.GSOProjectManager.AddProject("+proj=tmerc +lat_0=0 +lon_0=120 +k=1 +x_0=499885 +y_0=6 +a=6378140 +b=6356755.288157528 +units=m +no_defs");
                    double x = Convert.ToDouble(textBoxXLon.Text);
                    double y = Convert.ToDouble(textBoxXLat.Text);
                    GeoScene.Data.GSOPoint2d pt2d = new GeoScene.Data.GSOPoint2d(x, y);
                    GeoScene.Data.GSOPoint2d result = GeoScene.Data.GSOProjectManager.Forward(pt2d, ID);
                    textBox7.Text = result.Y.ToString();
                    textBox2.Text = result.X.ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Y坐标转换失败:" + ex.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void FrmAddSinMark_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (globeControl1 != null)
            {
                globeControl1.MouseClick -= new MouseEventHandler(globeControl1_MouseClick);
            }
            if (markerFeature != null)
            {
                markerFeature.Delete();
                globeControl1.Refresh();
            }
        }

    }
}