[Last Call] Learn how to a build a cloud-first strategyRegister Now


Insert Mtext Using VBA with user defined location

Posted on 2005-04-14
Medium Priority
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
  • 3
  • 3
  • 2
LVL 10

Expert Comment

ID: 13785470
hey bill,
are you using 2005?
LVL 11

Author Comment

ID: 13785489
I have both Map2004 and Map2005 installed
LVL 10

Accepted Solution

norrin_radd earned 1000 total points
ID: 13786225
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 11

Author Comment

ID: 13786609
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.


Expert Comment

ID: 13787253
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.


Assisted Solution

haikle earned 1000 total points
ID: 13789936

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

ID: 13790237
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

ID: 13790595
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.

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
In this article, we will see the basic design consideration while designing a Multi-tenant web application in a simple manner. Though, many frameworks are available in the market to develop a multi - tenant application, but do they provide data, cod…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

830 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