c# CAD二次开发 类库 创建各种图形、直线、圆、多段线、正方形、点等

news/2024/11/29 4:33:04/

c# CAD二次开发 类库 创建各种图形、直线、圆、多段线、正方形、点等
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _02创建图形
{
public static partial class AddEntityTool
{
///
/// 将图形对象添加到图形文件中
///
/// 图形数据库
/// 图形对象
/// 图形的ObjectId
public static ObjectId AddEnityToModelSpace(this Database db,Entity ent)
{
//声明ObjectId,用于返回
ObjectId entId = ObjectId.Null;
//开启事务处理
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);
//添加图形到块表记录
entId = btr.AppendEntity(ent);
//更新数据信息
trans.AddNewlyCreatedDBObject(ent, true);
//提交事务
trans.Commit();
}
return entId;
}
///
/// 将图形对象添加到图形文件中
///
/// 图形数据库
/// 图形对象,可变参数
/// 图形的ObjectId,数组返回
public static ObjectId[] AddEnityToModelSpace(this Database db, params Entity[] ent)
{
//声明ObjectId,用于返回
ObjectId[] entId = new ObjectId[ent.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 < ent.Length; i++)
{
//添加图形到块表记录
entId[i] = btr.AppendEntity(ent[i]);
//更新数据信息
trans.AddNewlyCreatedDBObject(ent[i], true);
}

//提交事务
trans.Commit();
}
return entId;
}
///
/// 绘制直线
///
/// 图形数据库
/// 起点坐标
/// 终点坐标
/// ObjectId
public static ObjectId AddLineToModelSpace(this Database db, Point3d startPoint, Point3d endPoint)
{
return db.AddEnityToModelSpace(new Line(startPoint, endPoint));
}
///
/// 绘制直线
///
/// 图形数据库
/// 起点坐标
/// 直线长度
/// 与X轴正方向的角度
/// ObjectId
public static ObjectId AddLineToModelSpace(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.AddEnityToModelSpace(new Line(startPoint, endPoint));
}
///
/// 绘制圆弧
///
/// 图形数据库
/// 圆弧所在圆的圆心点
/// 圆弧的半径
/// 圆弧的起始角度
/// 圆弧的终止角度
/// ObjectId
public static ObjectId AddArcToModelSpace(this Database db, Point3d center, double radius, double startDegree, double endDegree)
{
return db.AddEnityToModelSpace(new Arc(center, radius, startDegree.DegreeToAngle(), endDegree.DegreeToAngle()));
}
///
/// 通过三点创建圆弧
///
/// 图形数据库
/// 起点
/// 圆弧上的点
/// 终点
///
public static ObjectId AddArcToModelSpace(this Database db, Point3d startPoint, Point3d pointOnArc, Point3d endPoint)
{
//先判断三点是否在同一条直线上
if (startPoint.IsOnOneLine(pointOnArc,endPoint))
{
return ObjectId.Null;
}

//创建几何类对象
CircularArc3d cArc = new CircularArc3d(startPoint, pointOnArc, endPoint);
//通过几何类对象获取其属性
//double radius = cArc.Radius; //半径
//Point3d center = cArc.Center; //所在圆的圆心
//Vector3d cs = center.GetVectorTo(startPoint); //圆心到起点的向量
//Vector3d ce = center.GetVectorTo(endPoint); //圆心到终点的向量
//Vector3d xVector = new Vector3d(1, 0, 0); //X正方向的向量
圆弧的起始角度
//double startAngle = cs.Y > 0 ? xVector.GetAngleTo(cs) : -xVector.GetAngleTo(cs);
圆弧的终止角度
//double endAngle = ce.Y > 0 ? xVector.GetAngleTo(ce) : -xVector.GetAngleTo(ce);

        //创建圆弧对象Arc arc = new Arc(cArc.Center, cArc.Radius, cArc.Center.GetAngleToXAxis(startPoint), cArc.Center.GetAngleToXAxis(endPoint));//加入图形数据库return db.AddEnityToModelSpace(arc);}/// <summary>/// 通过圆心、起点,夹角绘制圆弧/// </summary>/// <param name="db">图形数据库</param>/// <param name="center">圆心</param>/// <param name="startPoint">起点</param>/// <param name="degree">夹角,角度值</param>/// <returns>ObjectId</returns>public static ObjectId AddArcToModelSpace(this Database db, Point3d center, Point3d startPoint, double degree){//获取半径double radius = center.GetDistanceBetweenTwoPoint(startPoint);//获取起点角度double startAngle = center.GetAngleToXAxis(startPoint);//声明圆弧对象Arc arc = new Arc(center, radius, startAngle, startAngle + degree.DegreeToAngle());return db.AddEnityToModelSpace(arc);}/// <summary>/// 绘制圆/// </summary>/// <param name="db">图形据据库</param>/// <param name="center">圆心</param>/// <param name="radius">半径</param>/// <returns>ObjectId</returns>public static ObjectId AddCircleModelSpace(this Database db, Point3d center, double radius){return db.AddEnityToModelSpace(new Circle(center, new Vector3d(0, 0, 1), radius));}/// <summary>/// 两点绘制圆/// </summary>/// <param name="db">图形数据库</param>/// <param name="point1">第一个点</param>/// <param name="point2">第二个点</param>/// <returns>ObjectId</returns>public static ObjectId AddCircleModelSpace(this Database db, Point3d point1, Point3d point2){//获取中心点Point3d center = point1.GetCenterPointBetweenTwoPoint(point2);//获取半径double radius = point1.GetDistanceBetweenTwoPoint(center);return db.AddCircleModelSpace(center, radius);}/// <summary>/// 三点绘制圆/// </summary>///<param name="db">图形数据库</param>/// <param name="point1">第一个点</param>/// <param name="point2">第二个点</param>/// <param name="point3">第三个点</param>/// <returns>ObjectId</returns>public static ObjectId AddCircleModelSpace(this Database db, Point3d point1, Point3d point2, Point3d point3){//先判断三点是否在同一条直线上if (point1.IsOnOneLine(point2, point3)){return ObjectId.Null;}//声明几何类的CircularArc3d对象CircularArc3d cArc = new CircularArc3d(point1, point2, point3);return db.AddCircleModelSpace(cArc.Center, cArc.Radius);}/// <summary>/// 绘制折线多段线/// </summary>/// <param name="db">图形数据库</param>/// <param name="isClosed">是否闭合</param>/// <param name="constantWidth">线宽</param>/// <param name="vertices">多段线的定点,可变参数</param>/// <returns>ObjectId</returns>public static ObjectId AddPolyLineToModelSpace(this Database db, bool isClosed, double constantWidth, params Point2d[] vertices){if (vertices.Length < 2 ){return ObjectId.Null;}//声明一个多段线对象Polyline pLine = new Polyline();//添加多段线的顶点for (int i = 0; i < vertices.Length; i++){pLine.AddVertexAt(i, vertices[i], 0, 0, 0);}//判断是否闭合if (isClosed){pLine.Closed = true;}//设置多段线的线宽pLine.ConstantWidth = constantWidth;return db.AddEnityToModelSpace(pLine);}/// <summary>/// 绘制矩形/// </summary>/// <param name="db">图形数据库</param>/// <param name="point1">第一个点</param>/// <param name="point2">对角点</param>/// <returns>ObjectId</returns>public static ObjectId AddRectToModelSpace(this Database db, Point2d point1, Point2d point2){//声明多段线Polyline pLine = new Polyline();//计算矩形的四个顶点坐标Point2d p1 = new Point2d(Math.Min(point1.X, point2.X), Math.Min(point1.Y, point2.Y));Point2d p2 = new Point2d(Math.Max(point1.X, point2.X), Math.Min(point1.Y, point2.Y));Point2d p3 = new Point2d(Math.Max(point1.X, point2.X), Math.Max(point1.Y, point2.Y));Point2d p4 = new Point2d(Math.Min(point1.X, point2.X), Math.Max(point1.Y, point2.Y));//添加多段线的顶点pLine.AddVertexAt(0, p1, 0, 0, 0);pLine.AddVertexAt(0, p2, 0, 0, 0);pLine.AddVertexAt(0, p3, 0, 0, 0);pLine.AddVertexAt(0, p4, 0, 0, 0);//闭合多段线pLine.Closed = true;return db.AddEnityToModelSpace(pLine);}/// <summary>/// 绘制内接正多边形/// </summary>/// <param name="db">图形数据库</param>/// <param name="center">多边形所在圆的圆心</param>/// <param name="radius">所在圆的半径</param>/// <param name="sideNum">边数</param>/// <param name="startDegree">起始角度</param>/// <returns>ObjectId</returns>public static ObjectId AddPolygonToModelSpace(this Database db, Point2d center,double radius,int sideNum,double startDegree){//声明多段线对象Polyline pLine = new Polyline();//判断边数是否符合要求if (sideNum < 3){return ObjectId.Null;}Point2d[] point = new Point2d[sideNum];double angle = startDegree.DegreeToAngle();//计算每个顶点的坐标for (int i = 0; i < sideNum; i++){point[i] = new Point2d(center.X + radius * Math.Cos(angle), center.Y + radius * Math.Sin(angle));pLine.AddVertexAt(i, point[i], 0, 0, 0);angle += Math.PI * 2 / sideNum;}//闭合多段线pLine.Closed = true;return db.AddEnityToModelSpace(pLine);}/// <summary>/// 绘制椭圆/// </summary>/// <param name="db">图形数据库</param>/// <param name="center">椭圆中心</param>/// <param name="majorRadius">长轴长度</param>/// <param name="shortRadius">短轴长度</param>/// <param name="degere">长轴与X轴夹角,角度值</param>/// <param name="startDegree">起始角度</param>/// <param name="endDegree">终止角度</param>/// <returns>ObjectId</returns>public static ObjectId AddEllipseToModelSpace(this Database db, Point3d center, double majorRadius, double shortRadius, double degere,double startDegree,double endDegree){//计算相关参数double ratio = shortRadius/majorRadius;Vector3d majorAxis = new Vector3d(majorRadius * Math.Cos(degere.DegreeToAngle()),majorRadius * Math.Sin(degere.DegreeToAngle()),0);//(new Point3d(100, 100, 0), Vector3d.ZAxis, new Vector3d(100, 0, 0), 0.4, 0, 2*Math.PI);//声明椭圆对象Ellipse elli = new Ellipse(center, Vector3d.ZAxis, majorAxis, ratio, startDegree.DegreeToAngle(), endDegree.DegreeToAngle());return db.AddEnityToModelSpace(elli);}/// <summary>/// 绘制椭圆/// </summary>/// <param name="db">图形数据库</param>/// <param name="majorPoint1">长轴端点1</param>/// <param name="majorPoint2">长轴端点2</param>/// <param name="shortRadius">短轴的长度</param>/// <returns>ObjectId</returns>public static ObjectId AddEllipseToModelSpace(this Database db, Point3d majorPoint1, Point3d majorPoint2, double shortRadius){//椭圆的圆心Point3d center = majorPoint1.GetCenterPointBetweenTwoPoint(majorPoint2);//短轴与长轴的比double ratio = 2*shortRadius / majorPoint1.GetDistanceBetweenTwoPoint(majorPoint2);//长轴的向量Vector3d majorAxis = majorPoint2.GetVectorTo(center);Ellipse elli = new Ellipse(center, Vector3d.ZAxis,majorAxis, ratio, 0, 2 * Math.PI);return db.AddEnityToModelSpace(elli);}/// <summary>/// 绘制椭圆/// </summary>/// <param name="db">图形数据库</param>/// <param name="point1">所在矩形的顶点1</param>/// <param name="point2">所在矩形的顶点1</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);//声明椭圆对象Ellipse elli = new Ellipse(center, Vector3d.ZAxis, majorVector, ratio, 0, 2 * Math.PI);return db.AddEnityToModelSpace(elli);}
}

}


