简易排序方法如下:
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();
}
}