How do I allow user to press escape in command (VB.NET)

How do I allow user to press escape in command (VB.NET)

Anonymous
Not applicable
4,723 Views
9 Replies
Message 1 of 10

How do I allow user to press escape in command (VB.NET)

Anonymous
Not applicable

I am using sendcommand to allow users to have access to such functions as INSERT, etc.  I need the user to be able to hit the escape key and it takes them completely out of the function.  How do I go about doing that in VB.NET?

 

Thanks

 

0 Likes
Accepted solutions (1)
4,724 Views
9 Replies
Replies (9)
Message 2 of 10

Balaji_Ram
Alumni
Alumni

Hi Jeff,

 

Did you try these ?

 

http://through-the-interface.typepad.com/through_the_interface/2007/02/allowing_users_.html

 

http://adndevblog.typepad.com/autocad/2012/07/allowing-users-to-escape-from-long-operations-in-autoc...

 

The escape should end the command in AutoCAD automatically.

But your code would still need to recognize another escape to stop sending the next command.

The code from the blog posts should help recognize the key press.

 

Regards,

 

Balaji

 



Balaji
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 3 of 10

Anonymous
Not applicable

I looked at these posts before, but I didn't think it would benefit my issue properly.  I have pasted my code below.  There is an insert command that is generated through the sendcommand.  I understand that the user can ESC out of the actual command, but I have no way of determining if the INSERT command was actually successful.  So, what my code does after the INSERT command is fired is allows the user to select a component.  This would be where I would be able to take advantge of the code for ESC, but the focus is once again passed to the user to select a component.  Where and how would I put the code into the following example?

 

Thanks!

 

Dim doc As Document = Application.DocumentManager.MdiActiveDocument

Dim db As Database = doc.Database

Dim ed As Editor = doc.Editor

Dim app As AcadApplication = Application.AcadApplication

Using doc.LockDocument()


app.ActiveDocument.SendCommand("-insert" & vbCr & Flag1Blk & vbCr & "S" & vbCr & "1" & vbCr)


******** User hit ESC to exit completely out of command, but of course, it just continues executing code

 

Using acTrans As Transaction = db.TransactionManager.StartTransaction()

Dim selectionOpts As PromptSelectionOptions = New PromptSelectionOptions
selectionOpts.MessageForAdding = "Select the System for the flag to be assigned to."

******** This is second point where user could interrupt code. I would need to add code here so that if ESC
******** is pressed again, it would exit the function completely.

Dim acSSPrompt As PromptSelectionResult = doc.Editor.GetSelection(selectionOpts)

If acSSPrompt.Status = PromptStatus.OK Then
Dim acSSet As SelectionSet = acSSPrompt.Value

For Each acSSObj As SelectedObject In acSSet
If Not IsDBNull(acSSObj) Then
Dim acEnt As Entity = acTrans.GetObject(acSSObj.ObjectId, OpenMode.ForRead)

If Not IsDBNull(acEnt) Then
LyrVal = acEnt.Layer
Else
Exit Sub
End If
End If
Next

End If

End Using

0 Likes
Message 4 of 10

norman.yuan
Mentor
Mentor

In your case, if you need a CAD operation/process not only inserting something, but also doing a lot of thing after, which depend on the result of the inserting, then you'd better CODE your own inserting process, so that you have full control to the workflow, including user's cancellation.

 

Using a exasting chunk of process predesigned (SendCommand()/SendStringToExecute()) may simplifies your work, but you have less control to the outcome. As programmer, you need to choose the approach that fits your need. In this particular case, you need to write code to insert, and you can certainly make your insertion look similar or the same as "INSERT" command with the block ghost image changing with user interaction, while having full control to the outcome.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 10

Anonymous
Not applicable

I was looking at going the approach of writing my own INSERT command, but I give up all the functionality of the AutoCAD insert command.  The insertion process has to be the same as regular autocad as the user could place the block in question at any rotation, and it is guaged by their feeling at the moment of placement.  I just didn't want to have to reinvent the wheel.  That is why I want to focus on the escape key capture more than anything else.

 

0 Likes
Message 6 of 10

Anonymous
Not applicable

I need the user to be able to break out of any function fired by my application quickly by hitting the escapre key, so really my only solution is to be able to code the app so that whenever the user hits escape, it brings them back to the main application form.  

0 Likes
Message 7 of 10

Anonymous
Not applicable

