各种排序分组

简易排序方法如下:

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();
    }
}
相关推荐
云和数据.ChenGuang9 分钟前
Django 应用安装脚本 – 如何将应用添加到 INSTALLED_APPS 设置中 原创
数据库·django·sqlite
woshilys37 分钟前
sql server 查询对象的修改时间
运维·数据库·sqlserver
Hacker_LaoYi38 分钟前
SQL注入的那些面试题总结
数据库·sql
2401_857439692 小时前
SSM 架构下 Vue 电脑测评系统:为电脑性能评估赋能
开发语言·php
建投数据2 小时前
建投数据与腾讯云数据库TDSQL完成产品兼容性互认证
数据库·腾讯云
SoraLuna2 小时前
「Mac畅玩鸿蒙与硬件47」UI互动应用篇24 - 虚拟音乐控制台
开发语言·macos·ui·华为·harmonyos
xlsw_2 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
Hacker_LaoYi3 小时前
【渗透技术总结】SQL手工注入总结
数据库·sql
岁月变迁呀3 小时前
Redis梳理
数据库·redis·缓存
独行soc3 小时前
#渗透测试#漏洞挖掘#红蓝攻防#护网#sql注入介绍06-基于子查询的SQL注入(Subquery-Based SQL Injection)
数据库·sql·安全·web安全·漏洞挖掘·hw