Abort Pickobject()

Abort Pickobject()

basionyecg
Participant Participant
3,942 Views
9 Replies
Message 1 of 10

Abort Pickobject()

basionyecg
Participant
Participant

Hello,

 

Is there a way to abort pickobject() other than the user press ESC.

I am using Pickobject() in a loop in an External Form :

 

while (!user_pressed_stop)
    {
             
        Selection sel = revit.Application.ActiveUIDocument.Selection;
        ElementPickFilter myfilter = new ElementPickFilter();
        Reference pickedRef = null;
        pickedRef = sel.PickObject(ObjectType.Element, myfilter, "Please select an Element");

        Element elem =document.GetElement(pickedRef);
        listelement.Add(elem);
                    
     }

 

I need to stop the "pick object" from waiting the user input when he clicks the stop button,

I even tried to make work around by sending ESC Key to Revit but it doesnot work properly Every time

 

private void buttonstop_Click(object sender, EventArgs e)
        {
            static WindowHandle _hWndRevit = null;
            IntPtr ptr;
            
            Process[] processes = Process.GetProcessesByName("Revit");
                if (0 < processes.Length)
                {
                    ptr = processes[0].MainWindowHandle;
                    _hWndRevit = new WindowHandle(ptr);
                }

            SetForegroundWindow(ptr);
            SendKeys.SendWait("{ESC}");
            
        }

Any Proper way to cancel that Function?

 

 

Regards,

Basiony

0 Likes
3,943 Views
9 Replies
Replies (9)
Message 2 of 10

arnostlobel
Alumni
Alumni

Basiony,

 

please forgive me for being so straightforward, but it is my opinion that you approach is not exactly right and is is potentially misleading (the user, that is). Let me explain: If an action of picking invoked, it prompts the user to select elements and the only actions allowed in the use should be:

  1. selecting an element
  2. pressing the Esc key (to cancel)
  3. pressing a Finish button, if available, in Revit (to finish picking)
  4. pressing a Cancel button, if available, in Revit (to cancel picking)

Most of or all other UI elements should be disable to indicate clear to the user that the above actions are the only ones allowed during the picking context.

 

For that reason I suggest you do following: When you invoke a picking action do disable all controls in your dialog and enable them again only after the picking is finished (through means provided by Revit). This what your users will not have to learn that your application behaves differently than other external applications and differently than Revit too.

 

By the way, I am sure you know that but I mention it anyway: be aware that if the user cancels a picking process, Revit throws an exception back to the API application that invoked the picking.

 

Arnošt Löbel
0 Likes
Message 3 of 10

basionyecg
Participant
Participant

 

Thanks Arnošt for repling.

 

The only reason I need to perform actions in that sequence is that I donot know how many objects the user want to select so, I used a loop to collect all elements the user had selected and to end the loop the user presses Stop button that is when I need to abort the last pickobject.

 

 

 

Regards,

Basiony

0 Likes
Message 4 of 10

stever66
Advisor
Advisor

Can you use PickObjects instead of PickObject?   That lets the user select multiple objects and it provides a "finish" button at the top of the screen the user can press when they are finished selecting objects.

 

0 Likes
Message 5 of 10

basionyecg
Participant
Participant

 

I thought of it but unfortunatly it does not return objects in the same order the user selected which is important in my case.

0 Likes
Message 6 of 10

Anonymous
Not applicable
Place the pickobject inside a try block and set the catch block to only catch the operationcancelled exception.
Place all of that inside a while loop and place a break; statement in the exception handler. When the user hits escape Your loop will exit and you can begin working withe the selected elements
Message 7 of 10

basionyecg
Participant
Participant

 

Thanks Scott for your reply,

 

I already have a similar way to handle when user presses ESC , For usability reasons I still need to give the user the control to stop the loop through the same interface he used to start it to keep things logical.

 

 

Regards,

Basiony

 

 

 

0 Likes
Message 8 of 10

Anonymous
Not applicable

maybe you could just do the same as I suggest and throw your own custom exception when the button is pressed to abort the pickobject command and break the loop. I do a similar thing with my progress bar to abort long processes.

Message 9 of 10

jlpgy
Advocate
Advocate

Hello:

It has been more than 2 years since your posting this article, I'm not sure if your are gonna read this.

Still hope I can help.

Here are my thoughts about this question:

First thing is to set focus on the revit main window.

  1. Add reference to your Visual Studio project: AdWindows.dll. It is in the same directory as the Revit.exe is in.
  2. Using Autodesk.Windows namespace
  3. Make the following call as illustrated:图像 1.png
  4. We'll be using this IntPtr later. For now, let's turn to get some help from Windows API. Declare some methods as illustrated:图像 2.png
  5. The first method is used to set focus on a window; the second one is used to send virtual KEY BOARD signal to windows. (I think, by now, you have already realized what to do next. Yes, focus Revit window, and send ESC down, then ESC up):图像 3.png
  6. Ignore the blue words in the above picture. I declared those methods in another class 🙂 . So far, now we are able to focus on Revit window, and send ESC PRESSED (with an ESC down followed by an ESC up)

 

I'm afraid that this still cannot help you to reach your aim, but, at least it helps us to abort PickObject()

 

🙂

 

 

 

 

 

单身狗;代码狗;健身狗;jolinpiggy@hotmail.com
Message 10 of 10

mgp
Observer
Observer

Many thanks for your solution! It works perfectly!

0 Likes