public static void Clear() { Database acCurDb = HostApplicationServices.WorkingDatabase; Document acDoc = Application.DocumentManager.MdiActiveDocument; Editor acEdt = acDoc.Editor; /// 让用户选择是否清除块内的图元,Y:清除,N:不清除 PromptStringOptions optRecurseIn = new PromptStringOptions("是否包括块内和锁定图层内的图元?[是(Y)/否(N)]") { AllowSpaces = false, DefaultValue = "N" }; PromptResult resStringResult; do { resStringResult = acEdt.GetString(optRecurseIn); } while (resStringResult.StringResult.ToUpper() != "Y" && resStringResult.StringResult.ToUpper() != "N"); bool recurseIn = (resStringResult.StringResult.ToUpper() == "Y"); //对图形进行修改 using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { ulong countTotal = 0; ulong countCleared = 0; //遍历模型空间的清零 BlockTableRecord btRecord = (BlockTableRecord)acTrans.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(acCurDb), OpenMode.ForRead, false, true); btRecord.Clear(ref countTotal, ref countCleared, recurseIn); acEdt.WriteMessage("共检查了" + countTotal + "个对象,其中" + countCleared + "个对象的Z坐标已经清除。\n"); //提交执行 acTrans.Commit(); } }
/// <summary> /// 对传入的BlockTableRecord中的每个图元单独运行清零函数 /// </summary> /// <param name="btRecord">待清零的块定义</param> /// <param name="countTotal">检查过的图元计数</param> /// <param name="countCleared">清零的图元计数</param> /// <param name="recurseIn">是否清零块内和锁定图层内的图元的Z坐标,true:清除,false:不清除</param> /// <returns></returns> internal static void Clear(this BlockTableRecord btRecord, ref ulong countTotal, ref ulong countCleared, bool recurseIn = false) { Database acCurDb = HostApplicationServices.WorkingDatabase; List <Entity> objList = new List <Entity>(); using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { foreach (ObjectId id in btRecord) { Entity myObj = (Entity)acTrans.GetObject(id, OpenMode.ForWrite, false, true); LayerTable acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable; LayerTableRecord acLyrTblRec = acTrans.GetObject(myObj.LayerId, OpenMode.ForWrite) as LayerTableRecord; bool curLayerLocked = acLyrTblRec.IsLocked; acLyrTblRec.IsLocked = false; //对块内和锁定图层内的图元进行清零操作,递归 if (myObj is BlockReference && recurseIn) { BlockReference br = acTrans.GetObject(id, OpenMode.ForRead) as BlockReference; BlockTableRecord myBlock = (BlockTableRecord)br.BlockTableRecord.GetObject(OpenMode.ForWrite, false, true); myBlock.Clear(ref countTotal, ref countCleared, recurseIn); } //对普通图元进行操作 if (!curLayerLocked || recurseIn) { if (myObj.Clear()) { countCleared++; } } countTotal++; acLyrTblRec.IsLocked = curLayerLocked; } acTrans.Commit(); } return; }