I have a question about the sequence of events in VB.NET when I'm trying to draw a line and have a message box pop after the line is drawn.
Below is simplified code of what I'm trying to do, just to make things easier to understand. If I can get this to work, I can apply what I've learned to my real application.
In this application, I want the user to pick two Point3Ds where a line will be drawn between the two points (and I'll save the length of the vector). A message box will pop up asking if they want to pick more points, if they click YES, it will loop, if they click NO it will end and the total lengths of all the vectors will be tallied and displayed.
The problem occurs where it does not draw the lines until after I've exited the loop. I realize my myTrans.Commit code is outside of my loop, but when I've tried to move it closer to the .AddNewlyCreatedDBObject line, it crashes out after the first loop.
What I want is (in this order);
a) User picks 2 points
b) A line is drawn
c) The message box pop up asking if the user wants to continue
It is doing it the order a, c, b.
What am I doing wrong here? I am new to VB.NET and AutoCAD.
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD
Public Class DrawLineMsgBox
<CommandMethod("dlmb")> _
Public Sub DrawLineMBox()
'THIS IS A TEST TO PICK PIONTS AND ADD LINE LENGTHS, THEN HAVE THEM DISAPPEAR WHEN THE USER FINISHES THE COMMAND
'USES A MESSAGE BOX TO SEE IF THE LINE IS COMMITTED BEFORE THE MBOX DISPLAYS
ApplicationServices.Application.SetSystemVariable("BLIPMODE", 1) 'ON
Dim myTransMan As DatabaseServices.TransactionManager
Dim myTrans As DatabaseServices.Transaction
Dim myDwg As Document
Dim myBT As BlockTable
Dim myBTR As BlockTableRecord
Dim dblLC As Double = 0 'LINE COUNT
Dim dblLL As Double = 0 'LINE LENGTHS
Dim blExit As Boolean = False
myDwg = ApplicationServices.Application.DocumentManager.MdiActiveDocument
myTransMan = myDwg.TransactionManager
myTrans = myTransMan.StartTransaction
Dim ptPick1 As Geometry.Point3d 'THE FIRST CLICK POINT
Dim ptPick2 As Geometry.Point3d 'THE SECOND POINT (CREATED BY JIG)
myBT = myDwg.Database.BlockTableId.GetObject(DatabaseServices.OpenMode.ForRead)
myBTR = myBT(DatabaseServices.BlockTableRecord.ModelSpace).GetObject(DatabaseServices.OpenMode.ForWrite)
Try
Do Until blExit = True
'SET COUNTER
dblLC = dblLC + 1
'SELECT THE FIRST POINT
ptPick1 = myDwg.Editor.GetPoint("Select FIRST point: ").Value
'SELECT THE SECOND POINT
ptPick2 = myDwg.Editor.GetPoint("Select SECOND point: ").Value
'GET THE VECTOR BETWEEN THE TWO POINTS
Dim myVector As Geometry.Vector3d = ptPick1.GetVectorTo(ptPick2)
'ADD UP LENGTHS
dblLL = dblLL + myVector.Length
'DRAW A LINE IN MODELSPACE
Dim myLine As New DatabaseServices.Line(ptPick1, ptPick2)
myBTR.AppendEntity(myLine)
myTrans.AddNewlyCreatedDBObject(myLine, True)
'SHOW MESSAGE BOX, OK TO ADD, CANCEL TO END
If MsgBox("Add more lines?", MsgBoxStyle.YesNo) = MsgBoxResult.No Then _
blExit = True
Loop
MsgBox("Line lengths total: " & dblLL.ToString)
Catch ex As Autodesk.AutoCAD.Runtime.Exception
MsgBox("Error during AddLinesAndDelete - " & Err.ToString)
Finally
myTrans.Commit()
myTrans.Dispose()
myTransMan.Dispose()
End Try
ApplicationServices.Application.SetSystemVariable("BLIPMODE", 0) 'OFF
End Sub
End Class