找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1289|回复: 1

[原创] 练习 闭合Pline两两相交面域

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-7-24 19:02:46 | 显示全部楼层 |阅读模式

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

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

×

  1.         [CommandMethod("searchregion")]
  2.         public void GetIntersectRegion()
  3.         {
  4.             Document doc = Application.DocumentManager.MdiActiveDocument;
  5.             Database db = doc.Database;
  6.             Editor ed = doc.Editor;

  7.             TypedValue[] values = new TypedValue[3]
  8.             {
  9.                 new TypedValue(0, "*POLYLINE"),
  10.                 new TypedValue( -4,"&="),
  11.                 new TypedValue( 70 ,0)
  12.             };
  13.             PromptSelectionOptions pso = new PromptSelectionOptions();
  14.             pso.MessageForAdding = "Select Closed Polyline";

  15.             PromptSelectionResult psr = ed.GetSelection(pso, new SelectionFilter(values));
  16.             if (psr.Status != PromptStatus.OK)
  17.             {
  18.                 return;
  19.             }
  20.             using (Transaction tr = db.TransactionManager.StartTransaction())
  21.             {
  22.                 try
  23.                 {
  24.                     SelectionSet ss = psr.Value;
  25.                     ObjectId[] ids = ss.GetObjectIds();
  26.                     BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  27.                     for (int i = 0; i < ids.Length; i++)
  28.                     {
  29.                         Entity ent1 = (Entity)ids[i].GetObject(OpenMode.ForRead);
  30.                         Extents3d ext1 = ent1.GeometricExtents;
  31.                         for (int j = i + 1; j < ids.Length; j++)
  32.                         {
  33.                             Entity ent2 = (Entity)ids[j].GetObject(OpenMode.ForRead);

  34.                             Extents3d ext2 = ent2.GeometricExtents;
  35.                             if (RectIntersects(ext1, ext2))
  36.                             {
  37.                                 DBObjectCollection cvs = new DBObjectCollection();
  38.                                 cvs.Add(ent1);
  39.                                 cvs.Add(ent2);
  40.                                 DBObjectCollection regions = Region.CreateFromCurves(cvs);

  41.                                 Region reg1 = (Region)regions[0];
  42.                                 Region reg2 = (Region)regions[1];
  43.                                 //reg2 dis
  44.                                 reg1.BooleanOperation(BooleanOperationType.BoolIntersect, reg2);
  45.                                 reg1.ColorIndex = 1;
  46.                                 btr.AppendEntity(reg1);
  47.                                 tr.AddNewlyCreatedDBObject(reg1, true);
  48.                             }
  49.                         }
  50.                     }
  51.                     tr.Commit();
  52.                 }
  53.                 catch (Exception)
  54.                 {
  55.                     throw;
  56.                 }
  57.             }
  58.         }
  59.         private static bool RectIntersects(Extents3d first, Extents3d second)
  60.         {
  61.             bool one = second.MinPoint.X < first.MaxPoint.X &&
  62.                 first.MinPoint.X < second.MaxPoint.X &&
  63.                 second.MinPoint.Y < first.MaxPoint.Y &&
  64.                 first.MinPoint.Y < second.MaxPoint.Y;
  65.             bool two = first.MinPoint.X < second.MaxPoint.X &&
  66.               second.MinPoint.X < first.MaxPoint.X &&
  67.               first.MinPoint.Y < second.MaxPoint.Y &&
  68.               second.MinPoint.Y < first.MaxPoint.Y;
  69.             return one | two;
  70.         }
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 859个

财富等级: 财运亨通

 楼主| 发表于 2014-7-24 20:36:21 | 显示全部楼层
本帖最后由 csharp 于 2014-7-26 19:56 编辑

判断还少了一步, 可能没有相交面域

  1. if (!reg1.IsNull)
  2. {
  3.      reg1.ColorIndex = 1;
  4.      btr.AppendEntity(reg1);
  5.      tr.AddNewlyCreatedDBObject(reg1, true);
  6. }
复制代码

ext 判断只保留一个
程序中判断用
if (RectIntersects(ext1,ext2) | RectIntersects (ext2,ext1))
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-1 09:07 , Processed in 0.369167 second(s), 29 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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