明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1269|回复: 3

【转载】关于单行\多行文字对齐点问题

[复制链接]
发表于 2024-11-15 09:04:30 | 显示全部楼层 |阅读模式
本帖最后由 qq1254582201 于 2024-11-15 09:49 编辑

DBText有两个属性用于确定文本位置,AlignmentPoint(对齐点)和 Position(位置)
方法 Justify(对齐方式)用于确定文本对齐方式,当 Justify 发生变化时,Position 会随之变化,AlignmentPoint 则保持不变
开发过程中,我们一般会指定文本的 AlignmentPoint 和 Justify,而不会麻烦的去计算 Position,这时候可以用 DBText.AdjustAlignment(Database) 方法来自动更新 Position,正常情况下使用没毛病,但在新建数据库里使用就不行查资料发现这里作为参数的 Database 必须是当前活动数据库,否则该方法无效重点来了:

DBText dbText = new DBText();
dbText.SetDatabaseDefaults(db);
Database prevWorkingDb = HostApplicationServices.WorkingDatabase;
HostApplicationServices.WorkingDatabase = db;
dbText.AdjustAlignment(db);
HostApplicationServices.WorkingDatabase = prevWorkingDb;
这里面除了切换当前活动数据库是关键外,dbText.SetDatabaseDefaults(db)也是必要的,具体原因还没来得及深究
以下是外网上找的代码:

  1. public void TextAlign()
  2. {
  3.     Document activeDoc = Application.DocumentManager.MdiActiveDocument;
  4.     // For adding a text with alignment to the current document
  5.     bool isInMemory = false;
  6.     Database db = activeDoc.Database;

  7.     // For adding a text to an in-memory database
  8.     //bool isInMemory = true;
  9.     //Database db = new Database(true, false);
  10.     using (Transaction tr = db.TransactionManager.StartTransaction())
  11.     {
  12.         BlockTable bt = tr.GetObject(
  13.                                         db.BlockTableId,
  14.                                         OpenMode.ForRead
  15.                                     ) as BlockTable;

  16.         BlockTableRecord mSpace = tr.GetObject
  17.                                 (
  18.                                     bt[BlockTableRecord.ModelSpace],
  19.                                     OpenMode.ForWrite
  20.                                 ) as BlockTableRecord;

  21.         DBText dbText = new DBText();
  22.         dbText.SetDatabaseDefaults(db);
  23.         dbText.Position = Point3d.Origin;
  24.         dbText.Height = 5.0;
  25.         dbText.TextString = "Autodesk";
  26.         dbText.HorizontalMode = TextHorizontalMode.TextRight;
  27.         dbText.AlignmentPoint = Point3d.Origin;

  28.         if (isInMemory)
  29.         {   // For adding a text with an alignment to an in-memory database
  30.             // set the working database before using AdjustAlignment
  31.             Database prevWorkingDb = HostApplicationServices.WorkingDatabase;
  32.             HostApplicationServices.WorkingDatabase = db;
  33.             dbText.AdjustAlignment(db);
  34.             HostApplicationServices.WorkingDatabase = prevWorkingDb;
  35.         }
  36.         else
  37.         {   // For adding a text with alignment to the current document
  38.             // working database is already set
  39.             dbText.AdjustAlignment(db);
  40.         }
  41.         mSpace.AppendEntity(dbText);
  42.         tr.AddNewlyCreatedDBObject(dbText, true);
  43.         tr.Commit();
  44.     }

  45.     if (isInMemory)
  46.     {
  47.         db.SaveAs("C:\\Temp\\Test.dwg", DwgVersion.Current);
  48.         db.Dispose();
  49.     }
  50. }

代码来自:https://adndevblog.typepad.com/a ... ext-alignment-.html
 楼主| 发表于 2024-11-15 09:22:52 | 显示全部楼层
本帖最后由 qq1254582201 于 2024-11-15 09:44 编辑

指定 MText 对象的连接点。


指定 MText 对象的连接点。
支持的平台:仅窗口
签名
工 务 局:
object.AttachmentPoint对象
类型:MText
此属性适用的对象。

属性值
只读:
类型:枚举acAttachmentPoint
  • acAttachmentPointTopLeft
  • acAttachmentPointTopCenter
  • acAttachmentPointTopRight
  • acAttachmentPointMiddleLeft
  • acAttachmentPointMiddleCenter
  • acAttachmentPointMiddleRight
  • acAttachmentPointBottomLeft
  • acAttachmentPointBottomCenter
  • acAttachmentPointBottomRight

