Table 2: IExternalCommand.Result
Member Name Description
Autodesk.Revit.UI.Result.Succeeded The external command completed successfully. Revit keeps all changes made by the external command.
Autodesk.Revit.UI.Result.Failed
The external command failed to complete the task. Revit reverses operations performed by the external command. If the message
parameter of Execute is set, Revit displays a dialog with the text "Error - cannot be ignored".
Autodesk.Revit.UI.Result.Cancelled
The user cancelled the external command. Revit reverses changes made by the external command. If the message parameter of
Execute is set, Revit displays a dialog with the text "Warning - can be ignored".
The following example displays a greeting message and allows the user to select the return value. Use the Execute() method as the entrance to the Revit application.
Code Region 3-4: Prompting the user
view plaincopy to clipboardprint?
1. public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData,
2. ref string message, ElementSet elements)
3. {
4. try
5. {
6. Document doc = commandData.Application.ActiveUIDocument.Document;
7. UIDocument uidoc = commandData.Application.ActiveUIDocument;
8. // Delete selected elements
9. ICollection<Autodesk.Revit.DB.ElementId> ids =
10. doc.Delete(uidoc.Selection.GetElementIds());
11.
12. TaskDialog taskDialog = new TaskDialog("Revit");
13. taskDialog.MainContent =
14. ("Click Yes to return Succeeded. Selected members will be deleted.\n" +
15. "Click No to return Failed. Selected members will not be deleted.\n" +
16. "Click Cancel to return Cancelled. Selected members will not be deleted.");
17. TaskDialogCommonButtons buttons = TaskDialogCommonButtons.Yes |
18. TaskDialogCommonButtons.No | TaskDialogCommonButtons.Cancel;
19. taskDialog.CommonButtons = buttons;
20. TaskDialogResult taskDialogResult = taskDialog.Show();
21.
22. if (taskDialogResult == TaskDialogResult.Yes)
23. {
24. return Autodesk.Revit.UI.Result.Succeeded;
25. }
26. else if (taskDialogResult == TaskDialogResult.No)
27. {
28. elements = uidoc.Selection.Elements;
29. message = "Failed to delete selection.";
30. return Autodesk.Revit.UI.Result.Failed;
31. }
32. else
33. {
34. return Autodesk.Revit.UI.Result.Cancelled;
35. }
36. }
37. catch
38. {
39. message = "Unexpected Exception thrown.";
40. return Autodesk.Revit.UI.Result.Failed;
41. }
42. }
IExternalCommandAvailability
This interface allows you control over whether or not an external command button may be pressed. The IsCommandAvailable interface method passes the application and
a set of categories matching the categories of selected items in Revit to your implementation. The typical use would be to check the selected categories to see if they meet
the criteria for your command to be run.
In this example the accessibility check allows a button to be clicked when there is no active selection, or when at least one wall is selected:
Code Region 3-5: Setting Command Availability
view plaincopy to clipboardprint?
1. public class SampleAccessibilityCheck : IExternalCommandAvailability
2. {
3. public bool IsCommandAvailable(AutodeskAutodesk.Revit.UI.UIApplication applicationData,
4. CategorySet selectedCategories)
5. {
6. // Allow button click if there is no active selection
7. if (selectedCategories.IsEmpty)
8. return true;
9. // Allow button click if there is at least one wall selected
10. foreach (Category c in selectedCategories)
11. {
12. if (c.Id.IntegerValue == (int)BuiltInCategory.OST_Walls)
13. return true;
14. }
15. return false;
16. }