重叠的图行进行分组,效果如下:
纵向投影重叠(横向移动冲突)可以分组:
纵向冲突也可以分组:
也可根据颜色不同分组:
部分代码如下,完整代码见文章下方名片
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;
}