言论
连接点指定插入点与文本边界对齐的位置。您选择的选项确定相对于文本边界的文本对齐和文本溢出。
文本对齐的选项包括“左”、“右”和“居中”。文本溢出的选项包括“顶部”、“中间(中间)”和“底部”。


左上角
左对齐,溢出


顶部中心
居中对齐,向下溢出


右上
右对齐,溢出


左中
左对齐,上下溢出


中中心
居中对齐,上下溢出


右中
右对齐,上下溢出


左下角
左对齐,溢出


底部中心
居中对齐,溢出


右下角
正确对齐,溢出

更改属性时,现有边界框的位置不会更改;文本只是在边界框中重新对齐。
但是,属性反映正在使用的连接点的坐标,因此属性的值将更改以反映对齐方式的变化。
AttachmentPoint
InsertionPoint
InsertionPoint

例子
工 务 局:
  1. Sub Example_AttachmentPoint()
  2.     Dim MTextObj As AcadMText
  3.     Dim width As Double
  4.     Dim text As String
  5.     Dim count As Integer
  6.     Dim attachPoint As String
  7.     Dim corner(0 To 2) As Double
  8.     corner(0) = 3#: corner(1) = 3#: corner(2) = 0#
  9.     width = 10
  10.     text = "Hello, World."
  11.     ' Creates a MText object in model space
  12.     Set MTextObj = ThisDrawing.ModelSpace.AddMText(corner, width, text)
  13.     For count = 1 To 9
  14.         MTextObj.AttachmentPoint = count
  15.         ' Gets the attachment point of an MText object
  16.         attachPoint = Choose(MTextObj.AttachmentPoint, "TopLeft", "TopCenter",
  17.                               "TopRight", "MiddleLeft", "MiddleCenter",
  18.                               "MiddleRight", "BottomLeft", "BottomCenter",
  19.                               "BottomRight")
  20.         ThisDrawing.Regen True
  21.         MsgBox "The attachment point of the MText is now: "
  22. & attachPoint, vbInformation, "AttachmentPoint Example"
  23.     Next
  24. End Sub

  1. public void ExampleAttachmentPoint()
  2. {
  3.     AcadMText mTextObject;
  4.     double width;
  5.     string text;
  6.     int count;
  7.     string attachPoint;
  8.     double[] corner = new double[3];
  9.     corner[0] = 3.0; corner[1] = 3.0; corner[2] = 0.0;
  10.     width = 10;
  11.     text = "Hello, World.";
  12.     // Creates a MText object in model space
  13.     mTextObject = ThisDrawing.ModelSpace.AddMText(corner, width, text);
  14.     for (count = 1; count <= 9; count++)
  15.     {
  16.         mTextObject.AttachmentPoint = count;
  17.         // Gets the attachment point of an MText object
  18.         attachPoint = Choose(mTextObject.AttachmentPoint, "TopLeft", "TopCenter", "TopRight",
  19.                               "MiddleLeft", "MiddleCenter", "MiddleRight",
  20.                               "BottomLeft", "BottomCenter", "BottomRight");
  21.         ThisDrawing.Regen(true);
  22.         System.Windows.Forms.MessageBox.Show("The attachment point of the MText is now: " + attachPoint,
  23.                                               "AttachmentPoint Example",
  24.                                               System.Windows.Forms.MessageBoxButtons.OK,
  25.                                               System.Windows.Forms.MessageBoxIcon.Information);
  26.     }
  27. }
复制代码

