Newer
Older
GHFX_REFACTOR / FrmAccessoryAnalysis.cs
using System;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using GeoScene.Data;
using GeoScene.Engine;
using GeoScene.Globe;

namespace Cyberpipe
{
    public partial class FrmAccessoryAnalysis : Office2007Form
    {
        GSOGlobeControl globeControl1;
        GSOGeoPoint3D pt;
        GSOLayer layerTemp;
        public FrmAccessoryAnalysis(GSOGlobeControl _globeControl1,GSOLayer layerTemp1)
        {
            InitializeComponent();
            globeControl1 = _globeControl1;
            layerTemp = layerTemp1;
        }

        private void txtRadius_TextChanged(object sender, EventArgs e)
        {
            dataGridViewX1.Rows.Clear();
            if (txtRadius.Text == "")
            {
                return;
            }
            double radius = 0.0;

            if (double.TryParse(txtRadius.Text, out radius) == false)
            {
                MessageBox.Show("请输入正确的缓冲半径!", "提示");
                txtRadius.Text = "";
            }
            else
            {
                if (txtRadius.Text != "" && pt != null)
                {
                    drawPolygon(pt, radius);

                }
            }

        }
        GSOFeature new_feat;
        GSOGeoPolygon3D resPolygon;
        private void drawPolygon(GSOGeoPoint3D _pnt, double r)
        {
            if (new_feat != null)
            {
                layerTemp.RemoveFeatureByID(new_feat.ID);
            }
            new_feat = new GSOFeature();
            GSOPoint3ds pnts = new GSOPoint3ds();
            GSOPoint3d pnt = new GSOPoint3d();
            pnt.X = _pnt.X;
            pnt.Y = _pnt.Y;
            pnt.Z = _pnt.Z;
            pnts.Add(pnt);
            pnt.X = _pnt.X;
            pnt.Y = _pnt.Y - 0.0000000005;
            pnt.Z = _pnt.Z;
            pnts.Add(pnt);
            GSOGeoPolyline3D line = new GSOGeoPolyline3D();
            line.AddPart(pnts);
            resPolygon = line.CreateBuffer(r * 2, true, 5, true, false);
            resPolygon.MoveZ(0.3);
            resPolygon.AltitudeMode = EnumAltitudeMode.RelativeToGround;
            new_feat.Geometry = resPolygon;
            layerTemp.AddFeature(new_feat);
            globeControl1.Refresh();
        }
        /// <summary>
        /// 键盘按键按下事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtRadius_KeyPress(object sender, KeyPressEventArgs e)
        {
            TextBox text = sender as TextBox;
            if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8) && e.KeyChar != 46)
            {
                e.Handled = true;
            }
            if (e.KeyChar == 46)                           //小数点
            {
                if (text.Text.Length <= 0)
                    e.Handled = true;   //小数点不能在第一位
                else                                             //处理不规则的小数点
                {
                    float f;
                    float oldf;
                    bool b1 = false, b2 = false;
                    b1 = float.TryParse(text.Text, out oldf);
                    b2 = float.TryParse(text.Text + e.KeyChar, out f);
                    if (b2 == false)
                    {
                        if (b1)
                            e.Handled = true;
                        else
                            e.Handled = false;
                    }
                }
            }
        }
        /// <summary>
        /// 确定按钮事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEnter_Click(object sender, EventArgs e)
        {
            dataGridViewX1.Rows.Clear();
            if (txtRadius.Text == "")
            {
                MessageBox.Show("请输入缓冲半径!", "提示");
                return;
            }
            for (int i = globeControl1.Globe.Layers.Count - 1; i >= 0; i--)
            {
                GSOLayer layer = globeControl1.Globe.Layers[i];
                if (layer.Dataset != null && layer.Dataset.IsFeatureDataset)
                {
                    string name = layer.Name;
                    if (layer.Type == EnumLayerType.FeatureLayer && !name.Contains("\\"))
                    {
                        if (layer.Caption.EndsWith("附属物"))
                        {
                            Polygon_Contain_PointAnalysis(resPolygon, layer.Caption);
                        }
                    }
                }
            }
        }
        private void Polygon_Contain_PointAnalysis(GSOGeoPolygon3D polygon, string layername)//方法修改,返回类型由int类型修改为GSOFeatures
        {

            GSOLayer layer = globeControl1.Globe.Layers.GetLayerByCaption(layername);
            GSOFeatureLayer flayer = layer as GSOFeatureLayer;
            if (flayer == null) return;
            GSOFeatures feats = polygon == null ? flayer.GetAllFeatures() : flayer.FindFeaturesInPolygon(polygon, false);
            if (feats.Length <= 0) return;
            for (int i = 0; i < feats.Length; i++)
            {

                string featNum = feats[i].GetFieldAsString("编号");
                string featType = feats[i].GetFieldAsString("附属物名称");
                if (feats[i].GetFieldDefn("标识器编号") != null)
                {
                    featNum = feats[i].GetFieldAsString("标识器编号");
                }
                DataGridViewRow row = new DataGridViewRow();
                int index = dataGridViewX1.Rows.Add(row);
                row = dataGridViewX1.Rows[index];
                row.Cells["图层名称"].Value = layername;
                row.Cells["附属物名称"].Value = featType;
                row.Cells["编号"].Value = featNum;
            }
        }

        private void FrmAccessoryAnalysis_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (new_feat != null)
            {
                layerTemp.RemoveFeatureByID(new_feat.ID);
            }
        }
        //双击定位功能
        GSOFeature m_feature;
        private void dataGridViewX1_MouseClick(object sender, MouseEventArgs e)
        {
            if (m_feature != null && m_feature.HighLight)
            {
                m_feature.HighLight = false;
            }
            m_feature = null;
            if (e.Button == MouseButtons.Left)
            {

                DataGridView.HitTestInfo hittestinfo = dataGridViewX1.HitTest(e.X, e.Y);
                //idx = hittestinfo.RowIndex;
                if (hittestinfo.RowIndex <= -1) return;
                string featureName = dataGridViewX1.Rows[hittestinfo.RowIndex].Cells["编号"].Value.ToString().Trim();
                string layerName = dataGridViewX1.Rows[hittestinfo.RowIndex].Cells["图层名称"].Value.ToString().Trim();

                //GSOLayer layer = globeControl1.Globe.Layers.GetLayerByID((int)(Utility.LayerLabel_LayerIDs[layerName]));
                GSOLayer layer = globeControl1.Globe.Layers.GetLayerByCaption(layerName);
                if (layer == null) return;
                GSOFeatures features = layer.GetFeatureByName(featureName, false);
                for (int j = 0; j < features.Length; j++)
                {
                    if (features[j].Name == featureName)
                    {
                        m_feature = features[j];
                    }
                }
            }

            else if (e.Button == MouseButtons.Right)
            {
                DataGridView.HitTestInfo hittestinfo = dataGridViewX1.HitTest(e.X, e.Y);
                if (hittestinfo.RowIndex <= -1) return;
                string featureName = dataGridViewX1.Rows[hittestinfo.RowIndex].Cells["编号"].Value.ToString();
                string layerName = dataGridViewX1.Rows[hittestinfo.RowIndex].Cells["图层名称"].Value.ToString().Trim();
                contextMenuStrip1.Show(dataGridViewX1, e.X, e.Y);
                featureName = featureName.Trim();

                //GSOLayer layer = globeControl1.Globe.Layers.GetLayerByID((int)(Utility.LayerLabel_LayerIDs[layerName]));
                GSOLayer layer = globeControl1.Globe.Layers.GetLayerByCaption(layerName);
                if (layer == null)
                    return;
                GSOFeatures features = layer.GetFeatureByName(featureName, false);

                for (int j = 0; j < features.Length; j++)
                {
                    if (features[j].Name == featureName)
                    {
                        m_feature = features[j];
                        break;
                    }
                }
            }
        }
        int count;
        private string flashflag = "single";
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (count < 40)
            {
                count++;
                if (!flashflag.Equals("single")) return;
                if (m_feature == null) return;
                if (count % 2 != 0)
                {
                    m_feature.HighLight = true;
                    globeControl1.Refresh();
                }
                else
                {
                    m_feature.HighLight = false;
                    globeControl1.Refresh();
                }
            }
            else
            {
                timer1.Stop();
                count = 0;
            }
        }

        private void dataGridViewX1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {

                if (e.Button == MouseButtons.Left)
                {
                    DataGridView.HitTestInfo hittestinfo = dataGridViewX1.HitTest(e.X, e.Y);
                    if (hittestinfo.RowIndex > -1)
                    {
                        string featureName = dataGridViewX1.Rows[hittestinfo.RowIndex].Cells["编号"].Value.ToString().Trim();
                        string layerName = dataGridViewX1.Rows[hittestinfo.RowIndex].Cells["图层名称"].Value.ToString().Trim();

                        //GSOLayer layer = globeControl1.Globe.Layers.GetLayerByID((int)(Utility.LayerLabel_LayerIDs[layerName]));
                        GSOLayer layer = globeControl1.Globe.Layers.GetLayerByCaption(layerName);
                        if (layer == null) return;
                        GSOFeatures features = layer.GetFeatureByName(featureName, false);
                        for (int j = 0; j < features.Length; j++)
                        {
                            if (!features[j].Name.Equals(featureName)) continue;
                            m_feature = features[j];
                            if (m_feature.Geometry != null)
                            {
                                globeControl1.Globe.FlyToGeometry(m_feature.Geometry, 0, 45, 5);
                            }
                            else
                            {
                                globeControl1.Globe.FlyToFeature(m_feature, 0, 45, 5);
                            }
                            LightMenu_Click(sender, e);
                            break;
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LogError.PublishError(ex);
            }

        }

        private void FlyToMenu_Click(object sender, EventArgs e)
        {
            if (m_feature == null) return;
            if (m_feature.Geometry != null && m_feature.Geometry.Type == EnumGeometryType.GeoPolyline3D)
            {
                GSOGeoPolyline3D line = m_feature.Geometry as GSOGeoPolyline3D;
                double length = line.GetSpaceLength(true, 6378137);
                GSOGeoPolyline3D lineLine = line.GetSegment(0, length / 2);
                GSOPoint3d point3d = lineLine[lineLine.PartCount - 1][lineLine[lineLine.PartCount - 1].Count - 1];
                pt.X = point3d.X;
                pt.Y = point3d.Y;
                pt.Z = point3d.Z;
                globeControl1.Globe.FlyToPosition(point3d, EnumAltitudeMode.Absolute,0,45,5);
            }
            else
            {
                globeControl1.Globe.FlyToFeature(m_feature, 0, 45, 3);
            }
            LightMenu_Click(sender, e);
        }

        private void LightMenu_Click(object sender, EventArgs e)
        {
            flashflag = "single";
            timer1.Start();
        }

        private void FrmAccessoryAnalysis_Load(object sender, EventArgs e)
        {
            globeControl1.MouseClick += globeControl1_MouseClick;
        }

        void globeControl1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                GSOPoint3d point;
                GSOLayer templayer;
                GSOFeature feature1 = globeControl1.Globe.HitTest(e.X, e.Y, out templayer, out point, false, true, 0);
                if (point.X == 0 && point.Y == 0 && point.Z == 0)
                {
                    point = globeControl1.Globe.ScreenToScene(e.X, e.Y);
                }
                pt = new GSOGeoPoint3D();
                pt.X = point.X;
                pt.Y = point.Y;
                pt.Z = point.Z;
                if (txtRadius.Text != "" && pt != null)
                {
                    drawPolygon(pt, Convert.ToDouble(txtRadius.Text));
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (new_feat != null)
            {
                layerTemp.RemoveFeatureByID(new_feat.ID);
            }
            Close();
        }
    }
}