C#取得多段線中圓弧中點座標

 C#取得多段線中圓弧中點座標
搞了好久才弄明白怎麼取得多段線中點的座標值,以下是代碼,和大家交流一下!其實還有更好的方法,我也是寫出這段代碼之後纔在高手指導下知道第二種方法的,也一起貼出來。

using System;

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
[assembly: ExtensionApplication(typeof(ClassLibrary.Lab8Class))]
[assembly: CommandClass(typeof(ClassLibrary.Lab8Class))]

namespace ClassLibrary
{
 public class Lab8Class:IExtensionApplication
 {
  public void Initialize()
  {
   Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\n調試程序命令LV");//初始化操作

  }
  public void Terminate()
  {
   //清除操作
  }
  
  public Lab8Class()
  {
   //
   // TODO: Add constructor logic here
   //
  }
  Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  Database db = Autodesk.AutoCAD.DatabaseServices.HostApplicationServices.WorkingDatabase;
  Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = Autodesk.AutoCAD.DatabaseServices.HostApplicationServices.WorkingDatabase.TransactionManager;
  // Define Command "AsdkCmd1"
  [CommandMethod("LV")]
  public void getPlPoint() // This method can have any name
  {
   try
   {
    Transaction trans=tm.StartTransaction();
//    BlockTableRecord btr;
//    BlockTable bt;
    using(trans)
    {
                     PromptEntityResult per = ed.GetEntity("請選擇多段線");
     if(per.Status == PromptStatus.OK)
     {
      DBObject obj = trans.GetObject(per.ObjectId, OpenMode.ForRead);
      {
       Polyline PL = obj as Polyline;
       int vn = PL.NumberOfVertices;
       for(int i = 0; i<= vn; i++)
       {
        Point3d pt3d = PL.GetPoint3dAt(i-1);

        double vBulge = PL.GetBulgeAt(i);
        if(vBulge != 0)
        {

         //方法一,比較笨的方法。

  double len0 = PL.GetDistAtPoint(PL.GetPoint3dAt(i));
  double len1 = PL.GetDistAtPoint(PL.GetPoint3dAt(i+1));
  double midlen = (len0 + len1)/2;

  ed.WriteMessage("\n第二種方法計算的圓弧中點是:" + midP3d.ToString());

//方法二,但是速度比較慢好像,因爲如果加上下面的代碼,運行速度明顯慢,前面是感覺不出來的,後面的要停頓一下,可能有異常

  Point3d midL = PL.GetPointAtParameter(i+0.5);
  ed.WriteMessage("\n第二種方法計算的圓弧中點是:" + midL.ToString());
   
        }

       }
      }
     }
     trans.Commit();
     trans.Dispose();
    }
   }
   catch{}
   finally
   {
   }
  }

 }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章