COM Programming

Can anyone provide some insight into setting text justification using COM. I am using delphi and can create the text required but cant set the justification. A code snippet follows: T:=VarArrayCreate([0,1],varDouble); T[0]:=100; T[1]:=100; myText:=Doc.ModelSpace.AddText('Hello',VarArrayRef(T),2); mytext.HorizontalAlignment:=4; myText.VerticalAlignment:=1;T is defined as an OLEvariant and holds the insertion point and mytext is an ACadText element. If i leave out the last two lines the text creates fine but as soon as I run the last two lines it fails to create the text. Thanks in advance.

Comments

  • That should work, you might try calling the objects update method. i.e C# example

    [CommandMethod("AddText")] public static void addtext() { try { AcadApplication app = Application.AcadApplication as AcadApplication; if (app == null) throw new Exception("Could Not Get Application"); AcadDocument doc = app.ActiveDocument; AcadDatabase db = doc.database; AcadModelSpace space = db.ModelSpace; double[] pt1 = new double[] { 0, 0, 0 }; ; AcadText acadText = space.AddText("Hello World", pt1, doc.ActiveTextStyle.Height); acadText.Height = doc.ActiveTextStyle.Height; acadText.HorizontalAlignment = AcHorizontalAlignment.acHorizontalAlignmentMiddle; // = 4 acadText.VerticalAlignment = AcVerticalAlignment.acVerticalAlignmentBottom;// = 1 acadText.Update(); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } }
  • Hi Dan, thanks for the suggestion. I did in fact try this but the same result occurs. I have just noticed however that the text does in fact get created but the insertion point gets set to 0,0 instead of the original coordinates (hence the reason i thought nothing was happening before). As a workaround I tried setting the Insertion Point for a second time but again it is not updating from 0,0 ? Any further ideas - all other properties seem to be working except the justification.

  • I've just realised I can do a very messy workaround where I use the text elements move function to move it from it's incorrect insertion point back to the original coordinates. This is less than satisfactory though and it seems like a bug or maybe an incompatibility with Delphi. I assume it is working correctly for you with c# Dan?

  • Ok, you need to set the TextAlignmentPoint See if you can follow this C# code.

    // Sample command to add a line to modelspace    [CommandMethod("AddText")]    public static void addtext()    {      try      {        AcadApplication app = Application.AcadApplication as AcadApplication;        if (app == null)          throw new Exception("Could Not Get Application");        AcadDocument doc = app.ActiveDocument;        AcadDatabase db = doc.database;        AcadModelSpace space = db.ModelSpace;        AcadText acadText =          space.AddText("Hello World", new double[] { 0, 0, 0 }, 2);        acadText.HorizontalAlignment =          AcHorizontalAlignment.acHorizontalAlignmentMiddle; //4        acadText.VerticalAlignment =          AcVerticalAlignment.acVerticalAlignmentBottom;//1        acadText.TextAlignmentPoint = new double[] { 100 , 100 , 0 };        acadText.Update();      }      catch (Exception ex)      {        System.Windows.Forms.MessageBox.Show(ex.Message);      }    }
  • Hi Daniel,I think it's a bug. In VBA:

    Sub main() Dim t(0 To 2) As Double t(0) = 100 t(1) = 100 t(2) = 0 Dim myText As AcadText Set myText = ThisDrawing.ModelSpace.AddText("Hello", t, 5) ' text appears at (100,100) 'myText.Alignment = acAlignmentBottomLeft myText.Alignment = 12 myText.Update ' text has jumped to (0,1.6667) myText.InsertionPoint = t myText.Update ' text is still at (0,1.6667)End Sub

    Will you log this as a support request? If not I will - just let me know.

  • Thanks guys, I tried the textalignmentpoint but it didnt seem to have any effect. I have also submitted a support request so hopefully we can sort something out!

  • Hi Damian,According to the doc if you have the alignment set at bottom left, the insert point is the same as the TextAlignmentPointTry this code

    Sub main()Dim t(0 To 2) As Doublet(0) = 0t(1) = 0t(2) = 0Dim n(0 To 2) As Doublen(0) = 100n(1) = 100n(2) = 0Dim myText As AcadTextSet myText = ThisDrawing.ModelSpace.AddText("Hello", t, 5)myText.Alignment = 4myText.InsertionPoint = tmyText.TextAlignmentPoint = nmyText.UpdateEnd Sub
  • and with another alignment setting

    Sub main()Dim t(0 To 2) As Doublet(0) = 0t(1) = 0t(2) = 0Dim n(0 To 2) As Doublen(0) = 100n(1) = 100n(2) = 0Dim myText As AcadTextSet myText = ThisDrawing.ModelSpace.AddText("Hello", t, 5)myText.Alignment = 12myText.InsertionPoint = tmyText.TextAlignmentPoint = nmyText.UpdateEnd Sub
  • Damian,I think you should find that following VBA code (based on your earleir post) works as expected.

    Sub Damian2() Dim t(0 To 2) As Double t(0) = 100 t(1) = 100 t(2) = 0 Dim mytext As AcadText Set mytext = ThisDrawing.ModelSpace.AddText("Hello", t, 5) mytext.Alignment = acAlignmentBottomRight mytext.TextAlignmentPoint = tEnd Sub

    the properties bar for the text entities gives:Justify: Bottom rightText alignment: 100,100,0Position:81.6667,101.6667

  • Thanks guys. I tried setting the alignment point and this time it worked without a hitch (I have a feeling all my bad attempts left something set incorrectly in BC which wasn't fixed until i rebooted). Is this documented anywhere else other than the online help because I didn't think it made it that obvious that you had to set the alignment point separately to setting the justification parameters? Anyway thanks again!

This discussion has been closed.