- 积分
- 467
- 明经币
- 个
- 注册时间
- 2024-3-15
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.Geometry;
- using System.Collections.Generic;
- using System.Linq;
- using Autodesk.AutoCAD.EditorInput;
- using System;
- [assembly: CommandClass(typeof(MyAutoCADPlugin.MyCommands))]
- namespace MyAutoCADPlugin
- {
- public class MyCommands
- {
- [CommandMethod("Add")]
- public void AddRedFrames()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- // 选择所有对象
- PromptSelectionResult selResult = doc.Editor.GetSelection();
- if (selResult.Status != PromptStatus.OK)
- {
- doc.Editor.WriteMessage("\nSelection failed.");
- return;
- }
- // 获取选择的实体
- SelectionSet selSet = selResult.Value;
- List<Entity> entities = new List<Entity>();
- foreach (SelectedObject selObj in selSet)
- {
- Entity ent = tr.GetObject(selObj.ObjectId, OpenMode.ForRead) as Entity;
- if (ent != null)
- {
- entities.Add(ent);
- }
- }
- // 计算所有对象的边界框
- if (entities.Count == 0)
- {
- doc.Editor.WriteMessage("\nNo entities selected.");
- return;
- }
- // 过滤掉多行文字
- entities = entities.Where(e => !(e is MText)).ToList();
- // 计算边界框
- Extents3d combinedExtents = new Extents3d();
- foreach (Entity ent in entities)
- {
- combinedExtents.AddExtents(ent.GeometricExtents);
- }
- // 设置容差
- double tolerance = 7.0; // 调整容差值
- // 分块
- List<List<Entity>> groupedEntities = GroupEntitiesByProximity(entities, tolerance);
- // 添加红色外框
- foreach (var group in groupedEntities)
- {
- Extents3d groupExtents = new Extents3d();
- foreach (Entity ent in group)
- {
- groupExtents.AddExtents(ent.GeometricExtents);
- }
- // 绘制红色外框
- AddRedFrame(db, tr, groupExtents);
- }
- tr.Commit();
- }
- }
- private void AddRedFrame(Database db, Transaction tr, Extents3d extents)
- {
- using (BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite))
- {
- Polyline poly = new Polyline();
- poly.AddVertexAt(0, new Point2d(extents.MinPoint.X, extents.MinPoint.Y), 0, 0, 0);
- poly.AddVertexAt(1, new Point2d(extents.MaxPoint.X, extents.MinPoint.Y), 0, 0, 0);
- poly.AddVertexAt(2, new Point2d(extents.MaxPoint.X, extents.MaxPoint.Y), 0, 0, 0);
- poly.AddVertexAt(3, new Point2d(extents.MinPoint.X, extents.MaxPoint.Y), 0, 0, 0);
- poly.Closed = true;
- poly.ColorIndex = 1; // Red color
- btr.AppendEntity(poly);
- tr.AddNewlyCreatedDBObject(poly, true);
- }
- }
- private List<List<Entity>> GroupEntitiesByProximity(List<Entity> entities, double tolerance)
- {
- List<List<Entity>> groupedEntities = new List<List<Entity>>();
- bool[] visited = new bool[entities.Count];
- for (int i = 0; i < entities.Count; i++)
- {
- if (visited) continue;
- List<Entity> group = new List<Entity>();
- Queue<Entity> queue = new Queue<Entity>();
- queue.Enqueue(entities);
- while (queue.Count > 0)
- {
- Entity current = queue.Dequeue();
- if (visited[entities.IndexOf(current)]) continue;
- visited[entities.IndexOf(current)] = true;
- group.Add(current);
- foreach (Entity neighbor in entities)
- {
- if (!visited[entities.IndexOf(neighbor)] && AreEntitiesClose(current, neighbor, tolerance))
- {
- queue.Enqueue(neighbor);
- }
- }
- }
- groupedEntities.Add(group);
- }
- return groupedEntities;
- }
- private bool AreEntitiesClose(Entity e1, Entity e2, double tolerance)
- {
- // 检查两个实体是否相近的逻辑
- Extents3d ext1 = e1.GeometricExtents;
- Extents3d ext2 = e2.GeometricExtents;
- double distX = Math.Abs(ext1.MinPoint.X - ext2.MinPoint.X);
- double distY = Math.Abs(ext1.MinPoint.Y - ext2.MinPoint.Y);
- return distX < tolerance && distY < tolerance;
- }
- }
- }
写了一个一键给分堆成块加框,但是处理速度太慢 ,求助大佬帮忙修改,怎么样子才能处理速度快一些,自定义容差没写进去,外框外扩%比也没加
|
|