I have many functions where I absolutely need to have the ability for the user to hit the escape key.  Take a look at the following code where I put the user into a continuous loop to erase objects off the drawing which then deletes records from a database.  For efficiency (like most of the commands in my application), I need to keep the user always in the function until escape is hit.

 

Dim acdoc As Document = Application.DocumentManager.MdiActiveDocument

Dim db As Database = acdoc.Database

Dim ed As Editor = acdoc.Editor

Dim HandleNm As String
Dim ShortNm As String
Dim TempStr As String
Dim FullDir As String

Using acdoc.LockDocument()

ShortNm = TempStr
Me.Hide()
Dim counterC As Short
Dim msgOut As String
Dim selEntity As Autodesk.AutoCAD.Interop.Common.AcadEntity
Dim rstFlag As New ADODB.Recordset
Dim Handle_Renamed As String
Dim sset As Autodesk.AutoCAD.Interop.AcadSelectionSet
Dim ArraySize As Short
Dim selPts() As Object
Dim Pt As Object

 

Do While counterC < 1000

 

Using acTrans As Transaction = db.TransactionManager.StartTransaction()

'' Request for objects to be selected in the drawing area

Dim selectionOpts As PromptSelectionOptions = New PromptSelectionOptions
selectionOpts.MessageForAdding = "Select the System Flag for deletion."

Dim acSSPrompt As PromptSelectionResult = acdoc.Editor.GetSelection(selectionOpts)

'' 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
If Not IsDBNull(acSSObj) Then

Dim acEnt As Entity = acTrans.GetObject(acSSObj.ObjectId, OpenMode.ForWrite)

If Not IsDBNull(acEnt) Then
If TypeOf (acEnt) Is BlockReference Then
Dim blkDel As BlockReference
blkDel = acEnt
If InStr(1, blkDel.Name, "FLAG") > 0 Then
HandleNm = blkDel.Handle.ToString
'MsgBox(HandleNm)

acEnt.Erase()
acEnt.Dispose()
rstFlag.Open("SELECT * FROM FLAG WHERE HANDLE = '" & HandleNm & "' AND PID = '" & ShortNm & "';", dbs, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
If rstFlag.EOF = False Then
rstFlag.Delete()
rstFlag.Update()
Else
End If
rstFlag.Close()

End If
End If
End If
End If
Next

 

End If
acTrans.Commit()

End Using

 


rstFlag = Nothing

counterC = counterC + 1
Loop

0 Likes
Message 8 of 10

Balaji_Ram
Alumni
Alumni

Hi Jeff,

 

I do not find a way to know if the previous SendCommand was cancelled using Escape key.

As Norman said, you will have less control over the functionality if a series of commands are being sent to complete a task.

 

Coding the functionality using AutoCAD .Net API or ObjectARX C++ API is a better option.

 

There are several blog posts on using Jigs for block insertion in Kean's blog post, AutoCAD Devblog, Swamp and the spiderinnet1 blogs.

You can choose the one that suits your requirements and work with it.

 

Regards,

Balaji



Balaji
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 9 of 10

Anonymous
Not applicable

ok, I am not so worried about the INSERT command escape, but like I said, I have a bunch of functions that I need the user to be able to hit ESCAPE to exit them out of the function they are in and return them to the main form.  In VBA it was automatic.  Why can this not be done easily in .NET?

 

I saw a post about capturing the autocad command line content and looking for the string *Cancel*, but that seems like a weak solution.  I really need to be able to capture the ESCAPE key press.

 

Thanks

0 Likes
Message 10 of 10

norman.yuan
Mentor
Mentor
Accepted solution

I do not believe there is such thing that in VBA pressing Esc would just break a chunk of code execution unless it is specifically designed to.

 

That is, usually, pressing Esc is used when the code execution is waiting for user input, which is implemented with one of the AcadUtiltity.GetXXX() method.

 

It is the same with .NET API: with Editor.GetXXX() method, your can test the PromptResult's status: if Esc is hit, the status is PromptStatus.Cancel. Then your code can do things accordingly: cancelling the code execute, or even let the code continue, you as programmer control it.

 

In .NET, though, the hitting of Esc is handled better: it returns PromptStatus.Cancel, intead of raising an exception in VBA.

 

Bottom line is that as accepted AutoCAD user interaction, Esc is used to cancel the code execution when it is waiting for user input. So, if you write code you should do it the same way as standard AutoCAD interaction all users are familia with

Norman Yuan

Drive CAD With Code

EESignature