您的位置:首页 > 其它

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

2012-11-26 11:06 176 查看
[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);
      }
    }
  }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