Revit .NET has provided the Drag & Drop API (UIApplication.DoDragDrop) since version 2013. In this series of posts, we are going to explore the Revit Drag & Drop .NET API (UIApplication.DoDragDrop) case by case.
We loaded a family (.RFA) such as a column into the current document through drag & drop from a WPF label element, opened a project (.RVT) file as well through drag & drop but from a WPF button, and loaded all good families from a folder through drag & drop but from a WPF CheckBox element in previous cases. We also imported an AutoCAD drawing (.DWG file) into the current Revit document through drag & drop an item (ComboBoxItem) of a WPF combo box (ComboBox).
In this case, let’s import an image (.PNG) also through drag & drop an item (ComboBoxItem) of a WPF combo box (ComboBox).
• Make sure the WPF Window UI and class are still there in the project.
• Add one more ComboBoxItem to the ComboBox of the same WPF Window and make the ComboBoxItem as dragging source.
<ComboBoxItem Name="cbItem2" Tag="C:\temp\sample.png" MouseMove="comboBox1_MouseMove" >Revit Drag and Drop - Import PNG</ComboBoxItem>
<ComboBoxItem Name="cbItem8" Tag="C:\temp\sample.jpg" MouseMove="comboBox1_MouseMove" >Revit Drag and Drop - Import JPG</ComboBoxItem>
• Make sure the comboBox1_MouseMove callback is still there. No need to change anything to it!
• Make sure the following code is still there in the Execute() of the external command:
Window1 win = new Window1();
The check for whether the left mouse button is pressed is done here in the MouseMove callback; otherwise it might be impossible to select the item or do any interactions to the ComboBoxItem due to the endless message loop. It is a good practice to do the same for other static WPF elements such as Label, Button, and CheckBox, which were demonstrated before, even if they don’t provide much interaction capability.
By the way, the drag & drop behavior here (default or standard) is provided by the UIApplication.DoDragDrop call automatically. That is, if the PNG is good as specified in the drag & drop data, it will be imported into the current Revit document directly.
That is about it. If we press the F5 key to give it a try in Revit 2014, as chosen during the project creation time by Revit .NET Addin Wizard (RevitNetAddinWizard), we will get the assembly loaded and the command registered properly by Revit. After the command being issued, the WPF window will appear.
After the PNG ComboBoxItem drag & drop into the Revit drawing area, the PNG file as specified by the tag of the current ComboBoxItem will be imported into the current document.
Revit Addin Wizard (RevitAddinWizard) provides various wizards, coders and widgets to help program Revit addins. It can be downloaded from the Download link at the bottom of the blog index page.