通过一点选择所有经过该点的线(转载Kean的文章)

[ CommandMethod( "WinDb")]

    public void SelectCrossingDatabase()

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Editor ed =

        doc.Editor;


      try

      {

        PromptPointOptions ptOpts =

          new PromptPointOptions(

            "\nPick a point at which to select all lines: "

          );

        PromptPointResult ptRes = ed.GetPoint(ptOpts);

        if (PromptStatus.OK == ptRes.Status)

        {

          ObjectIdCollection ids =

            new ObjectIdCollection();

          Point3d p = ptRes.Value;

          Database db = doc.Database;

          Transaction tr =

            db.TransactionManager.StartTransaction();

          using( tr )

          {

            BlockTable bt =

              (BlockTable)tr.GetObject(

                db.BlockTableId,

                OpenMode.ForRead

              );

            BlockTableRecord btr =

              (BlockTableRecord)tr.GetObject(

                bt[BlockTableRecord.ModelSpace],

                OpenMode.ForRead

              );

            foreach( ObjectId id in btr )

            {

              DBObject obj =

                tr.GetObject( id, OpenMode.ForRead );

              Line dbLine = obj as Line;

              if (dbLine != null)

              {

                LineSegment3d lineSegment =

                  new LineSegment3d(

                    dbLine.StartPoint,

                    dbLine.EndPoint

                  );

                PointOnCurve3d q =

                  lineSegment.GetClosestPointTo(p);

                if (p.DistanceTo( q.Point ) < 0.01)

                {

                  ids.Add( dbLine.ObjectId );

                }

              }

            }

            tr.Commit();

          }

          int n = ids.Count;

          ed.WriteMessage(

            string.Format(

              "\n{0} line{1} selected.",

              n, 1 == n ? "" : "s"

            )

          );

        }

      }

      catch( System.Exception e )

      {

        ed.WriteMessage("\nException {0}.", e);

      }

    }

  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值