CAD二次开发 创建椭圆

找到椭圆的圆心

  Point3d center = point1.GetCenterPointBetweenTwoPoint(point2);

长轴和短轴比

double ratio = Math.Abs((point1.Y - point2.Y) / (point1.X - point2.X) );

长轴向量

Vector3d majorVector = new Vector3d(Math.Abs(point1.X - point2.X) / 2, 0, 0);
if (ratio>=1)
{
    ratio = Math.Abs((point1.X - point2.X) / (point1.Y - point2.Y));
    majorVector = new Vector3d(0, Math.Abs(point1.Y - point2.Y) / 2, 0);
}

声明椭圆形并添加到图形文件中

Ellipse elli = new Ellipse(center, Vector3d.ZAxis, majorVector, ratio, 0, Math.PI * 2);
return db.AddEntityToModelSpace(elli);

将图形对象添加到图形文件中(AddEntityToModelSpace)

/// <summary>
/// 将图形对象添加到图形文件中
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="entity">图形对象,可变参数</param>
/// <returns>图形的objectId,数组返回</returns>
public static ObjectId[] AddEntityToModelSpace(this Database db,params Entity entity)
{
    //声明ObjectId 用于返回
    ObjectId[] entityId = new ObjectId[entity.Length];
    //开启事务
    using (Transaction trans = db.TransactionManager.StartTransaction())
    {
        //打开块表
        BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,OpenMode.ForRead);
        // 打开块表记录
        BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForWrite);

        for (int i = 0; i < entity.Length; i++)
        {
            //添加图形到块表记录
            entityId[i] = btr.AppendEntity(entity[i]);
            //更新数据消息
            trans.AddNewlyCreatedDBObject(entity[i],true);
        }
        trans.Commit();
    }

    return entityId;
}

完整代码

/// <summary>
/// 绘制椭圆
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="point1">所在矩形的顶点1</param>
/// <param name="point2">所在矩形的顶点2</param>
/// <returns>ObjectId</returns>
public static ObjectId AddEllipseToModelSpace(this Database db, Point3d point1, Point3d point2)
{
    //椭圆的圆心
    Point3d center = point1.GetCenterPointBetweenTwoPoint(point2);
    double ratio = Math.Abs((point1.Y - point2.Y) / (point1.X - point2.X) );
    Vector3d majorVector = new Vector3d(Math.Abs(point1.X - point2.X) / 2, 0, 0);
    if (ratio>=1)
    {
        ratio = Math.Abs((point1.X - point2.X) / (point1.Y - point2.Y));
        majorVector = new Vector3d(0, Math.Abs(point1.Y - point2.Y) / 2, 0);
    }
    
    //声明椭圆形
    Ellipse elli = new Ellipse(center, Vector3d.ZAxis, majorVector, ratio, 0, Math.PI * 2);
    return db.AddEntityToModelSpace(elli);
}

调用

[CommandMethod("EllipseDemo")]
public void EllipseDemo()
{
    //声明图形数据库
    Database db = HostApplicationServices.WorkingDatabase;

    db.AddEllipseToModelSpace(new Point3d(100,100,0),new Point3d(500,500,0));
    db.AddRectToModelSpace(new Point2d(100, 100), new Point2d(500, 500));
    db.AddEllipseToModelSpace(new Point3d(100,100,0),new Point3d(500,400,0));
    db.AddRectToModelSpace(new Point2d(100, 100), new Point2d(500, 400));
    db.AddEllipseToModelSpace(new Point3d(100,100,0),new Point3d(200,400,0));
    db.AddRectToModelSpace(new Point2d(100, 100), new Point2d(200, 400));
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周杰伦fans

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值