Get and change MTEXT in Block

Get and change MTEXT in Block

dalton_northwind
Enthusiast Enthusiast
466 Views
3 Replies
Message 1 of 4

Get and change MTEXT in Block

dalton_northwind
Enthusiast
Enthusiast

Hello,

 

I'm messing around with a custom conveyor model using Factory Design Utility. I'd like to be able to add labels and possible add dimension to the autocad file. *attached*

The text position for the 'annotations text' reset when you update the model (image 1). How do I access the mtext position and change it? image2

 

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

Norman_Yuan
Mentor
Mentor

It seems, to me, the said '...annotation texts...' are attributes of the INSERTED block (AcadBlockReference, in AutoCAD VBA term), not "MTEXT in Block" as the title of your post described.

 

Further more, it seems, the block (named as "FDS_xxxxxxxxxxxxxxxxx") itself DOES NOT DEFINED with attribute definition in it. Therefore, user cannot insert this block manually (with AutoCAD's "INSERT" command) with the attributes (annotation texts).

 

You may or may not know that attributes (attribute reference, not attribute definition defined in block definition) CAN BE ADDED to an inserted block (block reference). While this is not a normal way to insert block with attribute, but can be done with programming, but not with AutoCAD built-in "INSERT" command, nor from AutoCAD COM API (i.e. VBA code).

 

So, I would guess, this block comes with the "Factory Design Utility" and should only be inserted into drawing by that utility and automatically reset by that utility in certain conditions (as you said "...when you update the model..."). 

 

With VBA, you can move the attributes in the block reference. So, yes, after the the model updated, you can use VBA code to find the attributes in the block reference and move its position TextAligmentPoint property.

 

As for adding labels/dimension, you can add add Leaders/Text/MText/Dimension to the drawing, but they are simply separate entities, not part of the block reference as the attributes ("annotation texts").

 

Not knowing your VBA knowledge, I am not going into code details of moving attribute references in a inserted block.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 4

dalton_northwind
Enthusiast
Enthusiast

Thank you for responding. My vba knowledge for autocad is novice at best. How would you go about getting the property of the attributes?

 

I was able to get the attributes in the block (with the help from your previous post)

 

Sub test()

Dim ent As AcadEntity
Dim blk As AcadBlockReference
Dim atts As Variant

For Each ent In ThisDrawing.ModelSpace

	If TypeOf ent Is AcadBlockReference Then
		atts = ent.GetAttributes
		For X = LBound(atts) To UBound(atts)
			Debug.Print atts(X).TextString, atts(X).TagString
			'Debug.Print atts(X).TextAlignmentPoint(0)???
		Next X
	End If
Next

End Sub
0 Likes
Message 4 of 4

Norman_Yuan
Mentor
Mentor
Accepted solution

Moving entity is rather simple: simply call AcadEntity.Move() method. Of course you need to supply required 2 points for the move, which can either be from user input (asking user to pick point), or be calculated based on the block reference's information. Following code shows how to move, where the "from point" is the attribute's TextAlignmentPoint, the "to point" is asked for users to pick:

Option Explicit

Public Sub MoveAtts()
    
    Dim ent As AcadEntity
    Dim pt As Variant
    Dim blk As AcadBlockReference
    
    On Error Resume Next
    ThisDrawing.Utility.GetEntity ent, pt, vbCr & "Select the block:"
    If ent Is Nothing Then Exit Sub
    
    Set blk = ent
    MoveAttributesInBlockReference blk
    
End Sub

Private Sub MoveAttributesInBlockReference(blk As AcadBlockReference)
    
    Dim atts As Variant
    Dim i As Integer
    Dim att As AcadAttributeReference
    
    atts = blk.GetAttributes()
    For i = 0 To UBound(atts)
        Set att = atts(i)
        If Not MoveAttributeReference(att) Then
            Exit For
        End If
    Next
    
End Sub

Private Function MoveAttributeReference(att As AcadAttributeReference) As Boolean
    
    Dim pt As Variant
    Dim base As Variant
    base = att.TextAlignmentPoint
    
    On Error Resume Next
    pt = ThisDrawing.Utility.GetPoint(base, vbCr & "Pick point to move attribute """ & att.TagString & """:")
    If Err.Number <> 0 Then
        MsgBox "Moving is cancelled."
        MoveAttributeReference = False
        Exit Function
    End If
    
    att.Move base, pt
    MoveAttributeReference = True
    
End Function

 

The video clip below shows how the code is in action:

(view in My Videos)

 

You'll notice that the block has an "Invisible" attribute "Descriptor", which you can decide whether you want to move it or not in the code, of course.

 

HTH

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes