乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 172|回复: 7

CAD二次开发基础之图层管理

[复制链接]

3

主题

10

帖子

2

银币

初来乍到

Rank: 1

铜币
22
发表于 2014-4-2 22:22:00 | 显示全部楼层 |阅读模式
本人初学CAD二次开发,已有半载,感觉中途还是走了很多弯路,在此共享些基础东西。和前辈们探讨。
本文主要是图层管理,如常见的新建、关闭、删除等。基本上都是基础东西,只列出了关键代码,没写注释。
新建图层
  1. ///
  2.         /// create new layer
  3.         ///
  4.         ///
  5.         public void CreateLayer(String layerName)
  6.         {
  7.             Document doc = Autodesk..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. ///
  2.         /// judge if exist layer
  3.         ///
  4.         ///
  5.         ///
  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. ///
  2.         /// lock layer
  3.         ///
  4.         ///
  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. ///
  2.         /// erase layer
  3.         ///
  4.         ///
  5.         ///
  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.         ///
  2.         /// set layer to current layer
  3.         ///
  4.         ///
  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. ///
  2.         /// get all layers names
  3.         ///
  4.         ///
  5.         public List GetAllLayersName()
  6.         {
  7.             List layers = new List();
  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. ///
  2.         /// get layer entity id
  3.         ///
  4.         ///
  5.         public void GetLayerEntity(String layerName, ref List 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.         }

回复

使用道具 举报

19

主题

154

帖子

5

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
230
发表于 2014-4-3 22:00:00 | 显示全部楼层

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

使用道具 举报

3

主题

10

帖子

2

银币

初来乍到

Rank: 1

铜币
22
发表于 2014-4-6 09:58:00 | 显示全部楼层

嗯嗯,好,是的,那里的确是有一处错误,谢谢指正
回复

使用道具 举报

0

主题

2

帖子

1

银币

初来乍到

Rank: 1

铜币
2
发表于 2016-10-8 16:07:00 | 显示全部楼层

楼主,求分享
1246462177@qq.com
回复

使用道具 举报

2

主题

17

帖子

2

银币

初来乍到

Rank: 1

铜币
25
发表于 2018-1-25 13:10:00 | 显示全部楼层
好东西,刚好要用到
回复

使用道具 举报

0

主题

6

帖子

3

银币

初来乍到

Rank: 1

铜币
6
发表于 2018-2-7 11:16:00 | 显示全部楼层
好东西,学习了
回复

使用道具 举报

0

主题

35

帖子

10

银币

初来乍到

Rank: 1

铜币
35
发表于 2018-2-23 22:48:00 | 显示全部楼层
好东西!先留名以后会用的着
回复

使用道具 举报

3

主题

6

帖子

2

银币

初来乍到

Rank: 1

铜币
18
发表于 2020-2-15 23:23:00 | 显示全部楼层
好东西,新手学习了。求教一下怎么将根据选择的图元移动到指定图层?
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-13 02:31 , Processed in 0.457996 second(s), 68 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表