Insert Mtext Using VBA with user defined location

I am writing a program that Prompts the user to select an object to measure the area.  When the user selects the object it will insert Mtext with the area values using this statment:


The only problem is that I want to allow the user to dynamically select the text placement point instead of specifying the coordinates ahead of time.  On a similar note I had toyed with the idea of having it insert the text using the centroid as the placement point.  Then I realized that Autocad closed polylines do not have centroids, unless you create one for it using a Map command.  Can you think of a way around this?
LVL 11
Who is Participating?
norrin_raddConnect With a Mentor Commented:
im glad you asked that question because I've been doing something very similar in a very clunky way, getting the length of a pline then putting it in an attrib, but if that line changes I have to remember to rerun my code and erase the old block. since Ive got 2005 now(from 2002) I've got a new friend, its call FIELDS. I just put this together check it out:
Sub GetObjArea()
    ' This example adds objects to a selection set by prompting the user
    ' to select ones to add.
    ' Create the selection set
    Dim ssetObj As AcadSelectionSet
    Set ssetObj = ThisDrawing.SelectionSets.Add("SSET")
    ' Add objects to a selection set by prompting user to select on the screen
    'MsgBox ssetObj.Item(1).ObjectID
    Dim textObj As IAcadMText2
    Dim text As String
    'Dim insertionPoint(0 To 2) As Double
    Dim insertionPoint As Variant
    Dim height As Double
    Dim WIDTH As Double
    Dim OBJID As Long
    OBJID = ssetObj.Item(0).ObjectID
    ' Define the text object
    text = "%<\AcObjProp Object(%<\_ObjId " & OBJID & ">%).Area>%"
    'insertionPoint(0) = 2: insertionPoint(1) = 2: insertionPoint(2) = 0
    insertionPoint = ThisDrawing.Utility.GetPoint(, "PICK LOCATION: ")
    height = ThisDrawing.GetVariable("DIMSCALE") * 0.09
    WIDTH = 0
    ' Create the text object in model space
    'Set textObj = ThisDrawing.ModelSpace.AddText(text, insertionPoint, height)
    Set textObj = ThisDrawing.ModelSpace.AddMText(insertionPoint, WIDTH, text)
    ' Return the current text string for the object
    text = textObj.FieldCode
    'MsgBox "The FieldCode for the text object equals: " & text, vbInformation, "FieldCode Example"
End Sub

you can see how I pieced it together, you could easily modify that to cycle through a selection of multiple objects too, and the field updates itself if the area changes,
maybe that will work for you, at least in 2005
hey bill,
are you using 2005?
BillPowellAuthor Commented:
I have both Map2004 and Map2005 installed
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

BillPowellAuthor Commented:
I think I got something working.

insertionPoint = ThisDrawing.Utility.GetPoint(, "PICK LOCATION: ")

This was the line of code I needed.  I have it working now except that I cant figure out how to specify the text height as Im adding it, or after Im adding it.  By default Autocad has a text height of 0.200 which is way too small for the drawings I do.

You should be able to set the text size before creating the Mtext.

ThisDrawing.SetVariable "TextSize", TextsizeValue

Also, you need to check the text size for the current text style.
If the text size defined in the text style is zero, then new text placed uses the "TextSize" variable.
If the text size defined in the text style is greater than zero, then any text created using that text style will ignore the "TextSize" variable.

I will confirm this in the morning.

haikleConnect With a Mentor Commented:

The above still holds true in AutoCAD 2005.

Also, you could try

    Set MTextObj = ThisDrawing.ModelSpace.AddMText(insertPt, width, text)

In this case, the textsize is independent of the text style's text height.
sorry bill I set a variable for the height but never implemented it with the thisdrawing.setvariable
I like haikles " MTextObj.height=TextSizeValue" that may work better.

just a little off topic but haikle mentioned text size being defined in the text style, I dont know about you guys but I always found that to cause problems, do you find alot of people do it that way? just curious.
BillPowellAuthor Commented:
Cant say that Ive ever changed the default size of a text style.
Thanks guys.
Stay tuned for my next question about how to reference object data for a selected entity.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.