Xóa tất cả các thực thể bên trong một hình chữ nhật
Contents
[
Hide
]Cách xóa tất cả các thực thể bên trong một hình chữ nhật
Vấn đề: Cách xóa tất cả các thực thể bên trong một hình chữ nhật (CADNET-1351).
Mẹo: Điều này yêu cầu lấy tất cả các thực thể, sau đó tìm một hình chữ nhật cho chúng, sau đó kiểm tra xem thực thể có nằm trong hình chữ nhật bạn muốn hay không và xóa nó.
Ví dụ:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cadImage.GetBounds(); | |
foreach (CadBaseEntity entity in cadImage.Entities) | |
{ | |
if (entity.TypeName == CadEntityTypeName.TEXT) | |
{ | |
System.Console.WriteLine(entity.Bounds[0].X + " " + entity.Bounds[0].Y + " " + entity.Bounds[0].Z + " " + entity.Bounds[1].X + " " + entity.Bounds[1].Y + " " + entity.Bounds[1].Z); | |
} | |
} | |
DeleteAllEntities(cadImage, new Rect(new PointF(77, 29), new PointF(128, 49))); | |
private static void DeleteAllEntities(CadImage cadImage, Rect rectangle) | |
{ | |
foreach (CadBaseEntity baseEntity in cadImage.Entities) | |
{ | |
if (baseEntity.LayerName != "0") | |
{ | |
continue; | |
} | |
if (baseEntity.SpaceMode != CadEntitySpaceMode.ModelSpace) | |
{ | |
continue; | |
} | |
List<Cad3DPoint> entityBounds = baseEntity.Bounds; | |
PointF minPoint = new PointF((float)entityBounds[0].X, (float)entityBounds[0].Y); | |
PointF maxPoint = new PointF((float)entityBounds[1].X, (float)entityBounds[1].Y); | |
Rect entityRect = new Rect(minPoint, maxPoint); | |
if (rectangle.Contains(entityRect)) | |
{ | |
cadImage.RemoveEntity(baseEntity); | |
} | |
} | |
} | |
private class Rect | |
{ | |
public PointF Min; | |
public PointF Max; | |
public Rect(PointF min, PointF max) | |
{ | |
this.Min = min; | |
this.Max = max; | |
} | |
public bool Contains(Rect rect) | |
{ | |
return this.Min.X <= rect.Min.X && this.Max.X >= rect.Max.X && this.Min.Y <= rect.Min.Y | |
&& this.Max.Y >= rect.Max.Y; | |
} | |
} |