各种排序分组

简易排序方法如下:

cs 复制代码
  public static  void XX()
  {
      var circles = Z.db.SelectEntities<Circle>();

      circles = circles.OrderByDescending(x => x.Center.Y).ThenBy(x => x.Center.X).ToList();
      for (int i = 0; i < circles.Count; i++)
      {
          DBText text = new DBText() { Position = circles[i].Center, TextString = i.ToString(),Height = 1,ColorIndex= 1 };
          Z.db.AddEntityToModeSpace(text);
      }
      
      //Debugger.Break();
     



  }
cs 复制代码
 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;
 }
cs 复制代码
 public static ObjectId AddEntityToModeSpace(this Database db, Entity ent)
 {
     // 声明ObjectId 用于返回
     ObjectId entId = ObjectId.Null;
     // 开启事务处理
     using (Transaction trans = db.TransactionManager.StartTransaction())
     {
         // 打开块表
         BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
         // 打开块表记录
         BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
         // 添加图形到块表记录
         entId = btr.AppendEntity(ent);
         // 更新数据信息
         trans.AddNewlyCreatedDBObject(ent, true);
         // 提交事务
         trans.Commit();
     }
     return entId;
 }

调整后的效果:

cs 复制代码
   public static  void XX()
   {
       int k = 0;
       var circles = Z.db.SelectEntities<Circle>();
       List < List < Circle >> bigteam = Z.GroupEntities<Circle>(circles,Z.IsHorizontalOverlap);
       bigteam = bigteam.OrderByDescending(x => x[0].Center.Y).ToList();
       for (int i = 0; i < bigteam.Count; i++)
       {
           bigteam[i] = bigteam[i].OrderBy(x => x.Center.X).ToList();
           for ( int j = 0; j < bigteam[i].Count; j++)
           {
               k = k + 1;
               DBText text = new DBText() { Position = bigteam[i][j].Center, TextString = k.ToString(), Height = 0.8, ColorIndex = 1 };
               Z.db.AddEntityToModeSpace(text);
           }
       }
   }
cs 复制代码
  public static List<List<T>> GroupEntities<T>(List<T> entities,Func<Entity,Entity,bool>
      func)where T : Entity
  {
      List<List<T>> result = new List<List<T>>();
      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 (func(entities[i], entities[j]))
              {
                  friends[i].Friends.Add(friends[j]);
                  friends[j].Friends.Add(friends[i]);
              }
          }
      }
      while (friends.Count > 0)
      {
          List<T> list = new List<T>();
          Queue<Friend> queue = new Queue<Friend>();
          queue.Enqueue(friends[0]);
          friends.RemoveAt(0);
         *****
          result.Add(list);
      }

      return result;
  }

之字形排序:

cs 复制代码
 public static  void XX()
 {
     int k = 0;
     var circles = Z.db.SelectEntities<Circle>();
     List < List < Circle >> bigteam = Z.GroupEntities<Circle>(circles,Z.IsHorizontalOverlap);
     bigteam = bigteam.OrderByDescending(x => x[0].Center.Y).ToList();
     for (int i = 0; i < bigteam.Count; i++)
     {
         if (i % 2 != 0)
         {
             bigteam[i] = bigteam[i].OrderByDescending(x => x.Center.X).ToList();
             for (int j = 0; j < bigteam[i].Count; j++)
             {
                 k = k + 1;
                 DBText text = new DBText() { Position = bigteam[i][j].Center, TextString = k.ToString(), Height = 0.8, ColorIndex = 1 };
                 Z.db.AddEntityToModeSpace(text);
             }
         }
         else
         {
             bigteam[i] = bigteam[i].OrderBy(x => x.Center.X).ToList();
         for ( int j = 0; j < bigteam[i].Count; j++)
         {
             k = k + 1;
             DBText text = new DBText() { Position = bigteam[i][j].Center, TextString = k.ToString(), Height = 0.8, ColorIndex = 1 };
             Z.db.AddEntityToModeSpace(text);
         }
         }
         
     }
 }
