Posts: 17
Registered: ‎09-16-2011
Message 1 of 4 (445 Views)
Accepted Solution

Moving text within an AutoCAD drawing

445 Views, 3 Replies
09-16-2011 09:31 AM


I am, as of now, trying to learn the VB.NET programming for AutoCAD applications from scratch and I have done a few tutorials and labs from DevTV and googling :smileyhappy: 

The question I wanted to ask the devs out there is that, is there any way or a funtion to move a text object within a layer to a different geometric location? I was thinking it could be accomplished by translating the text or offsetting it. If someone could guide me in the right direction, it would be great help. 


And sorry if I mistakingly did not observe forum rules or something :smileyhappy:


Thanks in advance! 

*Expert Elite*
Posts: 1,285
Registered: ‎06-21-2004
Message 2 of 4 (436 Views)

Re: Moving text within an AutoCAD drawing

09-16-2011 10:59 AM in reply to: hassanhafeez33

You can move the Text by Changing it's position properties


Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput

Public Class Class2

    <CommandMethod("MoveText")> _
    Public Sub MoveText()
        Using db As Database = HostApplicationServices.WorkingDatabase()
            Using tr As Transaction = db.TransactionManager.StartTransaction()
                Dim myDWG As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
                Using lock As DocumentLock = myDWG.LockDocument

                    Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices. _

                    Dim sOptions As New PromptEntityOptions(vbCr & "Select Text: ")
                    Dim sRes As PromptEntityResult = Nothing

                    While (True)
                        ' Select Text
                        sRes = ed.GetEntity(sOptions)
                        If sRes.Status <> PromptStatus.OK Then
                            Exit While
                            Dim obj As Object = tr.GetObject(sRes.ObjectId, OpenMode.ForRead)
                            If TypeOf obj Is DBText Then
                                Dim objdbTEXT As DBText = tr.GetObject(obj.objectid, OpenMode.ForWrite)
                                ' Move Text to Position
                                Dim POINT3D As New Autodesk.AutoCAD.Geometry.Point3d(0, 0, 0)
                                objdbTEXT.Position = POINT3D
                                ' Update Changes
                            End If
                        End If
                    End While
                End Using
                ' Save Changes
            End Using
        End Using
    End Sub
End Class



(defun botsbuildbots() (botsbuildbots))
Posts: 17
Registered: ‎09-16-2011
Message 3 of 4 (427 Views)

Re: Moving text within an AutoCAD drawing

09-16-2011 12:33 PM in reply to: arcticad

Thanks Arcticad, Thats a bit much of a code for me. I havent had the chance to see this many fuctions yet. 


Ive got a couple more of requests. Could you comment the code that you wrote as well?

and here is another bit of code I am trying to use but for some reason, the objects in the other two layers get green colour inspite of the inner transaction running on the condition of layer 0 selection. 


<CommandMethod("SelectObjectsOnscreen")> _
        Public Sub SelectObjectsOnscreen()
            ' Get the current document and database
            Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
            Dim acCurDb As Database = acDoc.Database
            ' Start a transaction
            Using acTrans1 As Transaction = acCurDb.TransactionManager.StartTransaction()
                ' Open the Layer table for read
                Dim acLyrTbl As LayerTable
                acLyrTbl = acTrans1.GetObject(acCurDb.LayerTableId, _
                Dim sLayerName As String = "0"
                If acLyrTbl.Has(sLayerName) = True Then
                    Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

                        ' Request for objects to be selected in the drawing area
                        Dim acSSPrompt As PromptSelectionResult = acDoc.Editor.GetSelection()
                        ' If the prompt status is OK, objects were selected
                        If acSSPrompt.Status = PromptStatus.OK Then
                            Dim acSSet As SelectionSet = acSSPrompt.Value

                            ' Step through the objects in the selection set
                            For Each acSSObj As SelectedObject In acSSet
                                'Check to make sure a valid SelectedObject object was returned
                                If Not IsDBNull(acSSObj) Then
                                    ' Open the selected object for write
                                    Dim acEnt As Entity = acTrans.GetObject(acSSObj.ObjectId, _
                                    If Not IsDBNull(acEnt) Then
                                        ' Change the object's color to Green
                                        acEnt.ColorIndex = 3
                                    End If
                                End If
                            If sLayerName = "0" Then
                                ' Save the new object to the database
                            End If
                        End If
                    End Using
                    ' Dispose of the transaction
                End If


            End Using
        End Sub



Active Contributor
Posts: 42
Registered: ‎11-06-2012
Message 4 of 4 (256 Views)

Re: Moving text within an AutoCAD drawing

11-11-2012 10:55 PM in reply to: hassanhafeez33

Hello Pro,

How to select block and put them by group with layer or tag...on form.

Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.