.NET

Reply
Active Contributor
matt.worland
Posts: 35
Registered: ‎11-13-2006
Message 1 of 4 (285 Views)
Accepted Solution

Add text in nested transaction

285 Views, 3 Replies
10-09-2013 12:09 PM

I am fairly new to .net and Im trying to figure out what I can do differently in my code to have it show the newly placed text immediately after choosing an insertion point. Im guessing it has to do with a nested transaction and my loop, but Im not sure how to fix.

 

Also, anything else to optimize in the code, please feel free to pick apart.

 

The code below is to label the layer of a selected entity. If in paperspace, label in paperspace and if in modelspace label in modelspace. Ive been testing this on the "blocks_and_tables_-_imperial.dwg"

 

Thanks,

matt

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

Public Class Class2

    Public Function myAnnotate(myDbIn As Database, mySpace As String, myTextContents As String, mySTPT As Point3d) As ObjectId
        Try
            Using myTrans As Transaction = myDbIn.TransactionManager.StartTransaction
                'Dim myStartPt As Point3d = DocumentManager.MdiActiveDocument.Editor.GetPoint("Pick insertion point: ").Value
                Dim myBT As BlockTable = myDbIn.BlockTableId.GetObject(OpenMode.ForRead)
                Dim myInSpace As BlockTableRecord
                Dim myMText As New MText()
                myMText.Location = mySTPT
                If mySpace = "MODEL" Then
                    myInSpace = myBT(BlockTableRecord.ModelSpace).GetObject(OpenMode.ForWrite)
                Else
                    myInSpace = myBT(BlockTableRecord.PaperSpace).GetObject(OpenMode.ForWrite)
                End If
                myMText.Contents = myTextContents
                myMText.TextHeight = 0.0625
                myMText.Attachment = AttachmentPoint.BottomLeft
                myMText.BackgroundFill = True
                myInSpace.AppendEntity(myMText)
                myTrans.AddNewlyCreatedDBObject(myMText, True)
                myTrans.Commit()
            End Using
        Catch ex As System.Exception
        End Try
    End Function


    <CommandMethod("LayAnno")> _
    Public Sub LayAnno()
        Dim myDoc As Document = Application.DocumentManager.MdiActiveDocument
        Dim myDb As Database = myDoc.Database
        Dim myEd As Editor = myDoc.Editor
        Dim myTr As Transaction = myDb.TransactionManager.StartTransaction
        Using myTr
            Dim mybt As BlockTable = myTr.GetObject(myDb.BlockTableId, OpenMode.ForRead)
            Dim mybtr As BlockTableRecord = myTr.GetObject(myDb.CurrentSpaceId, OpenMode.ForRead)
            Dim myCSFlag As String
            If myDb.TileMode Then
                myCSFlag = "MODEL"
            ElseIf myDb.CurrentSpaceId = SymbolUtilityServices.GetBlockModelSpaceId(myDb) Then
                myCSFlag = "MSPACE"
            Else
                myCSFlag = "PAPER"
                Dim vp As Viewport = TryCast(myTr.GetObject(myEd.CurrentViewportObjectId, OpenMode.ForRead), Viewport)
                myEd.SwitchToModelSpace()
            End If
            Dim myContFlag As Boolean = True
            Do While myContFlag = True
                Dim mycwPEO As New PromptEntityOptions("Select an entity to annotate:")
                Dim mycwPER As PromptEntityResult = myEd.GetEntity(mycwPEO)
                Select Case mycwPER.Status
                    Case EditorInput.PromptStatus.OK
                        Dim myEnt2 As Entity = mycwPER.ObjectId.GetObject(OpenMode.ForRead)
                        Dim myppo As PromptPointOptions = New PromptPointOptions(vbLf & "Select a Point: ")
                        Dim myppr As PromptPointResult
                        If myCSFlag = "MODEL" Then
                            myppr = myEd.GetPoint(myppo)
                        Else
                            myEd.SwitchToPaperSpace()
                            myppr = myEd.GetPoint(myppo)
                            myEd.SwitchToModelSpace()
                        End If
                        Dim myVal As String = myEnt2.Layer.ToString
                        If myppr.Status <> PromptStatus.OK Then Exit Sub
                        Dim myStartPt As Point3d = myppr.Value
                        myAnnotate(myDb, myCSFlag, myVal, myStartPt)
                        myContFlag = True

                    Case EditorInput.PromptStatus.Cancel
                        myEd.WriteMessage("You cancelled.")
                        myContFlag = False
                    Case EditorInput.PromptStatus.Error
                        myContFlag = False
                        MsgBox("Error warning.")
                End Select
            Loop
            If myCSFlag = "MODEL" Then
            Else
                myEd.SwitchToPaperSpace()
            End If
            myTr.Commit()
        End Using
    End Sub
End Class

 

matt worland

On the quick glance you have to force

TransactionManager methods,

see marked (as <-- addedd) lines inline:

 <CommandMethod("LayAnno")> _
