自学内容网 自学内容网

030集——分组法——C# CAD二次开发

重叠的图行进行分组,效果如下:

纵向投影重叠(横向移动冲突)可以分组:

 纵向冲突也可以分组:

也可根据颜色不同分组:

部分代码如下,完整代码见文章下方名片 

 public class Class1
 {
     
     [CommandMethod("xx")]
     public void XX()
     {

         var entities = Z.db.SelectEntities<Entity>();
         var bigList = Z.GroupEntities(entities);
         foreach (var smallList in bigList)
         {
             //Extents3d extents = new Extents3d();
             //entity.ForEach(x => extents.AddExtents(x.GeometricExtents));//x=>extents.
             Z.GetboxsAndDraw(smallList);
         }

     }
  public static List<T> SelectEntities<T>(this Database db ) where T : Entity
  {
      List<T> result = new List<T>();
      Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
      var pso = new PromptSelectionOptions();
      pso.MessageForAdding = "\n请选择:";
      PromptSelectionResult psr = editor.GetSelection(pso);
      if (psr.Status == PromptStatus.OK)
      {
          ObjectId[] objectids = psr.Value.GetObjectIds();
          Database database = HostApplicationServices.WorkingDatabase;
          using (Transaction tran = database.TransactionManager.StartTransaction())
          {
              foreach (var item in objectids)
              {
                  Entity entity = item.GetObject(OpenMode.ForRead) as Entity;
                  if (entity is T)
                  {
                      result.Add(entity as T);
                  }

              }
          }
      }
      return result;
  }
      class Friend
      {
          public int Id { set; get; }
          public List<Friend> Friends { get; set; }
          public Friend(int id)
          {
              Id = id;
              Friends = new List<Friend>();
          }
      }
  public static bool IsOverlapping(Entity entity1, Entity entity2)
  {
      Point3dCollection pos = new Point3dCollection();
      entity1.IntersectWith(entity2, Intersect.OnBothOperands, pos, IntPtr.Zero, IntPtr.Zero);

      return pos.Count > 0;
  }
     public static ObjectId GetboxsAndDraw(List<Entity> entities)
     {
         List<double> lis = new List<double>();
         if (entities.Count == 0)
         {
             return ObjectId.Null;
         }
         double minx = entities.Min(x => x.Bounds.Value.MinPoint.X);
         double miny = entities.Min(x => x.Bounds.Value.MinPoint.Y);
         double maxx = entities.Max(x => x.Bounds.Value.MaxPoint.X);
         double maxy = entities.Max(x => x.Bounds.Value.MaxPoint.Y);
         lis.Add(minx);
         lis.Add(miny);
         lis.Add(maxx);
         lis.Add(maxy);
         Autodesk.AutoCAD.DatabaseServices.Polyline pl = new Autodesk.AutoCAD.DatabaseServices.Polyline();
         pl.AddVertexAt(0, new Point2d(minx ,miny),0,0,0);
         pl.AddVertexAt(0, new Point2d(maxx, miny), 0, 0, 0);
         pl.AddVertexAt(0, new Point2d(maxx, maxy), 0, 0, 0);
         pl.AddVertexAt(0, new Point2d(minx, maxy), 0, 0, 0);
         pl.Closed = true;
         pl.ColorIndex = 1;
         db.AddEntityToModeSpace(pl);
         return pl.ObjectId;

     }

   public static List<List<Entity>> GroupEntities(List<Entity> entities)
   {
       List<List<Entity>> result = new List<List<Entity>>();
       List<Friend> friends = new List<Friend>();
       for (int i = 0; i < entities.Count; i++)
       {
           Friend friend = new Friend(i);
           friends.Add(friend);
       }
       for (int i = 0; i < entities.Count - 1; i++)
       {
           for (int j = 0; j < entities.Count; j++)
           {
               if (Z.IsOverlapping(entities[i], entities[j]))
               {
                   friends[i].Friends.Add(friends[j]);
                   friends[j].Friends.Add(friends[i]);
               }
           }
       }
****部分代码

       return result;
   }


原文地址:https://blog.csdn.net/yongshiqq/article/details/143580694

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!