明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 197|回复: 6

【转载】C# 实现CAD的Isolate隔离对象

[复制链接]
发表于 3 天前 | 显示全部楼层 |阅读模式
本帖最后由 qq1254582201 于 2024-9-25 16:51 编辑

  1. [CommandMethod("ISOL")]
  2.         public static void IsolateSelection()
  3.         {
  4.             var doc = Application.DocumentManager.MdiActiveDocument;
  5.             var db = doc.Database;
  6.             var ed = doc.Editor;
  7.             var selection = ed.GetSelection();
  8.             if (selection.Status != PromptStatus.OK)
  9.                 return;
  10.             var ids = new HashSet<ObjectId>(selection.Value.GetObjectIds());
  11.             using (var tr = new OpenCloseTransaction())
  12.             {
  13.                 var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  14.                 foreach (ObjectId id in curSpace)
  15.                 {
  16.                     if (!ids.Contains(id))
  17.                     {
  18.                         var entity = (Entity)tr.GetObject(id, OpenMode.ForWrite);
  19.                         entity.Visible = false;
  20.                     }
  21.                 }
  22.                 tr.Commit();
  23.             }
  24.         }

  25.         [CommandMethod("UNISOL")]
  26.         public static void UnisolateAll()
  27.         {
  28.             var db = HostApplicationServices.WorkingDatabase;
  29.             using (var tr = new OpenCloseTransaction())
  30.             {
  31.                 var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  32.                 foreach (ObjectId id in curSpace)
  33.                 {
  34.                     var entity = (Entity)tr.GetObject(id, OpenMode.ForRead);
  35.                     if (!entity.Visible)
  36.                     {
  37.                         entity.UpgradeOpen();
  38.                         entity.Visible = true;
  39.                     }
  40.                 }
  41.                 tr.Commit();
  42.             }
  43.         }

  1. //Summary: Lets user select seed point -> Zooms Extent -> Selects Boundary set -> Isolates them -> Creates boundary -> Unisolates all -> Returns Boundaries
  2.         public DBObjectCollection CreateBoundary(Transaction trans, BlockTableRecord btr, DBObjectCollection boundarySet, Point3d internalSeedPoint)
  3.         {
  4.             var doc = Application.DocumentManager.MdiActiveDocument;
  5.             var db = doc.Database;
  6.             var ed = doc.Editor;
  7.             var ResultingBoundaries = new DBObjectCollection(); //Return
  8.             var ResultingBoundaryIds = new ObjectIdCollection();

  9.             try
  10.             {
  11.                 //Summary: Isolate Boundary set -> Create boundary -> Unisolates Boundary set ;

  12.                 //Isolate the boundarySet entities.
  13.                 var hiddenEntities = new List<Entity>();
  14.                 foreach (ObjectId id in btr)
  15.                 {
  16.                     var entity = (Entity)trans.GetObject(id, OpenMode.ForWrite);
  17.                     if (boundarySet.Contains(entity))
  18.                     {
  19.                         entity.Visible = true;
  20.                     }
  21.                     else
  22.                     {
  23.                         entity.Visible = false;
  24.                         hiddenEntities.Add(entity);
  25.                     }
  26.                 }

  27.                 //Creating and adding boundary objects to the drawing. Also collecting its ObjectIds for later use.
  28.                 //Tracing the bondary out of input curves. Here InternalSeedPoint is a point inside the boundary that is going to be generated.
  29.                 ResultingBoundaries = ed.TraceBoundary(internalSeedPoint, false);
  30.                 //Appending the boundary to the databse
  31.                 foreach (Polyline boundary in ResultingBoundaries)
  32.                 {
  33.                     btr.AppendEntity(boundary);
  34.                     trans.AddNewlyCreatedDBObject(boundary, true);

  35.                     //Collecting ResultingBoundaryIds for later use (if required)
  36.                     ResultingBoundaryIds.Add(boundary.ObjectId);
  37.                 }

  38.                 //Unisolate the boundarySet entities.
  39.                 foreach (Entity entity in hiddenEntities)
  40.                 {
  41.                     entity.Visible = true;
  42.                 }
  43.             }
  44.             catch (Exception ex)
  45.             {
  46.                 Application.ShowAlertDialog("Error: " + ex.Message);
  47.             }
  48.             return ResultingBoundaries;
  49.         }


  1. public void MakeSelection(DBObjectCollection selectedObjects)
  2.         {
  3.             Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  4.             ObjectId[] selectedObjIds = new ObjectId[selectedObjects.Count];
  5.             for (int i = 0; i < selectedObjects.Count; i++)
  6.             {
  7.                 selectedObjIds[i] = selectedObjects[i].ObjectId;
  8.             }
  9.             //Mark selection on the drawing
  10.             ed.SetImpliedSelection(selectedObjIds);
  11.         }
