AutoCAD .Net禁止圖元被刪除的方法
本文為大家分享了AutoCAD .Net禁止圖元被刪除的具體代碼,供大家參考,具體內(nèi)容如下
禁止圖元被用戶刪除可以通過 ObjectOverrule 實(shí)現(xiàn)。
以下代碼:
1、命令 AddEraseOverrule 創(chuàng)建兩個(gè)圖元并禁止被刪除。
2、命令 RemoveEraseOverrule 移除刪除限制。
static EraseOverrule eraseRule = null; public class EraseOverrule : ObjectOverrule { public override void Erase(DBObject dbObject, bool erasing) { throw new Autodesk.AutoCAD.Runtime.Exception( Autodesk.AutoCAD.Runtime.ErrorStatus.NotApplicable); //base.Erase(dbObject, erasing); } } [CommandMethod("AddEraseOverrule")] public static void AddEraseOverrule() { if (eraseRule == null) { eraseRule = new EraseOverrule(); Overrule.AddOverrule(RXObject.GetClass(typeof(Entity)), eraseRule, true); Overrule.Overruling = true; eraseRule.SetIdFilter(CreateEntities()); } } [CommandMethod("RemoveEraseOverrule")] public static void RemoveEraseOverrule() { if (eraseRule != null) { Overrule.Overruling = false; Overrule.RemoveOverrule(RXObject.GetClass(typeof(Entity)), eraseRule); eraseRule.Dispose(); eraseRule = null; } } private static ObjectId[] CreateEntities() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; ObjectId[] oids = new ObjectId[2]; using (Transaction tr = db.TransactionManager.StartTransaction()) { BlockTableRecord space = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord; // Line line = new Line(); line.StartPoint = new Point3d(0, 0, 0); line.EndPoint = new Point3d(100, 100, 0); space.AppendEntity(line); tr.AddNewlyCreatedDBObject(line, true); oids[0] = line.ObjectId; // Circle circle = new Circle(); circle.Center = new Point3d(0, 0, 0); circle.Radius = 50; space.AppendEntity(circle); tr.AddNewlyCreatedDBObject(circle, true); oids[1] = circle.ObjectId; tr.Commit(); } return oids; }
1、類 EraseOverrule 繼承自 ObjectOverrule,并重寫了 Erase 方法。
在該方法中,通過拋異常而不是執(zhí)行刪除操作,禁止圖元被刪除。
2、SetIdFilter 確保該規(guī)則只對特定 ObjectId 的圖元有效。
ObjectOverrule 還有方法 SetXDataFilter,可以用來設(shè)置只對擁有特定 XData 的對象有效。
參考文章: Prevent deletion/erasing of entity
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ScriptManager.RegisterStartupScript()方法在ajax頁面無效的解決方法
ScriptManager.RegisterStartupScript()方法在ajax頁面無效的解決方法2010-03-03ASP.NET實(shí)現(xiàn)個(gè)人信息注冊頁面并跳轉(zhuǎn)顯示
這篇文章主要介紹了ASP.NET實(shí)現(xiàn)個(gè)人信息注冊頁面并跳轉(zhuǎn)顯示的相關(guān)資料,本文圖文并茂給大家介紹的非常詳細(xì),需要的朋友可以參考下2016-11-11Coolite Cool Study 1 在Grid中用ComboBox 來編輯數(shù)據(jù)
作為Coolite的第一個(gè)教程,我想展現(xiàn)給大家能夠體現(xiàn)Coolite強(qiáng)大的例子(當(dāng)然也比官方例子稍微復(fù)雜一點(diǎn))。2009-05-05ASP.NET Core實(shí)現(xiàn)中間件的幾種方式
這篇文章介紹了ASP.NET Core實(shí)現(xiàn)中間件的幾種方式,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08WEB上調(diào)用HttpWebRequest奇怪問題的解決方法
WEB上調(diào)用HttpWebRequest奇怪問題的解決方法...2007-04-04