明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 13627|回复: 7

[资源] CAD二次开发基础之图层管理

[复制链接]
发表于 2014-4-2 22:22 | 显示全部楼层 |阅读模式
本人初学CAD二次开发,已有半载,感觉中途还是走了很多弯路,在此共享些基础东西。和前辈们探讨。
本文主要是图层管理,如常见的新建、关闭、删除等。基本上都是基础东西,只列出了关键代码,没写注释。
  • 新建图层
  1. /// <summary>
  2.         /// create new layer
  3.         /// </summary>
  4.         /// <param name="layerName"></param>
  5.         public void CreateLayer(String layerName)
  6.         {
  7.             Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  8.             Database db = doc.Database;
  9.             Transaction trs = db.TransactionManager.StartTransaction();
  10.             using (trs)
  11.             {
  12.                 LayerTable lt = trs.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;
  13.                 if (!lt.Has(layerName))
  14.                 {
  15.                     LayerTableRecord ltr = new LayerTableRecord();
  16.                     ltr.Name = layerName;
  17.                     lt.Add(ltr);
  18.                     trs.AddNewlyCreatedDBObject(ltr, true);
  19.                 }
  20.                 trs.Commit();
  21.             }
  22.         }
  • 判断图层是否存在
  1. /// <summary>
  2.         /// judge if exist layer
  3.         /// </summary>
  4.         /// <param name="layerName"></param>
  5.         /// <returns></returns>
  6.         public bool IsExistLayer(String layerName)
  7.         {
  8.             bool retval = false;
  9.             Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  10.             Database db = doc.Database;
  11.             Transaction trs = db.TransactionManager.StartTransaction();
  12.             using (trs)
  13.             {
  14.                 LayerTable lt = trs.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
  15.                 if (lt.Has(layerName))
  16.                 {
  17.                     retval = true;
  18.                 }
  19.                 trs.Commit();
  20.             }
  21.             return retval;
  22.         }





  • 锁定图层
  1. /// <summary>
  2.         /// lock layer
  3.         /// </summary>
  4.         /// <param name="layerName"></param>
  5.         public void LockLayer(String layerName)
  6.         {
  7.             if (!IsExistLayer(layerName))
  8.             {
  9.                 return;
  10.             }            
  11.             Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  12.             Database db = doc.Database;
  13.             Transaction trs = db.TransactionManager.StartTransaction();
  14.             using (trs)
  15.             {
  16.                 LayerTable lt = trs.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
  17.                 LayerTableRecord ltr = trs.GetObject(lt[layerName], OpenMode.ForWrite) as LayerTableRecord;
  18.                 if (!ltr.IsLocked)
  19.                 {
  20.                     ltr.IsLocked = true;
  21.                 }
  22.                 trs.Commit();
  23.             }
  24.         }



  • 删除图层
  1. /// <summary>
  2.         /// erase layer
  3.         /// </summary>
  4.         /// <param name="layerName"></param>
  5.         /// <returns></returns>
  6.         public bool EraseLayer(String layerName)
  7.         {
  8.             bool retval = false;
  9.             if (!IsExistLayer(layerName))
  10.             {
  11.                 return retval;
  12.             }
  13.             Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  14.             Database db = doc.Database;
  15.             Transaction trs = db.TransactionManager.StartTransaction();
  16.             using (trs)
  17.             {
  18.                 LayerTable lt = trs.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;
  19.                 LayerTableRecord ltr = trs.GetObject(lt[layerName], OpenMode.ForWrite) as LayerTableRecord;
  20.                 lt.GenerateUsageData();
  21.                 if ((db.Clayer == lt[layerName]) || ltr.IsUsed
  22.                     || layerName == "0" || layerName == "Defpoints")
  23.                 {
  24.                     trs.Commit();
  25.                     return retval;
  26.                 }
  27.                 ltr.Erase(true);
  28.                 retval = true;
  29.                 trs.Commit();
  30.             }

  31.             return retval;
  32.         }



  • 设置当前图层
  1.         /// <summary>
  2.         /// set layer to current layer
  3.         /// </summary>
  4.         /// <param name="layerName"></param>
  5.         public void SetLayerCurrent(String layerName)
  6.         {
  7.             if (!IsExistLayer(layerName))
  8.             {
  9.                 return;
  10.             }
  11.             Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  12.             Database db = doc.Database;
  13.             Transaction trs = db.TransactionManager.StartTransaction();
  14.             using (trs)
  15.             {
  16.                 LayerTable lt = trs.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
  17.                 db.Clayer = lt[layerName];
  18.                 trs.Commit();
  19.             }
  20.         }



  • 获取所有图层名称
  1. /// <summary>
  2.         /// get all layers names
  3.         /// </summary>
  4.         /// <returns></returns>
  5.         public List<String> GetAllLayersName()
  6.         {
  7.             List<String> layers = new List<String>();
  8.             Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  9.             Database db = doc.Database;
  10.             Transaction trs = db.TransactionManager.StartTransaction();
  11.             using (trs)
  12.             {
  13.                 LayerTable lt = trs.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
  14.                 foreach (ObjectId id in lt)
  15.                 {
  16.                     LayerTableRecord ltr = trs.GetObject(id, OpenMode.ForRead) as LayerTableRecord;
  17.                     layers.Add(ltr.Name);
  18.                 }
  19.                 trs.Commit();
  20.             }
  21.             return layers;
  22.         }



  • 获取图层上的实体id
  1. /// <summary>
  2.         /// get layer entity id
  3.         /// </summary>
  4.         /// <param name="entitys"></param>
  5.         public void GetLayerEntity(String layerName, ref List<ObjectId> entitys)
  6.         {
  7.             if (!IsExistLayer(layerName))
  8.             {
  9.                 return;
  10.             }
  11.             
  12.             Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  13.             Database db = doc.Database;
  14.             Editor ed = doc.Editor;
  15.             TypedValue []values = { new TypedValue((int)DxfCode.LayerName, layerName)};
  16.             SelectionFilter filters = new SelectionFilter(values);            
  17.             PromptSelectionResult result = ed.SelectAll();
  18.             if (result.Status == PromptStatus.OK)
  19.             {
  20.                 ObjectId []ids = result.Value.GetObjectIds();
  21.                 foreach (ObjectId id in ids)
  22.                 {
  23.                     entitys.Add(id);
  24.                 }
  25.             }
  26.             else
  27.             {
  28.                 return;
  29.             }
  30.             
  31.         }



