明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1067|回复: 4

[图元] 获取文字包围盒

  [复制链接]
发表于 2022-11-16 09:06 | 显示全部楼层 |阅读模式
本帖最后由 MyNameIsLiLei 于 2022-11-16 09:28 编辑

今天又学到一招,嘻嘻。
  1.    
  2. [DllImport("accore.dll", CharSet = CharSet.Unicode,CallingConvention = CallingConvention.Cdecl, EntryPoint = "acedTextBox")]
  3. static extern System.IntPtr acedTextBox(IntPtr rb, double[] point1, double[] point2);


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

  7.         mirrored |= text.IsMirroredInY ? 4 : 0;

  8.         var rb = new ResultBuffer(
  9.                 new TypedValue(1, text.TextString),
  10.                 new TypedValue(40, text.Height),
  11.                 new TypedValue(41, text.WidthFactor),
  12.                 new TypedValue(51, text.Oblique),
  13.                 new TypedValue(7, text.TextStyleName),
  14.                 new TypedValue(71, mirrored),
  15.                 new TypedValue(72, (int)text.HorizontalMode),
  16.                 new TypedValue(73, (int)text.VerticalMode));

  17.         var point1 = new double[3];

  18.         var point2 = new double[3];

  19.         acedTextBox(rb.UnmanagedObject, point1, point2);

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

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

  24.         return new Point3d[]
  25.         {
  26.             extents.MinPoint.TransformBy(xform),
  27.             new Point3d(extents.MaxPoint.X, extents.MinPoint.Y, 0.0).TransformBy(xform),
  28.             extents.MaxPoint.TransformBy(xform),
  29.             new Point3d(extents.MinPoint.X, extents.MaxPoint.Y, 0.0).TransformBy(xform)
  30.         };
  31. }

效果还不错,嘻嘻。



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
发表于 2022-11-16 10:59 | 显示全部楼层
请教,这个是否可以通过加大基点的坐标 y 值,扩大包围到上下相邻的文字?
发表于 2022-11-26 21:02 | 显示全部楼层
学到了,厉害
发表于 2022-12-5 15:31 | 显示全部楼层
本帖最后由 yxr_MJTD 于 2022-12-5 15:46 编辑

这个功能好,学习了!我通过这个功能有一个扩展,希望喜欢:
  1. /// <summary>        
  2. /// 获取文字的大小      
  3. /// </summary>      
  4. /// <param name="text"></param>      
  5. /// <returns></returns>        
  6. public static Size GetTextSize(this DBText text)        
  7. {            
  8.   var pBound = text.GetTextBound();
  9.   double pW = pBound[1].DistanceTo(pBound[2]);   //获取文字宽度            
  10.   double pH = pBound[0].DistanceTo(pBound[1]);   //获取文字高度           
  11.    return new Size(pW,pH);
  12. }


发表于 2022-12-5 15:42 | 显示全部楼层
本帖最后由 yxr_MJTD 于 2022-12-5 15:49 编辑

这条是多余的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-28 21:01 , Processed in 0.966030 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表