cs 复制代码
 while (queue.Count > 0)
          {
              Friend friend = queue.Dequeue();
              list.Add(entities[friend.Id]);
              foreach (Friend f in friend.Friends)
              {
                  if (friends.Contains(f))
                  {
                      queue.Enqueue(f);
                      friends.Remove(f);
                  }
              }
          }

规定组数分组法,区别与横向碰撞分组,一刀切分组。

cs 复制代码
[CommandMethod("CirsOrderBy")]
public void CirsOrderBy()
{
    double fenShu = 5;
    List<Circle> cirs = new List<Circle>();
    Editor ed = Tool.GetEditor();
    PromptSelectionOptions selectionOptions = new PromptSelectionOptions();
    SelectionFilter sf = new SelectionFilter(new TypedValue[] { new TypedValue((int)DxfCode.Start,"Circle")});
    PromptSelectionResult psr = ed.SelectAll(sf);
    if (psr == null) return;
    using (Transaction tr = Tool.GetCurDatabse().TransactionManager.StartTransaction())
    {
        ObjectId[] ids=psr.Value.GetObjectIds();
        for (int i = 0; i < ids.Length; i++) 
        {
            Circle cir= tr.GetObject(ids[i],OpenMode.ForRead)as Circle;
            cirs.Add(cir);
        }
        tr.Commit();
    }
    cirs=cirs.OrderBy(c => c.Center.Y).ToList();
    double totalHeight=cirs.Last().Center.Y-cirs.First().Center.Y;
    double quJian = totalHeight / fenShu;
    int quJianShu =(int)Math.Ceiling(totalHeight / fenShu);
    List<List<Circle>>cirGroups= new List<List<Circle>>();
    List<Circle>cirGroup= new List<Circle>();
    for (int i = 0; i < quJianShu; i++) 
    {
        cirGroup=cirs.Where(c=>c.Center.Y>=cirs.First().Center.Y+i*quJian&& c.Center.Y < cirs.First().Center.Y + (i+1) * quJian).ToList();
        if (i % 2 == 0)
        {
            cirGroup = cirGroup.OrderBy(c => c.Center.X).ToList();
        }
        else
        {
            cirGroup = cirGroup.OrderByDescending(c => c.Center.X).ToList();
        }
        cirGroups.Add(cirGroup);
    }
    cirs.Clear();
    foreach (var item in cirGroups)
    {
        foreach (var circle in item)
        { 
            cirs.Add(circle);
        }
    }
    Database db = Tool.GetCurDatabse();
    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        BlockTable bt=tr.GetObject(db.BlockTableId,OpenMode.ForRead)as BlockTable;
        BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForWrite) as BlockTableRecord;
        for (int i = 0; i < cirs.Count; i++) 
        {
            DBText txt= new DBText();
            txt.Position = cirs[i].Center;
            txt.VerticalMode = TextVerticalMode.TextVerticalMid;
            txt.HorizontalMode = TextHorizontalMode.TextMid;
            txt.AlignmentPoint = cirs[i].Center;
            txt.TextString=(i+1).ToString();
            txt.Height = 50;
            txt.ColorIndex = 2;
            btr.AppendEntity(txt);
            tr.AddNewlyCreatedDBObject(txt, true);
        }
        tr.Commit();
    }
}
相关推荐
qq_433099401 小时前
Ubuntu20.04从零安装IsaacSim/IsaacLab
数据库
Dlwyz1 小时前
redis-击穿、穿透、雪崩
数据库·redis·缓存
Theodore_10222 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
工业甲酰苯胺3 小时前
Redis性能优化的18招
数据库·redis·性能优化
冰帝海岸3 小时前
01-spring security认证笔记
java·笔记·spring
世间万物皆对象3 小时前
Spring Boot核心概念:日志管理
java·spring boot·单元测试
没书读了4 小时前
ssm框架-spring-spring声明式事务
java·数据库·spring
----云烟----4 小时前
QT中QString类的各种使用
开发语言·qt
lsx2024064 小时前
SQL SELECT 语句:基础与进阶应用
开发语言
小二·4 小时前
java基础面试题笔记(基础篇)
java·笔记·python