Delete mtext objects created by VBA

Delete mtext objects created by VBA

Anonymous
Not applicable
1,363 Views
1 Reply
Message 1 of 2

Delete mtext objects created by VBA

Anonymous
Not applicable

Hi,

 I have created mtext above my block through VBA code. Once created can i delete those mtext using code. Im using that mtext to display some text regarding the block. 

 

Private Sub GetBlockInfo()

Dim ent As AcadEntity
Dim block As AcadBlockReference
Dim vAtts As Variant
Dim StrNo As String
Dim result
Dim I As Long
Dim point1, point2, point3 As Double

For Each ent In ThisDrawing.ModelSpace
If TypeOf ent Is AcadBlockReference Then
Set block = ent
If (block.Name) = "RMNUM" Then
If block.HasAttributes Then
vAtts = block.GetAttributes
For I = LBound(vAtts) To UBound(vAtts) If vAtts(I).TagString = "NUMBER" Then StrNo = vAtts(I).textString End If Next
point1 = block.InsertionPoint(0) point2 = block.InsertionPoint(1) point3 = block.InsertionPoint(2) result = CreateMText(point1, point2, point3, StrNo) End If End If End If Next End Sub Function CreateMText(ByVal x As Double, ByVal y As Double, ByVal z As Double, ByVal str As String) Dim mtextObj As AcadMText Dim insertPoint(0 To 2) As Double Dim width As Double Dim textString As String insertPoint(0) = x - 1 insertPoint(1) = y + 3 insertPoint(2) = z width = 4 textString = "Block number is" & str 'This is sample Set mtextObj = ThisDrawing.ModelSpace.AddMText(insertPoint, width, textString) ZoomAll End Function

Here i chose mtext because the details which im going to display has multiple rows. IS there anyother best thing to display like a popup would be fine.

 

After creating mtext for all blocks, once the user doesn't want these details, then another macro should delete this mtext . As mtext doesn't have 'Name', i cannot search and delete those though VBA. Pls advice me on this.

 

 

0 Likes
1,364 Views
1 Reply
Reply (1)
Message 2 of 2

norman.yuan
Mentor
Mentor

If the entities (MText, in your case) created by your code in a macro execution needs to be erased in the execution of another macro/command, or even another AutoCAD session (i.e. user runs your code that creates those MText, and then user may close the drawing/AutoCAD for lunch and want to erase them when he/she comes back later), then you need to have some way for your code to identify those particular entities when they are created. For example, you could create a temporary layer and only place those entities on that layer. Then when you want to clean things up, simply erase everything on that layer; or you can embed XData to those entities, and later only search drawing for entities with particular XData for erasing; or you can even save thoese entities' Handle to a external file, and later read the file and search entities with these handles for erasing...There could be other ways, but it is completely up to your code to somehow ID the created entities for later operation (erasing, in this case).

 

Judging from your previously posted question, I'd say adding MText entities into drawing for user to see the block information and then erase them after user's viewing isn't very good solution in my opinion. You should use a UserForm to provide a better UI for viewing without mess around entities in drawing; If you only want user to see some text information with simplest presentation, you can even call up AutoCAD's text window and print the text information there. I can imagine based on what you have described previously, a very similar process like AutoCAD command "LIST" would be the simplest solution for you. Once you get more familiar to AutoCAD and its programming, you can move toward a bit more advanced approach. Again, be warned, unless you are ABSOLUTELY sure you only need very simple VBA solution for your business needs, investing too much time to VBA programming may not be worth of the effort.

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes