using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using GrxCAD.ApplicationServices; using GrxCAD.DatabaseServices; using GrxCAD.EditorInput; using System.Collections; using GrxCAD.Geometry; using System.IO; using System.Windows.Forms; namespace HCTools { class LayerChange { public static string dwgpath;//dwg文件所在位置 public static List lys;//要删除/打开/关闭的图层 public static string savepath;//保存位置 public static int flag; public void chagly() { //try //{ List dwgname = GetNameList(dwgpath, ".dwg");//获取dwg文件 if (dwgname.Count == 0) { MessageBox.Show("没有找到dwg文件!", "警告"); return; } foreach (string dwgfile in dwgname) { string dwg = dwgpath + "\\" + dwgfile; Document doc = GrxCAD.ApplicationServices.Application.DocumentManager.Add(dwg); if (flag == 0|| flag == 3 || flag == 4) { Delete(lys, doc, dwgfile); } else if (flag == 1) { Closely(lys, doc, dwgfile); } else if (flag == 2) { Openly(lys, doc, dwgfile); } doc.CloseAndDiscard(); } //} //catch (GrxCAD.Runtime.Exception ex) //{ // GrxCAD.ApplicationServices.Application.ShowAlertDialog("Error:\n" + ex.Message); //} } private void Delete(List lys, Document doc, string dwgfile) { Database database = doc.Database; Editor ed = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; if (flag == 0) { using (Transaction trans = doc.Database.TransactionManager.StartTransaction()) { LayerTable layerTable = trans.GetObject(database.LayerTableId, OpenMode.ForRead) as LayerTable; for (int i = 0; i < lys.Count; i++) { if (layerTable.Has(lys[i])) { TypedValue[] value = new TypedValue[] { new TypedValue((int)DxfCode.LayerName,lys[i]) }; SelectionFilter filter = new SelectionFilter(value); PromptSelectionResult psr = ed.SelectAll(filter); if (psr.Status == PromptStatus.OK) { SelectionSet ss = psr.Value; ObjectIdCollection idcoll = new ObjectIdCollection(ss.GetObjectIds()); for (int ii = 0; ii < idcoll.Count; ii++) { Entity ent = trans.GetObject(idcoll[ii], OpenMode.ForWrite) as Entity; ent.Erase(); } } } } database.SaveAs(savepath + "\\" + dwgfile, true, DwgVersion.AC1021, doc.Database.SecurityParameters); ed.WriteMessage("删除成功!\n"); trans.Commit(); } } if (flag == 3) { using (Transaction transaction = database.TransactionManager.StartTransaction()) { LayerTable layerTable = transaction.GetObject(database.LayerTableId, OpenMode.ForRead) as LayerTable; foreach (ObjectId objid in layerTable) { LayerTableRecord layerTableRecord = transaction.GetObject(objid, OpenMode.ForWrite) as LayerTableRecord; //if (layerTableRecord == null) // continue; layerTableRecord.IsLocked = false; if (database.Clayer == layerTableRecord.ObjectId) continue; TypedValue[] value = new TypedValue[] { new TypedValue((int)DxfCode.LayerName,layerTableRecord.Name) }; SelectionFilter filter = new SelectionFilter(value); PromptSelectionResult psr = ed.SelectAll(filter); if (psr.Status == PromptStatus.Error&& lys.Contains(layerTableRecord.Name)) layerTableRecord.Erase(); } transaction.Commit(); database.SaveAs(savepath + "\\" + dwgfile, true, DwgVersion.AC1021, doc.Database.SecurityParameters); } ed.WriteMessage("删除成功!\n"); } if (flag == 4) { using (Transaction transaction = database.TransactionManager.StartTransaction()) { LayerTable layerTable = transaction.GetObject(database.LayerTableId, OpenMode.ForRead) as LayerTable; for (int i = 0; i < lys.Count; i++) { if (layerTable.Has(lys[i])) { LayerTableRecord layerTableRecord = transaction.GetObject(layerTable[lys[i]], OpenMode.ForWrite) as LayerTableRecord; if (layerTableRecord == null) continue; if (database.Clayer == layerTableRecord.ObjectId) { database.Clayer = layerTable["0"]; } layerTableRecord.IsLocked = false; layerTableRecord.Erase(); BlockTable bt = transaction.GetObject(database.BlockTableId, OpenMode.ForRead) as BlockTable; foreach (ObjectId btrId in bt) { BlockTableRecord btr = transaction.GetObject(btrId, OpenMode.ForRead) as BlockTableRecord; foreach (ObjectId entId in btr) { Entity entity = transaction.GetObject(entId, OpenMode.ForRead) as Entity; if (entity.Layer == lys[i]) { entity.UpgradeOpen(); try { entity.Erase(); } catch { continue; } } } } } } database.SaveAs(savepath + "\\" + dwgfile, true, DwgVersion.AC1021, doc.Database.SecurityParameters); transaction.Commit(); ed.WriteMessage("删除成功!\n"); } } } private void Openly(List lys, Document doc, string dwgfile) { Database database = doc.Database; Editor ed = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; using (Transaction transaction = database.TransactionManager.StartTransaction()) { LayerTable layerTable = transaction.GetObject(database.LayerTableId, OpenMode.ForRead) as LayerTable; for (int i = 0; i < lys.Count; i++) { if (layerTable.Has(lys[i])) { LayerTableRecord layerTableRecord = transaction.GetObject(layerTable[lys[i]], OpenMode.ForWrite) as LayerTableRecord; if (layerTableRecord == null) continue; layerTableRecord.IsOff = false; } } database.SaveAs(savepath + "\\" + dwgfile, true, DwgVersion.AC1021, doc.Database.SecurityParameters); transaction.Commit(); } ed.WriteMessage("打开成功!\n"); } private void Closely(List lys, Document doc, string dwgfile) { Database database = doc.Database; Editor ed = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; using (Transaction transaction = database.TransactionManager.StartTransaction()) { LayerTable layerTable = transaction.GetObject(database.LayerTableId, OpenMode.ForRead) as LayerTable; for (int i = 0; i < lys.Count; i++) { if (layerTable.Has(lys[i])) { LayerTableRecord layerTableRecord = transaction.GetObject(layerTable[lys[i]], OpenMode.ForWrite) as LayerTableRecord; if (layerTableRecord == null) continue; layerTableRecord.IsOff = true; } } database.SaveAs(savepath + "\\" + dwgfile, true, DwgVersion.AC1021, doc.Database.SecurityParameters); transaction.Commit(); } ed.WriteMessage("关闭成功!\n"); } /// /// 获取文件夹中相应格式的所有文件 /// private static List GetNameList(String filespath, String filetype) { DirectoryInfo root = new DirectoryInfo(filespath); List filename = new List(); //文件名 FileInfo[] files = root.GetFiles(); foreach (FileInfo file in files) { if (file.Name.Substring(file.Name.Length - 4, 4) != filetype) continue; else filename.Add(file.Name); } return filename; } } }