Jump to content
RS_Chip

VBA - Mtext properties "defined height" and "Position Y"

Recommended Posts

RS_Chip

Hello,

 

I'm facing a problem while extracting properties from Mtext's.

How can we extract "defined height" and "Position Y" with VBA-code?
Normal height, width, text i don't have problems, but those 2 that i need, i can't find a way.

 

Can someone help me a little bit?

 

Thanks!
 

Share this post


Link to post
Share on other sites
PeterPan9720
Posted (edited)

Here the code

Sub SelectionSetFilterText()
    Dim MySelection As AcadSelectionSet
    Dim filterType(1) As Integer ' DIM to (0) if Not LAYER filter Applied in Selection Set
    Dim filterData(1) As Variant ' DIM to (0) if Not LAYER filter Applied in Selection Set
    Dim MyX, MyY, MyZ, MyWidth, MyHeight, MyWidth As Double
	Dim MyText As Variant
    Dim MyPosition() As Double
    
    On Error Resume Next
    On Error GoTo 0

    For Each MySelection In ThisDrawing.SelectionSets
        If MySelection.Name = "PP1" Then
            MySelection.Delete
        End If
    Next

    Set MySelection = ThisDrawing.SelectionSets.Add("PP1")
    
    filterType(0) = 0
    filterType(1) = 8 'LAYER SELECTION FILTER, DELETE IF DIM SET TO 0
    filterData(0) = "TEXT,MTEXT"
    filterData(1) = "TAG"                            'LAYER NAME SELECTION, DELETE IF DIM SET TO 0
    
    MySelection.Select acSelectionSetAll, , , filterType, filterData
    For Each DWGSearchText In MySelection                          ' For Each Text find in the model space selection
    If DWGSearchText.Layer = "TAG" Then
        If TypeOf DWGSearchText Is AcadText Or TypeOf DWGSearchText Is AcadMText Then
            Debug.Print DWGSearchText.TextString
            ReDim MyPosition(UBound(DWGSearchText.InsertionPoint))
            MyPosition = DWGSearchText.InsertionPoint
            MyX = MyPosition(0)
            MyY = MyPosition(1)
            MyZ = MyPosition(2)
            MyHeight = DWGSearchText.Height
            MyText = DWGSearchText.TextString
            MyWidth = DWGSearchText.Width
            'Do something here
        End If
    End If
    Next

End Sub

Let me know if you will have any issue.

Concerning the DEFINED HEIGHT I do not understand what you mean. With the above code you will have the text height (See MyHeight variable)

Edited by PeterPan9720

Share this post


Link to post
Share on other sites
RS_Chip
On 3/10/2020 at 11:56 PM, PeterPan9720 said:

Here the code


Sub SelectionSetFilterText()
    Dim MySelection As AcadSelectionSet
    Dim filterType(1) As Integer ' DIM to (0) if Not LAYER filter Applied in Selection Set
    Dim filterData(1) As Variant ' DIM to (0) if Not LAYER filter Applied in Selection Set
    Dim MyX, MyY, MyZ, MyWidth, MyHeight, MyWidth As Double
	Dim MyText As Variant
    Dim MyPosition() As Double
    
    On Error Resume Next
    On Error GoTo 0

    For Each MySelection In ThisDrawing.SelectionSets
        If MySelection.Name = "PP1" Then
            MySelection.Delete
        End If
    Next

    Set MySelection = ThisDrawing.SelectionSets.Add("PP1")
    
    filterType(0) = 0
    filterType(1) = 8 'LAYER SELECTION FILTER, DELETE IF DIM SET TO 0
    filterData(0) = "TEXT,MTEXT"
    filterData(1) = "TAG"                            'LAYER NAME SELECTION, DELETE IF DIM SET TO 0
    
    MySelection.Select acSelectionSetAll, , , filterType, filterData
    For Each DWGSearchText In MySelection                          ' For Each Text find in the model space selection
    If DWGSearchText.Layer = "TAG" Then
        If TypeOf DWGSearchText Is AcadText Or TypeOf DWGSearchText Is AcadMText Then
            Debug.Print DWGSearchText.TextString
            ReDim MyPosition(UBound(DWGSearchText.InsertionPoint))
            MyPosition = DWGSearchText.InsertionPoint
            MyX = MyPosition(0)
            MyY = MyPosition(1)
            MyZ = MyPosition(2)
            MyHeight = DWGSearchText.Height
            MyText = DWGSearchText.TextString
            MyWidth = DWGSearchText.Width
            'Do something here
        End If
    End If
    Next

End Sub

Let me know if you will have any issue.

Concerning the DEFINED HEIGHT I do not understand what you mean. With the above code you will have the text height (See MyHeight variable)

 

Hi PeterPan9720,

The Position Y i got it. Thanks to you!

The defined height is not the text height.

