明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2887|回复: 3

[符号表] 图层合并函数

[复制链接]
发表于 2014-4-8 21:56 | 显示全部楼层 |阅读模式
和大家分享一下,可能有bug,欢迎交流指正。
  1.         /*
  2.          * 图层合并函数
  3.          * 源图层下的对象将被合并到目标图层
  4.          * 被移动的对象,颜色、线型、线宽等均被设置为ByLayer
  5.          * 源图层将被删除
  6.          *  可能引发异常:
  7.          *  1.源图层是0层、Defpoints层或者当前图层
  8.          *  2.源图层或目标图层名称不存在
  9.          */
  10.         public static void LayerMerge(string destLayer, string sourceLayer)
  11.         {
  12.             if (string.IsNullOrEmpty(destLayer) || string.IsNullOrEmpty(sourceLayer))
  13.                 return;
  14.             if(sourceLayer=="0"||sourceLayer=="Defpoints")
  15.                 throw new System.Exception("Layer 0 or Defpoints could be as Source Layer ");
  16.             Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  17.             Database db = doc.Database;

  18.             DocumentLock dl = doc.LockDocument();
  19.             Transaction trans = db.TransactionManager.StartTransaction();
  20.             try
  21.             {
  22.                 LayerTable lt = trans.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;
  23.                 LayerTableRecord layer1 = trans.GetObject(lt[destLayer], OpenMode.ForWrite) as LayerTableRecord;
  24.                 if (layer1 == null)
  25.                     throw new System.Exception("Destination Layer not Exist");
  26.                 LayerTableRecord layer2 = trans.GetObject(lt[sourceLayer], OpenMode.ForWrite) as LayerTableRecord;
  27.                 if (layer2 == null)
  28.                     throw new System.Exception("Source Layer not Exist");
  29.                 if(layer2.Id==db.Clayer)
  30.                     throw new System.Exception("current layer could not be as Source Layer");
  31.                 //work chunk
  32.                 TypedValue[] tv = new TypedValue[] { new TypedValue((int)DxfCode.LayerName, sourceLayer) };
  33.                 SelectionFilter sf = new SelectionFilter(tv);
  34.                 SelectionSet ss = doc.Editor.SelectAll(sf).Value;
  35.                 if (ss != null && ss.Count != 0)
  36.                 {
  37.                     foreach (SelectedObject so in ss)
  38.                     {
  39.                         Entity ent = (Entity)trans.GetObject(so.ObjectId, OpenMode.ForWrite);
  40.                         ent.Layer = destLayer;
  41.                         ent.Linetype = "ByLayer";
  42.                         ent.ColorIndex = 256;
  43.                         ent.LineWeight = layer1.LineWeight;
  44.                     }
  45.                 }

  46.                 lt.GenerateUsageData();
  47.                 layer2.Erase(true);
  48.                 trans.Commit();
  49.             }
  50.             catch
  51.             {
  52.                 throw;
  53.                 trans.Abort();
  54.             }
  55.             finally
  56.             {
  57.                 trans.Dispose();
  58.                 dl.Dispose();
  59.             }
  60.         }

发表于 2014-7-2 10:44 来自手机 | 显示全部楼层
只考虑当前视口的对象 实用性不是很好
块中对象咋办?"呵呵
 楼主| 发表于 2014-7-6 21:41 | 显示全部楼层
雪山飞狐_lzh 发表于 2014-7-2 10:44
只考虑当前视口的对象 实用性不是很好
块中对象咋办?"呵呵

自己扩展吧,我只当是一个抛砖引玉的作用,还有很多异常可能没有预料到,或者没有处理呢~
发表于 2014-7-9 13:52 | 显示全部楼层
[检查文字 <未命名-0> 正在加载...]
...............................
; 错误: 表达式中有错误函数: ("Layer 0 or Defpoints could be as Source Layer ")
........................................
; 错误: 表达式中有错误函数: ("Destination Layer not Exist")
.............
; 错误: 表达式中有错误函数: ("Source Layer not Exist")
......
; 错误: 表达式中有错误函数: ("current layer could not be as Source Layer")
.......................
; 错误: 输入中的点位置不正确
; 检查完成.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-3-29 14:14 , Processed in 0.190017 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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