Insert Mtext Using VBA with user defined location

Posted on 2005-04-14
Last Modified: 2012-06-27
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?
Question by:BillPowell
    LVL 10

    Expert Comment

    hey bill,
    are you using 2005?
    LVL 11

    Author Comment

    I have both Map2004 and Map2005 installed
    LVL 10

    Accepted Solution

    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
    LVL 11

    Author Comment

    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.

    LVL 5

    Expert Comment

    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.

    LVL 5

    Assisted Solution


    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.
    LVL 10

    Expert Comment

    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.
    LVL 11

    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Our company has proprietary drawing files that we do not give to our customers for final documentation. Our customers now require that we give them these drawings. They understand that they are proprietary and do not require them to be in AutoCAD bu…
    The following article will describe how to add/edit a dimension style through AutoCAD VBA. AutoCAD VBA has its quirks and when it comes to dimensions and controlling how they look through VBA.  This is where AutoCAD can be vividly confusing. The…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now