复制代码

  1. public static void IsolateSelection()
  2.         {
  3.             var doc = Application.DocumentManager.MdiActiveDocument;
  4.             var db = doc.Database;
  5.             var ed = doc.Editor;

  6.             var selection = ed.GetSelection();
  7.             if (selection.Status != PromptStatus.OK) return;

  8.             var ids = new HashSet<ObjectId>(selection.Value.GetObjectIds());
  9.             using (var tr = new OpenCloseTransaction())
  10.             {
  11.                 var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  12.                 foreach (ObjectId id in curSpace)
  13.                 {
  14.                     if (!ids.Contains(id))
  15.                     {
  16.                         var entity = (Autodesk.AutoCAD.DatabaseServices.Entity)tr.GetObject(id, OpenMode.ForWrite);
  17.                         entity.Visible = false;
  18.                     }
  19.                 }
  20.                 tr.Commit();
  21.             }
  22.         }
  1. public static void UnisolateAll()
  2.         {
  3.             var db = HostApplicationServices.WorkingDatabase;
  4.             using (var tr = new OpenCloseTransaction())
  5.             {
  6.                 var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  7.                 foreach (ObjectId id in curSpace)
  8.                 {
  9.                     var entity = (Autodesk.AutoCAD.DatabaseServices.Entity)tr.GetObject(id, OpenMode.ForWrite);
  10.                     if (!entity.Visible)
  11.                     {
  12.                         //curSpace.DowngradeOpen();
  13.                         //entity.UpgradeOpen();
  14.                         entity.Visible = true;
  15.                         //entity.DowngradeOpen(); //Temporarily Added.
  16.                     }
  17.                 }
  18.                 tr.Commit();
  19.             }
  20.         }


评分

参与人数 1明经币 +1 金钱 +10 收起 理由
tigcat + 1 + 10 很给力!

