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

CAD C# 批量替换当前图中块

 本案例功能为选择当前文档中一个块(旧块),然后选择新图元(新块),运行插件后新块将替换图中所有的旧块。

效果如下:

 public static class Class1{//选取对象替换块定义[CommandMethod("TT")]public static void BLKREDEF(){Document doc =Z.doc;Database db = doc.Database;Editor ed = doc.Editor;ed.WriteMessage("图块编辑替换 BK");//提示选择块PromptEntityOptions peo = new PromptEntityOptions("\n请选择需要编辑的块");peo.SetRejectMessage("\n你选择的不是块");peo.AddAllowedClass(typeof(BlockReference), true); //只让选择块PromptEntityResult per = ed.GetEntity(peo);if (per.Status != PromptStatus.OK) { return; }ObjectId blockID = per.ObjectId;string blockName;Point3d blockInPt;double blockAngle;Matrix3d blockMatri, blockInMatri;using (Transaction trans = doc.TransactionManager.StartTransaction()){BlockReference blockref = (BlockReference)trans.GetObject(blockID, OpenMode.ForRead); //块参照blockName = blockref.Name;   //块名blockInPt = blockref.Position; //块插入点blockAngle = blockref.Rotation; //块旋转blockMatri = blockref.BlockTransform;blockInMatri = blockMatri.Inverse(); //逆矩阵}ed.WriteMessage("\n您编辑的块名为: " + blockName);//提示选择对象PromptSelectionOptions pso = new PromptSelectionOptions();pso.MessageForAdding = "请选择需要生成块的图形";pso.RejectObjectsOnLockedLayers = true; //不能选在锁定图层上的PromptSelectionResult psr = ed.GetSelection(pso);if (psr.Status != PromptStatus.OK) { return; }SelectionSet ss = psr.Value;//新对象的基点//注意交互下的坐标都是UCS,需要处理PromptPointOptions ppo = new PromptPointOptions("请选择新块基点:");ppo.UseBasePoint = true;ppo.BasePoint = blockInPt.TransformBy(ed.CurrentUserCoordinateSystem.Inverse());PromptPointResult ppr = ed.GetPoint(ppo);if (ppr.Status != PromptStatus.OK) { return; }Point3d blockInPtN = ppr.Value;Point3d inPtNWCS = blockInPtN.TransformBy(ed.CurrentUserCoordinateSystem);//坐标系变换,从UCS转换到WCSVector3d vt = blockInPt - inPtNWCS;Matrix3d matMove = Matrix3d.Displacement(vt); //平移using (Transaction trans = doc.TransactionManager.StartTransaction()){//获取块参照的定义BlockReference blockRef = (BlockReference)trans.GetObject(blockID, OpenMode.ForWrite);BlockTableRecord btr = (BlockTableRecord)blockRef.BlockTableRecord.GetObject(OpenMode.ForWrite);//遍历删除原块内所有对象foreach (ObjectId id in btr){Entity ent = (Entity)trans.GetObject(id, OpenMode.ForWrite);ent.Erase();}//实体列表加入新建的块表记录中foreach (ObjectId id in ss.GetObjectIds()){Entity ent = (Entity)trans.GetObject(id, OpenMode.ForWrite);//要排除自身嵌套Bugif (ent is BlockReference){if (((BlockReference)ent).Name == blockName){continue;}}IdMapping mapping = new IdMapping();Entity ent1 = (Entity)ent.Clone(); //复制新的并加入块// Entity ent1 = (Entity)ent.DeepClone(ent, mapping, false);//块定义以时候会以(0,0,0)为基点//插入的时候坐标会以飞一个相对(0,0,0)到inPt的距离,这里要处理一下。//预先把对象移到原点ent1.TransformBy(blockInMatri * (matMove));btr.AppendEntity(ent1);trans.AddNewlyCreatedDBObject(ent1, true);//ent.Erase(); //删除原来的}//通知事务处理//trans.AddNewlyCreatedDBObject(bt, true);//刷新块显示//blockRef.RecordGraphicsModified(true);//模型空间打开块表BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);//打开指定块名的块表记录BlockTableRecord btr1 = (BlockTableRecord)trans.GetObject(bt[blockName], OpenMode.ForRead);//获取指定块名的块参照集合的IdObjectIdCollection blockIds = btr1.GetBlockReferenceIds(false, true);foreach (ObjectId id in blockIds) // 遍历块参照的Id{//获取块参照BlockReference block = (BlockReference)trans.GetObject(id, OpenMode.ForWrite);block.Visible = block.Visible;}trans.Commit();}}}

http://www.lryc.cn/news/501157.html

相关文章:

  • Android -- [SelfView] 自定义多行歌词滚动显示器
  • vscode 配置C/C++环境控制台参数
  • 【HarmonyOS学习日志(13)】计算机网络之TCP/IP协议族(二)
  • 多系统对接的实现方案技术分析
  • kv类型算子使用
  • 3维建模blender
  • 百问FB网络编程 - UDP编程简单示例
  • 面试题:什么是ThreadLocal,如何实现的?
  • js后端开发之Next.js、Nuxt.js 与 Express.js
  • 飞牛Nas如何实现阿里云盘、百度网盘的资料迁移!
  • 如何在小米平板5上运行 deepin 23 ?
  • 【PlantUML系列】流程图(四)
  • 操作系统:进程、线程与作业
  • 先验地图--slam学习笔记
  • 空指针异常:软件开发中的隐形陷阱
  • 【Java从入门到放弃 之 GC】
  • 【C++】等差数列末项计算题解析及优化
  • vue中父组件接收子组件的多个参数的方法:$emit或事件总线
  • 2024.12.10——攻防世界Web_php_include
  • 【机器学习算法】——数据可视化
  • 如何在 Android 项目中实现跨库传值
  • JavaCV之FFmpegFrameFilter视频转灰度
  • Redis:基于PubSub(发布/订阅)、Stream流实现消息队列
  • C#飞行棋(新手简洁版)
  • 【OpenCV】图像转换
  • 力扣 重排链表-143
  • 【Kubernetes理论篇】容器集群管理系统Kubernetes(K8S)
  • Kubernetes 常用操作大全:全面掌握 K8s 基础与进阶命令
  • 爬虫基础之Web网页基础
  • k8s, deployment