发表于 2014-4-3 22:00 | 显示全部楼层
本帖最后由 cdinten 于 2014-4-3 22:04 编辑

嗯,做的不错。不过还是有两个地方先交流一下。
获取指定图层上的实体id的方法,可以考虑使用如下函数签名:
ObjectIdCollection GetObjectsOnLayer(string layerName);
这样和ObjectARX的使用风格保持统一,还是一个问题就是:
PromptSelectionResult result = ed.SelectAll();
应该需要加上过滤吧?如下:
PromptSelectionResult result = ed.SelectAll(filters);
不然感觉没有过滤?不知道我说的对不对,你可以检验一下。
 楼主| 发表于 2014-4-6 09:58 | 显示全部楼层
cdinten 发表于 2014-4-3 22:00
嗯,做的不错。不过还是有两个地方先交流一下。
获取指定图层上的实体id的方法,可以考虑使用如下函数签名 ...

嗯嗯,好,是的,那里的确是有一处错误,谢谢指正
发表于 2016-10-8 16:07 | 显示全部楼层
本帖最后由 hjki_i 于 2016-10-8 16:45 编辑

楼主,求分享
1246462177@qq.com
发表于 2018-1-25 13:10 | 显示全部楼层
好东西,刚好要用到
发表于 2018-2-7 11:16 | 显示全部楼层
好东西,学习了
发表于 2018-2-23 22:48 | 显示全部楼层
好东西!先留名以后会用的着
发表于 2020-2-15 23:23 | 显示全部楼层
好东西,新手学习了。求教一下怎么将根据选择的图元移动到指定图层?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-26 22:39 , Processed in 0.248634 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表