明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2050|回复: 4

[基础] 如何更新块定义?

[复制链接]
发表于 2018-2-22 11:55 | 显示全部楼层 |阅读模式
书上、视频里面都是讲了新建块和添加块参照,
却没有讲重新定义块。
起初我考虑修改块表,先删除块中所有图元,然后重新加入,运行没有报错,但不知为何运行完代码后变成了匿名块;
后来我考虑删除块记录,然后重新加入块记录,仍然不成功,代码如下。
这个问题困扰我两天了,希望有前辈指点,谢谢。
我用的是C#:

  1.         public static ObjectId AddBlockTableRecord(this Database db, string btrName, List<Entity> ents)
  2.         {
  3.             ObjectId btrId = ObjectId.Null;
  4.             using (Transaction trans = db.TransactionManager.StartTransaction())
  5.             {
  6.                 BlockTable bt = (BlockTable) trans.GetObject(db.BlockTableId, OpenMode.ForRead);

  7.                 BlockTableRecord btr;
  8.                 if (!bt.Has(btrName))
  9.                 {
  10.                     btr = new BlockTableRecord();
  11.                     btr.Name = btrName;
  12.                     for (int i = 0; i < ents.Count; i++)
  13.                     {
  14.                         btr.AppendEntity(ents[i]);
  15.                     }
  16.                     bt.UpgradeOpen();
  17.                     bt.Add(btr);
  18.                     bt.DowngradeOpen();
  19.                     trans.AddNewlyCreatedDBObject(btr, true);
  20.                 }
  21.                 else
  22.                 {
  23.                     btr = bt[btrName].GetObject(OpenMode.ForWrite) as BlockTableRecord;
  24.                     btr.Erase();
  25.                     btr = new BlockTableRecord();
  26.                     btr.Name = btrName;
  27.                     for (int i = 0; i < ents.Count; i++)
  28.                     {
  29.                         btr.AppendEntity(ents[i]);
  30.                     }
  31.                     bt.UpgradeOpen();
  32.                     bt.Add(btr);
  33.                     bt.DowngradeOpen();
  34.                     trans.AddNewlyCreatedDBObject(btr, true);
  35.                 }

  36.                 trans.Commit();
  37.                 btrId = bt[btrName];
  38.             }

  39.             
  40.             return btrId;
  41.         }


 楼主| 发表于 2018-2-22 12:07 来自手机 | 显示全部楼层
修改或删除块表记录里的图元,没问题
 楼主| 发表于 2018-2-22 12:08 来自手机 | 显示全部楼层
但是块表记录加入块表后,我还想往块表记录里加图元就会变匿名块……
 楼主| 发表于 2018-2-24 09:54 | 显示全部楼层
试了好多次,还就这种写法没问题,块可以正常定义和更新,大神们有什么更简便的写法么?

