天天看點

AutoCAD .Net 建立圓Circle

以下代碼展示:

往模型空間中添加一個圓。圓心為(100, 100, 0),半徑為20,圓所在平面為XOY平面。

設定圓的圖層、顔色、線型、線寬請參考文章AutoCAD .Net 建立直線Line

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Colors;

[CommandMethod("NewCircle")]
public static void NewCircle()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;

    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        //-------------------------------
        // 擷取模型空間
        //-------------------------------
        BlockTable blockTbl = tr.GetObject(
            db.BlockTableId, OpenMode.ForRead) as BlockTable;
        BlockTableRecord modelSpace = tr.GetObject(
            blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

        //-------------------------------
        // 建立圓
        //-------------------------------
        Circle circle = new Circle();
        circle.Center = new Point3d(, , );
        circle.Normal = new Vector3d(, , );
        circle.Radius = ;

        //-------------------------------
        // 添加到模型空間并送出到資料庫
        //-------------------------------
        modelSpace.AppendEntity(circle);
        tr.AddNewlyCreatedDBObject(circle, true);
        tr.Commit();
    }
}
           

Circle.Normal 指定圓所在平面的法向量。

AutoCAD .Net API 中沒有提供求過3點圓的方法。

附上在2維平面内求過三點圓的方法

/// <summary>
/// 求過平面3點的圓
/// (x1, y1) --- 點1
/// (x2, y2) --- 點2
/// (x3, y3) --- 點3
/// (cx, cy) --- 求出的圓心坐标
/// radius   --- 求出的圓心半徑
/// 傳回值:
///     存在則傳回true
///     不存在則傳回false
/// </summary>
public static bool CircleFrom3Points(
    double x1, double y1,
    double x2, double y2,
    double x3, double y3,
    out double cx, out double cy, out double radius)
{
    double a = x1 - x2;
    double b = y1 - y2;
    double c = x1 - x3;
    double d = y1 - y3;
    double e = ((x1 * x1 - x2 * x2) + (y1 * y1 - y2 * y2)) / ;
    double f = ((x1 * x1 - x3 * x3) + (y1 * y1 - y3 * y3)) / ;
    double det = b * c - a * d;

    if (Math.Abs(det) < )
    {
        cx = ;
        cy = ;
        radius = ;
        return false;
    }
    else
    {
        cx = -(d * e - b * f) / det;
        cy = -(a * f - c * e) / det;
        radius = Math.Sqrt(
            (x1 - cx) * (x1 - cx) + (y1 - cy) * (y1 - cy));
        return true;
    }
}