当前位置: 首页 > ds >正文

cad c#二次开发 图层封装 获取当前层

    public static class 图层助手{/// <summary>/// 将模型空间所有实体的图层修改为当前文档的当前图层/// </summary>/// <param name="doc">当前文档对象</param>/// <param name="editor">CAD编辑器对象(用于输出提示信息)</param>/// <returns>成功修改的实体数量</returns>public static int 改所有实体到当前图层(Document doc, Editor editor)
{int modifiedCount = 0;string currentLayer = ""; // 当前图层名称// 1. 获取当前图层名称(关键:从文档获取当前活动图层)try{//object clayerValue = doc.Database.Clayer;object clayerValue = Application.GetSystemVariable("CLAYER");currentLayer = clayerValue.ToString();if (string.IsNullOrEmpty(currentLayer)){currentLayer = "0"; // 默认图层名称}}catch (Exception ex){}if (string.IsNullOrEmpty(currentLayer)){currentLayer = "0"; // 默认图层名称}using (Transaction trans = doc.TransactionManager.StartTransaction()){try{// 2. 打开模型空间块表记录(写模式,因为需要修改实体)BlockTable blockTable = trans.GetObject(doc.Database.BlockTableId, OpenMode.ForRead) as BlockTable;BlockTableRecord modelSpace = trans.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;// 3. 遍历模型空间所有实体IDforeach (ObjectId entityId in modelSpace){Entity entity = trans.GetObject(entityId, OpenMode.ForWrite) as Entity;if (entity == null || entity.IsErased) continue; // 跳过无效实体// 4. 执行图层修改(仅当当前图层不同时操作)if (entity.Layer != currentLayer){entity.Layer = currentLayer; // 直接赋值图层名称modifiedCount++;}entity.Dispose(); // 显式释放实体(虽然事务结束会自动释放,但显式调用更安全)}trans.Commit(); // 提交事务// editor.WriteMessage($"\n成功修改 {modifiedCount} 个实体的图层到【{currentLayer}】");}catch (System.Exception ex){trans.Abort(); // 异常时回滚事务//editor.WriteMessage($"\n批量改图层失败:{ex.Message}");}}return modifiedCount;
}public static string 获取当前层(this Database db) {Document doc = Application.DocumentManager.MdiActiveDocument;string currentLayer = ""; // 当前图层名称// 1. 获取当前图层名称(关键:从文档获取当前活动图层)try{//object clayerValue = doc.Database.Clayer;object clayerValue = Application.GetSystemVariable("CLAYER");currentLayer = clayerValue.ToString();if (string.IsNullOrEmpty(currentLayer)){currentLayer = "0"; // 默认图层名称}}catch (Exception ex){}if (string.IsNullOrEmpty(currentLayer)){currentLayer = "0"; // 默认图层名称}return currentLayer;#region//using (Transaction trans = doc.TransactionManager.StartTransaction())//{//    try//    {//        // 2. 打开模型空间块表记录(写模式,因为需要修改实体)//        BlockTable blockTable = trans.GetObject(//            doc.Database.BlockTableId, OpenMode.ForRead//        ) as BlockTable;//        BlockTableRecord modelSpace = trans.GetObject(//            blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite//        ) as BlockTableRecord;//        // 3. 遍历list实体ID//        ents.ForEach(ent =>//        {//            ent.Layer = currentLayer;//        }); // 直接赋值图层名称 //        trans.Commit(); // 提交事务//                        // editor.WriteMessage($"\n成功修改 {modifiedCount} 个实体的图层到【{currentLayer}】");//    }//    catch (System.Exception ex)//    {//        trans.Abort(); // 异常时回滚事务//                       //editor.WriteMessage($"\n批量改图层失败:{ex.Message}");//    }//}#endregion}/// <summary>/// 获取指定图层的颜色索引/// </summary>/// <param name="layerName">目标图层名称</param>/// <returns>图层的颜色索引(若不存在返回-1)</returns>public static int 获取图层颜色(this string layerName ){// 获取当前文档和数据库Document doc = Application.DocumentManager.MdiActiveDocument;Database db = doc.Database;// 使用事务保证数据库操作安全using (Transaction tr = db.TransactionManager.StartTransaction()){try{// 打开图层表(只读模式)LayerTable layerTable = tr.GetObject(db.LayerTableId,OpenMode.ForRead) as LayerTable;// 检查图层是否存在if (!layerTable.Has(layerName)){return 0; // 返回表示图层不存在}// 获取图层记录(只读模式)ObjectId layerId = layerTable[layerName];LayerTableRecord layer = tr.GetObject(layerId,OpenMode.ForRead) as LayerTableRecord;// 返回颜色索引return layer.Color.ColorIndex;}finally{tr.Commit(); // 提交事务(只读操作仍需要提交)}}}}
 public static  class 图层助手1{public static void SetCurrentLayer(this Database db, Entity ent){if (db == null||ent is null||ent.IsErased) return;try{using (Transaction tr = db.TransactionManager.StartTransaction()){var getent = tr.GetObject(ent.ObjectId, OpenMode.ForWrite) as Entity;//LayerTable layerTable = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;//var ltr = tr.GetObject(db.Clayer, OpenMode.ForRead) as LayerTableRecord;//ent.Layer = ltr.Name;getent.LayerId = db.Clayer;tr.Commit();}}catch (Exception ex){Z.WriteEX(ex);}}/// <summary>/// 将指定的图层设置为当前层/// </summary>/// <param name="db">数据库对象</param>/// <param name="layerName">图层名</param>/// <returns>如果设置成功,则返回ture</returns>public static bool SetCurrentLayer(this Database db, string layerName){#region 前置校验(避免非法输入)if (string.IsNullOrEmpty(layerName))return false; // 图层名不能为空// 检查图层名是否包含AutoCAD禁止的特殊字符(如<、>、:等)if (layerName.IndexOfAny(new[] { '<', '>', ':', '"', '/', '\\', '|', '?', '*' }) != -1)return false;#endregion// 获取当前文档(确保在AutoCAD界面中运行)Document doc = Application.DocumentManager.MdiActiveDocument;if (doc == null) return false;// 启动事务(关键!保证数据库操作的原子性)using (Transaction tr = db.TransactionManager.StartTransaction()){try{#region 步骤1:获取图层表(LayerTable)// 以读模式打开图层表(所有图层的容器)LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;if (lt == null) return false; // 图层表获取失败#endregion#region 步骤2:检查图层是否存在if (!lt.Has(layerName))return false; // 图层不存在#endregion#region 步骤3:获取目标图层IDObjectId targetLayerId = lt[layerName];#endregion#region 步骤4:判断是否已为当前图层(避免重复设置)// 使用 LayerManager 获取当前图层ID(新版本推荐方式)if (db.Clayer == targetLayerId)return false; // 已是当前图层#endregion#region 步骤5:设置当前图层(新版本推荐方式)db.Clayer = targetLayerId;#endregiontr.Commit(); // 提交事务(所有操作生效)return true;}catch (Exception ex){tr.Abort(); // 异常时回滚事务doc.Editor.WriteMessage($"\n设置当前图层失败:{ex.Message}");return false;}}}public static string SetLayer(this Database db, Entity ent, string layname){if (ent.Layer == layname) return layname;// Database db = HostApplicationServices.WorkingDatabase;Document mdiActiveDocument = Application.DocumentManager.MdiActiveDocument;using (Transaction transaction = db.TransactionManager.StartTransaction()){LayerTable layerTable = transaction.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;if (!layerTable.Has(layname)){LayerTableRecord layerTableRecord = new LayerTableRecord();layerTableRecord.Color = Color.FromColorIndex(ColorMethod.ByAci, 0);layerTableRecord.Name = layname;layerTable.UpgradeOpen();layerTable.Add(layerTableRecord);transaction.AddNewlyCreatedDBObject(layerTableRecord, add: true);}//// 以只读方式打开块表   Open the Block table for read//BlockTable acBlkTbl = transaction.GetObject(db.BlockTableId,//                             OpenMode.ForRead) as BlockTable;//// 以写方式打开模型空间块表记录   Open the Block table record Model space for write//BlockTableRecord acBlkTblRec = transaction.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],//                                OpenMode.ForWrite) as BlockTableRecord;//// 设置图层Entity entInTrans = transaction.GetObject(ent.ObjectId, OpenMode.ForWrite) as Entity;entInTrans.Layer = layname;////acBlkTblRec.AppendEntity(ent);//transaction.AddNewlyCreatedDBObject(ent, true);// Save the changes and dispose of the transactiontransaction.Commit();}return layname;}/// <summary>/// 添加图层/// </summary>/// <param name="db">图形数据库</param>/// <param name="layerName">图层名</param>/// <returns>AddLayerResult</returns>public static AddLayerResult AddLayer(this Database db, string layerName){//声明AddLayerResult类型的数据,用户返回AddLayerResult res = new AddLayerResult();try{SymbolUtilityServices.ValidateSymbolName(layerName, false);}catch (Exception){res.statuts = AddLayerStatuts.IllegalLayerName;return res;}using (Transaction trans = db.TransactionManager.StartTransaction()){//打开层表LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForRead);//新建层表记录if (!lt.Has(layerName)){LayerTableRecord ltr = new LayerTableRecord();//判断要创建的图层名是否已经存在,不存在则创建ltr.Name = layerName;//升级层表打开权限lt.UpgradeOpen();lt.Add(ltr);//降低层表打开权限lt.DowngradeOpen();trans.AddNewlyCreatedDBObject(ltr, true);trans.Commit();res.statuts = AddLayerStatuts.AddLayerOK;res.layerName = layerName;}else{res.statuts = AddLayerStatuts.LayerNameExist;}}return res;}public enum AddLayerStatuts{AddLayerOK,IllegalLayerName,LayerNameExist}//添加图层的返回值public struct AddLayerResult{public AddLayerStatuts statuts;public string layerName;}//修改图层属性的返回状态public enum ChangeLayerPropertyStatus{ChangeOK,LayerIsNotExist}}

CAD 二次开发、插件定制↓↓↓

http://www.xdnf.cn/news/17345.html

相关文章:

  • LeetCode 分类刷题:611. 有效三角形的个数
  • VGG16训练和测试Fashion和CIFAR10
  • Verilog 仿真问题:打拍失败
  • jdk动态代理如何实现
  • 对 .NET线程 异常退出引发程序崩溃的反思
  • 八股——IM项目
  • C++ 运算符重载:避免隐式类型转换的艺术
  • 译 | 在 Python 中从头开始构建 Qwen-3 MoE
  • 【ArcGIS】分区统计中出现Null值且Nodata无法忽略的问题以及shp擦除(erase)的使用——以NDVI去水体为例
  • 最新教程 | CentOS 7 下 MySQL 8 离线部署完整手册(含自动部署脚本)
  • vite项目中集成vditor文档编辑器
  • 低代码系统的技术深度:超越“可视化操作”的架构与实现挑战
  • 【机器学习篇】02day.python机器学习篇Scikit-learn基础操作
  • 疯狂星期四文案网第30天运营日记
  • 自学嵌入式 day43 中断系统
  • 数据结构与算法的认识
  • Linux 防火墙(firewalld)详解与配置
  • 【概念学习】早期神经网络
  • IPS知识点
  • spring-dubbo
  • ##Anolis OS 8.10 安装oracle19c
  • 从零开始的CAD|CAE开发: 单柱绕流+多柱绕流
  • vue封装一个cascade级联 多选 全选组件 ,原生写法Input,Checkbox,Button
  • 看不见的伪造痕迹:AI时代的鉴伪攻防战
  • Codeforces Round 987 (Div. 2)
  • 数据结构—队列和栈
  • 问题定位排查手记1 | 从Windows端快速检查连接状态
  • Java面试宝典:类加载器分层设计与核心机制解析
  • PyCharm vs. VSCode 到底哪个更好用
  • C++、STL面试题总结(二)