• C# CAD交互界面-自定义面板集-查找定位(六)


    运行环境 vs2022 c# cad2016  调试成功

    一、代码说明

    1. 类成员变量声明:

    1. List objectIds = new List(); // 用于存储AutoCAD实体对象的ObjectId列表
    2. private static Autodesk.AutoCAD.Windows.PaletteSet _ps2; // 自定义浮动面板集实例
    3. private CustomPaletteControl _customCtrl; // 定制控件实例,包含ListBox及事件处理程序
    4. private static System.Windows.Forms.Panel panel; // 面板容器
    5. // 其他已导入命名空间下的类型(略)

    2. CreatePalette() 方法: 这个方法负责创建和配置自定义浮动面板。

    • 创建并初始化一个 PaletteSet 实例,设置其名称和最小尺寸。
    • 创建文本框 textBox 并设置其属性如是否多行、位置、大小等。
    • 创建按钮 button1,设置文本、位置、大小,并为 Click 事件绑定 Button1_Click 处理程序。
    • 初始化或复用 _customCtrl 控件,并将其添加到 Panel 中。
    • 将文本框、按钮和自定义控件添加至 Panel 中。
    • 将 Panel 添加到 PaletteSet 的指定区域,并显示整个 PaletteSet。
    1. // 声明成员变量:存储对象ID的列表
    2. List objectIds = new List();
    3. // 创建PaletteSet实例
    4. private static Autodesk.AutoCAD.Windows.PaletteSet _ps2;
    5. // 创建CustomPaletteControl实例(假设这是一个包含ListBox的自定义控件)
    6. private CustomPaletteControl _customCtrl;
    7. // 创建Panel容器实例
    8. private static System.Windows.Forms.Panel panel;
    9. // 创建并配置自定义浮动面板的方法
    10. public void CreatePalette()
    11. {
    12. // 初始化 PaletteSet,并设置其名称和最小尺寸
    13. _ps2 = new PaletteSet("我的窗体");
    14. _ps2.MinimumSize = new System.Drawing.Size(300, 300);
    15. // 创建并配置文本框控件
    16. TextBox textBox = new TextBox();
    17. textBox.Multiline = false;
    18. textBox.Location = new Point(10, 10);
    19. textBox.Size = new Size(240, 20);
    20. textBox.Text = "403";
    21. // 创建并配置按钮控件
    22. Button button1 = new Button();
    23. button1.Text = "查找";
    24. button1.Location = new Point(10, 40);
    25. button1.Size = new Size(80, 25);
    26. // 给按钮添加Click事件处理程序
    27. button1.Click += new EventHandler(Button1_Click);
    28. // 初始化或复用_customCtrl,并设置位置与大小
    29. if (_customCtrl == null)
    30. {
    31. _customCtrl = new CustomPaletteControl(ListBoxItemSelected);
    32. }
    33. _customCtrl.Location = new Point(10, 70);
    34. _customCtrl.Size = new Size(280, 250);
    35. // 示例性地向ListBox添加一个项目
    36. _customCtrl.ListBox1.Items.Add(new CommandItem("00", "00"));
    37. // 创建Panel并添加控件
    38. System.Windows.Forms.Panel localPanel = new System.Windows.Forms.Panel(); // 注意这里的panel是局部变量
    39. localPanel.Controls.Add(textBox);
    40. localPanel.Controls.Add(button1);
    41. localPanel.Controls.Add(_customCtrl);
    42. // 将Panel添加到PaletteSet中
    43. _ps2.Add("快捷键02", localPanel);
    44. // 显示PaletteSet
    45. _ps2.Visible = true;
    46. }

    3. Button1_Click 事件处理程序: 当查找按钮被点击时执行的操作:

    • 获取文本框中的输入内容。
    • 根据输入的内容筛选出预编号层上的文本对象。
    • 遍历所有匹配的对象,并将 ObjectId 加入 objectIds 列表。
    • 如果找到匹配项,则更新 _customCtrl 中 ListBox 的项目,添加与输入文本匹配的实体信息。
    1. // 按钮点击事件处理程序
    2. private void Button1_Click(object sender, EventArgs e)
    3. {
    4. // 获取TextBox中的文本,并进行查找操作...
    5. // ...省略具体查找逻辑...
    6. // 如果找到匹配项,则更新_customCtrl中的ListBox内容
    7. if (_customCtrl != null && objectIds.Count() > 0)
    8. {
    9. // 更新视图状态,然后遍历每个ObjectId并将信息添加至ListBox
    10. // ...省略具体代码实现...
    11. }
    12. }

    4. ListBoxItemSelected 事件处理程序:

    • 当 ListBox 中的项目被选中时,根据选定项目所关联的 ObjectId 找到对应的实体并高亮显示。

    1. // ListBoxItemSelected事件处理程序
    2. private void ListBoxItemSelected(object sender, EventArgs e)
    3. {
    4. // 当ListBox项被选中时,获取所选项目的ObjectId并高亮显示相关实体
    5. // ...省略具体代码实现...
    6. }

    5. ZoomToExtent 方法:

    • 缩放 AutoCAD 视图以适应特定实体的几何范围。
    • 这个方法获取当前文档、数据库、编辑器等信息,启动事务,修改视图属性,然后提交事务并更新屏幕。
    1. // 缩放视图至指定范围的方法
    2. public static void ZoomToExtent(Extents3d extent)
    3. {
    4. // 计算视图中心点及修改视图属性
    5. // ...省略具体计算和修改视图属性的代码...
    6. // 更新视图并提交事务
    7. acDoc.Editor.SetCurrentView(acView);
    8. acDoc.Editor.UpdateScreen();
    9. acTrans.Commit();
    10. }

    6. TextBox_KeyDown 事件处理程序: 虽然此事件处理器在给出的代码块中未实际使用,但它的作用是监听文本框内的按键事件。在这里,如果按下的是回车键,则会触发相应的逻辑操作。

    1. // TextBox回车键按下事件处理程序
    2. private void TextBox_KeyDown(object sender, KeyEventArgs e)
    3. {
    4. if (e.KeyCode == Keys.Enter)
    5. {
    6. // 当用户在TextBox中按回车键时执行的操作
    7. // ...省略具体实现...
    8. }
    9. }

    总结来说,这段代码主要实现了以下功能:

    • 在AutoCAD环境中创建一个带有用户交互元素(文本框、按钮)的自定义浮动面板。
    • 根据用户在文本框中输入的预编号搜索相关的图形实体。
    • 显示搜索结果并在用户选择后高亮显示相关实体。
    • 缩放视图以便更好地查看所选实体。

    二、完整代码

    1. using Autodesk.AutoCAD.ApplicationServices;//CAD实体
    2. using Autodesk.AutoCAD.DatabaseServices;//数据库服务
    3. using Autodesk.AutoCAD.EditorInput;//命令栏
    4. using Autodesk.AutoCAD.Geometry;//几何图形
    5. using Autodesk.AutoCAD.Windows;
    6. using System;
    7. using System.Collections.Generic;
    8. using System.Drawing;
    9. using System.Linq;
    10. using Application = Autodesk.AutoCAD.ApplicationServices.Application;
    11. using Button = System.Windows.Forms.Button;
    12. using TextBox = System.Windows.Forms.TextBox;
    13. namespace cad自定义面板集.forms
    14. {
    15. internal class showbox
    16. {
    17. List objectIds = new List(); // 用实际的数据填充这个列表
    18. private static Autodesk.AutoCAD.Windows.PaletteSet _ps2;
    19. //private static CustomPaletteControl _customCtrl;
    20. private CustomPaletteControl _customCtrl;
    21. private static System.Windows.Forms.Panel panel;
    22. public void CreatePalette()
    23. {
    24. _ps2 = new PaletteSet("我的窗体");
    25. _ps2.MinimumSize = new System.Drawing.Size(300, 300);
    26. // 创建并配置TextBox与Button控件
    27. TextBox textBox = new TextBox();
    28. textBox.Multiline = false;
    29. textBox.Location = new System.Drawing.Point(10, 10);
    30. textBox.Size = new System.Drawing.Size(240, 20); // 设置文本框大小
    31. textBox.Text = "403";
    32. Button button1 = new Button();
    33. button1.Text = "查找";
    34. button1.Location = new System.Drawing.Point(10, 40);
    35. button1.Size = new System.Drawing.Size(80, 25); // 设置按钮大小
    36. button1.Click += new EventHandler(Button1_Click);// 添加Button的Click事件处理程序
    37. // 如果尚未初始化_customCtrl,则在这里进行初始化
    38. if (_customCtrl == null)
    39. {
    40. _customCtrl = new CustomPaletteControl(ListBoxItemSelected);
    41. }
    42. _customCtrl.Location = new Point(10, 70);
    43. _customCtrl.Size = new Size(280, 250);
    44. _customCtrl.ListBox1.Items.Add(new CommandItem("00", "00"));
    45. // 将控件添加到Panel或其他容器控件
    46. System.Windows.Forms.Panel panel = new System.Windows.Forms.Panel();
    47. panel.Controls.Add(textBox);
    48. panel.Controls.Add(button1);
    49. panel.Controls.Add(_customCtrl);
    50. _ps2.Add("快捷键02", panel);
    51. // 显示面板
    52. _ps2.Visible = true;
    53. }
    54. // 定义Button点击事件处理程序
    55. private void Button1_Click(object sender, EventArgs e)
    56. {
    57. // 获取TextBox中的文本
    58. TextBox textBox = (sender as Button).Parent.Controls.OfType().FirstOrDefault();
    59. if (textBox != null)
    60. {
    61. string inputText = textBox.Text;
    62. //System.Windows.Forms.MessageBox.Show($"您输入的内容是:{inputText}");
    63. Document doc = Application.DocumentManager.MdiActiveDocument;
    64. Database db = doc.Database;
    65. Editor ed = doc.Editor;
    66. string ybh = inputText;
    67. // ed.WriteMessage(ybh + "\n");
    68. using (Transaction tr = db.TransactionManager.StartTransaction())
    69. {
    70. // 获取所有预编号文本对象
    71. TypedValue[] filter = new TypedValue[]
    72. {
    73. new TypedValue((int)DxfCode.LayerName, "预编号")
    74. };
    75. SelectionFilter sf = new SelectionFilter(filter);
    76. PromptSelectionResult psr = ed.SelectAll(sf);
    77. if (psr.Status == PromptStatus.OK)
    78. {
    79. SelectionSet SS = psr.Value;
    80. Entity current_entity = null;
    81. objectIds.Clear();
    82. foreach (ObjectId id in SS.GetObjectIds())
    83. {
    84. Entity textEnt = (Entity)tr.GetObject(id, OpenMode.ForRead);
    85. if (textEnt is DBText)
    86. {
    87. DBText dbText = (DBText)textEnt;
    88. string te = dbText.TextString;
    89. Point3d tkp = dbText.Position;
    90. int index = te.IndexOf(ybh);
    91. // ed.WriteMessage(index + "-index\n");
    92. // ed.WriteMessage(te + "-te\n");
    93. // ed.WriteMessage(ybh + "-ybh\n");
    94. if (index != -1)
    95. {
    96. //ed.WriteMessage("-找到\n");
    97. //current_entity = textEnt;
    98. objectIds.Add(id);
    99. }
    100. }
    101. }
    102. if (_customCtrl != null && objectIds.Count() > 0)
    103. {
    104. _ps2.Visible = false;
    105. ed.WriteMessage(objectIds.Count() + "-objectIds.Count()\n");
    106. foreach (ObjectId id in objectIds)
    107. {
    108. Entity textEnt = (Entity)tr.GetObject(id, OpenMode.ForRead);
    109. DBText dbText = (DBText)textEnt;
    110. string te = dbText.TextString;
    111. var item = new formsCommandItem(te, id);
    112. _customCtrl.ListBox1.Items.Add(item);
    113. }
    114. if (!_ps2.Visible)
    115. {
    116. _ps2.Visible = true;
    117. }
    118. }
    119. if (current_entity != null)
    120. {
    121. current_entity.Highlight();//高亮显示实体
    122. ZoomToExtent(current_entity.GeometricExtents);
    123. }
    124. }
    125. else
    126. {
    127. ed.WriteMessage("没找到\n");
    128. }
    129. tr.Commit();
    130. }
    131. }
    132. }
    133. private void ListBoxItemSelected(object sender, EventArgs e)
    134. {
    135. Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
    136. Database db = HostApplicationServices.WorkingDatabase;
    137. Editor ed = doc.Editor;
    138. if (_customCtrl.ListBox1.SelectedItem is formsCommandItem selectedCommandItem && selectedCommandItem.ObjectId != ObjectId.Null)
    139. {
    140. using (var tr = db.TransactionManager.StartTransaction())
    141. {
    142. Entity current_entity = tr.GetObject(selectedCommandItem.ObjectId, OpenMode.ForRead) as Entity;
    143. // ... 进行与选定 ObjectId 相关的操作 ...
    144. current_entity.Highlight();//高亮显示实体
    145. ZoomToExtent(current_entity.GeometricExtents);
    146. tr.Commit();
    147. }
    148. }
    149. }
    150. //
    151. /// 缩放至指定范围
    152. ///
    153. ///
    154. public static void ZoomToExtent(Extents3d extent)
    155. {
    156. Point3d pMin = extent.MinPoint;
    157. Point3d pMax = extent.MaxPoint;
    158. //获取当前文档及数据库
    159. Document acDoc = Application.DocumentManager.MdiActiveDocument;
    160. Database acCurDb = acDoc.Database;
    161. Editor ed = acDoc.Editor;
    162. // 启动事务
    163. using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    164. {
    165. // 获取当前视图
    166. using (ViewTableRecord acView = acDoc.Editor.GetCurrentView())
    167. {
    168. ed.WriteMessage($" 设置视图的高01:" + acView.Height + "\n");
    169. ed.WriteMessage($" 设置视图的宽01:" + acView.Width + "\n");
    170. ed.WriteMessage($" 设置视图中心01:" + acView.CenterPoint + "\n");
    171. // 修改视图属性
    172. acView.Height = 33.1615367318681;
    173. acView.Width = 69.9654061867447;
    174. acView.CenterPoint = new Point2d(-201556.0997, -1520456.661);
    175. // 修改视图属性
    176. // acView.Height = Math.Abs(pMin.Y - pMax.Y);
    177. //acView.Width = Math.Abs(pMin.X - pMax.X);
    178. acView.CenterPoint = new Point2d((pMin.X - 612277.2549), (pMin.Y - 4556539.37));
    179. ed.WriteMessage($" 设置视图的高02:" + acView.Height + "\n");
    180. ed.WriteMessage($" 设置视图的宽02:" + acView.Width + "\n");
    181. ed.WriteMessage($" 设置视图中心02:" + acView.CenterPoint + "\n");
    182. // 更新当前视图
    183. acDoc.Editor.SetCurrentView(acView);
    184. acDoc.Editor.UpdateScreen();
    185. acTrans.Commit();
    186. }
    187. // 提交更改
    188. }
    189. }
    190. }
    191. }

    //感谢大家的点赞,收藏,转发,关注 

  • 相关阅读:
    【第八章 多线程、并行并发、多线程的创建方式】
    leetcode算法-哈希表总结
    APISIX 在君润人力云原生平台的架构实践
    自己动手写一个分库分表中间件(九)兼容性处理之事务之 Spring 怎么看是一个事务
    phpstorm、IDEA 添加方法、类等注释
    YoloX大升级:阿里巴巴提出新框架,超越Yolov6和PPYoloE(附源代码)
    【SpringMVC篇】详解SpringMVC入门案例
    基于javaweb+mysql的新闻发布管理系统
    redis哨兵练习
    质疑标普,理解标普,加入标普
  • 原文地址:https://blog.csdn.net/hb_ljj/article/details/136104435