找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 839|回复: 0

[分享] Testing whether a point is on an AutoCAD polyline using .NET

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-5-16 08:02:04 | 显示全部楼层 |阅读模式

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

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

×
Testing whether a point is on an AutoCAD polyline using .NET



Occasionally I come across a topic that I’m sure I’ve addressed in a previous post, but for the life of me I can’t track it down. The code in a recent response by Balaji Ramamoorthy, from the DevTech India team, fits firmly into this category: it shows how to iterate through a polyline’s segments, testing whether a point is on the polyline.

I did some refactoring of Balaji’s code to extract it into a helper function (although not an extension method, which people who use them may prefer), but otherwise this C# code is basically his.

  1. using Autodesk.AutoCAD.ApplicationServices;

  2. using Autodesk.AutoCAD.DatabaseServices;

  3. using Autodesk.AutoCAD.EditorInput;

  4. using Autodesk.AutoCAD.Geometry;

  5. using Autodesk.AutoCAD.Runtime;



  6. namespace PolylineTesting

  7. {

  8.   public class Commands

  9.   {

  10.     [CommandMethod("POP")]

  11.     public void PointOnPolyline()

  12.     {

  13.       Document doc =

  14.         Application.DocumentManager.MdiActiveDocument;

  15.       Database db = doc.Database;

  16.       Editor ed = doc.Editor;



  17.       PromptEntityOptions peo =

  18.         new PromptEntityOptions("\nSelect a polyline");

  19.       peo.SetRejectMessage("Please select a polyline");

  20.       peo.AddAllowedClass(typeof(Polyline), true);



  21.       PromptEntityResult per = ed.GetEntity(peo);

  22.       if (per.Status != PromptStatus.OK)

  23.         return;



  24.       PromptPointResult ppr = ed.GetPoint("\nSelect a point");

  25.       if (ppr.Status != PromptStatus.OK)

  26.         return;



  27.       Transaction tr = db.TransactionManager.StartTransaction();

  28.       using (tr)

  29.       {

  30.         Polyline polyline =

  31.           tr.GetObject(per.ObjectId, OpenMode.ForRead) as Polyline;

  32.         if (polyline != null)

  33.         {

  34.           bool isOn = IsPointOnPolyline(polyline, ppr.Value);

  35.           ed.WriteMessage(

  36.             "\nSelected point is {0}on the polyline.",

  37.             isOn ? "" : "not "

  38.           );

  39.         }

  40.         tr.Commit();

  41.       }

  42.     }



  43.     private bool IsPointOnPolyline(Polyline pl, Point3d pt)

  44.     {

  45.       bool isOn = false;

  46.       for (int i = 0; i < pl.NumberOfVertices; i++)

  47.       {

  48.         Curve3d seg = null;



  49.         SegmentType segType = pl.GetSegmentType(i);

  50.         if (segType == SegmentType.Arc)

  51.           seg = pl.GetArcSegmentAt(i);

  52.         else if (segType == SegmentType.Line)

  53.           seg = pl.GetLineSegmentAt(i);



  54.         if (seg != null)

  55.         {

  56.           isOn = seg.IsOn(pt);

  57.           if (isOn)

  58.             break;

  59.         }

  60.       }

  61.       return isOn;

  62.     }

  63.   }

  64. }

When you run the POP command and select a polyline and a point, you’ll see a message on the command-line indicating whether the latter is on the former

[url]http://through-the-interface.typepad.com/through_the_interface/2012/01/testing-whether-a-point-is-on-an-autocad-polyline-using-net.html[/url]
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-1-6 01:15 , Processed in 0.379013 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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