Guest User

Untitled

a guest
Jun 29th, 2024
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. using Autodesk.Revit.Attributes;
  2. using Autodesk.Revit.DB;
  3. using Autodesk.Revit.UI;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Windows.Forms;
  8.  
  9. namespace scripts
  10. {
  11. [Transaction(TransactionMode.Manual)]
  12. public class InvokeRevitCommand : IExternalCommand
  13. {
  14. public Result Execute(
  15. ExternalCommandData commandData,
  16. ref string message,
  17. ElementSet elements)
  18. {
  19. // Get all built-in Revit postable command identifiers
  20. var commandEntries = new List<Dictionary<string, object>>();
  21. var commandIds = new Dictionary<string, RevitCommandId>();
  22.  
  23. // Get all values from PostableCommand enum
  24. foreach (var commandName in Enum.GetNames(typeof(PostableCommand)))
  25. {
  26. var postableCommand = (PostableCommand)Enum.Parse(typeof(PostableCommand), commandName);
  27. var commandId = RevitCommandId.LookupPostableCommandId(postableCommand);
  28.  
  29. if (commandId != null)
  30. {
  31. commandEntries.Add(new Dictionary<string, object> { { "Command", commandName } });
  32. commandIds[commandName] = commandId; // Store the command id
  33. }
  34. }
  35.  
  36. // Use CustomGUIs.DataGrid to let user select a command
  37. List<string> propertyNames = new List<string> { "Command" };
  38. var selectedCommand = CustomGUIs.DataGrid(commandEntries, propertyNames, false).FirstOrDefault();
  39.  
  40. if (selectedCommand != null)
  41. {
  42. string commandName = selectedCommand["Command"].ToString();
  43. RevitCommandId commandId = commandIds[commandName]; // Get the command id
  44.  
  45. try
  46. {
  47. // Invoke the selected command using PostCommand
  48. commandData.Application.PostCommand(commandId);
  49. return Result.Succeeded;
  50. }
  51. catch (Exception ex)
  52. {
  53. message = $"An error occurred: {ex.Message}";
  54. return Result.Failed;
  55. }
  56. }
  57.  
  58. return Result.Failed;
  59. }
  60. }
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment