MyNameIsLiLei 发表于 2022-11-16 09:06:40

获取文字包围盒

本帖最后由 MyNameIsLiLei 于 2022-11-16 09:28 编辑

今天又学到一招,嘻嘻。
   

static extern System.IntPtr acedTextBox(IntPtr rb, double[] point1, double[] point2);


public Point3d[] GetTextBound(DBText text)
{
      int mirrored = text.IsMirroredInX ? 2 : 0;

      mirrored |= text.IsMirroredInY ? 4 : 0;

      var rb = new ResultBuffer(
                new TypedValue(1, text.TextString),
                new TypedValue(40, text.Height),
                new TypedValue(41, text.WidthFactor),
                new TypedValue(51, text.Oblique),
                new TypedValue(7, text.TextStyleName),
                new TypedValue(71, mirrored),
                new TypedValue(72, (int)text.HorizontalMode),
                new TypedValue(73, (int)text.VerticalMode));

      var point1 = new double;

      var point2 = new double;

      acedTextBox(rb.UnmanagedObject, point1, point2);

      var extents = new Extents3d(new Point3d(point1), new Point3d(point2));

      var xform = Matrix3d.Displacement(text.Position.GetAsVector()) *
            Matrix3d.Rotation(text.Rotation, text.Normal, Point3d.Origin) *
            Matrix3d.WorldToPlane(new Plane(Point3d.Origin, text.Normal));

      return new Point3d[]
      {
            extents.MinPoint.TransformBy(xform),
            new Point3d(extents.MaxPoint.X, extents.MinPoint.Y, 0.0).TransformBy(xform),
            extents.MaxPoint.TransformBy(xform),
            new Point3d(extents.MinPoint.X, extents.MaxPoint.Y, 0.0).TransformBy(xform)
      };
}
效果还不错,嘻嘻。



AndyWang 发表于 2022-11-16 10:59:22

请教,这个是否可以通过加大基点的坐标 y 值,扩大包围到上下相邻的文字?

d1742647821 发表于 2022-11-26 21:02:38

学到了,厉害

yxr_MJTD 发表于 2022-12-5 15:31:41

本帖最后由 yxr_MJTD 于 2022-12-5 15:46 编辑

这个功能好,学习了!我通过这个功能有一个扩展,希望喜欢:

/// <summary>      
/// 获取文字的大小      
/// </summary>      
/// <param name="text"></param>      
/// <returns></returns>      
public static Size GetTextSize(this DBText text)      
{            
var pBound = text.GetTextBound();
double pW = pBound.DistanceTo(pBound);   //获取文字宽度            
double pH = pBound.DistanceTo(pBound);   //获取文字高度         
   return new Size(pW,pH);
}

yxr_MJTD 发表于 2022-12-5 15:42:18

本帖最后由 yxr_MJTD 于 2022-12-5 15:49 编辑

这条是多余的。
页: [1]
查看完整版本: 获取文字包围盒