找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 3010|回复: 7

[分享] Tracing a boundary defined by AutoCAD geometry using .NET

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-5-8 15:31:52 来自手机 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
本帖最后由 csharp 于 2014-5-9 07:05 编辑

http://through-the-interface.typepad.com/through_the_interface/2010/06/tracing-a-boundary-defined-by-autocad-geometry-using-net.html

Tracing a boundary defined by AutoCAD geometry using .NET



As alluded to in this previous post, today we’re going to see some code that makes use of a very cool new API feature in AutoCAD 2011: the Editor.TraceBoundary() function. This little gem performs something developers have struggled with – and have asked us for – for many years. Previously you had to jump through significant hoops (or should that be loops? <groan>) to get this to work: one common approach would be to drive the BOUNDARY command programmatically and check the results appended to the database. All very messy.

Anyway, we got there eventually. This function takes a “seed” point and returns a collection of newly-created entities that define our boundary. We can then either pump these through the transient graphics sub-system or – and this is the approach we’re going to take – add them to the drawing to make them persistent.

You need to bear in mind that this function ultimately has similar constraints to the BOUNDARY and BHATCH commands: it also works off AutoCAD’s display list and so the user will need to be appropriately ZOOMed into the geometry that is to be used for boundary detection.

Here’s some C# code to create boundaries – with a lineweight applied and of an automatically incremented colour index – for points the user selects:

  1. using Autodesk.AutoCAD.ApplicationServices;

  2. using Autodesk.AutoCAD.EditorInput;

  3. using Autodesk.AutoCAD.DatabaseServices;

  4. using Autodesk.AutoCAD.Runtime;



  5. namespace TraceBoundary
  6. {
  7.   public class Commands
  8.   {
  9.     static int _index = 1;
  10.     [CommandMethod("TB")]
  11.     public void TraceBoundary()
  12.     {
  13.       Document doc =  Application.DocumentManager.MdiActiveDocument;
  14.       Database db = doc.Database;
  15.       Editor ed = doc.Editor;

  16.       // Select a seed point for our boundary

  17.       PromptPointResult ppr =  ed.GetPoint("
  18. Select internal point: ");

  19.       if (ppr.Status != PromptStatus.OK)        return;

  20.       // Get the objects making up our boundary

  21.       DBObjectCollection objs =  ed.TraceBoundary(ppr.Value, true);

  22.       if (objs.Count > 0)
  23.       {
  24.         Transaction tr =
  25.           doc.TransactionManager.StartTransaction();

  26.         using (tr)
  27.         {

  28.           // We'll add the objects to the model space

  29.           BlockTable bt =
  30.             (BlockTable)tr.GetObject(
  31.               doc.Database.BlockTableId,
  32.               OpenMode.ForRead
  33.             );

  34.           BlockTableRecord btr =
  35.             (BlockTableRecord)tr.GetObject(
  36.               bt[BlockTableRecord.ModelSpace],
  37.               OpenMode.ForWrite
  38.             );

  39.           // Add our boundary objects to the drawing and
  40.           // collect their ObjectIds for later use

  41.           ObjectIdCollection ids = new ObjectIdCollection();
  42.           foreach (DBObject obj in objs)

  43.           {

  44.             Entity ent = obj as Entity;
  45.             if (ent != null)
  46.             {

  47.               // Set our boundary objects to be of
  48.               // our auto-incremented colour index

  49.               ent.ColorIndex = _index;

  50.               // Set the lineweight of our object

  51.               ent.LineWeight = LineWeight.LineWeight050;

  52.               // Add each boundary object to the modelspace
  53.               // and add its ID to a collection

  54.               ids.Add(btr.AppendEntity(ent));

  55.               tr.AddNewlyCreatedDBObject(ent, true);

  56.             }
  57.           }

  58.           // Increment our colour index

  59.           _index++;

  60.           // Commit the transaction

  61.           tr.Commit();

  62.         }
  63.       }
  64.     }
  65.   }
  66. }

Here’s our TB command in action.

Before…
untitled1.png
untitled2.png
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 604个

财富等级: 财运亨通

发表于 2014-5-9 11:41:53 | 显示全部楼层
LZ,没有lisp版的?

点评

可以写成一个lisp函数,就如xdrxapi  详情 回复 发表于 2014-5-9 11:46
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 859个

财富等级: 财运亨通

 楼主| 发表于 2014-5-9 11:46:38 来自手机 | 显示全部楼层
本帖最后由 csharp 于 2014-5-9 12:21 编辑
/db_自贡黄明儒_ 发表于 2014-5-9 11:41
LZ,没有lisp版的?


可以写成一个lisp函数,就如xdrxapi
低版本的Net没有这个函数,2011以上
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 5060个

财富等级: 富甲天下

发表于 2019-4-23 09:02:48 来自手机 | 显示全部楼层
hpgaptol系统变量不能自动找到封闭区域,有办法自定义traceboundary吗?
来自: 微社区
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2019-6-30 16:26:41 | 显示全部楼层
非常感谢楼主,学习一下
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2019-9-1 11:51:54 来自手机 | 显示全部楼层
感谢楼主分享!学习中!
来自: 微社区
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2019-9-1 11:53:36 来自手机 | 显示全部楼层
感谢楼主分享!学习中!
来自: 微社区
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 58个

财富等级: 招财进宝

发表于 2019-10-17 19:48:11 来自手机 | 显示全部楼层
图很大的时候 这个会很慢吧
来自: 微社区
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-4-20 14:00 , Processed in 0.193612 second(s), 44 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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