VBA routine (sub or function) to insert text into a drawing file, referenced from a point

VBA routine (sub or function) to insert text into a drawing file, referenced from a point

lgabrielRB55F
Participant Participant
686 Views
3 Replies
Message 1 of 4

VBA routine (sub or function) to insert text into a drawing file, referenced from a point

lgabrielRB55F
Participant
Participant

I have written the following VBA code to insert text into a drawing referenced from a point, which works fine.

 

Inspt = ThisDrawing.Utility.PolarPoint(Spt, 0.785398, 10.3337)
Set TextObj = ThisDrawing.PaperSpace.AddText(pbkw, Inspt, 0.05)
TextObj.Alignment = acAlignmentCenter
TextObj.TextAlignmentPoint = Inspt

 

What I want is to create a routine (sub or function) that handles the above, by passing all the necessary arguments as follows:

 

InsText(startpoint,radians,distance,textstring,height,txtalignment)

 

I have tried several iterations and I cannot get anything to work. My main issue is incorrect references. I know someone has already written this and would appreciate any and all assistance in this matter.

0 Likes
Accepted solutions (1)
687 Views
3 Replies
Replies (3)
Message 2 of 4

almutaz_86
Advocate
Advocate
Accepted solution

I suppose this code will work:

Function InsText(Start_P_X As Double, Start_P_Y As Double, Rad As Double, Distance As Double, Txt_String As String, Height As Double, Txt_Align As AcAlignment) As AcadText
    
    Dim Insert_Point As Variant, My_Txt As AcadText, Start_Point(0 To 2) As Double
    Start_Point(0) = Start_P_X: Start_Point(1) = Start_P_Y: Start_Point(2) = 0
    Insert_Point = ThisDrawing.Utility.PolarPoint(Start_Point, Rad, Distance)
    Set My_Txt = ThisDrawing.ModelSpace.AddText(Txt_String, Insert_Point, Height)
    My_Txt.Alignment = Txt_Align
    My_Txt.TextAlignmentPoint = Insert_Point
    Set InsText = My_Txt
End Function

 

calling :

Sub Calling_Test()
    InsText 1.23, 4.56, 1, 5, "Hi", 5, acAlignmentBottomRight
    ZoomAll
End Sub
0 Likes
Message 3 of 4

Ed__Jobe
Mentor
Mentor

You may want to change from ModelSpace to ActiveSpace like this.

Function InsText(Start_P_X As Double, Start_P_Y As Double, Rad As Double, Distance As Double, Txt_String As String, Height As Double, Txt_Align As AcAlignment) As AcadText
    
    Dim Insert_Point As Variant, My_Txt As AcadText, Start_Point(0 To 2) As Double
    Start_Point(0) = Start_P_X: Start_Point(1) = Start_P_Y: Start_Point(2) = 0
    Insert_Point = ThisDrawing.Utility.PolarPoint(Start_Point, Rad, Distance)
    If ThisDrawing.ActiveSpace = acModelSpace Then
      Set My_Txt = ThisDrawing.ModelSpace.AddText(Txt_String, Insert_Point, Height)
    Else
      Set My_Txt = ThisDrawing.PaperSpace.AddText(Txt_String, Insert_Point, Height)
    EndIf
    My_Txt.Alignment = Txt_Align
    My_Txt.TextAlignmentPoint = Insert_Point
    Set InsText = My_Txt
End Function

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 4 of 4

lgabrielRB55F
Participant
Participant

Thank you. Works perfectly. Now I can replace 7 lines of code with one for every text I insert into the drawing