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.
In this case, let’s create some note into the active view of Revit from a valid image file as specified through drag & drop rather than import it as the standard drag & drop behavior provides. As introduced earlier, if we provide an image file to the Autodesk.Revit.UI.UIApplication.DoDragDrop() call and rely on Revit itself for the drag and drop action, the image no matter PNG or BMP will be imported into the current model, but what if users want to create some note about the image instead?
The Revit .NET Drag & Drop API provides another mechanism to support custom drag & drop behavior. More specifically, another signature of the Autodesk.Revit.UI.UIApplication.DoDragDrop() method and the IDropHandler interface come to rescue.
• Add a RadioButton to the WPF Window and make it the dragging source.
<RadioButton Content="Revit Drag & Drop - Create Note from Image" Height="16" HorizontalAlignment="Left" Margin="12,202,0,0" Name="radioButton1" VerticalAlignment="Top" MouseMove="radioButton1_MouseMove" Tag="C:\temp\sample.png" />
• Implement the MouseMove event callback as follows into the Window1 class to make the WPF element as the drag & drop source, the Revit window as the drag & drop target, and our custom drag & drop handler as the action:
private void radioButton1_MouseMove(object sender, MouseEventArgs e)
{
NoteFromImageDropHandler dropHandler = new NoteFromImageDropHandler();
Autodesk.Revit.UI.UIApplication.DoDragDrop((sender as FrameworkElement).Tag, dropHandler);
}
• Implement the custom IDropHandler interface as follows:
public class NoteFromImageDropHandler : Autodesk.Revit.UI.IDropHandler
{
public void Execute(Autodesk.Revit.UI.UIDocument document, object data)
{
string path = (string)data;
try
{
using (Transaction trans = new Transaction(document.Document, "RevitNoteFromImage"))
{
trans.Start();
System.Drawing.Bitmap img = new System.Drawing.Bitmap(path);
TextNote text = document.Document.Create.NewTextNote(
document.Document.ActiveView,
document.Document.ActiveView.Origin,
new XYZ(1, 0, 0),
new XYZ(0, 0, 1),
2,
TextAlignFlags.TEF_ALIGN_CENTER,
path + string.Format(" has resolution of ({0}, {1}).", img.Width, img.Height));
text.Width = 400;
trans.Commit();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, path);
}
}
}
• Make sure the following code is still in the Execute of the external command class:
Window1 win = new Window1();
win.Show();
That’s about it. After Revit starts up, the command is launched and the WPF window shows up, the radio button which represents an image file can be drag and drop into the Revit drawing area. In case the active view is good to host a TextNote and the image is valid, some note will be created into the current view at its center.
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.