PostCommand & Modal dialog

PostCommand & Modal dialog

minet.axel
Enthusiast Enthusiast
1,183 Views
3 Replies
Message 1 of 4

PostCommand & Modal dialog

minet.axel
Enthusiast
Enthusiast

Hi

 

I want to use the Revit PostCommand through a button that is in a window (ShowDialog), because I want to keep Revit locked. But the problem is that the PostCommand only launches when my form is closed.

 

I had already done this using a While:
I create the form
I give him the datacontext
I am using a CommandToRun property on my ViewModel to determine what to do.
At the click of a button, I change my CommandToRun property and I close the form.
And then I determine whether or not to quit the while.

But the problem here is that the postcommand does not launch, because I open a form directly after clicking on the button.

 

public bool? ShowSeparationByZoneModalForm()
        {
            bool? res = null;
            Boolean reallyExit = false;
            while (!reallyExit)
            {
                // form.PopulateFormData();
                if (viewModel.CommandToRun == RunCommand.None)
                {
                    SeparationByZoneDialog form = new SeparationByZoneDialog();
                    form.Helper().Owner = RevitWindowHelper.GetRevitHandle();
                    form.DataContext = viewModel;
                    res = form.ShowDialog();
                    switch (viewModel.CommandToRun)
                    {
                        case RunCommand.DrawLines:
                            {
                                App.DocumentChanged += OnDocumentChanged;
                                UIApp.Idling += OnIdling;
                                RevitCommandId modelLineCmd = RevitCommandId.LookupPostableCommandId(PostableCommand.ModelLine);
                                UIApp.PostCommand(modelLineCmd);
                            }
                            break;
                        case RunCommand.SelectLines:
                            {
                                viewModel.CommandToRun = RunCommand.None;
                                var uidoc = new UIDocument(_doc);
                                Selection choices = uidoc.Selection;
                                try
                                {
                                    Reference hasPickOne = choices.PickObject(ObjectType.Element);
                                    if (hasPickOne != null)
                                        viewModel.LineCounter += 1;
                                }
                                catch (Autodesk.Revit.Exceptions.OperationCanceledException oce)
                                { }
                            }
                            break;
                        case RunCommand.None:
                            reallyExit = true;
                            break;
                        default:
                            reallyExit = true;
                            break;
                    }
                }
            }
            return res;
        }

 

This method works great for making element selections in Revit keeping the window blocking but with postcommands it doesn't work.

0 Likes
Accepted solutions (1)
1,184 Views
3 Replies
Replies (3)
Message 2 of 4

dreamofbaby0507
Contributor
Contributor

I'm not good at English! 🙂

You can refer to using template to work with revit api.

Link : https://github.com/imAliAsad/VisualStudioRevitTemplate

Hope to help you!!

0 Likes
Message 3 of 4

RPTHOMAS108
Mentor
Mentor
Accepted solution

PostCommand is done after your context has finished. So can't be used for your scenario.

 

If all you need to do is draw a line or select lines from the same modal dialogue then the easiest way would be to reshow the same dialogue in-between other API methods (not post command) e.g.

 

UIDocument.Selection.PickPoint x 2 & Document.Create.NewModelCurve

UIDocument.Selection.PickObjects

 

Otherwise you need to implement IExternalEventHandler with modeless dialogue.

0 Likes
Message 4 of 4

minet.axel
Enthusiast
Enthusiast

I want to use a post command. so i modified my form to use an IExternalEventHandler. thank you for the information.

0 Likes