当前位置:网站首页>C# autoCAD 几个经常用到的功能代码。
C# autoCAD 几个经常用到的功能代码。
2022-07-29 12:34:00 【laocooon】
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 块
{
static class Class2
{
/// <summary>
/// 判断点是否重合
/// </summary>
/// <param name="p1">点1</param>
/// <param name="p2">点2</param>
/// <param name="allowance">容差</param>
/// <returns></returns>
public static Boolean Coincide( this Point3d p1, Point3d p2, Double allowance)
{
if (p1.DistanceTo(p2) < allowance)
return true;
else
return false;
}
/// <summary>
/// 判断点是否在直线上
/// </summary>
/// <param name="line">直线</param>
/// <param name="point1">点</param>
/// <param name="allowance">容差</param>
/// <returns></returns>
public static Boolean Coincide(this Line line, Point3d point1, Double allowance)
{
Point3d p1 = line.StartPoint;
Point3d p2 = line.EndPoint;
//叉积是否为allowance,判断是否在同一直线上
if (Math.Abs((p1.X - point1.X) * (p2.Y - point1.Y) - (p2.X - point1.X) * (p1.Y - point1.Y)) < allowance)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 判断两条直线是否重合
/// </summary>
/// <param name="line1">线1</param>
/// <param name="line2">线2</param>
/// <param name="allowance">容差</param>
/// <returns></returns>
public static Boolean Coincide(this Line line1, Line line2, Double allowance)
{
LineSegment3d l1 = new LineSegment3d(line1.StartPoint, line1.EndPoint);
LineSegment3d l2 = new LineSegment3d(line2.StartPoint, line2.EndPoint);
Tolerance tol = new Tolerance(allowance, allowance);
return l1.IsColinearTo(l2, tol);
}
/// <summary>
/// 空间两点之间距离
/// </summary>
/// <param name="point1">点1</param>
/// <param name="point2">点2</param>
/// <param name="Decimal">小数位数</param>
/// <returns></returns>
public static Double Distance(this Point3d point1, Point3d point2, Int16 Decimal)
{
Double a = point1.X - point2.X;
Double b = point1.Y - point2.Y;
Double c = point1.Z - point2.Z;
Double r = a * a + b * b + c * c;
return Math.Round(Math.Abs(Math.Sqrt(r)), Decimal);
}
/// <summary>
/// 获得空间点到空间线段的垂直距离
/// </summary>
/// <param name="line">直线</param>
/// <param name="pt">空间的点</param>
/// <param name="Decimal">小数位数</param>
/// <returns>点到直线垂直距离</returns>
public static Double Distance(Line line, Point3d pt, Int16 Decimal)
{
Double t;
if (line != null)
{
Point3d StartPoint = line.StartPoint;
Point3d EndPoint = line.EndPoint;
//直线方向向量
Point3d dir = new Point3d(StartPoint.X - EndPoint.X, StartPoint.Y - EndPoint.Y, StartPoint.Z - EndPoint.Z);
//过点且与直线垂直的平面
t = -(Double)(dir.X * (StartPoint.X - pt.X) + dir.Y * (StartPoint.Y - pt.Y) + dir.Z * (StartPoint.Z - pt.Z)) / (dir.X * dir.X + dir.Y * dir.Y + dir.Z * dir.Z);
//过点的垂直于直线的平面与该直线的交点
Point3d fp = new Point3d(StartPoint.X + dir.X * t, StartPoint.Y + dir.Y * t, StartPoint.Z + dir.Z * t);
return Distance(pt, fp, Decimal);
}
else
{
return 0;
}
}
/// <summary>
/// 中点
/// </summary>
/// <param name="StartPoint">起点</param>
/// <param name="EndPoint">终点</param>
/// <returns></returns>
public static Point3d Midpoint(this Point3d StartPoint, Point3d EndPoint)
{
Double x = (StartPoint.X + EndPoint.X) / 2;
Double y = (StartPoint.Y + EndPoint.Y) / 2;
Double z = (StartPoint.Z + EndPoint.Z) / 2;
return new Point3d(x, y, z);
}
public static double AngleFromXAxis(this Point3d pt1, Point3d pt2)
{
Vector2d vector=new Vector2d(pt1.X-pt2.X, pt1.Y-pt2.Y);
return vector.Angle;
}
/// <summary>
/// 角度转换弧度
/// </summary>
/// <param name="degree">角度制</param>
/// <returns></returns>
public static double DegreeToAngle(this Double degree)
{
return degree * Math.PI / 180;
}
/// <summary>
/// 弧度转换角度
/// </summary>
/// <param name="angle">弧度制</param>
/// <returns></returns>
public static double AngleToDegree(this Double angle)
{
return angle * 180 / Math.PI;
}
/// <summary>
/// 环形阵列
/// </summary>
/// <param name="entId">图形对象的ObjectId</param>
/// <param name="num">图形数量</param>
/// <param name="degree">中心点到各个图形的夹角</param>
/// <param name="center">中心点</param>
/// <returns>List</returns> 已经加入图形数据库
public static List<Entity> ArrayPolarEntity(this ObjectId entId, int num, double degree, Point3d center)
{
// 声明一个List集合 用于返回
List<Entity> entList = new List<Entity>();
// 打开事务处理
using (Transaction trans = entId.Database.TransactionManager.StartTransaction())
{
// 打开块表
BlockTable bt = (BlockTable)trans.GetObject(entId.Database.BlockTableId, OpenMode.ForRead);
// 打开块表记录
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
Entity ent = (Entity)entId.GetObject(OpenMode.ForWrite);
// 限定阵列角度大小
degree = degree > 360 ? 360 : degree;
degree = degree < -360 ? 360 : degree;
int divAngnum = num - 1;
if (degree == 360 || degree == -360)
{
divAngnum = num;
}
// 计算变换矩阵
for (int i = 0; i < num; i++)
{
Matrix3d mt = Matrix3d.Rotation((i * degree / divAngnum).DegreeToAngle(), Vector3d.ZAxis, center);
Entity entA = ent.GetTransformedCopy(mt);
btr.AppendEntity(entA);
trans.AddNewlyCreatedDBObject(entA, true);
entList.Add(entA);
}
ent.Erase();
trans.Commit();
}
return entList;
}
}
}
边栏推荐
- Scala 简介一
- Wu En teacher machine learning course notes 6 logistic regression
- 来自 Qt 官网的呐喊
- MLX90640 infrared thermal imaging temperature measuring sensor module development notes (9)
- Go简单实现协程池
- JUC阻塞队列-ArrayBlockingQueue
- 金仓数据库KingbaseES安全指南--6.7. GSSAPI身份验证
- How Navicat Connects to MySQL
- js进阶四(map、reduce、filter、sort、箭头函数、class继承、yield)
- 命里有时终须有--记与TiDB的一次次擦肩而过
猜你喜欢

Chapter ten find and record the REST API

Error EPERM operation not permitted, mkdir ‘Dsoftwarenodejsnode_cache_cacach两种解决办法
![[Mysql] LENGTH函数](/img/a1/112cac6b42f8c7abec7e4a6629dffd.png)
[Mysql] LENGTH函数

Nacos分级存储模型-集群配置与NacosRule负载均衡

【云原生】-Docker容器迁移Oracle到MySQL
![[纯理论] FCOS](/img/a4/f4f28faf5764e1fdebd475e445e2b0.png)
[纯理论] FCOS
![[MySQL view] View concept, create, view, delete and modify](/img/dc/436dbaa0419b76cdab02a57436a782.png)
[MySQL view] View concept, create, view, delete and modify

MySQL基础(DDL、DML、DQL)
![[Cloud native] Introduction and use of Feign of microservices](/img/39/05cf7673155954c90e75a8a2eecd96.jpg)
[Cloud native] Introduction and use of Feign of microservices

The whole process of installing Oracle database on CentOS7
随机推荐
策略模式替代 if else
npm出现报错 npm WARN config global `--global`, `--local` are deprecated. Use `--location=global
JS advanced four (map, reduce, filter, sort, arrow function, class inheritance, yield)
学习的时候碰见的一个sql问题,希望大佬们可以解答一二?
gee引擎修改UI界面图文教程
Py之eli5:eli5库的简介、安装、使用方法之详细攻略
es6箭头函数讲解
MySQL 视图(详解)
金仓数据库KingbaseES客户端编程接口指南-JDBC(4. JDBC 创建语句对象)
70行代码撸一个桌面自动翻译神器!
TiDB 操作实践 -- 备份与恢复
PHP 基础知识
Windows系统Mysql8版本的安装教程
piglit_get_gl_enum_from_name 参数遍历
A recent paper summarizes
Mysql各个大版本之间的区别
The meaning of "last in first out" in stack and "first in first out" in queue
Nacos hierarchical storage model - the cluster configuration and NacosRule load balance
【c ++ primer 笔记】第6章 函数
CentOS7安装Oracle数据库的全流程