http://www.ppmy.cn/news/112844.html

相关文章

6月销量狂欢季:测评自养号助力,引爆跨境电商销量!

随着夏季的到来&#xff0c;跨境电商卖家们迎来了一个极佳的销售机会。6月作为夏季的重要节点&#xff0c;各种活动和节日都为卖家们提供了引流和销售的良机。然而&#xff0c;要真正实现销量的爆发&#xff0c;单纯依靠传统的营销手段可能难以达到预期的效果。在这篇文章中&am…

低代码开发重要工具:jvs-rules 规则引擎功能介绍(二)

一、JVS规则引擎接口管理 接口管理是将逻辑引擎对接所有的接口进行统一管理&#xff0c;便于各种应用统一引用。 接口的界面展示 接口列表的展示&#xff0c;接口列表是根据系统内部支持的接口进行展现&#xff0c;包括查看接口的详情 详情中包括&#xff0c;入参与出参的展…

安装mysql8时遇到的问题

设置弱密码 由于我是用于测试环境所以想将密码调整为弱密码 使用root用户或具有管理员权限的用户登录MySQL服务器。 执行以下命令进入MySQL命令行界面&#xff1a; mysql -u root -p首次安装时生成的随机密码可以使用sudo grep temporary password /var/log/mysqld.log命令找…

你了解LED显示屏灰度等级和灰度原理吗?

LED显示屏的灰度等级是指显示屏能够显示的不同灰度级别的数量。每个像素可以显示的灰度级别越多&#xff0c;显示出的图像就越细腻和真实。 灰度原理是LED显示屏控制每个像素的亮度来实现不同灰度级别的显示。LED显示屏是由许多小型LED灯组成的&#xff0c;每个LED灯称为一个像…

op3

op3教程&#xff1a; http://emanual.robotis.com/docs/en/platform/op3/introduction/ DYNAMIXEL SDK教程 http://emanual.robotis.com/docs/en/software/dynamixel/dynamixel_sdk/overview/

对比Warm Audio话筒在通鼓录音上的表现 ITM30

在In The Mix EP30里&#xff0c;我们将跟随Joe Carrell尝试Warm Audio不同的麦克风在通鼓上会有什么样的表现。 这将帮助您一定程度上更加了解这些麦克风的特性&#xff0c;从而帮助您精准的作出适合自己的选择。 点击观看视频&#xff1a;https://www.bilibili.com/video/B…

EPRIM3

Eprime3 每一次存盘、运行都要等10秒&#xff0c;好烦啊