首页 > 代码库 > .net中取得CAD的当前图层名称

.net中取得CAD的当前图层名称

 /// <summary>
        /// 取得当前图层名称
        /// </summary>
        /// <param name="db">数据库对象</param>
        /// <returns></returns>
        public  static string  GetCurrentLayer(this Database db)
        {
            //Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                //获取当前图形中所有颜色为红色的图层层名
                var CurrentLayer = (from layer in db.GetAllLayers()
                                 where layer.Id==db.Clayer
                                 select layer.Name).ToList();
                return CurrentLayer[0];
                trans.Commit();
            }
        }

上面这个办法比较麻烦,可以按下面的办法做:

        /// <summary>
        /// 取得当前图层名称
        /// </summary>
        /// <param name="db">数据库对象</param>
        /// <returns></returns>
        public  static string  GetCurrentLayer(this Database db)
        {
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                LayerTableRecord ltr = (LayerTableRecord)db.Clayer.GetObject(OpenMode.ForRead);
                return ltr.Name;
                trans.Commit();
            }
        }