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
{
}
}
}