CAD二次开发:将绘制直线进行封装

通过起点和终点的坐标生成直线

        /// <summary>
        /// 绘制直线
        /// </summary>
        /// <param name="db">图形数据库</param>
        /// <param name="startPoint">起点</param>
        /// <param name="endPoint">终点</param>
        /// <returns></returns>
        public static ObjectId AddLineModelSpace(this Database db, Point3d startPoint, Point3d endPoint)
        {  
           return  db.addEntityToolModelSpace(new Line(startPoint, endPoint));
        }

通过起点坐标和长度、角度生成直线

        /// <summary>
        /// 绘制直线
        /// </summary>
        /// <param name="db">图形数据库</param>
        /// <param name="startPoint">起点</param>
        /// <param name="length">长度</param>
        /// <param name="degree">与X轴正方向的夹角</param>
        /// <returns>ObjectID</returns>
        public static ObjectId AddLineModelspace(this Database db, Point3d startPoint, Double length, Double degree)
        {
            //计算终点坐标
            double X = startPoint.X + length * Math.Cos(degree.DegreeToAngle());
            double Y = startPoint.Y + length * Math.Sin(degree.DegreeToAngle());
            Point3d endPoint = new Point3d(X,Y,0);
            return db.addEntityToolModelSpace(new Line(startPoint, endPoint));
        }

角度和弧度互转的方法

public static partial class BaseTool
    {
        /// <summary>
        /// 角度转弧度
        /// </summary>
        /// <param name="degree">角度值</param>
        /// <returns>弧度值</returns>
        public static Double DegreeToAngle(this double degree) 
        {
            return degree*Math.PI/180;
        }

        /// <summary>
        /// 弧度转角度
        /// </summary>
        /// <param name="hudu">弧度值</param>
        /// <returns>角度值</returns>
        public static Double AngleToDegree(this double hudu)
        {
            return hudu * 180 / Math.PI;
        }
    }

画线

Database db = HostApplicationServices.WorkingDatabase;
db.AddLineModelspace(new Point3d(200, 100, 0), 200, 50);