AutoCad 二次开发 .net 之层表的增加 删除 修改图层颜色 遍历 设置当前层
AutoCad 二次开发 .net 之层表的增加 删除 修改图层颜色 遍历 设置当前层
我理解的图层的作用大概是把相同作用的功能聚集在一起,以便好选择。比如,把一副图块中的所有标注定义为一个图层,把编号定义为一个图层,把相同的块参照定义为一个图层。图层表的操作和块表的操作类似。
在这里我就分享一下自己从书《AUTOCAD VBA&VB.NET开发基础与实例教程(C#版) 第2版》学习到的关于图层的一些操作的代码,我觉得主要是图层的删除需要注意一下。
使用的成员变量:
Document Doc = Application.DocumentManager.MdiActiveDocument;
Editor Ed = Application.DocumentManager.MdiActiveDocument.Editor;
Database Db = Application.DocumentManager.MdiActiveDocument.Database;
一、图层的删除 代码:
/// <summary> /// 图层0和图层Defpoints不能被删除,当前图层不能被删除,图层上有块参照,实体时不能被删除 /// 删除前需刷新,使用LayerTableRecord.GenerateUsageData(); /// </summary> public bool DeleteLayer(Database db, string layerName) { using (var trans = db.TransactionManager.StartTransaction()) { var lyTbl = trans.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable; if (!lyTbl.Has(layerName)) return false; if (layerName == "0" || layerName == "Defpoints") return false; ObjectId oId = lyTbl[layerName]; if (oId == db.Clayer) return false; var lyTblRec = trans.GetObject(lyTbl[layerName], OpenMode.ForRead) as LayerTableRecord; lyTbl.GenerateUsageData(); if (lyTblRec.IsUsed) return false; lyTblRec.UpgradeOpen(); lyTblRec.Erase(true); trans.Commit(); return true; } }
二、图层的增加:
public ObjectId AddLayer(Database db, string layerName) { ObjectId oId = ObjectId.Null; using (var trans = db.TransactionManager.StartTransaction()) { var lyerTbl = db.LayerTableId.GetObject(OpenMode.ForWrite) as LayerTable; if (lyerTbl.Has(layerName)) { trans.Commit(); return lyerTbl[layerName]; } var lyerTblRec = new LayerTableRecord(); lyerTblRec.Name = layerName; lyerTbl.Add(lyerTblRec); trans.AddNewlyCreatedDBObject(lyerTblRec, true); lyerTbl.DowngradeOpen(); trans.Commit(); return lyerTbl[layerName]; } }
三、修改图层颜色:
public bool SetLayerColor(Database db, string layerName, short colorIndexs) { using (var trans = db.TransactionManager.StartTransaction()) { var lyTbl = db.LayerTableId.GetObject(OpenMode.ForRead) as LayerTable; if (lyTbl.Has(layerName)) { var lyTblRec = trans.GetObject(lyTbl[layerName], OpenMode.ForWrite) as LayerTableRecord; if (colorIndexs < 0 || colorIndexs > 255) { colorIndexs = 1; } lyTblRec.Color = Color.FromColorIndex(ColorMethod.ByAci, colorIndexs); lyTblRec.DowngradeOpen(); trans.Commit(); return true; } else { return false; } } }
四、遍历图层:
public List<LayerTableRecord> GetAllLayer(Database db) { List<LayerTableRecord> listLyTblRec = new List<LayerTableRecord>(); using (var trans = db.TransactionManager.StartTransaction()) { var lyTbl = trans.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable; foreach (ObjectId oId in lyTbl) { var lyTblRec = trans.GetObject(oId, OpenMode.ForRead) as LayerTableRecord; listLyTblRec.Add(lyTblRec); } return listLyTblRec; } }
五、设置当前图层:
Databse.Clayer表示当前图层 public bool SetCurrentLayer(Database db, string layerName) { using (var trans = db.TransactionManager.StartTransaction()) { var lyTbl = trans.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable; if (!lyTbl.Has(layerName)) return false; //var lyTblRec = trans.GetObject(lyTbl[layerName], OpenMode.ForRead) as LayerTableRecord; if (db.Clayer != lyTbl[layerName]) { db.Clayer = lyTbl[layerName]; } return true; } }
六、冻结图层:
[CommandMethod("ECDLayerHide")] public void HiddenSelectLayer() { var propSel = new PromptSelectionOptions(); var propRes = Ed.GetSelection(propSel); if (propRes.Status != PromptStatus.OK) { return; } ObjectId[] oIds = propRes.Value.GetObjectIds(); using(var trans = Db.TransactionManager.StartTransaction()) { var lyTbl = trans.GetObject(Db.LayerTableId, OpenMode.ForRead) as LayerTable; var blkTbl = trans.GetObject(Db.BlockTableId, OpenMode.ForRead) as BlockTable; for (int i = 0; i < oIds.Length; i++) { var ent = trans.GetObject(oIds[i], OpenMode.ForRead) as Entity; var layerId = ent.LayerId; var lyTblRec = trans.GetObject(layerId, OpenMode.ForWrite) as LayerTableRecord; if (lyTblRec.Name == "0") { return; } lyTblRec.IsFrozen = true; lyTblRec.IsHidden = true; } trans.Commit(); } }