- 积分
- 3649
- 明经币
- 个
- 注册时间
- 2008-2-17
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
和大家分享一下,可能有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[destLayer], OpenMode.ForWrite) as LayerTableRecord;
- if (layer1 == null)
- throw new System.Exception("Destination Layer not Exist");
- LayerTableRecord layer2 = trans.GetObject(lt[sourceLayer], 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();
- }
- }
|
|