【转载】关于单行\多行文字对齐点问题
本帖最后由 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)也是必要的,具体原因还没来得及深究
以下是外网上找的代码:
public void TextAlign()
{
Document activeDoc = Application.DocumentManager.MdiActiveDocument;
// For adding a text with alignment to the current document
bool isInMemory = false;
Database db = activeDoc.Database;
// For adding a text to an in-memory database
//bool isInMemory = true;
//Database db = new Database(true, false);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(
db.BlockTableId,
OpenMode.ForRead
) as BlockTable;
BlockTableRecord mSpace = tr.GetObject
(
bt,
OpenMode.ForWrite
) as BlockTableRecord;
DBText dbText = new DBText();
dbText.SetDatabaseDefaults(db);
dbText.Position = Point3d.Origin;
dbText.Height = 5.0;
dbText.TextString = "Autodesk";
dbText.HorizontalMode = TextHorizontalMode.TextRight;
dbText.AlignmentPoint = Point3d.Origin;
if (isInMemory)
{ // For adding a text with an alignment to an in-memory database
// set the working database before using AdjustAlignment
Database prevWorkingDb = HostApplicationServices.WorkingDatabase;
HostApplicationServices.WorkingDatabase = db;
dbText.AdjustAlignment(db);
HostApplicationServices.WorkingDatabase = prevWorkingDb;
}
else
{ // For adding a text with alignment to the current document
// working database is already set
dbText.AdjustAlignment(db);
}
mSpace.AppendEntity(dbText);
tr.AddNewlyCreatedDBObject(dbText, true);
tr.Commit();
}
if (isInMemory)
{
db.SaveAs("C:\\Temp\\Test.dwg", DwgVersion.Current);
db.Dispose();
}
}
代码来自:https://adndevblog.typepad.com/a ... ext-alignment-.html 本帖最后由 qq1254582201 于 2024-11-15 09:44 编辑
指定 MText 对象的连接点。
指定 MText 对象的连接点。支持的平台:仅窗口签名工 务 局:object.AttachmentPoint对象类型:MText此属性适用的对象。
属性值只读:不类型:枚举acAttachmentPoint
[*]acAttachmentPointTopLeft
[*]acAttachmentPointTopCenter
[*]acAttachmentPointTopRight
[*]acAttachmentPointMiddleLeft
[*]acAttachmentPointMiddleCenter
[*]acAttachmentPointMiddleRight
[*]acAttachmentPointBottomLeft
[*]acAttachmentPointBottomCenter
[*]acAttachmentPointBottomRight
言论连接点指定插入点与文本边界对齐的位置。您选择的选项确定相对于文本边界的文本对齐和文本溢出。文本对齐的选项包括“左”、“右”和“居中”。文本溢出的选项包括“顶部”、“中间(中间)”和“底部”。
https://help.autodesk.com/cloudhelp/2021/ENU/AutoCAD-ActiveX-Reference/images/GUID-99266066-EB5D-4BE8-ADE4-88CBBEC52A56.gif
左上角左对齐,溢出
https://help.autodesk.com/cloudhelp/2021/ENU/AutoCAD-ActiveX-Reference/images/GUID-055EA5F1-1FC9-4D02-B0E5-30DA8B9B25FF.gif
顶部中心居中对齐,向下溢出
https://help.autodesk.com/cloudhelp/2021/ENU/AutoCAD-ActiveX-Reference/images/GUID-BB408649-9D1A-4642-BADA-1802571D6117.gif
右上右对齐,溢出
https://help.autodesk.com/cloudhelp/2021/ENU/AutoCAD-ActiveX-Reference/images/GUID-1DAF7E35-4C50-47B1-8BBE-840949593F71.gif
左中左对齐,上下溢出
https://help.autodesk.com/cloudhelp/2021/ENU/AutoCAD-ActiveX-Reference/images/GUID-438515E8-611A-4176-94D3-A59AD5BA8D36.gif
中中心居中对齐,上下溢出
https://help.autodesk.com/cloudhelp/2021/ENU/AutoCAD-ActiveX-Reference/images/GUID-F3A12463-7ABF-4735-8093-F18CAA04F24F.gif
右中右对齐,上下溢出
https://help.autodesk.com/cloudhelp/2021/ENU/AutoCAD-ActiveX-Reference/images/GUID-898B422F-B4EC-4866-AC3B-D8D8EBCBEE2A.gif
左下角左对齐,溢出
https://help.autodesk.com/cloudhelp/2021/ENU/AutoCAD-ActiveX-Reference/images/GUID-9F984298-7707-4CF9-8738-2D82B61F22D8.gif
底部中心居中对齐,溢出
https://help.autodesk.com/cloudhelp/2021/ENU/AutoCAD-ActiveX-Reference/images/GUID-9F588693-7BED-4806-A781-500ABC2F0F20.gif
右下角正确对齐,溢出
更改属性时,现有边界框的位置不会更改;文本只是在边界框中重新对齐。但是,属性反映正在使用的连接点的坐标,因此属性的值将更改以反映对齐方式的变化。AttachmentPointInsertionPointInsertionPoint
例子工 务 局:Sub Example_AttachmentPoint()
Dim MTextObj As AcadMText
Dim width As Double
Dim text As String
Dim count As Integer
Dim attachPoint As String
Dim corner(0 To 2) As Double
corner(0) = 3#: corner(1) = 3#: corner(2) = 0#
width = 10
text = "Hello, World."
' Creates a MText object in model space
Set MTextObj = ThisDrawing.ModelSpace.AddMText(corner, width, text)
For count = 1 To 9
MTextObj.AttachmentPoint = count
' Gets the attachment point of an MText object
attachPoint = Choose(MTextObj.AttachmentPoint, "TopLeft", "TopCenter",
"TopRight", "MiddleLeft", "MiddleCenter",
"MiddleRight", "BottomLeft", "BottomCenter",
"BottomRight")
ThisDrawing.Regen True
MsgBox "The attachment point of the MText is now: "
& attachPoint, vbInformation, "AttachmentPoint Example"
Next
End Sub
public void ExampleAttachmentPoint()
{
AcadMText mTextObject;
double width;
string text;
int count;
string attachPoint;
double[] corner = new double;
corner = 3.0; corner = 3.0; corner = 0.0;
width = 10;
text = "Hello, World.";
// Creates a MText object in model space
mTextObject = ThisDrawing.ModelSpace.AddMText(corner, width, text);
for (count = 1; count <= 9; count++)
{
mTextObject.AttachmentPoint = count;
// Gets the attachment point of an MText object
attachPoint = Choose(mTextObject.AttachmentPoint, "TopLeft", "TopCenter", "TopRight",
"MiddleLeft", "MiddleCenter", "MiddleRight",
"BottomLeft", "BottomCenter", "BottomRight");
ThisDrawing.Regen(true);
System.Windows.Forms.MessageBox.Show("The attachment point of the MText is now: " + attachPoint,
"AttachmentPoint Example",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information);
}
}
Visual LISP:(vl-load-com)
(defun c:Example_AttachmentPoint()
(setq acadObj (vlax-get-acad-object))
(setq doc (vla-get-ActiveDocument acadObj))
(setq corner (vlax-3d-point 3 3 0)
width 10
text "Hello, World.")
;; Creates a MText object in model space
(setq modelSpace (vla-get-ModelSpace doc))
(setq MTextObj (vla-AddMText modelSpace corner width text))
(setq count 1)
(repeat 9
(vla-put-AttachmentPoint MTextObj count)
;; Gets the attachment point of an MText object
(cond
((= (vla-get-AttachmentPoint MTextObj) 1)(setq attachPoint "TopLeft"))
((= (vla-get-AttachmentPoint MTextObj) 2)(setq attachPoint "TopCenter"))
((= (vla-get-AttachmentPoint MTextObj) 3)(setq attachPoint "TopRight"))
((= (vla-get-AttachmentPoint MTextObj) 4)(setq attachPoint "MiddleLeft"))
((= (vla-get-AttachmentPoint MTextObj) 5)(setq attachPoint "MiddleCenter"))
((= (vla-get-AttachmentPoint MTextObj) 6)(setq attachPoint "MiddleRight"))
((= (vla-get-AttachmentPoint MTextObj) 7)(setq attachPoint "BottomLeft"))
((= (vla-get-AttachmentPoint MTextObj) 8)(setq attachPoint "BottomCenter"))
((= (vla-get-AttachmentPoint MTextObj) 9)(setq attachPoint "BottomRight"))
)
(vla-Regen doc :vlax-true)
(alert (strcat "The attachment point of the MText is now: " attachPoint))
(setq count (1+ count))
)
)
页:
[1]