Select text inside block

Select text inside block

angelinabrown0123
Advocate Advocate
2,976 Views
9 Replies
Message 1 of 10

Select text inside block

angelinabrown0123
Advocate
Advocate

Hi All,

I want to select text inside block and show it in "msgbox", I have attached the drawing for testing.

I would appreciate the work, if help me.

0 Likes
Accepted solutions (1)
2,977 Views
9 Replies
Replies (9)
Message 2 of 10

grobnik
Collaborator
Collaborator

Hi @angelinabrown0123 

I had a look to your DWG but the text it's not part of block, it's part of entire drawing.

If text to be selected it's part of block I suggest to become text as attribute of block, or, on the opposite it's quiet complicate.

I guess that a solution could be find the specific block in the drawing, got the insertion point, search again for a text around the block coordinates plus an X,Y factor. On the opposite if the text it's a part of block as attribute it's more easy to manage and in case modify the text.

I know are simply suggestion no code to copy and paste, but we don't know your final scope, for example block it's existing and cannot be modified, or something like that, text will be always on a specific layer, block will be always the same.... a lot of questions that could change the right approach to an automation of your document.

Hope hear you again

0 Likes
Message 3 of 10

norman.yuan
Mentor
Mentor

When you say "select text inside a block...", do you mean to ask user to pick the text seen in a block reference? If so, you can use AcadUtility.GetSubEntity() method, quite similar to GetEntity() method.

 

If you mean to find a text entity in a block definition with code, then you need to get to the block definition and loop through its element entities and somehow decide which text entity is the one in interest, if there are multiple text entities in the block definition

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 4 of 10

angelinabrown0123
Advocate
Advocate

@grobnik 

yes you are right this attribute block will make it easier but i already have this text in many normal blocks and so it has to do with normal blocks

@norman.yuan 

I don't know much about vba code but AcadUtility.GetSubEntity()
With this code we have to select the text manually, however I have many blocks and I have to bring the text inside all the blocks in one layer. Like we use the below code for polyline.
objSSet.SelectByPolygon acSelectionSetWindowPolygon,....

I think it is possible to do it in two ways
I don't know if it's possible in the first way, the text can be searched from the coordinates of the blocks using this code "blk.GetBoundingBox minPt, maxPt"
And in the second method we will layer another polyline on top of the block and then it will be simple like "objSSet.SelectByPolygon acSelectionSetWindowPolygon,... " then it will be easy to do.

0 Likes
Message 5 of 10

norman.yuan
Mentor
Mentor

Sorry, I do not understand what you really want to do. In your original post, you say you want to know show a text entity's text value, which is in a block. I am sure you know, the block you see in AutoCAD editor is a block reference, which does not contain ANYTHING, except for attributes, it is just an reference (or an image) of the block definition. The text entity is in block definition. So, if you cannot use GetSubEntity() method, you need to get to the correct block definition (AcadBlock) and then loop through it to find the target text. If you also need to know the position the text appears in a block reference, then you need to get the text's position in the block definition and then transform the position based in the block reference's insertion/rotation/scale.

 

Again, not knowing what exactly you need to know/what you want to achieve, it is difficult to suggest/recommend meaningful solution. I am quite confused why you were talking about selectionset/boundingbox here.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 6 of 10

grobnik
Collaborator
Collaborator
Accepted solution

@angelinabrown0123 

Could you try to approach to your issue with a code like this

 

 

Sub BLText()
Dim ReturnObj As AcadObject
Dim MyObj As AcadBlockReference
Dim minPt As Variant
Dim maxPt As Variant
'ThisDrawing.Utility.GetEntity ReturnObj, basePnt, "Select an object"
For Each ReturnObj In ThisDrawing.ModelSpace
    If TypeOf ReturnObj Is AcadBlockReference Then
        Set MyObj = ReturnObj
        MyObj.GetBoundingBox minPt, maxPt

        If ThisDrawing.SelectionSets.Count = 0 Then
            Set SelectBlockText = ThisDrawing.SelectionSets.Add("block")
        
        Else
            Set SelectBlockText = ThisDrawing.SelectionSets.Item(0)
        
        End If
        ' blocks and text
        ReDim gpCode(0 To 0) As Integer
        gpCode(0) = 0
        
        ReDim dataValue(0 To 0) As Variant
        dataValue(0) = "insert,text"
        
        groupCode = gpCode
        dataCode = dataValue
        
        SelectBlockText.Select acSelectionSetWindow, minPt, maxPt, groupCode, dataCode
        For Each Item In SelectBlockText
            If TypeOf Item Is AcadText Then
                Debug.Print Item.TextString
            End If
        Next
    End If
Next
End Sub

 

 

As I indicated in my first message, procedure is searching for a block (could be refined with exactly block name if there is) get the boundary points and refine the search for a text in the bounding window.

Of course could be fixed and checked more but should work.

Let us know.

Bye

0 Likes
Message 7 of 10

grobnik
Collaborator
Collaborator

@norman.yuan 

Hi Norman, if you download the sample drawing @angelinabrown0123 attached to the post, you can see a text part of drawing and untied with block placed inside a box that is a block.

So this is the reason he/she talk about a text inside the block.

 @angelinabrown0123 could confirm my understand.

0 Likes
Message 8 of 10

norman.yuan
Mentor
Mentor

@grobnik 

That was why I said I was confused by the OP saying "text in block". By your understanding, it should have been said a text entity that happens to locate/overlap on top/at bottom of a block reference. 

 

If that is the case, there is a bug in your code: the selecting by window would ONLY work if the selecting window is WITHIN current view. If the selecting window (e.g. the block reference that overlaps with the text) is off the screen, the AcadSelectionSet.Select() method would not work. So, your code must ensure the selecting window is within current view before calling Select() method.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 9 of 10

grobnik
Collaborator
Collaborator

@norman.yuan 

Thank you for suggestion my code could be not perfect, as I stated in the post it's a starting approach, as you said before we don't know the requirements. It's based upon what the shared drawing is showing a text "inside" a block.

Thank you again.

Bye

 

0 Likes
Message 10 of 10

angelinabrown0123
Advocate
Advocate

@grobnik 

your code i tested it on my drawing it is working fine you are really amazing.

Thanks a lot!!!

 

@norman.yuan 

With @grobnik code my work is done and all my text is inside or completely outside the block that's why I have not received any bug from the @grobnik code.
And you got a little confused in my words, I apologize for that.

 

 

Thank you both so much!