Acces to AcadMText object problem

Acces to AcadMText object problem

228549BQGUH
Observer Observer
737 Views
2 Replies
Message 1 of 3

Acces to AcadMText object problem

228549BQGUH
Observer
Observer

Hi.

I can't access the MText object. What am I doing wrong? I'm trying to get to an MText object to be able to change a text string in it, or move it, or erase it. I can't connect to the MText object, even though I know the Index ID. How to acces to MText Box ?

 
 

 

 

Sub CreateMText()
    Dim mtextObj As AcadMText
    Dim insertPoint(0 To 2) As Double
    Dim targetPoint(0 To 2) As Double
    Dim width As Double
    Dim textString As String
    Dim s1 As Double
    Dim s2 As Double

    insertPoint(0) = 2
    insertPoint(1) = 2
    insertPoint(2) = 0
    width = 100
    textString = "mTextBox 1"
  
    ' Create a text Object in model space
    Set mtextObj = ThisDrawing.ModelSpace.AddMText(InsertionPoint:=insertPoint, width:=width, Text:=textString)
    s1 = mtextObj.ObjectID
    
    insertPoint(0) = 10
    insertPoint(1) = 10
    insertPoint(2) = 0
    width = 100
    textString = "mTextBox 2"
    ' Create a text Object in model space
    Set mtextObj = ThisDrawing.ModelSpace.AddMText(InsertionPoint:=insertPoint, width:=width, Text:=textString)
    s2 = mtextObj.ObjectID

    mtextObj.textString = "I don't know..."
    
    MsgBox "mTextBox 1 item number is: " & s1
    MsgBox "mTextBox 2 item number is: " & s2

    targetPoint(0) = 20
    targetPoint(1) = 20
    targetPoint(2) = 0
    
    ThisDrawing.ModelSpace.Item(s1).Move insertPoint, targetPoint
    
    ThisDrawing.ModelSpace.Item(s2).Delete

    ZoomAll
End Sub

 

Hi.  

0 Likes
738 Views
2 Replies
Replies (2)
Message 2 of 3

norman.yuan
Mentor
Mentor

[Object].Item() takes either a string or an integer as the index to identify an object in a Object collection. In collections like AcadBlocks/AcadLayers, items are indexed with string key, while in collections like AcadBlock/Model[Paper]Space, the items are indexed by integer numbers base, according to the item count of items in the collection.

 

While AcadObjectId is a number (long integer), it is NOT USED to key an item in a collection. That is why your code ran into error on line 38 and 39: you cannot use ObjectId as the index, and the index cannot be greater than the count of the items minus 1.

 

In you case, since your code created the MText (or any AcadEntity, for that matters), your code should hold a reference to it for later operation, if needed. That is:

 

Dim mtextObj1 As AcadMText

Dim mtextObj2 As AcadMText

...

Set mtextObj1=ThisDrawing.ModelSpace.AddMText(.......)

...

Set mtextObj2=ThisDrawing.ModelSpace.AddMText(.......)

...

mtextObj1.Move [pointA], [pointB]

mtextObj1.Update

...

mtextObj2.Delete

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 3

Ed__Jobe
Mentor
Mentor

When you add an entity using ThisDrawing.Modelspace.AddXXX methods, you can only set very few properties. By default, it is added with the "CE" or Create Entity variables, such as CELAYER, What you need to do is get the entity you just added and modify its properties to what you want. See the sample code below.

 


    ' Create a text Object in model space
    Set mtextObj = ThisDrawing.ModelSpace.AddMText(InsertionPoint:=insertPoint, width:=width, Text:=textString)
    With mtextObj
      .Layer = "A-ANNO-EXT"
      .AttachmentPoint = acAttachmentPointTopCenter
      .InsertionPoint =  new3DPoint
      .Width = 2
      .Update
    End With

 

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes