• AutoCad 二次开发 .net 之层表的增加 删除 修改图层颜色 遍历 设置当前层 冻结图层


    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;
                }
    
            }
    View Code

     六、冻结图层:

     [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();
    
                }
    
    
            }
  • 相关阅读:
    N皇后求解。万万没想到,只用一个一维数组就搞定了。还体现了回溯。
    图的存储与实现(一),使用邻接矩阵
    201671010139 2016-2017-2 《Java程序设计》关于java的初学体验总结
    题目常用解法
    【问题】如何批量导出AI文件里内嵌的图片
    安全算法基础(一)
    对安全的认知
    6月
    渗透--还是tp
    c++学习————VC报错解决方案(vc2013)
  • 原文地址:https://www.cnblogs.com/HelloQLQ/p/11848082.html
Copyright © 2020-2023  润新知