找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 4028|回复: 6

[每日一码] 判断点和封闭曲线的位置关系(内、外,曲线上)

[复制链接]

已领礼包: 6个

财富等级: 恭喜发财

发表于 2018-5-24 19:23:23 | 显示全部楼层 |阅读模式

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

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

×
  1. // 7:47 PM 6/29/2007 - LE! -
  2.         // from the MgdDbg SLN by Jim Awe AutoDesk
  3.         public static Database GetCurDwg()
  4.         {
  5.             Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
  6.             return db;
  7.         }

  8.         // from the MgdDbg SLN by Jim Awe AutoDesk
  9.         public static bool IsPaperSpace(Database db)
  10.         {
  11.             if (db.TileMode)
  12.                 return false;
  13.             Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  14.             if (db.PaperSpaceVportId == ed.CurrentViewportObjectId)
  15.                 return true;
  16.             return false;
  17.         }

  18.         // from the MgdDbg SLN by Jim Awe AutoDesk
  19.         public static Matrix3d GetUcsMatrix(Database db)
  20.         {
  21.             Point3d origin;
  22.             Vector3d xAxis, yAxis, zAxis;
  23.             if (IsPaperSpace(db))
  24.             {
  25.                 origin = db.Pucsorg;
  26.                 xAxis = db.Pucsxdir;
  27.                 yAxis = db.Pucsydir;
  28.             }
  29.             else
  30.             {
  31.                 origin = db.Ucsorg;
  32.                 xAxis = db.Ucsxdir;
  33.                 yAxis = db.Ucsydir;
  34.             }
  35.             zAxis = xAxis.CrossProduct(yAxis);
  36.             return Matrix3d.AlignCoordinateSystem(kOrigin, kXAxis, kYAxis, kZAxis, origin, xAxis, yAxis, zAxis);
  37.         }

  38.         // from the MgdDbg SLN by Jim Awe AutoDesk
  39.         public static Point3d Ucs2Wcs(Point3d pt)
  40.         {
  41.             Matrix3d m = GetUcsMatrix(GetCurDwg());
  42.             return pt.TransformBy(m);
  43.         }

  44.         // from the MgdDbg SLN by Jim Awe AutoDesk
  45.         public static Point3d Wcs2Ucs(Point3d pt)
  46.         {
  47.             Matrix3d m = GetUcsMatrix(GetCurDwg());
  48.             return pt.TransformBy(m.Inverse());
  49.         }

  50.         // by Luis Esquivel on June 29 2007
  51.         // using the idea/algorithm by John F. Uhden,
  52.         // one of the masters that use to frequent the customization ng of AutoDesk
  53.         static void isPointInside(Point3d inPt, ObjectId objid)
  54.         {
  55.             Database db  = HostApplicationServices.WorkingDatabase;
  56.             Document doc = acadApp.DocumentManager.MdiActiveDocument;
  57.             Editor   ed  = doc.Editor;
  58.             using (Transaction tr = db.TransactionManager.StartTransaction())
  59.             {
  60.                 Polyline pPoly = (Polyline)tr.GetObject(objid, OpenMode.ForWrite, false);
  61.                 if (pPoly is Polyline)
  62.                 {
  63.                     if (!pPoly.Closed)
  64.                     {
  65.                         ed.WriteMessage("\n*** Curve is not closed. "); return;
  66.                     }
  67.                     Point3d P = new Point3d();
  68.                     P = Ucs2Wcs(inPt);
  69.                     Point3d ClosestPoint = pPoly.GetClosestPointTo(P, false); //true);
  70.                     if (P.IsEqualTo(ClosestPoint))
  71.                     {
  72.                         ed.WriteMessage("\n*** Point is on curve. "); return;
  73.                     }
  74.                     Double ClosestParam, EndParam;
  75.                     ClosestParam = pPoly.GetParameterAtPoint(ClosestPoint);
  76.                     ClosestPoint = Wcs2Ucs(ClosestPoint);
  77.                     EndParam = pPoly.EndParam;
  78.                     Double Param1 = 0.0, Param2 = dVal, Deflection = 0.0, a2;
  79.                     Point3d StartPT = pPoly.StartPoint;
  80.                     StartPT = Wcs2Ucs(StartPT);
  81.                     Double a1 = Math.Atan2(StartPT.Y - inPt.Y, StartPT.X - inPt.X);
  82.                     while (Param2 <= EndParam)
  83.                     {
  84.                         Param2 = Math.Min(Param2, EndParam);
  85.                         if (Param1 < ClosestParam && ClosestParam < Param2)
  86.                         {
  87.                             a2 = Math.Atan2(ClosestPoint.Y - inPt.Y, ClosestPoint.X - inPt.X);
  88.                             Deflection = Deflection + delta(a1, a2);
  89.                             a1 = a2;
  90.                         }
  91.                         bool test = true;                       
  92.                         while (test)
  93.                         {
  94.                             try
  95.                             {
  96.                                 P = pPoly.GetPointAtParameter(Param2);
  97.                                 Param2 = Param2 + dVal;
  98.                             }
  99.                             catch (System.Exception ex)
  100.                             {
  101.                                 ed.WriteMessage("\nERROR = " + ex.Message);
  102.                             }
  103.                             finally
  104.                             {
  105.                                 test = false;
  106.                             }
  107.                         }
  108.                         P = Wcs2Ucs(P);
  109.                         a2 = Math.Atan2(P.Y - inPt.Y, P.X - inPt.X);
  110.                         Deflection = Deflection + delta(a1, a2);
  111.                         a1 = a2;
  112.                         Param1 = Param2;
  113.                         Param2 = Param2 + dVal;
  114.                     }
  115.                     if (Math.Abs(Deflection) > 4)
  116.                         ed.WriteMessage("\n*** Point is inside. ");
  117.                     else
  118.                         ed.WriteMessage("\n*** Point is outside. ");
  119.                 }
  120.                 tr.Commit();
  121.             }
  122.         }

  123.         [CommandMethod("INSIDE")]
  124.         static public void inside()
  125.         {
  126.             Database db = HostApplicationServices.WorkingDatabase;
  127.             Document doc = acadApp.DocumentManager.MdiActiveDocument;
  128.             Editor ed = doc.Editor;
  129.             PromptEntityResult res = ed.GetEntity("\nSelect polyline: ");
  130.             if (res.Status != PromptStatus.OK) return;
  131.             PromptPointResult pt2Res = ed.GetPoint("\nPick internal point: ");
  132.             if (pt2Res.Status != PromptStatus.OK) return;
  133.             isPointInside(pt2Res.Value, res.ObjectId);
  134.         }


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

