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<int, List> table1 = new Dictionary<int, List>(100);

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
}
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
} /// <summary> /// 搜索文本 /// </summary> /// <param name="sender"></param> ///
<param name="e"></param> 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<double> list1 = new
List<double>(); 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<double> list = new
List<double>(); //获取选择行数的坐标 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));
} } } /// <summary> /// 缩放到指定位置 /// </summary> /// <param
name="pt1">第一个点</param> /// <param name="pt2">第二个点</param> 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); } } /// <summary> /// 选择扩展属性符合的图形 /// </summary> ///
<param name="sender"></param> /// <param name="e"></param> 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<double> list1 = new
List<double>(); list1.Add(x1); list1.Add(y1); list1.Add(x2); list1.Add(y2);
table1.Add(i, list1); //获取图形的扩展属性 ResultBuffer rb = ent.XData; List<string>
list = new List<string>(); //将扩展属性的值添加到列表中 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); } } } } }
}

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信