Get the text associated with a line

Get the text associated with a line

aachiney
Contributor Contributor
2,090 Views
6 Replies
Message 1 of 7

Get the text associated with a line

aachiney
Contributor
Contributor

Capture1.JPG

 

Hi,

 

I have some lines with headings just above them, precisely starting at the same X co-ordinate of the line. 

Is it possible that I get the text associated with the line, if I click a particular line? If yes, HOW...?

 

Regards

0 Likes
Accepted solutions (2)
2,091 Views
6 Replies
Replies (6)
Message 2 of 7

grobnik
Collaborator
Collaborator

Hi @aachiney 

do you want select a specific line on drawing and get the text ? or select all lines and if there is a text in the same X get the text ? 

Do you know the Selection set ? and VBA ? I can suggest you a simple code for selecting all lines objects in the drawing and get the text if in the same X coord, or you can select by pointer each one do you want and get the text if in the same X coord.

Are you sure about that text will be in the same precise X coord ? or may we insert a deviation range ? I don't know for example 1 mm ? just to be sure about position.

Let me know I'll give you a sample code soon.

0 Likes
Message 3 of 7

grobnik
Collaborator
Collaborator
Accepted solution

Here the code, of course it can be optimized...

Sub Text_And_Line()
    Dim FilterType(0) As Integer
    Dim FilterData(0) As Variant
        'On Error Resume Next
        For Each ssDim In ThisDrawing.SelectionSets
            If ssDim.Name = "SS" Then ssDim.Delete
            Exit For
        Next ssDim
    Set BSelSet = ThisDrawing.SelectionSets.Add("SS")
    FilterType(0) = 0
    FilterData(0) = "LINE,TEXT,MTEXT"
    BSelSet.Clear
    BSelSet.SelectOnScreen FilterType, FilterData
If BSelSet.Count > 0 Then
    For Each ObJ In BSelSet
        If TypeOf ObJ Is AcadLine Then
            MyCoordsLine = ObJ.StartPoint
        End If
        If TypeOf ObJ Is AcadText Then
            MyCoordsText = ObJ.InsertionPoint
            MyString = ObJ.TextString
        End If
 Next
End If
End Sub

You have to select line and text together, the result will be a selection set of text and line.

MyString Will be the text related to the line selected

Bye

Message 4 of 7

norman.yuan
Mentor
Mentor
Accepted solution

It depends on what "associated" mean here, and "association" here could be strong, or weak, depends on how the association is created.

 

If it only relies on the text position point is at exact position of the line's end point (start/end point), then you must make sure the text is drawn accurately. You can also place each pair of text/line and a group, so that the group becomes the association mechanism (i.e. even the text is moved from the line's end point slightly or significantly, the text can still be identified being associated with a line); or you can establish the association by embed XData to each pair, so the two would be linked "hardly" by data that can be examined with VBA code.. 

 

So, before you spend time to get text from associated Text entity, you might want to make sure the association between the line and the text make business sense in your CAD operation process.

 

If all you care is the text's position being at the line's end point, then write code to find the "associated" text entity (and then get its text string) would be fairly easy. Here are the pseudo code/process (assuming they are in ModelSpace):

 

Dim ent As AcadEntity

Dim txt As AcadText

Dim line As AcadLine

For Each ent In ThisDrawing.ModelSpace

  If TypeOf ent Is AcadLine Then

    Set line = ent

    Set txt = FindConnectedText(line)

    If Not txt Is Nothing Then

      MsgBox "Text String: " & txt.TextString

    End If

  End If

Next

 

Function FindConnectedText(line As AcadLine) As AcadText

  Dim txt As AcadText

  Dim ent As AcadEntity

  For Each ent in ModelSpace

    If TypeOf ent Is AcadText Then

      Set txt=ent

      If TextAtPoint(txt.InsertionPoint, line.StartPoint) Or TextAtPoint(txt.InsertionPoint, line.EndPoint) Then

        Set FindConnectedText = txt

        Exit Function

      End If

    End If

  Next

  Set FindConnectedText = Nothing

End Function

 

Function TextAtPoint(textPoint As Variant, linePoint As Variant) As Boolean

   '' You compare the 2 inputs point (3-element array of Double numbers).

   '' whether they are the same or close enough, return True False

End Function

 

Obviously, if the business requires other more reliable "association" mechanism, the you need different code to identify the associated text entity.

 

HTH

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 5 of 7

aachiney
Contributor
Contributor

@grobnik 

 

I will always select a single line. The text associated with it will always be on the top of the line and X co-ordinate will be known. The user can click either at the topmost or bottom most point of the line. 

0 Likes
Message 6 of 7

grobnik
Collaborator
Collaborator

@aachiney 

Ok if you select both text and line in the same time, running the "simple" code I gave you you will reach the text near the line.

Of course my code it's not sophisticated as @norman.yuan suggest but should work,

Let us know.

Message 7 of 7

aachiney
Contributor
Contributor

@norman.yuan 

 

Thanks a lot. 🙂

0 Likes