已领礼包: 3718个

财富等级: 富可敌国

发表于 2018-5-24 20:38:28 | 显示全部楼层
本帖最后由 dnbcgrass 于 2018-5-26 09:50 编辑

谢谢分享!
是不是还差下面这些:
    const Double pi = Math.PI;
        public static Double dVal = 0.2;
        public static readonly Point3d kOrigin = new Point3d(0.0, 0.0, 0.0);
        public static readonly Vector3d kXAxis = new Vector3d(1.0, 0.0, 0.0);
        public static readonly Vector3d kYAxis = new Vector3d(0.0, 1.0, 0.0);
        public static readonly Vector3d kZAxis = new Vector3d(0.0, 0.0, 1.0);

        static Double delta(Double a1, Double a2)
        {
            Double ang;
            if (a1 > a2 + pi)
                ang = (a2 + pi + pi) - a1;
            else if (a2 > a1 + pi)
                ang = a2 - a1 - pi - pi;
            else
                ang = a2 - a1;
            return ang;
        }
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

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

使用道具 举报

已领礼包: 58个

财富等级: 招财进宝

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

使用道具 举报

已领礼包: 58个

财富等级: 招财进宝

发表于 2019-10-1 19:37:24 | 显示全部楼层
请教一下,我的写法怎么不能返回值给lisp,应该怎么写,谢谢,初学
  1. {

  2.         // 7:47 PM 6/29/2007 - LE! -

  3.         // from the MgdDbg SLN by Jim Awe AutoDesk
  4.         const Double pi = Math.PI;
  5.         public static Double dVal = 0.2;
  6.         public static readonly Point3d kOrigin = new Point3d(0.0, 0.0, 0.0);
  7.         public static readonly Vector3d kXAxis = new Vector3d(1.0, 0.0, 0.0);
  8.         public static readonly Vector3d kYAxis = new Vector3d(0.0, 1.0, 0.0);
  9.         public static readonly Vector3d kZAxis = new Vector3d(0.0, 0.0, 1.0);

  10.         static Double delta(Double a1, Double a2)
  11.         {
  12.             Double ang;
  13.             if (a1 > a2 + pi)
  14.                 ang = (a2 + pi + pi) - a1;
  15.             else if (a2 > a1 + pi)
  16.                 ang = a2 - a1 - pi - pi;
  17.             else
  18.                 ang = a2 - a1;
  19.             return ang;
  20.         }
  21.         public static Database GetCurDwg()

  22.         {

  23.             Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;

  24.             return db;

  25.         }


  26.         // from the MgdDbg SLN by Jim Awe AutoDesk

  27.         public static bool IsPaperSpace(Database db)

  28.         {

  29.             if (db.TileMode)

  30.                 return false;

  31.             Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

  32.             if (db.PaperSpaceVportId == ed.CurrentViewportObjectId)

  33.                 return true;

  34.             return false;

  35.         }


  36.         // from the MgdDbg SLN by Jim Awe AutoDesk

  37.         public static Matrix3d GetUcsMatrix(Database db)

  38.         {

  39.             Point3d origin;

  40.             Vector3d xAxis, yAxis, zAxis;

  41.             if (IsPaperSpace(db))

  42.             {

  43.                 origin = db.Pucsorg;

  44.                 xAxis = db.Pucsxdir;

  45.                 yAxis = db.Pucsydir;

  46.             }

  47.             else

  48.             {

  49.                 origin = db.Ucsorg;

  50.                 xAxis = db.Ucsxdir;

  51.                 yAxis = db.Ucsydir;

  52.             }

  53.             zAxis = xAxis.CrossProduct(yAxis);

  54.             return Matrix3d.AlignCoordinateSystem(kOrigin, kXAxis, kYAxis, kZAxis, origin, xAxis, yAxis, zAxis);

  55.         }


  56.         // from the MgdDbg SLN by Jim Awe AutoDesk

  57.         public static Point3d Ucs2Wcs(Point3d pt)

  58.         {

  59.             Matrix3d m = GetUcsMatrix(GetCurDwg());

  60.             return pt.TransformBy(m);

  61.         }


  62.         // from the MgdDbg SLN by Jim Awe AutoDesk

  63.         public static Point3d Wcs2Ucs(Point3d pt)

  64.         {

  65.             Matrix3d m = GetUcsMatrix(GetCurDwg());

  66.             return pt.TransformBy(m.Inverse());

  67.         }


  68.         // by Luis Esquivel on June 29 2007

  69.         // using the idea/algorithm by John F. Uhden,

  70.         // one of the masters that use to frequent the customization ng of AutoDesk

  71.         static ResultBuffer isPointInside(Point3d inPt, ObjectId objid)

  72.         {

  73.             Database db = HostApplicationServices.WorkingDatabase;

  74.             Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

  75.             Editor ed = doc.Editor;
  76.             //ResultBuffer resultBuffer = new ResultBuffer(); //初始化返回值链表  
  77.             TypedValue[] tv = new TypedValue[1];

  78.             using (Transaction tr = db.TransactionManager.StartTransaction())

  79.             {

  80.                 Polyline pPoly = (Polyline)tr.GetObject(objid, OpenMode.ForWrite, false);

  81.                 if (pPoly is Polyline)

  82.                 {

  83.                     if (!pPoly.Closed)

  84.                     {

  85.                         ed.WriteMessage("\n*** Curve is not closed. ");

  86.                     }

  87.                     Point3d P = new Point3d();

  88.                     P = Ucs2Wcs(inPt);

  89.                     Point3d ClosestPoint = pPoly.GetClosestPointTo(P, false); //true);

  90.                     if (P.IsEqualTo(ClosestPoint))

  91.                     {

  92.                         //ed.WriteMessage("\n*** Point is on curve. "); return;
  93.                         //resultBuffer.Add(new TypedValue((int)LispDataType.Text, "0"));
  94.                         tv[0] = new TypedValue((int)LispDataType.Int32, 0);

  95.                     }

  96.                     Double ClosestParam, EndParam;

  97.                     ClosestParam = pPoly.GetParameterAtPoint(ClosestPoint);

  98.                     ClosestPoint = Wcs2Ucs(ClosestPoint);

  99.                     EndParam = pPoly.EndParam;

  100.                     Double Param1 = 0.0, Param2 = dVal, Deflection = 0.0, a2;

  101.                     Point3d StartPT = pPoly.StartPoint;

  102.                     StartPT = Wcs2Ucs(StartPT);

  103.                     Double a1 = Math.Atan2(StartPT.Y - inPt.Y, StartPT.X - inPt.X);

  104.                     while (Param2 <= EndParam)

  105.                     {

  106.                         Param2 = Math.Min(Param2, EndParam);

  107.                         if (Param1 < ClosestParam && ClosestParam < Param2)

  108.                         {

  109.                             a2 = Math.Atan2(ClosestPoint.Y - inPt.Y, ClosestPoint.X - inPt.X);

  110.                             Deflection = Deflection + delta(a1, a2);

  111.                             a1 = a2;

  112.                         }

  113.                         bool test = true;

  114.                         while (test)

  115.                         {

  116.                             try

  117.                             {

  118.                                 P = pPoly.GetPointAtParameter(Param2);

  119.                                 Param2 = Param2 + dVal;

  120.                             }

  121.                             catch (System.Exception ex)

  122.                             {

  123.                                 ed.WriteMessage("\nERROR = " + ex.Message);

  124.                             }

  125.                             finally

  126.                             {

  127.                                 test = false;

  128.                             }

  129.                         }

  130.                         P = Wcs2Ucs(P);

  131.                         a2 = Math.Atan2(P.Y - inPt.Y, P.X - inPt.X);

  132.                         Deflection = Deflection + delta(a1, a2);

  133.                         a1 = a2;

  134.                         Param1 = Param2;

  135.                         Param2 = Param2 + dVal;

  136.                     }

  137.                     if (Math.Abs(Deflection) > 4)

  138.                         //ed.WriteMessage("\n*** Point is inside. ");
  139.                         //resultBuffer.Add(new TypedValue((int)LispDataType.Text, "-1"));
  140.                         tv[0] = new TypedValue((int)LispDataType.Int32, -1);
  141.                     else

  142.                         //ed.WriteMessage("\n*** Point is outside. ");
  143.                         //resultBuffer.Add(new TypedValue((int)LispDataType.Text, "1"));
  144.                         tv[0] = new TypedValue((int)LispDataType.Int32, 1);

  145.                 }

  146.                 tr.Commit();
  147.                
  148.                 ResultBuffer resb = new ResultBuffer(tv);
  149.                 //ed.WriteMessage("\n*** Point is inside. ");
  150.                 return resb;

  151.             }

  152.         }


  153.         [LispFunction("INSIDE")]

  154.         public static void inside(ResultBuffer rb)


  155.         {

  156.             Database db = HostApplicationServices.WorkingDatabase;

  157.             Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

  158.             Editor ed = doc.Editor;
  159.             TypedValue[] rbArr = rb.AsArray(); //Convoert to Array

  160.             TypedValue entId = rbArr[0]; //取出第一个参数
  161.             ObjectId objId = (ObjectId)entId.Value;
  162.             TypedValue pt = rbArr[1]; //取出第二个参数
  163.             Point3d pt1 = (Point3d)pt.Value;


  164.             //PromptEntityResult res = ed.GetEntity("\nSelect polyline: ");
  165.             ResultBuffer resultBuffer = new ResultBuffer(); //初始化返回值链表

  166.             //if (res.Status != PromptStatus.OK) return;

  167.             //PromptPointResult pt2Res = ed.GetPoint("\nPick internal point: ");

  168.             //if (pt2Res.Status != PromptStatus.OK) return;

  169.             //isPointInside(pt2Res.Value, res.ObjectId);
  170.             isPointInside(pt1, objId);

  171.         }

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

使用道具 举报

已领礼包: 58个

财富等级: 招财进宝

发表于 2019-10-5 15:45:31 来自手机 | 显示全部楼层
请教一下
1 打开事务管理器后,对图元进行坐标系转换,求最小包围盒结束后,如果不用tr.comit(),是否还有必要将图元转换为原来的坐标系

2 打开事务管理器后,在内存创建图元ent取得面积,如果不用tr.comit,是否用ent.dispose()直接销毁ent 即可,也等于释放内存了

我看您这里都用了commit,不太明白这个一定要用么
刚开始学习c#,很多问题不懂,希望能得到指点
来自: 微社区
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 82个

财富等级: 招财进宝

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-19 10:13 , Processed in 0.274777 second(s), 39 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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