Author Topic: VB.NET - Sequence of events question - Draw Line Commit vs Message Box  (Read 4045 times)

0 Members and 1 Guest are viewing this topic.

gablackburn

  • Guest
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.

Code: [Select]
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

n.yuan

  • Bull Frog
  • Posts: 348
Re: VB.NET - Sequence of events question - Draw Line Commit vs Message Box
« Reply #1 on: October 21, 2011, 01:33:07 PM »
I'd be very surpised that your code would get passed through compiling/building, let alone running through the loop for creating multiple lines and see them after you exit the loop.

Editor.GetPoint() returns PromptPointResult object, not a Point3d struct.

Yes, you'd better start transaction within the loop for creating each line. You only start a new Transaction once 2 points are picked correctly, and commit the transaction when a line is added into the current space. You do not hang on a transaction between user interactions, which could be seconds/minutes/hours, or the user may go home and never back.

Another thing is that showing a MessageBox after each line being created is not an AutoCAD style user interaction. The better approach (and more AutoCAD built-in like approach) would be to use PromptPointOptions.Keywords to give user other option besides picking the start point. If user pick a keywork option for quit rather than pick point, then you stop the loop. Of course, when you use PromptPointOptions, you can also check if user pressed "Esc" for exiting or not.

Here is some code I put together quickly:

Code: [Select]
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Runtime


Public Class Class1

    <CommandMethod("MyLines")> _
    Public Sub CreateLine()

        Dim dwg As Document = Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = dwg.Editor

        Dim startPT As Point3d
        Dim endPT As Point3d

        While True

            Dim opt As PromptPointOptions
            Dim res As PromptPointResult

            ''Get start point
            opt = New PromptPointOptions(vbCr & "Start point: ")
            opt.Keywords.Add("Exit")
            opt.AppendKeywordsToMessage = True
            res = ed.GetPoint(opt)
            If res.Status = PromptStatus.OK Then
                startPT = res.Value
            Else
                Exit While
            End If

            ''Get start point
            opt = New PromptPointOptions(vbCr & "End point: ")
            opt.Keywords.Add("Exit")
            opt.AppendKeywordsToMessage = True
            opt.UseBasePoint = True
            opt.UseDashedLine = True
            opt.BasePoint = startPT
            res = ed.GetPoint(opt)
            If res.Status = PromptStatus.OK Then
                endPT = res.Value
            Else
                Exit While
            End If

            DrawLine(startPT, endPT, dwg)
            ed.UpdateScreen()

        End While

    End Sub

    Private Sub DrawLine(ByVal pt1 As Point3d, ByVal pt2 As Point3d, ByVal dwg As Document)
        Using tran As Transaction = dwg.Database.TransactionManager.StartTransaction
            Dim l As Line = New Line(pt1, pt2)
            l.SetDatabaseDefaults(dwg.Database)
            Dim space As BlockTableRecord = tran.GetObject(dwg.Database.CurrentSpaceId, OpenMode.ForWrite)
            space.AppendEntity(l)
            tran.AddNewlyCreatedDBObject(l, True)
            tran.Commit()
        End Using
    End Sub

End Class
« Last Edit: October 21, 2011, 01:50:18 PM by n.yuan »

gablackburn

  • Guest
Re: VB.NET - Sequence of events question - Draw Line Commit vs Message Box
« Reply #2 on: October 21, 2011, 05:51:03 PM »
n.yuan,

Thank you for your response but I think you may have misunderstood my question.  It's specifically regarding the order of operations for the draw line.

I understand that what I requested may not be complying to standard AutoCAD style user interaction, but I prefer to use dialog boxes than command line prompts for our users, especially with the application which I'm working on.  As I said, what I posted was just test code, and I'd prefer not to muddy the waters with going through the explanation of what I really need.  If we can get this to work, I'm golden.

My main goal here is to get CAD to draw the line before displaying the message box.  Try this yourself, add the code MsgBox("Hello World") after the line is supposedly drawn in either CreateLine or DrawLine.  At no time does the line get drawn before the message box is displayed.