查看全部评分

 楼主| 发表于 3 天前 | 显示全部楼层
  1. public static void IsolateSelection()
  2.         {
  3.             var doc = Application.DocumentManager.MdiActiveDocument;
  4.             var db = doc.Database;
  5.             var ed = doc.Editor;

  6.             var selection = ed.GetSelection();
  7.             if (selection.Status != PromptStatus.OK) return;

  8.             var ids = new HashSet<ObjectId>(selection.Value.GetObjectIds());
  9.             using (var tr = new OpenCloseTransaction())
  10.             {
  11.                 var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  12.                 foreach (ObjectId id in curSpace)
  13.                 {
  14.                     if (!ids.Contains(id))
  15.                     {
  16.                         var entity = (Autodesk.AutoCAD.DatabaseServices.Entity)tr.GetObject(id, OpenMode.ForWrite);
  17.                         entity.Visible = false;
  18.                     }
  19.                 }
  20.                 tr.Commit();
  21.             }
  22.         }
 楼主| 发表于 3 天前 | 显示全部楼层
  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.Runtime;

  5. public class AdvancedIsolateObjects
  6. {
  7.     [CommandMethod("ADV_ISOLATEOBJS")]
  8.     public void IsolateObjectsByLayerAndGroup()
  9.     {
  10.         Document doc = Application.DocumentManager.MdiActiveDocument;
  11.         Database db = doc.Database;
  12.         Editor ed = doc.Editor;

  13.         // Prompt the user to select objects
  14.         PromptSelectionResult selectionResult = ed.GetSelection();
  15.         if (selectionResult.Status != PromptStatus.OK)
  16.             return;

  17.         // Get the selected object IDs
  18.         ObjectId[] selectedIds = selectionResult.Value.GetObjectIds();

  19.         using (Transaction tr = db.TransactionManager.StartTransaction())
  20.         {
  21.             BlockTableRecord currentSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

  22.             // Create a set to store layers and groups of selected objects
  23.             HashSet<string> layers = new HashSet<string>();
  24.             HashSet<string> groups = new HashSet<string>();

  25.             // Collect layers and groups of selected objects
  26.             foreach (ObjectId id in selectedIds)
  27.             {
  28.                 Entity entity = (Entity)tr.GetObject(id, OpenMode.ForRead);
  29.                 layers.Add(entity.Layer);

  30.                 // Check if the entity is part of a group
  31.                 if (entity.GroupId != ObjectId.Null)
  32.                 {
  33.                     Group group = (Group)tr.GetObject(entity.GroupId, OpenMode.ForRead);
  34.                     groups.Add(group.Name);
  35.                 }
  36.             }

  37.             // Iterate through all objects in the current space
  38.             foreach (ObjectId id in currentSpace)
  39.             {
  40.                 Entity entity = (Entity)tr.GetObject(id, OpenMode.ForWrite);

  41.                 // Check if the entity is in the selected layers or groups
  42.                 if (layers.Contains(entity.Layer) || (entity.GroupId != ObjectId.Null && groups.Contains(((Group)tr.GetObject(entity.GroupId, OpenMode.ForRead)).Name)))
  43.                 {
  44.                     // Make the entity visible
  45.                     entity.Visible = true;
  46.                 }
  47.                 else
  48.                 {
  49.                     // Hide the entity
  50.                     entity.Visible = false;
  51.                 }
  52.             }

  53.             tr.Commit();
  54.         }

  55.         // Regenerate the drawing to reflect changes
  56.         doc.Editor.Regen();
  57.     }
  58. }
发表于 3 天前 | 显示全部楼层
  1. [CommandMethod("ISOL")]
  2.         public static void IsolateSelection()
  3.         {
  4.             var doc = Application.DocumentManager.MdiActiveDocument;
  5.             var db = doc.Database;
  6.             var ed = doc.Editor;
  7.             var selection = ed.GetSelection();
  8.             if (selection.Status != PromptStatus.OK)
  9.                 return;
  10.             var ids = new HashSet<ObjectId>(selection.Value.GetObjectIds());
  11.             using (var tr = new OpenCloseTransaction())
  12.             {
  13.                 var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  14.                 foreach (ObjectId id in curSpace) //已经得到 selection.Value.GetObjectIds()了,为什么还要在curSpace遍历呢???
  15.                 {
  16.                     if (!ids.Contains(id))
  17.                     {
  18.                         var entity = (Entity)tr.GetObject(id, OpenMode.ForWrite);
  19.                         entity.Visible = false;
  20.                     }
  21.                 }
  22.                 tr.Commit();
  23.             }
  24.         }



foreach (ObjectId id in curSpace) //已经得到 selection.Value.GetObjectIds()了,为什么还要在curSpace遍历呢???
发表于 前天 09:06 | 显示全部楼层
guohq 发表于 2024-9-25 23:40
foreach (ObjectId id in curSpace) //已经得到 selection.Value.GetObjectIds()了,为什么还要在cu ...

十分有道理,脱裤子代码有点多
发表于 昨天 12:06 | 显示全部楼层
代码写的太长了吧
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-9-28 02:14 , Processed in 0.180446 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表