Public Sub LayAnno()
Dim myDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim myDb As Database = myDoc.Database
Dim myEd As Editor = myDoc.Editor
myDoc.TransactionManager.EnableGraphicsFlush(True) '<-- added
Dim myTr As Transaction = myDb.TransactionManager.StartTransaction
Using myTr
Dim mybt As BlockTable = myTr.GetObject(myDb.BlockTableId, OpenMode.ForRead)
Dim mybtr As BlockTableRecord = myTr.GetObject(myDb.CurrentSpaceId, OpenMode.ForRead)
Dim myCSFlag As String
If myDb.TileMode Then
myCSFlag = "MODEL"
ElseIf myDb.CurrentSpaceId = SymbolUtilityServices.GetBlockModelSpaceId(myDb) Then
myCSFlag = "MSPACE"
Else
myCSFlag = "PAPER"
Dim vp As Viewport = TryCast(myTr.GetObject(myEd.CurrentViewportObjectId, OpenMode.ForRead), Viewport)
myEd.SwitchToModelSpace()
End If
Dim myContFlag As Boolean = True
Do While myContFlag = True
Dim mycwPEO As New PromptEntityOptions("Select an entity to annotate:")
Dim mycwPER As PromptEntityResult = myEd.GetEntity(mycwPEO)
Select Case mycwPER.Status
Case EditorInput.PromptStatus.OK
Dim myEnt2 As Entity = mycwPER.ObjectId.GetObject(OpenMode.ForRead)
Dim myppo As PromptPointOptions = New PromptPointOptions(vbLf & "Select a Point: ")
Dim myppr As PromptPointResult
If myCSFlag = "MODEL" Then
myppr = myEd.GetPoint(myppo)
Else
myEd.SwitchToPaperSpace()
myppr = myEd.GetPoint(myppo)
myEd.SwitchToModelSpace()
End If
Dim myVal As String = myEnt2.Layer.ToString
If myppr.Status <> PromptStatus.OK Then Exit Sub
Dim myStartPt As Point3d = myppr.Value
myAnnotate(myDb, myCSFlag, myVal, myStartPt)
myTr.TransactionManager.QueueForGraphicsFlush() '<-- added
myContFlag = True
Case EditorInput.PromptStatus.Cancel
myEd.WriteMessage("You cancelled.")
myContFlag = False
Case EditorInput.PromptStatus.Error
myContFlag = False
MsgBox("Error warning.")
End Select
Loop
If myCSFlag = "MODEL" Then
Else
myEd.SwitchToPaperSpace()
End If
myDoc.TransactionManager.FlushGraphics() '<-- added
myTr.Commit()
End Using
End Sub

 

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 4 (274 Views)

Re: Add text in nested transaction

10-09-2013 01:18 PM in reply to: matt.worland

On the quick glance you have to force

TransactionManager methods,

see marked (as <-- addedd) lines inline:

        <CommandMethod("LayAnno")> _
        Public Sub LayAnno()
            Dim myDoc As Document = Application.DocumentManager.MdiActiveDocument
            Dim myDb As Database = myDoc.Database
            Dim myEd As Editor = myDoc.Editor
            myDoc.TransactionManager.EnableGraphicsFlush(True) '<--  added
            Dim myTr As Transaction = myDb.TransactionManager.StartTransaction
            Using myTr
                Dim mybt As BlockTable = myTr.GetObject(myDb.BlockTableId, OpenMode.ForRead)
                Dim mybtr As BlockTableRecord = myTr.GetObject(myDb.CurrentSpaceId, OpenMode.ForRead)
                Dim myCSFlag As String
                If myDb.TileMode Then
                    myCSFlag = "MODEL"
                ElseIf myDb.CurrentSpaceId = SymbolUtilityServices.GetBlockModelSpaceId(myDb) Then
                    myCSFlag = "MSPACE"
                Else
                    myCSFlag = "PAPER"
                    Dim vp As Viewport = TryCast(myTr.GetObject(myEd.CurrentViewportObjectId, OpenMode.ForRead), Viewport)
                    myEd.SwitchToModelSpace()
                End If
                Dim myContFlag As Boolean = True
                Do While myContFlag = True
                    Dim mycwPEO As New PromptEntityOptions("Select an entity to annotate:")
                    Dim mycwPER As PromptEntityResult = myEd.GetEntity(mycwPEO)
                    Select Case mycwPER.Status
                        Case EditorInput.PromptStatus.OK
                            Dim myEnt2 As Entity = mycwPER.ObjectId.GetObject(OpenMode.ForRead)
                            Dim myppo As PromptPointOptions = New PromptPointOptions(vbLf & "Select a Point: ")
                            Dim myppr As PromptPointResult
                            If myCSFlag = "MODEL" Then
                                myppr = myEd.GetPoint(myppo)
                            Else
                                myEd.SwitchToPaperSpace()
                                myppr = myEd.GetPoint(myppo)
                                myEd.SwitchToModelSpace()
                            End If
                            Dim myVal As String = myEnt2.Layer.ToString
                            If myppr.Status <> PromptStatus.OK Then Exit Sub
                            Dim myStartPt As Point3d = myppr.Value
                            myAnnotate(myDb, myCSFlag, myVal, myStartPt)
                            myTr.TransactionManager.QueueForGraphicsFlush() '<--  added
                            myContFlag = True

                        Case EditorInput.PromptStatus.Cancel
                            myEd.WriteMessage("You cancelled.")
                            myContFlag = False
                        Case EditorInput.PromptStatus.Error
                            myContFlag = False
                            MsgBox("Error warning.")
                    End Select
                Loop
                If myCSFlag = "MODEL" Then
                Else
                    myEd.SwitchToPaperSpace()
                End If
                myDoc.TransactionManager.FlushGraphics() '<--  added
                myTr.Commit()
            End Using
        End Sub

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Contributor
matt.worland
Posts: 35
Registered: ‎11-13-2006
Message 3 of 4 (262 Views)

Re: Add text in nested transaction

10-09-2013 02:01 PM in reply to: Hallex

Thank you, that works nicely.

matt worland
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 4 (259 Views)

Re: Add text in nested transaction

10-09-2013 02:05 PM in reply to: matt.worland

Glad to hear that, friend

Cheers :smileyhappy:

Oleg

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

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