Quote
I'd be very surpised that your code would get passed through compiling/building, let alone running through the loop for creating multiple lines and see them after you exit the loop.

Thanks for the constructive criticism of my code, as I mentioned, I'm new to this and any help I get is appreicated.  Surprisingly, the code I posted actually works as-is, so with AutoCAD being as forgiving as it is, unfortunately I can get away with my coding.

Jeff H

  • Needs a day job
  • Posts: 6151
Re: VB.NET - Sequence of events question - Draw Line Commit vs Message Box
« Reply #3 on: October 21, 2011, 08:06:51 PM »
Norman's reply and code show you how to do it.
 
As he said put the transaction in the loop, commit, and call Editor.Update
 
Here is one that
a) Ask for 2 points(exits if esc or 'Enter' is pressed then shows total Length if greater than 0)
b) The Line is printed to the screen
c) Shows Message box If want to add another line
--If Yes Loops
--If No shows total length of all lines
Code: [Select]
        <CommandMethod("dlmb")> _
        Public Sub DrawLineMBox()
 
            Dim db As Database = HostApplicationServices.WorkingDatabase
            Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
 
            Dim totalLength As Double = 0
 
            While True
 
                Dim ppo As New PromptPointOptions(Environment.NewLine & "Pick First Point")
                ppo.AllowNone = True
 
                Dim ppr As PromptPointResult = ed.GetPoint(ppo)
 
                If (Not ppr.Status = PromptStatus.OK) Or (ppr.Status = PromptStatus.None) Then
                    Exit While
                End If
 
                Dim startPnt As Point3d = ppr.Value
                ppo.UseDashedLine = True
 
                ppo.UseBasePoint = True
                ppo.BasePoint = startPnt
                ppr = ed.GetPoint(ppo)
 
                If (Not ppr.Status = PromptStatus.OK) Or (ppr.Status = PromptStatus.None) Then
                    Exit While
                End If
 
                Dim endPnt As Point3d = ppr.Value
 
                Using trx As Transaction = db.TransactionManager.StartTransaction()

                    Dim lne As New Line(startPnt, endPnt)
                    totalLength += lne.Length

                    Dim currentSpaceBtr As BlockTableRecord = trx.GetObject(db.CurrentSpaceId, OpenMode.ForWrite)
                    currentSpaceBtr.AppendEntity(lne)

                    trx.AddNewlyCreatedDBObject(lne, True)
                    trx.Commit()

                End Using
 
                ed.UpdateScreen()
 
                If MsgBox("Add more lines?", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
                    Exit While
                End If
 

            End While
 
            If totalLength > 0 Then
                MsgBox("Line lengths total: " & totalLength.ToString)
            End If

        End Sub


gablackburn

  • Guest
Re: VB.NET - Sequence of events question - Draw Line Commit vs Message Box
« Reply #4 on: October 24, 2011, 12:24:41 PM »
Jeff H, thanks for the reply and the sample code.

Quote
Norman's reply and code show you how to do it.
As he said put the transaction in the loop, commit, and call Editor.Update

My apologies to both you and Norman for being dense.  On Friday afternoon, I thought I played around with it enough that I did put the transaction in the loop in one of my many attempts, obviously I did not.

Jeff, your sample code is doing exactly what I was looking for, thanks again.  I will dissect it to improve my coding.

You both introduced me to .UseDashedLine so that's great, I thought I was going to have to use a jig to get that (my other examples from last week actually used a jig, but I still wasn't getting the message box after the line was drawn).

Thanks again all,
George

EDIT: Changed .UseBasePoint to .UseDashedLine and fixed my grammar in the 2nd sentence
« Last Edit: October 25, 2011, 10:39:21 AM by gablackburn »

Jeff H

  • Needs a day job
  • Posts: 6151
Re: VB.NET - Sequence of events question - Draw Line Commit vs Message Box
« Reply #5 on: October 24, 2011, 12:32:36 PM »
Glad you got something working!! :)