Calling IExternalCommand from WPF button

Calling IExternalCommand from WPF button

cugelkater
Explorer Explorer
551 Views
4 Replies
Message 1 of 5

Calling IExternalCommand from WPF button

cugelkater
Explorer
Explorer

Hello. I have the class SaveSectionBoxInfo in the IExternalCommand interface. I want to call it through button in the WPF form. There is error CS1729 'ExternalCommandData' does not contain a constructor that takes 0 arguments (marked by comment). I can't find the right argument for 'ExternalCommandData'. How can I do it properly? And is it correct to use the IExternalCommand interface in this case? Thanks for your answers.

 

WPF:

    public partial class BbWPF : Window
    {
        private readonly UIApplication _uiApp;
        public BbWPF(UIApplication uiApp)
        {
            InitializeComponent();
            _uiApp = uiApp;
        }
        public ExternalCommandData commandData { get; set; }

        private void BbWPFsavePos_Click(object sender, RoutedEventArgs e)
        {


            SaveSectionBoxInfo command = new SaveSectionBoxInfo();

// here CS1729  'ExternalCommandData' does not contain a constructor that takes 0 arguments
            ExternalCommandData commandData = new ExternalCommandData();
//

            string message = string.Empty;
            ElementSet elements = new ElementSet();

            Result result = command.Execute(commandData, ref message, elements);

        }

  

Class:

   [Transaction(TransactionMode.Manual)]
   public class BoxMenu : IExternalCommand
   {
       public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
       {
           UIApplication uiApp = commandData.Application;
           BbWPF window = new BbWPF(uiApp); 
           window.ShowDialog(); 
           return Result.Succeeded;
       }
   }
  
   public class SaveSectionBoxInfo : IExternalCommand
   {
       public Result Execute(ExternalCommandData commandData,
         ref string message, ElementSet elements)
       {
           UIApplication uiApp = commandData.Application;
           UIDocument uiDoc = commandData.Application.ActiveUIDocument;
           Document doc = uiDoc.Document;
           View3D view3D = doc.ActiveView as View3D;

           if (view3D != null && view3D.IsSectionBoxActive)
           {
               BoundingBoxXYZ sectionBox = view3D.GetSectionBox();

               XYZ min = sectionBox.Min;
               XYZ max = sectionBox.Max;
               XYZ center = (min + max) / 2;
               XYZ size = max - min;

               TaskDialog.Show("Coords:", $"Center: {center}\nSize: {size}");
           }
           else
           {
               TaskDialog.Show("Error", "Error");
           }

           return Result.Succeeded;
       }
   }

 

0 Likes
552 Views
4 Replies
Replies (4)
Message 2 of 5

jeremy_tammik
Alumni
Alumni

Look at the official Revit SDK sample ModelessDialog/ModelessForm_ExternalEvent. For further info, check out the numerous previous discussions on Idling and External Events for Modeless Access and Driving Revit from Outside:

  

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 5

Moustafa_K
Collaborator
Collaborator

does this answer helps

Moustafa Khalil
Cropped-Sharp-Bim-500x125-Autodesk-1
0 Likes
Message 4 of 5

jeremy_tammik
Alumni
Alumni

Here is yet another (and maybe the most recent) modeless add-in tutorial on ho to create a Revit add-in with a modeless WPF window and XAML, by Sergei Nefedov:

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 5 of 5

cugelkater
Explorer
Explorer

Thanks you all for replies, after endless attempts this one helped:

https://forums.autodesk.com/t5/revit-api-forum/call-class-from-windows-form/td-p/5984297

0 Likes