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.
In this case, let’s import an AutoCAD drawing (.DWG file) into the current Revit document 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 a ComboBox element along with a ComboBoxItem to the WPF Window and make the ComboBoxItem as dragging source.
<ComboBox Height="23" HorizontalAlignment="Left" Margin="12,106,0,0" Name="comboBox1" VerticalAlignment="Top" Width="254" SelectedIndex="0">
<ComboBoxItem Name="cbItem1" Tag="C:\temp\1.dwg" MouseMove="comboBox1_MouseMove" >Revit Drag and Drop - Import DWG</ComboBoxItem>
</ComboBox>
• Implement the MouseMove event callback as follows into the Window1 class to make the WPF ComboBoxItem as the drag & drop source but the Revit window as the drag & drop target:
private void comboBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
List<String> pathes = new List<String> { (sender as ComboBoxItem).Tag as string };
Autodesk.Revit.UI.UIApplication.DoDragDrop(pathes);
}
}
• Make sure the following code is still there in the Execute() of the external command:
Window1 win = new Window1();
win.Show();
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 DWG 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 DWG ComboBoxItem drag & drop into the Revit drawing area, the DWG 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.