# 0. 引言
由于很多代码都是重复性的,比如之前我们封装在.cs类文件中的工具性代码,包括用于添加图形实体的AddEntityTools.cs、填充工具类HatchTools.cs、以及基本工具类BaseTools.cs
这些类文件我们可以进一步封装成一个dll库文件,这样使用的时候添加进来,然后在头文件中添加命名空间即可!
这样会方便很多!
# 1. 封装dll库
新建一个项目,添加引用
新建三个类:添加图形实体的类AddEntityTools.cs、填充工具类HatchTools.cs、以及基本工具类BaseTools.cs
然后把之前的代码都加进来
AddEntityTools.cs
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 AcDoNetTools
{
public static partial class AddEntityTools
{
/// <summary>
/// 将图形对象添加到图形文件中
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="ent">图形对象</param>
/// <returns>图形的ObjectId</returns>
public static ObjectId AddEntityToModeSpace(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;
}
/// <summary>
/// 添加多个图形对象到图形文件中
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="ent">图形对象 可变参数</param>
/// <returns>图形的ObjectId 数组返回</returns>
public static ObjectId[] AddEntityToModeSpace(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;
}
/// <summary>
/// 绘制直线
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="startPoint">起点坐标</param>
/// <param name="endPoint">终点坐标</param>
/// <returns></returns>
public static ObjectId AddLineToModeSpace(this Database db, Point3d startPoint, Point3d endPoint)
{
return db.AddEntityToModeSpace(new Line(startPoint, endPoint));
}
/// <summary>
/// 起点坐标,角度,长度 绘制直线
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="startPoint">起点</param>
/// <param name="length">长苏</param>
/// <param name="degree">角度</param>
/// <returns></returns>
public static ObjectId AddLineToModeSpace(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.AddEntityToModeSpace(new Line(startPoint, endPoint));
}
// 封装圆弧对象函数
/// <summary>
/// 绘制圆弧
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="center">中心</param>
/// <param name="radius">半径</param>
/// <param name="startDegree">起始角度</param>
/// <param name="endDegree">终止角度</param>
/// <returns></returns>
public static ObjectId AddArcToModeSpace(this Database db, Point3d center, double radius, double startDegree, double endDegree)
{
return db.AddEntityToModeSpace(new Arc(center, radius, startDegree.DegreeToAngle(), endDegree.DegreeToAngle()));
}
/// <summary>
/// 三点画圆弧
/// </summary>
/// <param name="db">数据库对象</param>
/// <param name="startPoint">起点</param>
/// <param name="pointOnArc">圆弧上一点</param>
/// <param name="endPoint">终点</param>
/// <returns></returns>
public static ObjectId AddArcToModeSpace(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.AddEntityToModeSpace(arc);
}
/// <summary>
/// 绘制圆
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="center">圆心</param>
/// <param name="radius">半径</param>
/// <returns></returns>
public static ObjectId AddCircleModeSpace(this Database db, Point3d center, double radius)
{
return db.AddEntityToModeSpace(new Circle((center), new Vector3d(0, 0, 1), radius));
}
/// <summary>
/// 两点绘制圆
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="point1">第一个电</param>
/// <param name="point2">第二个点</param>
/// <returns></returns>
public static ObjectId AddCircleModeSpace(this Database db, Point3d point1, Point3d point2)
{
// 获取两点的中心点
Point3d center = point1.GetCenterPointBetweenTwoPoint(point2);
// 获取半径
double radius = point1.GetDistanceBetweenTwoPoint(center);
return db.AddCircleModeSpace(center, radius);
}
/// <summary>
/// 三点绘制圆
/// </summary>
/// <param name="db"></param>
/// <param name="point1"></param>
/// <param name="point2"></param>
/// <param name="point3"></param>
/// <returns></returns>
public static ObjectId AddCircleModeSpace(this Database db, Point3d point1, Point3d point2, Point3d point3)
{
// 先判断三点是否在同一直线上
if (point1.IsOnOneLine(point2, point3))
{
return ObjectId.Null;
}
// 声明几何类Circular3d对象
CircularArc3d cArc = new CircularArc3d(point1, point2, point3);
return db.AddCircleModeSpace(cArc.Center, cArc.Radius);
}
/// <summary>
/// 绘制折线多段线
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="isClosed">是否闭合</param>
/// <param name="contantWidth">线宽</param>
/// <param name="vertices">多线段的顶点 可变参数</param>
/// <returns></returns>
public static ObjectId AddPolyLineToModeSpace(this Database db, bool isClosed, double contantWidth, params Point2d[] vertices)
{
if (vertices.Length < 2) // 顶点个数小于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 = contantWidth;
return db.AddEntityToModeSpace(pline);
}
/// <summary>
/// 绘制矩形
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="point1">第一个点</param>
/// <param name="point2">对角点</param>
/// <returns></returns>
public static ObjectId AddRectToModeSpace(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.AddEntityToModeSpace(pLine);
}
/// <summary>
/// 绘制正多边形
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="center">多边形所在内接圆圆心</param>
/// <param name="radius">所在圆半径</param>
/// <param name="sideNum">边数</param>
/// <param name="startDegree">起始角度</param>
/// <returns></returns>
public static ObjectId AddPolygonToModeSpace(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.AddEntityToModeSpace(pLine);
}
/// <summary>
/// 绘制椭圆
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="center">椭圆中心</param>
/// <param name="majorRadius">长轴长度</param>
/// <param name="shortRadius">短轴长度</param>
/// <param name="degree">长轴与X夹角 角度值</param>
/// <param name="startDegree">起始角度</param>
/// <param name="endDegree">终止角度</param>
/// <returns></returns>
public static ObjectId AddEllipseToModeSpace(this Database db, Point3d center, double majorRadius, double shortRadius, double degree, double startDegree, double endDegree)
{
// 计算相关参数
double ratio = shortRadius / majorRadius;
Vector3d majorAxis = new Vector3d(majorRadius * Math.Cos(degree.AngleToDegree()), majorRadius * Math.Sin(degree.DegreeToAngle()), 0);
Ellipse elli = new Ellipse(center, Vector3d.ZAxis, majorAxis, ratio, startDegree.DegreeToAngle(), endDegree.DegreeToAngle()); // VVector3d.ZAxis 等价于 new Vector3d(0,0,1) 平行于z轴法向量
return db.AddEntityToModeSpace(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 AddEllipseToModeSpace(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.AddEntityToModeSpace(elli);
}
/// <summary>
/// 绘制椭圆 两点
/// </summary>
/// <param name="db"></param>
/// <param name="point1">所在矩形的顶点</param>
/// <param name="point2">所在矩形的顶点2</param>
/// <returns></returns>
public static ObjectId AddEllipseToModeSpace(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.AddEntityToModeSpace(elli);
}
}
}
HatchTools.cs
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AcDoNetTools
{
public static partial class HatchTools
{
/// <summary>
/// 填充图案名称
/// </summary>
public struct HatchPatternName
{
public static readonly string solid = "SOLID";
public static readonly string angle = "ANGLE";
public static readonly string ansi31 = "ANSI31";
public static readonly string ansi32 = "ANSI32";
public static readonly string ansi33 = "ANSI33";
public static readonly string ansi34 = "ANSI34";
public static readonly string ansi35 = "ANSI35";
public static readonly string ansi36 = "ANSI36";
public static readonly string ansi37 = "ANSI37";
public static readonly string ansi38 = "ANSI38";
public static readonly string arb816 = "AR-B816";
public static readonly string arb816C = "AR-B816C";
public static readonly string arb88 = "AR-B88";
public static readonly string arbrelm = "AR-BRELM";
public static readonly string arbrstd = "AR-BRSTD";
public static readonly string arbconc = "AR-CONC";
}
/// <summary>
/// 渐变填充名称
/// </summary>
public struct HatchGradientName
{
public static readonly string gr_linear = "Linear";
public static readonly string gr_cylinear = "Cylinear";
public static readonly string gr_invcylinear = "Invcylinear";
public static readonly string gr_spherical = "Spherical";
public static readonly string gr_hemisperical = "Hemisperical";
public static readonly string gr_curved = "Curved";
public static readonly string gr_invsperical = "Inveperical";
public static readonly string gr_invhemisperical = "Invhemisperical";
public static readonly string gr_invcurved = "Invcurved";
}
/// <summary>
/// 图案填充 无颜色
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="patternName">图案名称</param>
/// <param name="scale">填充比例</param>
/// <param name="degree">旋转角度</param>
/// <param name="entid">边界图形的ObjectId</param>
/// <returns></returns>
public static ObjectId HatchEnity(this Database db, string patternName, double scale, double degree, ObjectId entid)
{
ObjectId hatchId = ObjectId.Null;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// 声明一个图案填充对象
Hatch hatch = new Hatch();
// 设置填充比例
hatch.PatternScale = scale;
// 设置填充类型和图案名称
hatch.SetHatchPattern(HatchPatternType.PreDefined, "ANGLE");
// 加入图形数据库
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
hatchId = btr.AppendEntity(hatch);
trans.AddNewlyCreatedDBObject(hatch, true);
// 设置填充角度
hatch.PatternAngle = degree;
// 设置关联
hatch.Associative = true;
// 设置边界图形和填充方式
ObjectIdCollection obIds = new ObjectIdCollection();
obIds.Add(entid);
hatch.AppendLoop(HatchLoopTypes.Outermost, obIds);
// 计算填充并显示
hatch.EvaluateHatch(true);
// 提交事务
trans.Commit();
}
return hatchId;
}
/// <summary>
/// 图案填充 有填充颜色
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="patternName">图案名称</param>
/// <param name="scale">填充比例</param>
/// <param name="degree">旋转角度</param>
/// <param name="bkColor">背景色</param>
/// <param name="hatchColorIndex">填充图案的颜色</param>
/// <param name="entid">边界图形的ObjectId</param>
/// <returns></returns>
public static ObjectId HatchEnity(this Database db, string patternName, double scale, double degree, Color bkColor, int hatchColorIndex, ObjectId entid)
{
ObjectId hatchId = ObjectId.Null;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// 声明一个图案填充对象
Hatch hatch = new Hatch();
// 设置填充比例
hatch.PatternScale = scale;
// 设置背景色
hatch.BackgroundColor = bkColor;
// 设置填充图案颜色
hatch.ColorIndex = hatchColorIndex;
// 设置填充类型和图案名称
hatch.SetHatchPattern(HatchPatternType.PreDefined, "ANGLE");
// 加入图形数据库
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
hatchId = btr.AppendEntity(hatch);
trans.AddNewlyCreatedDBObject(hatch, true);
// 设置填充角度
hatch.PatternAngle = degree;
// 设置关联
hatch.Associative = true;
// 设置边界图形和填充方式
ObjectIdCollection obIds = new ObjectIdCollection();
obIds.Add(entid);
hatch.AppendLoop(HatchLoopTypes.Outermost, obIds);
// 计算填充并显示
hatch.EvaluateHatch(true);
// 提交事务
trans.Commit();
}
return hatchId;
}
/// <summary>
/// 图案填充
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="loopTypes"></param>
/// <param name="patternName">图案名称</param>
/// <param name="scale">填充比例</param>
/// <param name="degree">旋转角度</param>
/// <param name="entid">边界图形的ObjectId</param>
/// <returns></returns>
public static ObjectId HatchEnity(this Database db, List<HatchLoopTypes> loopTypes, string patternName, double scale, double degree, params ObjectId[] entid) // 一个方法只能传递一个可变参数 且需要放在最后
{
ObjectId hatchId = ObjectId.Null;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// 声明一个图案填充对象
Hatch hatch = new Hatch();
// 设置填充比例
hatch.PatternScale = scale;
// 设置填充类型和图案名称
hatch.SetHatchPattern(HatchPatternType.PreDefined, "ANGLE");
// 加入图形数据库
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
hatchId = btr.AppendEntity(hatch);
trans.AddNewlyCreatedDBObject(hatch, true);
// 设置填充角度
hatch.PatternAngle = degree;
// 设置关联
hatch.Associative = true;
// 设置边界图形和填充方式
ObjectIdCollection obIds = new ObjectIdCollection();
// 依次添加图形填充样式
for (int i = 0; i < entid.Length; i++)
{
obIds.Clear();
obIds.Add(entid[i]);
hatch.AppendLoop(loopTypes[i], obIds);
}
// 计算填充并显示
hatch.EvaluateHatch(true);
// 提交事务
trans.Commit();
}
return hatchId;
}
/// <summary>
/// 渐变填充
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="colorIndex1">颜色索引1</param>
/// <param name="colorIndex2">颜色索引2</param>
/// <param name="hatchGradientName">渐变图案</param>
/// <param name="entId">边界图形的ObjectId</param>
/// <returns>ObjectId</returns>
public static ObjectId HatchGradient(this Database db, short colorIndex1, short colorIndex2, string hatchGradientName, ObjectId entId)
{
// 声明ObjectId, 用于返回
ObjectId hatchId = ObjectId.Null;
ObjectIdCollection objIds = new ObjectIdCollection();
objIds.Add(entId);
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// 声明填充对象
Hatch hatch = new Hatch();
// 设置填充类型为渐变类型填充
hatch.HatchObjectType = HatchObjectType.GradientObject;
// 设置渐变填充的类型和渐变填充的图案名称
hatch.SetGradient(GradientPatternType.PreDefinedGradient, hatchGradientName);
// 设置填充颜色
Color color1 = Color.FromColorIndex(ColorMethod.ByColor, colorIndex1);
Color color2 = Color.FromColorIndex(ColorMethod.ByColor, colorIndex2);
GradientColor gColor1 = new GradientColor(color1, 0);
GradientColor gColor2 = new GradientColor(color2, 1);
hatch.SetGradientColors(new GradientColor[] { gColor1, gColor2 });
// 将填充对象加入图形数据库
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
hatchId = btr.AppendEntity(hatch);
trans.AddNewlyCreatedDBObject(hatch, true);
// 添加关联
hatch.Associative = true;
hatch.AppendLoop(HatchLoopTypes.Outermost, objIds);
// 计算并显示填充
hatch.EvaluateHatch(true);
// 提交事务处理
trans.Commit();
}
return hatchId;
}
}
}
BaseTools.cs
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AcDoNetTools
{
public static partial class BaseTools
{
/// <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="firstPoint">第一个点</param>
/// <param name="secondPoint">第二个点</param>
/// <param name="thirdPoint">第三个点</param>
/// <returns></returns>
public static bool IsOnOneLine(this Point3d firstPoint, Point3d secondPoint, Point3d thirdPoint)
{
Vector3d v21 = secondPoint.GetVectorTo(firstPoint);
Vector3d v23 = secondPoint.GetVectorTo(thirdPoint);
if (v21.GetAngleTo(v23) == 0 || v21.GetAngleTo(v23) == Math.PI)
{
return true;
}
else
{
return false;
}
}
public static double GetAngleToXAxis(this Point3d startPoint, Point3d endPoint)
{
// 声明一个与X轴平行的向量
Vector3d temp = new Vector3d(1, 0, 0);
// 获取七点到终点的向量
Vector3d VsToe = startPoint.GetVectorTo(endPoint);
return VsToe.Y > 0 ? temp.GetAngleTo(VsToe) : -temp.GetAngleTo(VsToe);
}
public static double GetDistanceBetweenTwoPoint(this Point3d point1, Point3d point2)
{
return (Math.Sqrt(Math.Pow((point1.X - point2.X), 2) + Math.Pow((point1.Y - point2.Y), 2) + Math.Pow((point1.Z + point2.Z), 2)));
}
/// <summary>
/// 获取两点的中心点
/// </summary>
/// <param name="point1">第一个点</param>
/// <param name="point2">第二个点</param>
/// <returns></returns>
public static Point3d GetCenterPointBetweenTwoPoint(this Point3d point1, Point3d point2)
{
return new Point3d((point1.X + point2.X) / 2, (point1.Y + point2.Y) / 2, (point1.Z + point2.Z) / 2);
}
}
}
右键项目,点击生成,在项目路径下回生成dll文件
这样就封装完毕了,其实跟之前是一样的
# 2. 如何使用自己封装的dll库
还是老样子,新建一个项目,把刚才生成dll库文件添加进来
值得说明的是其属性中的复制到本地需要设置为True
在命名空间中添加该库文件的引用
这样之前封装的那一堆东西就可以在新项目里使用了,也不用一个一个把之前封装的类添加进来
# 3. 测试—更改图形颜色
这里声明了两个圆对象,并通过两种不同的设置颜色方式对其颜色进行设置
完整代码地址:
https://gitee.com/yuzhaokai/cad_secondary_development_code.git
不折腾会死!Peace!
喜欢就关注!