/// <summary> /// 删除指定名称的图层 /// </summary> /// <param name="db">数据库对象</param> /// <param name="layerName">图层名</param> /// <returns>如果删除成功,则返回true,否则返回false</returns> public static bool DeleteLayer(this Database db, string layerName) { //打开层表 LayerTable lt = (LayerTable)db.LayerTableId.GetObject(OpenMode.ForRead); //如果层名为0或Defpoints,则返回(这两个图层不能删除) if (layerName == "0" || layerName == "Defpoints") { return(false); } //如果不存在名为layerName的图层,则返回 if (!lt.Has(layerName)) { return(false); } ObjectId layerId = lt[layerName];//获取名为layerName的层表记录的Id //如果要删除的图层为当前层,则返回(不能删除当前层) if (layerId == db.Clayer) { return(false); } //打开名为layerName的层表记录 LayerTableRecord ltr = (LayerTableRecord)layerId.GetObject(OpenMode.ForRead); //如果要删除的图层包含对象或依赖外部参照,则返回(不能删除这些层) lt.GenerateUsageData(); if (ltr.IsUsed) { return(false); } ltr.UpgradeOpen(); //切换层表记录为写的状态 ltr.Erase(true); //删除名为layerName的图层 return(true); //删除图层成功 }
public void testdelLayer() { Database db = HostApplicationServices.WorkingDatabase; Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; PromptStringOptions optStr = new PromptStringOptions("\nÊäÈëͼ²ãµÄÃû³Æ"); PromptResult resStr = ed.GetString(optStr); if (resStr.Status != PromptStatus.OK) { return; } String layName = resStr.StringResult; using (Transaction trans = db.TransactionManager.StartTransaction()) { LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForWrite); if (lt.Has(layName) == false) { ed.WriteMessage("\n¸Ãͼ²ã²»´æÔÚ£¡"); return; } LayerTableRecord ltr = (LayerTableRecord)trans.GetObject(lt[layName], OpenMode.ForWrite); LayerTableRecord curLtr = (LayerTableRecord)trans.GetObject(db.Clayer, OpenMode.ForRead); if (layName == curLtr.Name) { ed.WriteMessage("\n²»ÄÜɾ³ýµ±Ç°Í¼²ã£¡"); } else { ltr.Erase(true); } trans.Commit(); } }
public void DeleteReddit() { string layerName = ""; Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; using (Transaction tr = db.TransactionManager.StartTransaction()) { LayerTable layerTable; layerTable = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable; foreach (string layername in LayerNameList.GetLayerNames()) { //Select entities from layer names ObjectIdCollection objects = DrawEntity.SelectByLayer(layername); foreach (ObjectId obj in objects) { var ent = tr.GetObject(obj, OpenMode.ForWrite); ent.Erase(true); } //Now delete the layer LayerTableRecord layerTableRec = tr.GetObject(layerTable[layername], OpenMode.ForWrite) as LayerTableRecord; layerTableRec.Erase(true); } tr.Commit(); } }
public static void RemoveMyLayer() { Document doc = Application.DocumentManager.MdiActiveDocument; Database dab = doc.Database; using (Transaction trans = dab.TransactionManager.StartTransaction()) { LayerTable lyTbl = (LayerTable)trans.GetObject(dab.LayerTableId, OpenMode.ForRead); if (lyTbl.Has("MyLayer0") == true) { LayerTableRecord lyTblRec = (LayerTableRecord)trans.GetObject(lyTbl["MyLayer0"], OpenMode.ForWrite); try { lyTblRec.Erase(); doc.Editor.WriteMessage("\n'MyLayer0' was erased"); trans.Commit(); } catch { doc.Editor.WriteMessage("\n'MyLayer0' could not be erased"); } } else { doc.Editor.WriteMessage("\n'MyLayer0' does not exist"); } } }
public static void DeleteLayer(string layerName, Database db) //Удаление слоя { using (Transaction tr = db.TransactionManager.StartTransaction()) { LinetypeTable acLinTbl = tr.GetObject(db.LinetypeTableId, OpenMode.ForWrite) as LinetypeTable; var layerTable = tr.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable; foreach (var layer in layerTable) { LayerTableRecord ltr = (LayerTableRecord)tr.GetObject(layer, OpenMode.ForWrite, true); if (ltr.Name.Equals("0")) { db.Clayer = ltr.Id; //обязательная активация в текущий слой 0 } if (!ltr.Name.Equals(layerName)) { continue; } try { ltr.IsLocked = false; ltr.Erase(); break; } catch { } } tr.Commit(); } }
/// <summary> /// 删除图层 /// </summary> /// <param name="db">图形数据库</param> /// <param name="layerName">图层名</param> /// <returns>bool</returns> public static bool DeleteLayer(this Database db, string layerName) { if (layerName == "0" || layerName == "Defpoints") { return(false); } bool isDeleteOK = false; using (Transaction trans = db.TransactionManager.StartTransaction()) { LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForRead); lt.GenerateUsageData(); //判断要删除的图层名是否存在 if (lt.Has(layerName)) { LayerTableRecord ltr = (LayerTableRecord)lt[layerName].GetObject(OpenMode.ForWrite); if (!ltr.IsUsed && db.Clayer != lt[layerName]) { ltr.Erase(); isDeleteOK = true; } } else { isDeleteOK = true; } trans.Commit(); } return(isDeleteOK); }
// Deletes given layer internal static System.Boolean delLayer(Database db, String layerName, LayerTableRecord layer) { ObjectIdCollection idCollection = new ObjectIdCollection(); using (var tr = db.TransactionManager.StartOpenCloseTransaction()) { BlockTable blockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; foreach (ObjectId btrId in blockTable) { BlockTableRecord btr = tr.GetObject(btrId, OpenMode.ForWrite) as BlockTableRecord; if (btr.IsLayout) { foreach (ObjectId id in btr) { Entity ent = tr.GetObject(id, OpenMode.ForWrite) as Entity; if (ent.Layer.Equals(layerName, System.StringComparison.CurrentCultureIgnoreCase)) { // Delete entity idCollection.Add(ent.Id); ent.Erase(); } } } db.Purge(idCollection); // Delete layer layer.Erase(); } return(true); } }
/// <summary> /// 清理指定图层 /// </summary> /// <param name="strLayer">图层名</param> /// <param name="isErase">是否删除图层</param> public static void ClearLayer(string strLayer, bool isErase) { Database db = HostApplicationServices.WorkingDatabase; Autodesk.AutoCAD.DatabaseServices.TransactionManager transactionManager = db.TransactionManager; using (Autodesk.AutoCAD.DatabaseServices.Transaction trans = transactionManager.StartTransaction()) { BlockTable blkTab = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; foreach (ObjectId blkRecId in blkTab) { BlockTableRecord btr = trans.GetObject(blkRecId, OpenMode.ForRead) as BlockTableRecord; foreach (ObjectId id in btr) { Entity ent = trans.GetObject(id, OpenMode.ForRead) as Entity; if (ent.Layer == strLayer) { LayerTableRecord entLayer = trans.GetObject(ent.LayerId, OpenMode.ForRead) as LayerTableRecord; if (!(entLayer.IsFrozen || entLayer.IsLocked)) { ent.UpgradeOpen(); ent.Erase(true); } } } } trans.Commit(); } if (!isErase) { return; } using (Transaction trans = db.TransactionManager.StartTransaction()) { LayerTable laTab = trans.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable; if (laTab != null) { if (laTab.Has(strLayer)) { ObjectId layerTabRecId = laTab[strLayer]; LayerTableRecord layerTabRec = trans.GetObject(layerTabRecId, OpenMode.ForWrite) as LayerTableRecord; if (layerTabRec != null) { layerTabRec.Erase(); } } } trans.Commit(); } }
public void DeleteLayer() { Database db = HostApplicationServices.WorkingDatabase; Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; string nameNeedDelete = "fck13"; using (Transaction transaction = db.TransactionManager.StartTransaction()) { LayerTable layerTable = (LayerTable)transaction.GetObject(db.LayerTableId, OpenMode.ForWrite); LayerTableRecord currentLayer = (LayerTableRecord)transaction.GetObject(db.Clayer, OpenMode.ForWrite); if (currentLayer.Name.ToLower() == nameNeedDelete.ToLower()) { ed.WriteMessage("\nCan't delete current layer!"); } else { LayerTableRecord layerTableRecord = new LayerTableRecord(); if (layerTable.Has(nameNeedDelete)) { layerTableRecord = (LayerTableRecord)transaction.GetObject(layerTable[nameNeedDelete], OpenMode.ForWrite); if (layerTableRecord.IsErased) { ed.WriteMessage("\nThe Layer has been deleted!"); } else { ObjectIdCollection idCol = new ObjectIdCollection(); idCol.Add(layerTableRecord.ObjectId); db.Purge(idCol); if (idCol.Count == 0) { ed.WriteMessage("\nCan't delete layer with object"); } else { layerTableRecord.Erase(); } } } else { ed.WriteMessage("\nNo layer found!"); } } transaction.Commit(); } }
/// <summary> /// 删除所有未使用的图层 /// </summary> /// <param name="db"></param> public static void DeleteNotUsedLayer(this Database db) { using (Transaction trans = db.TransactionManager.StartTransaction()) { LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForRead); lt.GenerateUsageData(); foreach (ObjectId item in lt) { LayerTableRecord ltr = (LayerTableRecord)item.GetObject(OpenMode.ForWrite); if (!ltr.IsUsed) { ltr.Erase(); } } trans.Commit(); } }
/// <summary> /// 删除图层及内部数据 /// </summary> /// <param name="db"></param> /// <param name="layerName"></param> public static void DeleteLayer(this Database db, string layerName) { Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; using (Transaction trans = db.TransactionManager.StartTransaction()) { LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForWrite); LayerTableRecord currentLayer = (LayerTableRecord)trans.GetObject(db.Clayer, OpenMode.ForRead); if (currentLayer.Name.ToLower() == layerName.ToLower()) { ed.WriteMessage("\n不能删除当前层"); } else { LayerTableRecord ltr = new LayerTableRecord(); if (lt.Has(layerName)) { ltr = trans.GetObject(lt[layerName], OpenMode.ForWrite) as LayerTableRecord; if (ltr.IsErased) { ed.WriteMessage("\n此层已经被删除"); } else { ObjectIdCollection idCol = new ObjectIdCollection(); idCol.Add(ltr.ObjectId); db.Purge(idCol); if (idCol.Count == 0) { ed.WriteMessage("\n不能删除包含对象的图层"); } else { ltr.Erase(); } } } else { ed.WriteMessage("\n没有此图层"); } } trans.Commit(); } }
/// <summary> /// Deletes the layer. /// </summary> /// <param name="db">The db.</param> /// <param name="trans">The trans.</param> /// <param name="layername">The layername.</param> /// <returns></returns> public static bool DeleteLayer(Autodesk.AutoCAD.DatabaseServices.Database db, Transaction trans, string layername) //, ObjectId linetypeId, Color color, ObjectId plotStyleNameId, bool plots, bool frewezeInViewPorts) { //_logger.Debug("Start DeleteLayer"); if (trans == null) { throw new ArgumentNullException("trans", "The argument 'trans' was null."); } if (string.IsNullOrEmpty(layername)) { throw new ArgumentNullException("layername", "The argument 'layername' was null or empty."); } bool erased = false; try { using (LayerTable layerTable = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForWrite, false)) { if (layerTable.Has(layername)) { ObjectId layerId = layerTable[layername]; using (LayerTableRecord layerTableRecord = trans.GetObject(layerId, OpenMode.ForWrite, false) as LayerTableRecord) { if (!layerTableRecord.IsErased) { layerTableRecord.Erase(); erased = true; } } } } } catch (System.Exception ex) { PGA.MessengerManager.MessengerManager.AddLog("Error in DeleteLayer", ex); throw; } //_logger.Debug("End DeleteLayer"); return(erased); }
private void DeleteLayersWithPrefix(string prefix, Transaction tr) { // Open the Layer table for read var lyrTbl = (LayerTable)tr.GetObject(Doc.Database.LayerTableId, OpenMode.ForRead); foreach (ObjectId layerId in lyrTbl) { LayerTableRecord layer = (LayerTableRecord)tr.GetObject(layerId, OpenMode.ForRead); string layerName = layer.Name; if (layerName.StartsWith(prefix)) { layer.UpgradeOpen(); // cannot delete current layer: swap current layer to default layer "0" if current layer is to be deleted if (Doc.Database.Clayer == layerId) { var defaultLayerID = lyrTbl["0"]; Doc.Database.Clayer = defaultLayerID; } layer.IsLocked = false; // delete all objects on this layer // TODO: this is ugly! is there a better way to delete layer objs instead of looping through each one? var bt = (BlockTable)tr.GetObject(Doc.Database.BlockTableId, OpenMode.ForRead); foreach (var btId in bt) { var block = (BlockTableRecord)tr.GetObject(btId, OpenMode.ForRead); foreach (var entId in block) { var ent = (Entity)tr.GetObject(entId, OpenMode.ForRead); if (ent.Layer == layerName) { ent.UpgradeOpen(); ent.Erase(); } } } layer.Erase(); } } }
//create layer for signature private ObjectId createLayer(string lyname) { Editor ed = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument.Editor; Database db = HostApplicationServices.WorkingDatabase; //search the specific layer TypedValue[] filList = new TypedValue[1] { new TypedValue((int)DxfCode.LayerName, lyname) }; SelectionFilter filter = new SelectionFilter(filList); PromptSelectionResult selRes = ed.SelectAll(filter); if (selRes.Status != PromptStatus.OK && selRes.Status != PromptStatus.Error) //error means no such layer. can continue the following code { ed.WriteMessage("\nerror in getting all entities on a layer named " + lyname + " >>error:" + selRes.Status); return(ObjectId.Null); } LayerTableRecord signatureLayer = null; try { using (Transaction tr = db.TransactionManager.StartTransaction()) { if (selRes.Status == PromptStatus.OK) { ed.WriteMessage("\nTo delete entities on the signature layer"); //delete all signatures (solids) on the old layer ObjectId[] ids = selRes.Value.GetObjectIds(); foreach (ObjectId eachentid in ids) { Entity oEnt = tr.GetObject(eachentid, OpenMode.ForWrite) as Entity; oEnt.Erase(); } } ed.WriteMessage("\nTo delete signature layer"); LayerTable layerTable = tr.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable; if (layerTable.Has(lyname)) { //delete the old layer ObjectId LTR_id = layerTable[lyname]; ObjectIdCollection idCol = new ObjectIdCollection(); idCol.Add(LTR_id); db.Purge(idCol); LayerTableRecord oLTR = tr.GetObject(LTR_id, OpenMode.ForWrite) as LayerTableRecord; oLTR.Erase(); } ed.WriteMessage("\nTo create signature layer"); //create new layer signatureLayer = new LayerTableRecord(); signatureLayer.Name = lyname; layerTable.Add(signatureLayer); tr.AddNewlyCreatedDBObject(signatureLayer, true); tr.Commit(); } if (signatureLayer != null) { return(signatureLayer.ObjectId); } else { return(ObjectId.Null); } } catch (Autodesk.AutoCAD.Runtime.Exception ex) { ed.WriteMessage("\nerror in create layer " + ex.ToString()); return(ObjectId.Null); } }
public static void DeleteObstLayer(ObjectId objectId) { Editor ed = acApp.DocumentManager.MdiActiveDocument.Editor; Document acDoc = acApp.DocumentManager.MdiActiveDocument; Database acCurDb = acDoc.Database; // Upewnij sie czy istnieje warstwa inert Utils.Layers.CreateLayer("!FDS_OBST[inert](0)"); using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // Open the Layer table for read LayerTable acLyrTbl; acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable; LayerTableRecord acLyrTblRec; acLyrTblRec = acTrans.GetObject(objectId, OpenMode.ForWrite) as LayerTableRecord; string sLayerName = acLyrTblRec.Name; if (acLyrTbl.Has(sLayerName) == true) { Regex regEx = new Regex(@"\[(.+)\]", RegexOptions.IgnoreCase); Match match = regEx.Match(sLayerName); ed.WriteMessage("\nMatch success: " + match.Success.ToString()); string layerName = ""; if (match.Success) { System.Text.RegularExpressions.Group group = match.Groups[1]; layerName = group.ToString(); } ed.WriteMessage("\nOld layer: " + layerName); TypedValue[] filterlist = new TypedValue[1]; filterlist[0] = new TypedValue(8, "!FDS_OBST*" + layerName + "*"); SelectionFilter filter = new SelectionFilter(filterlist); // Request for objects to be selected in the drawing area PromptSelectionResult acSSPrompt; acSSPrompt = ed.SelectAll(filter); if (acSSPrompt.Status == PromptStatus.OK) { SelectionSet acSSet = acSSPrompt.Value; // Step through the objects in the selection set foreach (SelectedObject acSSObj in acSSet) { // Check to make sure a valid SelectedObject object was returned if (acSSObj != null) { // Open the selected object for write Entity acEnt = acTrans.GetObject(acSSObj.ObjectId, OpenMode.ForWrite) as Entity; acEnt.Layer = "!FDS_OBST[inert](0)"; } } } ObjectIdCollection acObjIdColl = new ObjectIdCollection(); // Step through the Layer table and print each layer name foreach (ObjectId acObjId in acLyrTbl) { acLyrTblRec = acTrans.GetObject(acObjId, OpenMode.ForRead) as LayerTableRecord; if (acLyrTblRec.Name.Contains("!FDS_OBST[" + layerName + "]")) { acObjIdColl.Add(acLyrTbl[acLyrTblRec.Name]); ed.WriteMessage("\n" + acLyrTblRec.Name); } } // Check to see if it is safe to erase layer acCurDb.Purge(acObjIdColl); if (acObjIdColl.Count > 0) { foreach (ObjectId acObjId in acObjIdColl) { LayerTableRecord acLyrObstTblRec = acTrans.GetObject(acObjId, OpenMode.ForWrite) as LayerTableRecord; try { // Erase the unreferenced layer acLyrObstTblRec.Erase(true); // Save the changes and dispose of the transaction } catch (System.Exception e) { // Layer could not be deleted ed.WriteMessage("WizFDS exception:\n" + e.Message); } } acTrans.Commit(); } } } }
public static void delLayer(Transaction acTrans, Database db, String layerName) { // If the layer is locked, unlock it using (LayerTable lt = acTrans.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable) { if (lt.Has(layerName)) { ObjectId layerOID = lt[layerName]; using (LayerTableRecord ltr = acTrans.GetObject(layerOID, OpenMode.ForWrite) as LayerTableRecord) { if (ltr.IsLocked) { ltr.IsLocked = false; } } } } // Delete all objects on layer ObjectIdCollection coutObjs = new ObjectIdCollection(); using (BlockTable bt = acTrans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable) { foreach (ObjectId oid in bt) { using (BlockTableRecord btr = acTrans.GetObject(oid, OpenMode.ForWrite) as BlockTableRecord) { foreach (ObjectId oidEnt in btr) { Entity ent = acTrans.GetObject(oidEnt, OpenMode.ForRead) as Entity; if (ent.Layer == layerName) { ent.UpgradeOpen(); coutObjs.Add(ent.Id); ent.Erase(true); } } } } } db.Purge(coutObjs); // Delete layer using (LayerTable lt = acTrans.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable) { if (lt.Has(layerName)) { // Check to see if it is safe to erase layer ObjectIdCollection acObjIdColl = new ObjectIdCollection(); acObjIdColl.Add(lt[layerName]); db.Purge(acObjIdColl); if (acObjIdColl.Count > 0) { LayerTableRecord acLyrTblRec = acTrans.GetObject(acObjIdColl[0], OpenMode.ForWrite) as LayerTableRecord; acLyrTblRec.Erase(true); } else { //Wrong: https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2016/ENU/AutoCAD-NET/files/GUID-DF8A64D3-AE09-4BCE-B9E1-1B642DA4FCFF-htm.html LayerTableRecord acLyrTblRec = acTrans.GetObject(acObjIdColl[0], OpenMode.ForWrite) as LayerTableRecord; acLyrTblRec.Erase(true); } db.Purge(acObjIdColl); } } }