思路很简单,先计算两点的中央点座标,再计算线段的旋转角,最后根据旋转角和标记离线段距离计算出标注点的座标。
线的正方向为从下往上。负方向则将旋转角加PI。
1 /// <summary>
2 /// 计算旋转角
3 /// </summary>
4 /// <param name="x1">起点X座标</param>
5 /// <param name="y1">起点Y座标</param>
6 /// <param name="x2">终点X座标</param>
7 /// <param name="y2">终点Y座标</param>
8 /// <returns>旋转角</returns>
9 private double CountRotation(double x1, double y1, double x2, double y2)
10 {
11 double rotation = 0;
12 //两点重合
13 if (y2==y1 && x2== x1)
14 {
15 rotation = 0;
16 }
17 else
18 {
19 //两点在水平线上
20 if (y2 == y1)
21 {
22 if (x2 > x1)
23 {
24 rotation = 0;
25 }
26 else
27 {
28 rotation = Math.PI;
29 }
30 }
31 //两点在垂直线上
32 if (x2==x1)
33 {
34 if (y2>y1)
35 {
36 rotation = Math.PI / 2;
37 }
38 else
39 {
40 rotation = Math.PI / 2 + Math.PI;
41 }
42 }
43 //一般情况
44 if (y2!=y1 && x2!= x1)
45 {
46 double k = k = (y2 - y1) / (x2 - x1);
47 //第一象限
48 if (y2>y1 && x2> x1)
49 {
50 rotation = Math.Atan(k);
51 }
52 //第二象限
53 if (y2>y1 && x2<x1)
54 {
55 rotation = Math.Atan(k) + Math.PI;
56 }
57 //第三象限
58 if (y2<y1 && x2<x1)
59 {
60 rotation = Math.Atan(k) + Math.PI;
61 }
62 //第四象限
63 if (y2<y1 && x2>x1)
64 {
65 rotation = Math.Atan(k)+2*Math.PI;
66 }
67 }
68 }
69 return rotation;
70 }
2 /// 计算旋转角
3 /// </summary>
4 /// <param name="x1">起点X座标</param>
5 /// <param name="y1">起点Y座标</param>
6 /// <param name="x2">终点X座标</param>
7 /// <param name="y2">终点Y座标</param>
8 /// <returns>旋转角</returns>
9 private double CountRotation(double x1, double y1, double x2, double y2)
10 {
11 double rotation = 0;
12 //两点重合
13 if (y2==y1 && x2== x1)
14 {
15 rotation = 0;
16 }
17 else
18 {
19 //两点在水平线上
20 if (y2 == y1)
21 {
22 if (x2 > x1)
23 {
24 rotation = 0;
25 }
26 else
27 {
28 rotation = Math.PI;
29 }
30 }
31 //两点在垂直线上
32 if (x2==x1)
33 {
34 if (y2>y1)
35 {
36 rotation = Math.PI / 2;
37 }
38 else
39 {
40 rotation = Math.PI / 2 + Math.PI;
41 }
42 }
43 //一般情况
44 if (y2!=y1 && x2!= x1)
45 {
46 double k = k = (y2 - y1) / (x2 - x1);
47 //第一象限
48 if (y2>y1 && x2> x1)
49 {
50 rotation = Math.Atan(k);
51 }
52 //第二象限
53 if (y2>y1 && x2<x1)
54 {
55 rotation = Math.Atan(k) + Math.PI;
56 }
57 //第三象限
58 if (y2<y1 && x2<x1)
59 {
60 rotation = Math.Atan(k) + Math.PI;
61 }
62 //第四象限
63 if (y2<y1 && x2>x1)
64 {
65 rotation = Math.Atan(k)+2*Math.PI;
66 }
67 }
68 }
69 return rotation;
70 }
代码如下:
/// <summary>
/// 生成线标注
/// </summary>
/// <param name="x">中央点X</param>
/// <param name="y">中央点Y</param>
/// <param name="z">中央点Z</param>
/// <param name="text">标注文字</param>
/// <returns>ObjectId</returns>
private ObjectId CreateLineText(double x,double y,double z,double rotation, string text)
{
ObjectId textId;
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead, false);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false);
Point3d point = new Point3d(x - 0.5 * Math.Sin(rotation), y + 0.5 * Math.Cos(rotation), z);
DBText objDBText = new DBText();
//HorizontalMode与VerticalMode非默认值时设置AlignmentPoint
objDBText.HorizontalMode = TextHorizontalMode.TextCenter;
objDBText.AlignmentPoint = point;
objDBText.Rotation = rotation;
objDBText.TextString = text;
objDBText.Height = 1;
textId = btr.AppendEntity(objDBText);
trans.AddNewlyCreatedDBObject(objDBText, true);
trans.Commit();
}
return textId;
}
/// 生成线标注
/// </summary>
/// <param name="x">中央点X</param>
/// <param name="y">中央点Y</param>
/// <param name="z">中央点Z</param>
/// <param name="text">标注文字</param>
/// <returns>ObjectId</returns>
private ObjectId CreateLineText(double x,double y,double z,double rotation, string text)
{
ObjectId textId;
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead, false);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false);
Point3d point = new Point3d(x - 0.5 * Math.Sin(rotation), y + 0.5 * Math.Cos(rotation), z);
DBText objDBText = new DBText();
//HorizontalMode与VerticalMode非默认值时设置AlignmentPoint
objDBText.HorizontalMode = TextHorizontalMode.TextCenter;
objDBText.AlignmentPoint = point;
objDBText.Rotation = rotation;
objDBText.TextString = text;
objDBText.Height = 1;
textId = btr.AppendEntity(objDBText);
trans.AddNewlyCreatedDBObject(objDBText, true);
trans.Commit();
}
return textId;
}