Hi Brian,
AutoCAD uses DBText.AlignmentPoint property to adjust alignment of a text if it is not horizontal left (TextHorizontalMode.TextLeft) and vertical base (TextVerticalMode.TextBase) by default.
DBText.Position defines a text insertion point while DBText.AlignmentPoint defines its alignment point. They are different. AlignmentPoint does not need to set (it will get default value 0,0,0), if the text is default horizontal left and vertical base (it will fail if we try to change AlignmentPoint at this time).
If we change horizontal and vertical alignments to be different with their default values, the Position property (lower left point of the text) becomes useless because it will be different depending on text height and width. Instead, we need to set AlignmentPoint property. The AlignmentPoint will be the text location at this time.
The problem is that the text is always at (0, 0, 0) point regardless of any DBText.Position because this text has alignment properties different with its default values.
So you just need to add acText.AlignmentPoint = Text_Location after modifying horizontal and vertical modes. I rewrite your code (in C#) with a test method to add texts in both Model and Paper spaces. The issue is the text alignment, not belongs to Paper space.
[CommandMethod("New_Text")]
public static void New_Text()
{
New_Text(null, "Model", new Point3d(5, 10, 0), 2, "My model text", "0", "Standard", "2", "center", "middle");
New_Text(null, "Layout", new Point3d(5, 10, 0), 2, "My paper text", "0", "Standard", "3", "left", "middle");
}
public static void New_Text(ObjectIdCollection acObjIdColl, string sSpace, Point3d Text_Location, double Text_Height,
string Text_String, string Layer, string Text_Style, string Color, string Justification,
string Vertical_Alignment)
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
using (DocumentLock acLckDoc = acDoc.LockDocument())
{
Application.SetSystemVariable("CLAYER", Layer);
Application.SetSystemVariable("TEXTSTYLE", Text_Style);
Application.SetSystemVariable("CECOLOR", Color);
// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl = (BlockTable)acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead);
// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
if ((sSpace == "Model"))
{
acBlkTblRec = (BlockTableRecord)acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
}
else
{
acBlkTblRec = (BlockTableRecord)acTrans.GetObject(acBlkTbl[BlockTableRecord.PaperSpace], OpenMode.ForWrite);
}
DBText acText = new DBText();
acText.SetDatabaseDefaults();
acText.Position = Text_Location;
acText.Height = Text_Height;
acText.TextString = Text_String;
acBlkTblRec.AppendEntity(acText);
switch (Justification.ToLower())
{
case "right":
if ((Vertical_Alignment.ToLower() == "middle"))
{
acText.HorizontalMode = TextHorizontalMode.TextRight;
acText.VerticalMode = TextVerticalMode.TextVerticalMid;
}
else
{
acText.HorizontalMode = TextHorizontalMode.TextRight;
}
break;
case "center":
if ((Vertical_Alignment.ToLower() == "middle"))
{
acText.HorizontalMode = TextHorizontalMode.TextCenter;
acText.VerticalMode = TextVerticalMode.TextVerticalMid;
}
else
{
acText.HorizontalMode = TextHorizontalMode.TextRight;
}
break;
case "left":
if ((Vertical_Alignment.ToLower() == "middle"))
{
acText.HorizontalMode = TextHorizontalMode.TextLeft;
acText.VerticalMode = TextVerticalMode.TextVerticalMid;
}
else
{
acText.HorizontalMode = TextHorizontalMode.TextRight;
}
break;
}
acText.AlignmentPoint = Text_Location;
acTrans.AddNewlyCreatedDBObject(acText, true);
// Save the changes and dispose of the transaction
acTrans.Commit();
}
}
}
-Khoa