啥也不说,放代码
namespace JoinBox { public enum XrefEnum { /// <summary> /// 卸载 /// </summary> UnloadXrefs, /// <summary> /// 重载 /// </summary> ReloadXrefs, /// <summary> /// 拆离 /// </summary> DetachXref, /// <summary> /// 绑定 /// </summary> BindXrefs, } public static class XrefTool { /// <summary> /// 修改外部参照 /// </summary> /// <param name="db">数据库</param> /// <param name="xe">设置的枚举状态</param> /// <param name="names">要处理的参照名称,null就处理所有的参照</param> public static void SetXref(this Database db, XrefEnum xe, IEnumerable<string> names = null) { db.ResolveXrefs(false, true);//解析外部参照(使用线性引擎,只做新的) var ids = new ObjectIdCollection(); var xg = db.GetHostDwgXrefGraph(true);//神马是僵尸参照 for (int i = 0; i < xg.NumNodes; i++) { var xNode = xg.GetXrefNode(i) as XrefGraphNode; // if (xNode.XrefStatus != XrefStatus.Resolved) //状态判断 if (xNode.BlockTableRecordId.IsOk()) { if (names != null) { foreach (var item in names) { //只加入名称相同的 if (item.ToUpper() == xNode.Name.ToUpper()) { ids.Add(xNode.BlockTableRecordId); } } } else { //每个都加入 ids.Add(xNode.BlockTableRecordId); } } } // btRec.IsFromOverlayReference //是覆盖? // XrefStatus 外部参照状态 // GetXrefDatabase 外部参照数据库 if (ids.Count == 0) { return; } switch (xe) { case XrefEnum.BindXrefs://绑定后会自动拆离 { db.BindXrefs(ids, true); } break; case XrefEnum.DetachXref: //拆离 { foreach (ObjectId item in ids) { db.DetachXref(item); } } break; case XrefEnum.UnloadXrefs://卸载 { db.UnloadXrefs(ids); } break; case XrefEnum.ReloadXrefs://重载 { db.ReloadXrefs(ids); } break; } } } } /// <summary> /// id有效,未被删除 /// </summary> /// <param name="id"></param> /// <returns></returns> public static bool IsOk(this ObjectId id) { return !id.IsNull && id.IsValid && !id.IsErased && !id.IsEffectivelyErased && id.IsResident; }
来源:https://www.cnblogs.com/JJBox/p/12407217.html