使用工具==CAD2019+VS2022
主要作用==针对复杂的CAD图纸,通过图层来删除指定的实体保留其他的实体不受干扰
效果图(框选删除井的编号而不影响其他的东西)
代码部分
public void Delet()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = acadApp.Application.DocumentManager.MdiActiveDocument.Editor;
//获取图层名称
string nameLay = GetLayername();//自定义函数
TypedValue[] types = new TypedValue[1];
types[0] = new TypedValue((int)DxfCode.LayerName, nameLay);
SelectionFilter filter = new SelectionFilter(types);
PromptSelectionResult psResult = ed.GetSelection(filter);
if (psResult.Status == PromptStatus.OK)
{
ObjectId[] ids= psResult.Value.GetObjectIds();
using (Transaction trans = db.TransactionManager.StartTransaction())
{
foreach (ObjectId item in ids)
{
DBObject ents = item.GetObject(OpenMode.ForWrite);
ents.Erase();
}
trans.Commit();
}
}
}
//自定义函数
public static string GetLayername()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = acadApp.Application.DocumentManager.MdiActiveDocument.Editor;
string layName = null;
PromptSelectionResult psLayer = ed.GetSelection();
if (psLayer.Status == PromptStatus.OK)
{
ObjectId[] ids = psLayer.Value.GetObjectIds();
using (Transaction trans = db.TransactionManager.StartTransaction())
{
foreach (var item in ids)
{
Entity ents = trans.GetObject(item, OpenMode.ForRead) as Entity;
layName = ents.Layer;
}
}
}
return layName;
}