Need help creating a simple leader

Need help creating a simple leader

NickBrege
Enthusiast Enthusiast
2,370 Views
5 Replies
Message 1 of 6

Need help creating a simple leader

NickBrege
Enthusiast
Enthusiast

I am trying to creating a simple leader in code ... a line with an arrow that points to a feature, connected to a short horizontal line, followed by a single line of text.  I started with this example from the documentation:

 

Sub Ch5_CreateLeader()
    Dim leaderObj As AcadLeader
    Dim points(0 To 8) As Double
    Dim leaderType As Integer
    Dim annotationObject As AcadObject

    points(0) = 0: points(1) = 0: points(2) = 0
    points(3) = 4: points(4) = 4: points(5) = 0
    points(6) = 4: points(7) = 5: points(8) = 0
    leaderType = acLineWithArrow
    Set annotationObject = Nothing

    ' Create the leader object in model space
    Set leaderObj = ThisDrawing.ModelSpace. _
          AddLeader(points, annotationObject, leaderType)
    ZoomAll
End Sub

My question is how do I specify the text I want to use?  I see there is a reference to an annotation object, but I don't know what I need to do with it.

Can anyone help me out with a code sample that shows what I need to do?  Thanks for any help...

0 Likes
Accepted solutions (1)
2,371 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable

as you can see in the AutoCAD ActiveX Reference Guide, you have to create an "annotation object" to hold the MText (or a Tolerance or a Block) as the object to be "connected" by means of the leader line

 

while your example is setting this "annotation object" to "nothing" (thus no text shown); you should set it to some MText, as per the following:

 

Option Explicit

Sub Ch5_CreateLeader()
    'create annotation object
    Dim annotationObject As AcadObject
    ReDim points(0 To 2) As Double
    points(0) = 10: points(1) = 10: points(2) = 0 '<--| adjust these coordinates as per your needs
    Set annotationObject = ThisDrawing.ModelSpace.AddMText(points, 4, "This is my annotation")
    
    ' Create the leader object in model space and
    Dim leaderObj As AcadLeader
    Dim leaderType As Integer
    leaderType = acLineWithArrow
    ReDim points(0 To 8) As Double
    points(0) = 0: points(1) = 0: points(2) = 0
    points(3) = 4: points(4) = 4: points(5) = 0
    points(6) = 4: points(7) = 5: points(8) = 0
    Set leaderObj = ThisDrawing.ModelSpace. _
          AddLeader(points, annotationObject, leaderType)
    ZoomAll
End Sub
0 Likes
Message 3 of 6

NickBrege
Enthusiast
Enthusiast

Thanks for the help, I got it to work now.  However, the text is not connected to the leader lines in any way.  When I reposition the text, the leader lines don't follow and I can delete each one separately.  They are two completely separate entities.  Is there any way to create it the way Autocad does when I manually add a leader to a drawing?

0 Likes
Message 4 of 6

Anonymous
Not applicable
Accepted solution

you could group them in a block

 

or you could use MLeader object

 

Sub CreateMLeader()
    Dim points(0 To 8) As Double
    
    ' Define the leader points
    points(0) = 2: points(1) = 2: points(2) = 0
    points(3) = 6: points(4) = 6: points(5) = 0
    points(6) = 6: points(7) = 7: points(8) = 0
    With ThisDrawing.ModelSpace.AddMLeader(points, 0) 
        .leaderType = AcMLeaderType.acStraightLeader
        .ArrowheadType = AcDimArrowheadType.acArrowClosed
        .TextString = "This is my annotation"
    End With
End Sub
0 Likes
Message 5 of 6

NickBrege
Enthusiast
Enthusiast

Thank you ... I will give that a try.

0 Likes
Message 6 of 6

Anonymous
Not applicable

you are welcome

 

and should my answer solve your question, you may want to come back and mark it as the solution

this will also help other and subsequent forum users 

 

you may want to do that in any other thread you started and got solutions for, too

 

good coding

0 Likes