Drag and drop event

Hi, guys and gals,

We are all familiar with the ease of use when it comes to dragging and dropping a file into Grasshopper canvas. For the supported file types it is wonderful, but when the file type is not supported we have to point to the location of the file and this is a bit cumbersome.

I am adapting a console app to work as a GH component and this drag and drop ability would do the trick for me.
Basically, the console app parses a file, reformats and pipes all the data to make it more manageable downstream. The file is in essence a .txt file, but it requires saving with a specific extension for standards compatibility.

What would be the best way to make this drag’n’drop event happen seamlessly in GH, with the creation of a component representation on the canvas, much like the Read File component?

Thank you, my friends! :slight_smile:

I’m not sure it is possible to override the drop behaviour, I’ll need to look at the source tomorrow.

In case it’s not possible to create a seamless drag and drop behaviour, what would be the next least cumbersome alternative, any ideas?

I have it working so far by concatenating a present working directory with a file name. Makes it bearable, but far from ideal when the pwd keeps changing.

To be clear, you want to drop a file with some curious extension onto an empty part of the canvas and instantiate a component at that spot?

You can override the dropping of files onto existing components by implementing the IGH_FileDropTarget interface.

I’ll take a look. Thanks, David!

I don’t think it’s possible to handle a drop if there’s no component for a file to be dropped on. The image sampler and read file components are handled specially by the canvas.

I guess you could try to subscribe to the canvas DragDrop event, but then you’d be handling it in parallel with Grasshopper, which could lead to problems. I have no idea what happens when a drop is handled more than once.

Handling it twice does actually work, but you’ll get that “I don’t know what you want me to do” error message.

Put the following into a C# script component:

  private void RunScript(object x, object y, ref object A)
    Instances.ActiveCanvas.DragDrop -= CanvasDragDrop;
    Instances.ActiveCanvas.DragDrop += CanvasDragDrop;

  // <Custom additional code> 
  private void CanvasDragDrop(object sender, DragEventArgs e)
    if (!e.Data.GetDataPresent(DataFormats.FileDrop)) return;

    var files = e.Data.GetData(DataFormats.FileDrop) as string[];
    if (files == null || files.Length == 0) return;

    foreach (var file in files)
      RhinoApp.WriteLine(file + " dropped onto canvas.");

The “I don’t know what you want me to do” message unfortunately is a bit of a deal breaker.

I surmise that with the aim of expanding the scope of the software with an ever increasing number of add-ons, this could be a worthy enough functionality to undergo further development, right?. :wink:

Yeah this can be made better. Initially I thought that pretty much all plug-in functionality would be provided through components, but that has proven to be a faulty assumption.

Here’s a better version. Just remember you’re playing with fire. This approach will specifically remove the default dragdrop handler from the canvas when it detects a drag enter of a file with extension *.ext. It then adds its own handler instead.

Please do a lot of testing to make sure you don’t destroy the standard drag-drop functionality.

dragdropnondealbreaking.gh (2.9 KB)

Thanks David. I will test this out.
So far I managed to devise an alternative that is quick, user friendly and allows inclusion in Galapagos genomes, something that I previously hadn’t considered as being an advantage, but turns out it is. :star_struck: Cheers.
Nevertheless, the drag’n’drop ability will be a nice addition for simple setups, so I’ll try to implement it later on.