qq1254582201 发表于 2024-9-25 16:48:02

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

本帖最后由 qq1254582201 于 2024-9-25 16:51 编辑


      public static void IsolateSelection()
      {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            var selection = ed.GetSelection();
            if (selection.Status != PromptStatus.OK)
                return;
            var ids = new HashSet<ObjectId>(selection.Value.GetObjectIds());
            using (var tr = new OpenCloseTransaction())
            {
                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                foreach (ObjectId id in curSpace)
                {
                  if (!ids.Contains(id))
                  {
                        var entity = (Entity)tr.GetObject(id, OpenMode.ForWrite);
                        entity.Visible = false;
                  }
                }
                tr.Commit();
            }
      }

      
      public static void UnisolateAll()
      {
            var db = HostApplicationServices.WorkingDatabase;
            using (var tr = new OpenCloseTransaction())
            {
                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                foreach (ObjectId id in curSpace)
                {
                  var entity = (Entity)tr.GetObject(id, OpenMode.ForRead);
                  if (!entity.Visible)
                  {
                        entity.UpgradeOpen();
                        entity.Visible = true;
                  }
                }
                tr.Commit();
            }
      }
//Summary: Lets user select seed point -> Zooms Extent -> Selects Boundary set -> Isolates them -> Creates boundary -> Unisolates all -> Returns Boundaries
      public DBObjectCollection CreateBoundary(Transaction trans, BlockTableRecord btr, DBObjectCollection boundarySet, Point3d internalSeedPoint)
      {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            var ResultingBoundaries = new DBObjectCollection(); //Return
            var ResultingBoundaryIds = new ObjectIdCollection();

            try
            {
                //Summary: Isolate Boundary set -> Create boundary -> Unisolates Boundary set ;

                //Isolate the boundarySet entities.
                var hiddenEntities = new List<Entity>();
                foreach (ObjectId id in btr)
                {
                  var entity = (Entity)trans.GetObject(id, OpenMode.ForWrite);
                  if (boundarySet.Contains(entity))
                  {
                        entity.Visible = true;
                  }
                  else
                  {
                        entity.Visible = false;
                        hiddenEntities.Add(entity);
                  }
                }

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

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

                //Unisolate the boundarySet entities.
                foreach (Entity entity in hiddenEntities)
                {
                  entity.Visible = true;
                }
            }
            catch (Exception ex)
            {
                Application.ShowAlertDialog("Error: " + ex.Message);
            }
            return ResultingBoundaries;
      }

public void MakeSelection(DBObjectCollection selectedObjects)
      {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            ObjectId[] selectedObjIds = new ObjectId;
            for (int i = 0; i < selectedObjects.Count; i++)
            {
                selectedObjIds = selectedObjects.ObjectId;
            }
            //Mark selection on the drawing
            ed.SetImpliedSelection(selectedObjIds);
      }

public static void IsolateSelection()
      {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

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

            var ids = new HashSet<ObjectId>(selection.Value.GetObjectIds());
            using (var tr = new OpenCloseTransaction())
            {
                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                foreach (ObjectId id in curSpace)
                {
                  if (!ids.Contains(id))
                  {
                        var entity = (Autodesk.AutoCAD.DatabaseServices.Entity)tr.GetObject(id, OpenMode.ForWrite);
                        entity.Visible = false;
                  }
                }
                tr.Commit();
            }
      }public static void UnisolateAll()
      {
            var db = HostApplicationServices.WorkingDatabase;
            using (var tr = new OpenCloseTransaction())
            {
                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                foreach (ObjectId id in curSpace)
                {
                  var entity = (Autodesk.AutoCAD.DatabaseServices.Entity)tr.GetObject(id, OpenMode.ForWrite);
                  if (!entity.Visible)
                  {
                        //curSpace.DowngradeOpen();
                        //entity.UpgradeOpen();
                        entity.Visible = true;
                        //entity.DowngradeOpen(); //Temporarily Added.
                  }
                }
                tr.Commit();
            }
      }

qq1254582201 发表于 2024-9-25 16:52:16

public static void IsolateSelection()
      {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

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

            var ids = new HashSet<ObjectId>(selection.Value.GetObjectIds());
            using (var tr = new OpenCloseTransaction())
            {
                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                foreach (ObjectId id in curSpace)
                {
                  if (!ids.Contains(id))
                  {
                        var entity = (Autodesk.AutoCAD.DatabaseServices.Entity)tr.GetObject(id, OpenMode.ForWrite);
                        entity.Visible = false;
                  }
                }
                tr.Commit();
            }
      }

qq1254582201 发表于 2024-9-25 17:09:15

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

public class AdvancedIsolateObjects
{
   
    public void IsolateObjectsByLayerAndGroup()
    {
      Document doc = Application.DocumentManager.MdiActiveDocument;
      Database db = doc.Database;
      Editor ed = doc.Editor;

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

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

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

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

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

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

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

                // Check if the entity is in the selected layers or groups
                if (layers.Contains(entity.Layer) || (entity.GroupId != ObjectId.Null && groups.Contains(((Group)tr.GetObject(entity.GroupId, OpenMode.ForRead)).Name)))
                {
                  // Make the entity visible
                  entity.Visible = true;
                }
                else
                {
                  // Hide the entity
                  entity.Visible = false;
                }
            }

            tr.Commit();
      }

      // Regenerate the drawing to reflect changes
      doc.Editor.Regen();
    }
}

tigcat 发表于 2024-9-25 18:53:22

谢谢分享!

guohq 发表于 2024-9-25 23:40:28


      public static void IsolateSelection()
      {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            var selection = ed.GetSelection();
            if (selection.Status != PromptStatus.OK)
                return;
            var ids = new HashSet<ObjectId>(selection.Value.GetObjectIds());
            using (var tr = new OpenCloseTransaction())
            {
                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                foreach (ObjectId id in curSpace) //已经得到 selection.Value.GetObjectIds()了,为什么还要在curSpace遍历呢???
                {
                  if (!ids.Contains(id))
                  {
                        var entity = (Entity)tr.GetObject(id, OpenMode.ForWrite);
                        entity.Visible = false;
                  }
                }
                tr.Commit();
            }
      }


foreach (ObjectId id in curSpace) //已经得到 selection.Value.GetObjectIds()了,为什么还要在curSpace遍历呢???

d1742647821 发表于 2024-9-26 09:06:23

guohq 发表于 2024-9-25 23:40
foreach (ObjectId id in curSpace) //已经得到 selection.Value.GetObjectIds()了,为什么还要在cu ...

十分有道理,脱裤子代码有点多

枫叶棋语 发表于 2024-9-27 12:06:05

代码写的太长了吧

qq1254582201 发表于 2024-9-29 15:23:58

guohq 发表于 2024-9-25 23:40
foreach (ObjectId id in curSpace) //已经得到 selection.Value.GetObjectIds()了,为什么还要在cu ...

没有研究过,估计是要剔除编组吧
页: [1]
查看完整版本: 【转载】C# 实现CAD的Isolate隔离对象