C#+AE 实现点击查询属性功能

实现效果如下:

 

 

 

 

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 ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Display;

namespace _0920tryTreeList
{
    public partial class Form1 : Form
    {
        //需要创建一个公共变量表格
        // public
       // public DataTable dt2 = new DataTable();
      //  DataRow dr2;
        public Form1()
        {
            InitializeComponent();
          


            //第二张表格
            //dt2.Columns.Add("Name");
            //dt2.Columns.Add("Value");
            //this.dataGridView2.DataSource = pDataTable;
            //this.dataGridView2.DataSource = dt2;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "*.mxd|*.mxd";
            ofd.ShowDialog();

            string fp = ofd.FileName;
            axMapControl1.LoadMxFile(fp,0,Type.Missing);
        }

        bool bu = false;
        private void button2_Click(object sender, EventArgs e)
        {
            bu = true;

        }

        //定义子节点的单击事件也用到的公共变量
      // public  IFeature pFeature = null;


       
        private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
        {

            try
            {
                if (bu)
                {
                    DataTable pDataTable = new DataTable();
                    DataRow pDataRow = null;
                    pDataTable.Columns.Add("ID");
                    pDataTable.Columns.Add("Name");
                    pDataTable.Columns.Add("ParentID");
                    pDataTable.Columns.Add("Value");

                    for (int i = 0; i < axMapControl1.Map.LayerCount;i++ )
                    {
                        pDataRow=pDataTable.NewRow();
                        string lyrName = axMapControl1.Map.get_Layer(i).Name;
                        pDataRow["ID"] = lyrName;
                        pDataRow["Name"] = lyrName;
                        pDataRow["ParentID"] = -1;
                      
                        pDataTable.Rows.Add(pDataRow);

                       
                        //开始点选查询
                        IMap pMap;
                        pMap = axMapControl1.Map as IMap;

                        //获取点图层
                        IFeatureLayer pFeatureLayer;
                        pFeatureLayer = pMap.get_Layer(i) as IFeatureLayer;
                        IFeatureClass pFeatureClass;
                        pFeatureClass = pFeatureLayer.FeatureClass;

                        //获取鼠标点击点
                        IPoint pPoint;
                        pPoint = new PointClass();
                        pPoint.PutCoords(e.mapX, e.mapY);

                        IGeometry pGeometry;

                        //定义缓冲区
                        double db = 2;
                        ITopologicalOperator pTop;
                        pTop = pPoint as ITopologicalOperator;
                        pGeometry = pTop.Buffer(db);

                        //选取
                        pMap.SelectByShape(pGeometry, null, false);
                        pMap.ClearSelection();

                        //空间过滤运算
                        ISpatialFilter pSpatialFilter = new SpatialFilterClass();
                        pSpatialFilter.Geometry = pGeometry;


                        //设置为不同的要素类型的图层
              
                   
                        switch (pFeatureClass.ShapeType)
                        {
                            case esriGeometryType.esriGeometryPoint:
                                pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;
                                break;
                            case esriGeometryType.esriGeometryPolyline:
                                pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelCrosses;
                                break;
                            case esriGeometryType.esriGeometryPolygon:
                                pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
                                break;

                        }
                        pSpatialFilter.GeometryField = pFeatureClass.ShapeFieldName;

                        //指针
                        IFeatureCursor pFeatureCursor;
                        pFeatureCursor = pFeatureClass.Search(pSpatialFilter, false);
                        IFeature pFeature;
                        pFeature = pFeatureCursor.NextFeature();

                        //开始遍历
                        while (pFeature != null)
                        {

                            //获取要素的字段名和字段值
                            int n = pFeature.Fields.FieldCount;
                            string sName;
                            string sValue;


                            //这句话的对象需要随着地图的改变而改变。可以是ID,FID 等带有唯一标识身份的 东西。
                            int index = pFeature.Fields.FindField("ObjectID");
                            if (index == -1)
                                return;
                            IField pField = pFeature.Fields.get_Field(index);
                            sName = pField.Name;

                            sValue = pFeature.get_Value(index).ToString();


                            pDataRow = pDataTable.NewRow();

                            //之所以这样赋值是为了保证ID的唯一性;
                            pDataRow["ID"] = lyrName + sValue;
                            pDataRow["Name"] = sValue;
                            pDataRow["ParentID"] = lyrName;
                            pDataTable.Rows.Add(pDataRow);

                            pFeature = pFeatureCursor.NextFeature();

                        }
                        //这个是师兄交的,指针等占内存的东西在用完之后一定要释放;!!!
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor);
                    }
                 
                    treeList1.DataSource = pDataTable;
                    treeList1.ParentFieldName="ParentID";
                

                 
                }

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

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        //换地图清除数据源
        private void axMapControl1_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e)
        {
            treeList1.DataSource = null;
           // dataGridView1.DataSource = null;
        }

   
        //************//出现的问题是:值不在预期范围内
        private void treeList1_FocusedNodeChanged_1(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
           
            try
            {
                int layerIndex;
                if (e.Node.HasChildren)
                {
                    return;
                }
                if (e.Node.ParentNode != null) //***********//这个存在bug,若节点超过两级则出错
                {
                    //创建一个新的表作为属性表
                    DataTable dt = new DataTable();
                    DataRow dr = null;
                    dt.Columns.Add("Name");
                    dt.Columns.Add("Value");


                    //循环图层
                    for (int i = 0; i < this.axMapControl1.LayerCount; i++)
                    {

                        //如果父节点名称和图层名相同,获取索引
                        if (e.Node.ParentNode.GetValue(0).ToString() == this.axMapControl1.get_Layer(i).Name)
                        {
                            layerIndex = i;
                            IFeature pFeature;                             
 
                           pFeature = (this.axMapControl1.get_Layer(layerIndex) as IFeatureLayer).FeatureClass.GetFeature(int.Parse(this.treeList1.FocusedNode.GetValue(0).ToString())); ;

                          if (pFeature != null)
                            {                            
                               //循环字段集,赋值给表dt
                                int n = pFeature.Fields.FieldCount;
                             
                                for (int k = 0; k < n-1; k++)
                                {
                                   
                                    IField pField = pFeature.Fields.get_Field(k);
                                    string fieldName = pField.Name;
                                    var  a=  pFeature.get_Value(k);
                                    string fieldValue = pFeature.get_Value(k).ToString();

                                    //赋值给表
                                    dr = dt.NewRow();
                                    dr["Name"]=fieldName;
                                     dr["Value"]=fieldValue;
                                     dt.Rows.Add(dr);
                                }  
                         
                                gridControl1.DataSource = dt;
                               
                            }
                            else
                                return;                  
                           
                        }
                    }

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

       
    }
}
 

转载于:https://www.cnblogs.com/yanhan/archive/2012/09/25/2701520.html

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
栅格计算器是一种常见的计算机应用程序,它允许用户在一个矩形的网格中输入数值,然后对这些数值进行各种计算。在本文中,我们将使用C#AE(Adobe After Effects)来实现一个简单的栅格计算器功能。 首先,在AE中创建一个新的合成(Composition),并添加一个文本层(Text Layer)。将文本层的内容设置为“栅格计算器”,并将其放置在合成的顶部。 接下来,我们需要创建一个矩形网格,以便用户可以输入数值。我们可以使用AE中的形状层(Shape Layer)来创建一个矩形。在AE中,选择“Layer”->“New”->“Shape Layer”来创建一个新的形状层。选择“Rectangle Tool”(矩形工具),并在画布上绘制一个矩形。然后,选择矩形图层,打开“Add”->“Fill”性,选择一种颜色填充矩形。 现在,我们需要将矩形网格分成若干个小格子,以便用户可以在其中输入数值。我们可以使用C#编写一个简单的脚本来完成这个任务。在Visual Studio中创建一个新的C#控制台应用程序,并将其命名为“GridCalculator”。 在GridCalculator项目中,我们需要添加一个引用,以便我们可以访问AE中的COM(Component Object Model)对象。选择“Project”->“Add Reference”,然后在“COM”选项卡中找到“Adobe After Effects XX.X Type Library”(其中XX.X表示你的AE版本号),并添加它。 接下来,我们需要编写一个C#脚本来创建网格。以下是一个示例脚本: ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using AE_COM; namespace GridCalculator { class Program { static void Main(string[] args) { // Create a new AE project var ae = new Application(); var proj = ae.NewProject(); // Create a new composition var comp = proj.Items.AddComp("Grid Calculator", 1920, 1080, 1.0, 10.0, 30); // Add a new shape layer var shapeLayer = comp.Layers.AddShape(); shapeLayer.Name = "Grid"; // Create a new rectangle shape var rect = new Shape(); var rectGroup = new ShapeGroup(); rectGroup.AddShape(rect); shapeLayer.Property("Contents").AddProperty("ADBE Vector Group").SetValue(rectGroup); // Set the rectangle size rect.Size = new PointF(400, 400); // Create the grid var rows = 10; var cols = 10; var cellSize = new PointF(rect.Size.X / cols, rect.Size.Y / rows); for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { // Create a new rectangle shape for each cell var cell = new Shape(); var cellGroup = new ShapeGroup(); cellGroup.AddShape(cell); shapeLayer.Property("Contents").AddProperty("ADBE Vector Group").SetValue(cellGroup); // Set the cell position and size var pos = new PointF(col * cellSize.X, row * cellSize.Y); cell.Position = pos; cell.Size = cellSize; } } // Save the project proj.Save(@"C:\GridCalculator.aep"); // Quit AE ae.Quit(); } } } ``` 在上面的脚本中,我们首先创建了一个新的AE项目和一个新的合成。然后,我们添加了一个新的形状层,并在其中创建了一个矩形。接下来,我们使用嵌套循环创建了一个网格,其中每个单元格都是一个独立的矩形。 最后,我们保存了AE项目,并退出AE应用程序。 现在,我们可以在AE中打开“GridCalculator.aep”文件,并将生成的网格图层拖放到我们之前创建的合成中。用户现在可以在每个单元格中输入数值,并使用AE中的表达式(Expressions)功能对这些数值进行计算。 总的来说,使用C#AE来创建栅格计算器功能是非常简单的。通过利用AE的COM对象和C#的编程能力,我们可以轻松地创建一个自定义的栅格计算器工具,以满足我们的具体需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值