Visual LISP:
  1. (vl-load-com)
  2. (defun c:Example_AttachmentPoint()
  3.     (setq acadObj (vlax-get-acad-object))
  4.     (setq doc (vla-get-ActiveDocument acadObj))

  5.     (setq corner (vlax-3d-point 3 3 0)  
  6.           width 10
  7.           text "Hello, World.")
  8.   
  9.     ;; Creates a MText object in model space
  10.     (setq modelSpace (vla-get-ModelSpace doc))  
  11.     (setq MTextObj (vla-AddMText modelSpace corner width text))
  12.   
  13.     (setq count 1)
  14.     (repeat 9
  15.         (vla-put-AttachmentPoint MTextObj count)

  16.         ;; Gets the attachment point of an MText object
  17.         (cond
  18.             ((= (vla-get-AttachmentPoint MTextObj) 1)(setq attachPoint "TopLeft"))
  19.             ((= (vla-get-AttachmentPoint MTextObj) 2)(setq attachPoint "TopCenter"))
  20.             ((= (vla-get-AttachmentPoint MTextObj) 3)(setq attachPoint "TopRight"))
  21.             ((= (vla-get-AttachmentPoint MTextObj) 4)(setq attachPoint "MiddleLeft"))
  22.             ((= (vla-get-AttachmentPoint MTextObj) 5)(setq attachPoint "MiddleCenter"))
  23.             ((= (vla-get-AttachmentPoint MTextObj) 6)(setq attachPoint "MiddleRight"))
  24.             ((= (vla-get-AttachmentPoint MTextObj) 7)(setq attachPoint "BottomLeft"))
  25.             ((= (vla-get-AttachmentPoint MTextObj) 8)(setq attachPoint "BottomCenter"))
  26.             ((= (vla-get-AttachmentPoint MTextObj) 9)(setq attachPoint "BottomRight"))
  27.         )
  28.    
  29.         (vla-Regen doc :vlax-true)
  30.         (alert (strcat "The attachment point of the MText is now: " attachPoint))
  31.       
  32.         (setq count (1+ count))
  33.     )
  34. )



 楼主| 发表于 昨天 15:05 | 显示全部楼层
.net中关于DBText注意事项
  • 需要设置AdjustAligment
    参数传入HostApplicationServieces.WorkingDatabase
    且必须放置在最后,否则拖拽前后位置会发生变化。
    1. DBText text = new DBText();
    2. text.SetDatabaseDefaults();//设置默认线型 图层等
    3. text.VerticalMode = TextVerticalMode.TextVerticalMid;//垂直对齐方式
    4. text.HorizontaiMode = TextHorizontalMode.TextLeft;//水平对齐方式
    5. text.ColorIndex = 3;//设置文字颜色索引
    6. text.WidthFactor = 0.65;//设置文字宽度因子
    7. text.Height = 5; //设置文字高度
    8. text.TextString = "Hello World"; //设置文本字符串
    9. text.Position = textPosition;  //设置文字位置
    10. text.TextStyleId = textStyleId;//一个非Null的Id
    11. //下面顺序不要变 否则过拽有问题
    12. text.AllignmentPoint = textPosition;//设置文字对齐位置
    13. text.AdjustAlignment(HostApplicationServieces.WorkingDatabase);//设置文字对齐
    复制代码


帮助文档对于AdjustAligment的解释是:
Normally when a text entity is closed,the text‘s position and allignment points are adjusted according to the text's justification settings and the text style.
But , if the text entity is embedded in another entity,it will never be closed in wihch case it won't automatically be adjusted.
Or, if the text is added to a transaction before it's first close(),and the graphics are flushed to the screen before the outemost transaction ends,the text will be displayed before the automatic adjustment takes place.
  • when this method is called,the database used must be the current working database(as return by HostApplicationServiece()->workingDatabase())
  • the text entity must have Non-Null text string data ,
  • and a valid text style objectId that resides in the database being used;
    • 传入的参数必须是当前活动的数据库
    • 文本必须为非Null
    • 文字样式必须有效

回复 支持 反对

使用道具 举报

发表于 昨天 18:26 | 显示全部楼层
本帖最后由 你有种再说一遍 于 2025-4-29 18:35 编辑

workingDatabase不应该是activeDatabase or currentDatabase.
应该翻译为工作数据库,它就像是一个厨师的案板.

工作数据库可能是不存在的,
例如你关闭全部文档之后操作后台数据库.
此时就不存在工作数据库,
此时后台操作是否发生文字偏移?大概率会发生.

CAD借用渲染层去处理文字包围盒,再回写到数据库,
所以会发生文字偏移,因此要保证工作数据库存在,
才有那么多规定.

设置text.Position在必须的,
你text.SetDefaultDatabase(db)就会默认给你一个,
为什么需要设置它,因为存在初始化顺序调整.

麻烦不是创建,而是修改.
在"调整"模式下,需要设置基点和对齐点,
然后组块之后修改属性,
你可以发现 对齐点=对齐点 之后,文字会跑.
原因是你需要把 属性的左下角换算到0,0 才可以.

这些基础的东西建议不要写了,
IFox都帮你写好了.

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-4-30 22:28 , Processed in 0.220377 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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