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
Solved! Go to Solution.
Solved by Hallex. Go to Solution.
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
Thank you, that works nicely.
Glad to hear that, friend
Cheers 🙂
Oleg
Can't find what you're looking for? Ask the community or share your knowledge.