123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- 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<string> lys;//要删除/打开/关闭的图层
- public static string savepath;//保存位置
- public static int flag;
-
- public void chagly()
- {
- //try
- //{
- List<String> 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<string> 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<string> 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<string> 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");
- }
-
- /// <summary>
- /// 获取文件夹中相应格式的所有文件
- /// </summary>
- private static List<String> GetNameList(String filespath, String filetype)
- {
- DirectoryInfo root = new DirectoryInfo(filespath);
- List<String> filename = new List<string>(); //文件名
- 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;
- }
-
- }
- }
|