代码如下:
  1.         [CommandMethod("kdy")]
  2.         public void kdy()
  3.         {
  4.             Database db = HostApplicationServices.WorkingDatabase;
  5.             Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

  6.             PromptSelectionOptions pso = new PromptSelectionOptions();
  7.             pso.MessageForAdding = "块定义";
  8.             PromptSelectionResult psr = ed.GetSelection(pso);
  9.             if (psr.Status != PromptStatus.OK) { return; }

  10.             PromptPointOptions ppo = new PromptPointOptions("选择块原点");
  11.             ppo.AllowNone = true;
  12.             PromptPointResult ppr = ed.GetPoint(ppo);
  13.             Point3d insertPoint = new Point3d();
  14.             if (ppr.Status != PromptStatus.OK)
  15.             {
  16.                 return;
  17.             }
  18.             else
  19.             {
  20.                 insertPoint = ppr.Value;
  21.             }

  22.             db.DefineABlock("MINE", psr, insertPoint);
  23.         }

  24.         [CommandMethod("crk")]
  25.         public void crk()
  26.         {
  27.             Database db = HostApplicationServices.WorkingDatabase;

  28.             // 是否存在 MINE 块记录
  29.             using (Transaction trans = db.TransactionManager.StartTransaction())
  30.             {
  31.                 BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
  32.                 if (!bt.Has("MINE")) return;
  33.             }

  34.             // 确定块插入点
  35.             Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  36.             PromptPointOptions ppo = new PromptPointOptions("选择块插入点");
  37.             ppo.AllowNone = true;
  38.             PromptPointResult ppr = ed.GetPoint(ppo);
  39.             Point3d insertPoint = new Point3d();
  40.             if (ppr.Status != PromptStatus.OK)
  41.             {
  42.                 return;
  43.             }
  44.             else
  45.             {
  46.                 insertPoint = ppr.Value;
  47.             }

  48.             // 模型空间中插入块参照
  49.             using (Transaction trans = db.TransactionManager.StartTransaction())
  50.             {
  51.                 BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
  52.                 BlockTableRecord btr = bt["MINE"].GetObject(OpenMode.ForRead) as BlockTableRecord;
  53.                 BlockReference blkRef = new BlockReference(insertPoint, btr.ObjectId);
  54.                 BlockTableRecord modelSpace = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  55.                 modelSpace.AppendEntity(blkRef);
  56.                 trans.AddNewlyCreatedDBObject(blkRef, true);
  57.                 trans.Commit();
  58.             }
  59.         }



  1.         /// <summary>
  2.         /// 定义块
  3.         /// </summary>
  4.         /// <param name="db">图形数据库</param>
  5.         /// <param name="btrName">块名</param>
  6.         /// <param name="psr">GetSelection的结果</param>
  7.         /// <param name="insertPoint">块原点</param>
  8.         /// <returns>块表记录Id</returns>
  9.         public static ObjectId DefineABlock(this Database db, string btrName, PromptSelectionResult psr, Point3d insertPoint)
  10.         {
  11.             SelectionSet SS = psr.Value;
  12.             ObjectId btrId = ObjectId.Null;
  13.             if (SS.Count == 0) return btrId;

  14.             //提取选择集中的各个图元
  15.             ObjectId[] entIds = SS.GetObjectIds();
  16.             List<Entity> entList = new List<Entity>();
  17.             using (Transaction trans = db.TransactionManager.StartTransaction())
  18.             {
  19.                 for (int i = 0; i < entIds.Count(); i++)
  20.                 {
  21.                     Entity ent = entIds[i].GetObject(OpenMode.ForRead) as Entity;
  22.                     Vector3d vec = insertPoint.GetVectorTo(Point3d.Origin);
  23.                     Matrix3d mt = Matrix3d.Displacement(vec);
  24.                     ent = ent.GetTransformedCopy(mt);
  25.                     entList.Add(ent);
  26.                 }
  27.             }

  28.             //判断是否已有同名块表记录
  29.             bool existed;
  30.             using (Transaction trans = db.TransactionManager.StartTransaction())
  31.             {
  32.                 BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
  33.                 if (bt.Has(btrName))
  34.                 {
  35.                     existed = true;
  36.                 }
  37.                 else
  38.                 {
  39.                     existed = false;
  40.                 }
  41.             }

  42.             // 定义块表            
  43.             using (Transaction trans = db.TransactionManager.StartTransaction())
  44.             {
  45.                 BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

  46.                 if (existed)
  47.                 {//重定义块表
  48.                     BlockTableRecord btr = bt[btrName].GetObject(OpenMode.ForWrite) as BlockTableRecord;

  49.                     //记录下已添加到模型空间的块参照
  50.                     List<BlockReference> oldRefs = new List<BlockReference>();
  51.                     foreach (ObjectId objID in btr.GetBlockReferenceIds(false, true))
  52.                     {
  53.                         BlockReference blkRef = objID.GetObject(OpenMode.ForWrite) as BlockReference;
  54.                         oldRefs.Add(blkRef);
  55.                     }

  56.                     //删除旧的块表记录
  57.                     btr.Erase();

  58.                     //增加新的同名块表记录
  59.                     btr = new BlockTableRecord();
  60.                     btr.Name = btrName;
  61.                     foreach (Entity ent in entList)
  62.                     {
  63.                         btr.AppendEntity(ent);
  64.                     }
  65.                     bt.UpgradeOpen();
  66.                     bt.Add(btr);
  67.                     bt.DowngradeOpen();
  68.                     trans.AddNewlyCreatedDBObject(btr, true);

  69.                     //更新模型空间中的块参照
  70.                     foreach (BlockReference blkRef in oldRefs)
  71.                     {
  72.                         blkRef.BlockTableRecord = btr.ObjectId;
  73.                         //blkRef.RecordGraphicsModified(true);
  74.                     }
  75.                 }
  76.                 else
  77.                 {//新定义块表
  78.                     BlockTableRecord btr = new BlockTableRecord();
  79.                     btr.Name = btrName;
  80.                     foreach (Entity ent in entList)
  81.                     {
  82.                         btr.AppendEntity(ent);
  83.                     }
  84.                     bt.UpgradeOpen();
  85.                     bt.Add(btr);
  86.                     bt.DowngradeOpen();
  87.                     trans.AddNewlyCreatedDBObject(btr, true);
  88.                 }

  89.                 trans.Commit();
  90.             }

  91.             return btrId;
  92.         }
发表于 2018-9-28 22:16 | 显示全部楼层
顶起赞一个,
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-5-2 05:17 , Processed in 0.239107 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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