图层合并函数
和大家分享一下,可能有bug,欢迎交流指正。/*
* 图层合并函数
* 源图层下的对象将被合并到目标图层
* 被移动的对象,颜色、线型、线宽等均被设置为ByLayer
* 源图层将被删除
*可能引发异常:
*1.源图层是0层、Defpoints层或者当前图层
*2.源图层或目标图层名称不存在
*/
public static void LayerMerge(string destLayer, string sourceLayer)
{
if (string.IsNullOrEmpty(destLayer) || string.IsNullOrEmpty(sourceLayer))
return;
if(sourceLayer=="0"||sourceLayer=="Defpoints")
throw new System.Exception("Layer 0 or Defpoints could be as Source Layer ");
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
DocumentLock dl = doc.LockDocument();
Transaction trans = db.TransactionManager.StartTransaction();
try
{
LayerTable lt = trans.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;
LayerTableRecord layer1 = trans.GetObject(lt, OpenMode.ForWrite) as LayerTableRecord;
if (layer1 == null)
throw new System.Exception("Destination Layer not Exist");
LayerTableRecord layer2 = trans.GetObject(lt, OpenMode.ForWrite) as LayerTableRecord;
if (layer2 == null)
throw new System.Exception("Source Layer not Exist");
if(layer2.Id==db.Clayer)
throw new System.Exception("current layer could not be as Source Layer");
//work chunk
TypedValue[] tv = new TypedValue[] { new TypedValue((int)DxfCode.LayerName, sourceLayer) };
SelectionFilter sf = new SelectionFilter(tv);
SelectionSet ss = doc.Editor.SelectAll(sf).Value;
if (ss != null && ss.Count != 0)
{
foreach (SelectedObject so in ss)
{
Entity ent = (Entity)trans.GetObject(so.ObjectId, OpenMode.ForWrite);
ent.Layer = destLayer;
ent.Linetype = "ByLayer";
ent.ColorIndex = 256;
ent.LineWeight = layer1.LineWeight;
}
}
lt.GenerateUsageData();
layer2.Erase(true);
trans.Commit();
}
catch
{
throw;
trans.Abort();
}
finally
{
trans.Dispose();
dl.Dispose();
}
}
只考虑当前视口的对象 实用性不是很好
块中对象咋办?"呵呵 雪山飞狐_lzh 发表于 2014-7-2 10:44 static/image/common/back.gif
只考虑当前视口的对象 实用性不是很好
块中对象咋办?"呵呵
自己扩展吧,我只当是一个抛砖引玉的作用,还有很多异常可能没有预料到,或者没有处理呢~ [检查文字 <未命名-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")
.......................
; 错误: 输入中的点位置不正确
; 检查完成.
页:
[1]