• c# cad二次开发实现注记搜索跟扩展属性搜索,并点击即可定位到位置,添加了界面操作


    c# cad二次开发实现注记搜索跟扩展属性搜索,并点击即可定位到位置,添加了界面操作
    在这里插入图片描述

    using Autodesk.AutoCAD.DatabaseServices;
    using Autodesk.AutoCAD.EditorInput;
    using Autodesk.AutoCAD.Geometry;
    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 Application = Autodesk.AutoCAD.ApplicationServices.Application;
    using Autodesk.AutoCAD.Geometry;

    namespace _7_属性查找
    {
    public partial class Form1selecte : Form
    {
    public Form1selecte()
    {
    InitializeComponent();
    }
    private static Dictionary table1 = new Dictionary(100);

    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {

        }
    
    • 1

    private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
    {

        }
    
    • 1

    private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {

        }
        /// 
        /// 搜索文本
        /// 
        /// 
        /// 
        private void button1_Click(object sender, EventArgs e)
        {
            table1.Clear();//清空全局变量
            try
            {
                listBox1.Items.Clear();//清空控件内容
                //启动图形事件
                Database db = HostApplicationServices.WorkingDatabase;
                Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
                //过滤选择条件
                string lay = comboBox1.Text;
                if (comboBox1.Text == "")
                {
                    lay = "**";
                }
                TypedValue[] values = new TypedValue[]
                {
                new TypedValue((int)DxfCode.Start,"*text*"),//选择图形为圆
                new TypedValue(1, string.Format("*{0}*", textBox1.Text)),
                new TypedValue(8, string.Format("*{0}*", lay))
                };
                SelectionFilter filter = new SelectionFilter(values);
                PromptSelectionResult psr = ed.SelectAll(filter);
                //判断是否有选择到图形
                if (psr.Status == PromptStatus.OK)
                {
                    SelectionSet sset = psr.Value;
                    using (Transaction trans = db.TransactionManager.StartTransaction())
                    {
                        int i = 0;
                        //循环每个要素
                        foreach (SelectedObject item in sset)
                        {
                            Entity ent = (Entity)item.ObjectId.GetObject(OpenMode.ForRead);
                            DBText textEntity = ent as DBText;
                            //获取文本内容
                            string sTxt = textEntity.TextString;
                            //获取文本坐标点
                            Point3d pos = textEntity.Position;
                            double x1 = ent.GeometricExtents.MinPoint.X;
                            double y1 = ent.GeometricExtents.MinPoint.Y;
                            double x2 = ent.GeometricExtents.MaxPoint.X;
                            double y2 = ent.GeometricExtents.MaxPoint.Y;
                            List list1 = new List();
                            list1.Add(x1);
                            list1.Add(y1);
                            list1.Add(x2);
                            list1.Add(y2);
                            //将坐标传递给全局变量
                            table1.Add(i, list1);
                            //文本内容写入界面控件中
                            listBox1.Items.Add(sTxt);
                            i++;
                        }
                        trans.Commit();//提交事务
                    }
    
                }
            }
            catch (System.DivideByZeroException ex)
            {
                MessageBox.Show(ex.ToString());
            }
    
        }
    
    
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //获取选择的行数
            int index = listBox1.SelectedIndex;
            if (index > -1)
            {
                List list = new List();
                //获取选择行数的坐标
                list = table1[index];
                if (list.Count == 3)
                {
                    double X = list[0];
                    double Y = list[1];
                    double H = list[2] * 2;
                    //缩放到指定位置
                    ZoomWindow(new Point3d(X - H, Y - H, 0), new Point3d(X + H, Y + H, 0));
                }
                else
                {
                    double X1 = list[0];
                    double Y1 = list[1];
                    double X2 = list[2];
                    double Y2 = list[3];
                    //缩放到指定位置
                    ZoomWindow(new Point3d(X1, Y1, 0), new Point3d(X2, Y2, 0));
                }
            }
        }
        /// 
        /// 缩放到指定位置
        /// 
        /// 第一个点
        /// 第二个点
        public static void ZoomWindow(Point3d pt1, Point3d pt2)
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            //创建一临时的直线用于获取两点表示的范围
            using (Line line = new Line(pt1, pt2))
            {
                //获取两点表示的范围
                Extents3d extents = new Extents3d(line.GeometricExtents.MinPoint, line.GeometricExtents.MaxPoint);
                //获取范围内的最小值点及最大值点
                Point2d minPt = new Point2d(extents.MinPoint.X, extents.MinPoint.Y);
                Point2d maxPt = new Point2d(extents.MaxPoint.X, extents.MaxPoint.Y);
                //得到当前视图
                ViewTableRecord view = ed.GetCurrentView();
                //设置视图的中心点、高度和宽度
                view.CenterPoint = minPt + (maxPt - minPt) / 2;
                view.Height = maxPt.Y - minPt.Y;
                view.Width = maxPt.X - minPt.X;
                //更新当前视图
                ed.SetCurrentView(view);
            }
        }
        /// 
        /// 选择扩展属性符合的图形
        /// 
        /// 
        /// 
        private void button2_Click(object sender, EventArgs e)
        {
            //清空全局变量
            table1.Clear();
            //清空界面的空间
            listBox1.Items.Clear();
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            //过滤选择条件
            string lay = comboBox1.Text;
            if (comboBox1.Text == "")
            {
                lay = "**";
            }
            TypedValue[] values = new TypedValue[]
            {
                new TypedValue(1001, string.Format("*{0}*", textBox2.Text)),
                new TypedValue(8, string.Format("*{0}*", lay))
            };
            SelectionFilter filter = new SelectionFilter(values);
            PromptSelectionResult psr = ed.SelectAll(filter);
            //判断选择是否有图形
            if (psr.Status == PromptStatus.OK)
            {
                SelectionSet sset = psr.Value;
                ObjectId[] ids = sset.GetObjectIds();
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    int i = 0;
                    foreach (SelectedObject item in sset)
                    {
                        Entity ent = (Entity)item.ObjectId.GetObject(OpenMode.ForRead);
                        //获取图形的最小点和最大点
                        double x1 = ent.GeometricExtents.MinPoint.X;
                        double y1 = ent.GeometricExtents.MinPoint.Y;
                        double x2 = ent.GeometricExtents.MaxPoint.X;
                        double y2 = ent.GeometricExtents.MaxPoint.Y;
                        List list1 = new List();
                        list1.Add(x1);
                        list1.Add(y1);
                        list1.Add(x2);
                        list1.Add(y2);
                        table1.Add(i, list1);
                        //获取图形的扩展属性
                        ResultBuffer rb = ent.XData;
                        List list = new List();
                        //将扩展属性的值添加到列表中
                        foreach (TypedValue tv in rb)
                        {
                            list.Add(tv.Value.ToString());
                        }
                        i++;
                        //将扩展属性添加到控件中
                        listBox1.Items.Add(String.Join("、", list.ToArray()));
                    }
                    trans.Commit();
                }
            }
        }
    
        private void button3_Click(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForRead);
                foreach (ObjectId item in lt)
                {
                    LayerTableRecord ltr = (LayerTableRecord)trans.GetObject(item, OpenMode.ForRead);
                    if (ltr != null)
                    {
                        comboBox1.Items.Add(ltr.Name);
                    }
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211

    }

  • 相关阅读:
    提升代码可读性,减少if-else的几个小技巧
    机器人C++库(3)Robotics Library 例程一
    C++笔记之vector的初始化以及assign()方法
    LVS,Nginx,Haproxy三种负载均衡产品的对比
    【附源码】计算机毕业设计JAVA疫情下的进出口食品安全信息管理系统
    重学java 58.红黑树相关集合
    2022年9月及10月
    uniapp、小程序canvas相关
    {} >= {} 返回 true
    基于Python实现的快递管理系统源码+数据库,采用PyQt6实现GUI界面
  • 原文地址:https://blog.csdn.net/weixin_43931979/article/details/130909904