It specifies the defined height of the MText, in other words, the height of the box forming the outer boundary of the MText.
I'm still trying to find a way to get this...

Share this post


Link to post
Share on other sites
PeterPan9720
Posted (edited)
32 minutes ago, RS_Chip said:

 

Hi PeterPan9720,

The Position Y i got it. Thanks to you!

The defined height is not the text height.

It specifies the defined height of the MText, in other words, the height of the box forming the outer boundary of the MText.
I'm still trying to find a way to get this...

Ok I understand, 

I'm not sure but you can try with :

 

GetBoundingBox Method (ActiveX)

 

object.GetBoundingBox MinPoint, MaxPoint

 

https://knowledge.autodesk.com/it/support/autocad/learn-explore/caas/CloudHelp/cloudhelp/2015/ITA/AutoCAD-ActiveX/files/GUID-A20C361C-BBF0-4EAB-8BE7-709154CEEE09-htm.html

 

Method should return the bounding box around the object.

MinPoint, MaxPoint represent set of x,y,z coords of lower left upper right corner.

Edited by PeterPan9720

Share this post


Link to post
Share on other sites
PeterPan9720
Posted (edited)

Hi,

due to I'm curious, I tried a simple DWG with MTEXT on 2 lines, and bounding box

Starting from the above routine I added the below code, after Mtext Object selection on DWG.

So if you need the defined height, in the same way of previous object insertion point, you can retrive only Y values of MinPoint and MaxPoint and calculate the difference between retrieved Y value from MinPoint and MaxPoint.

-----

            DWGSearchText.GetBoundingBox MinPoint, MaxPoint
            Set Dline = ThisDrawing.ModelSpace.AddLine(MinPoint, MaxPoint)
            
            MyXMaxPoint = MaxPoint(0)
            MyYMaxPoint = MaxPoint(1)
            MyZMaxPoint = MaxPoint(2)
            
            MyXMinPoint = MinPoint(0)
            MyYMinPoint = MinPoint(1)
            MyZMinPoint = MinPoint(2)
            
            DifferenceY = MyYMaxPoint - MyYMinPoint
            DifferenceX = MyXMaxPoint - MyXMinPoint

------
and the line traced starting from Bounding Box coords received it has exactly dimensions of MTEXT frame.

So if you check the value of DifferenceX it's exactly the DX of line traced, and equal to MTEXT  Defined Width of course DifferenceY should be the Defined height, even if you check the properties on drawing this is to 0 and I don't know why.

 

See attached dwg.

Let me know.

Bounding.dwg

Edited by PeterPan9720

Share this post


Link to post
Share on other sites
RS_Chip
4 hours ago, PeterPan9720 said:

Hi,

due to I'm curious, I tried a simple DWG with MTEXT on 2 lines, and bounding box

Starting from the above routine I added the below code, after Mtext Object selection on DWG.

So if you need the defined height, in the same way of previous object insertion point, you can retrive only Y values of MinPoint and MaxPoint and calculate the difference between retrieved Y value from MinPoint and MaxPoint.

-----

            DWGSearchText.GetBoundingBox MinPoint, MaxPoint
            Set Dline = ThisDrawing.ModelSpace.AddLine(MinPoint, MaxPoint)
            
            MyXMaxPoint = MaxPoint(0)
            MyYMaxPoint = MaxPoint(1)
            MyZMaxPoint = MaxPoint(2)
            
            MyXMinPoint = MinPoint(0)
            MyYMinPoint = MinPoint(1)
            MyZMinPoint = MinPoint(2)
            
            DifferenceY = MyYMaxPoint - MyYMinPoint
            DifferenceX = MyXMaxPoint - MyXMinPoint

------
and the line traced starting from Bounding Box coords received it has exactly dimensions of MTEXT frame.

So if you check the value of DifferenceX it's exactly the DX of line traced, and equal to MTEXT  Defined Width of course DifferenceY should be the Defined height, even if you check the properties on drawing this is to 0 and I don't know why.

 

See attached dwg.

Let me know.

Bounding.dwg 148.31 kB · 0 downloads

Hello PeterPan9720,

Once more you save the day.

I can get everything i want now. Thank you very much.

Its 0 in the properties, because for the property "Columns" = "Dynamic" you have in the options "Manual Height" selected. If you change it to "Auto Height", it will show the correct height.
Thank you!

Share this post


Link to post
Share on other sites
PeterPan9720
17 minutes ago, RS_Chip said:

Hello PeterPan9720,

Once more you save the day.

I can get everything i want now. Thank you very much.

Its 0 in the properties, because for the property "Columns" = "Dynamic" you have in the options "Manual Height" selected. If you change it to "Auto Height", it will show the correct height.
Thank you!

 

Your welcome, I don't know the reason because the Defined Width and Height seems not accessible from MTEXT VBA Properties, however I'm happy you solved your issue. And in the same time I discovered some missing features with MTEXT.

Regards

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...