How to get DXF file path

I want to manually open a DXF file in Rhino, but then I need to automatically get its file path in Grasshopper.

file%20path

Any ideas how to do that?

I have found several codes, which give you the path of a saved Rhino file, but they do not serve me for a loaded DXF (or any other file format).

A little tricky workaround could be to get all the command history lines and pick the
last one that start with “Successfully read file” and ends with “.dxf”

2019-08-01%2014_24_21-Grasshopper%20-%20opened_dxf_file_path
opened_dxf_file_path.gh (3.1 KB)

Use the trigger to restart the script (when you open a new file).
Note that this script will always return the path of the last .dxf file opened… if, after that, you open a .somethingelse file it will still retrieve the path of the last .dxf file.

From here it should be not complex to add rules inside the script.
Tell if you need help.

Code:

  private void RunScript(object x, ref object Path)
  {
    // Getting Command History text
    string text = Rhino.RhinoApp.CommandHistoryWindowText;
    // Splitting into array of lists
    string[] results = text.Split(new [] { '\r', '\n' });
    string path = null;
    foreach(string s in results){
      // Checking if the current command-line is the opening of a dxf file
      if(s.StartsWith("Successfully read file \"") && s.EndsWith(".dxf\"")){
        path = s;
        // Removing first characters
        path = path.Remove(0, 24);
        // Removing last character
        path = path.Remove(path.Length - 1, 1);
      }
    }
    Path = path;
  }
1 Like

My friend this is just perfect! Thank you so very much!

Any way to check for "Successfully read file “” or "Se ha leído correctamente el archivo “”. I need it to work both in English and Spanish.

Replace one line of the code, from:

if(s.StartsWith("Successfully read file \"") && s.EndsWith(".dxf\"")){

to

if((s.StartsWith("Successfully read file \"")||s.StartsWith("Se ha leído correctamente el archivo \"")) && s.EndsWith(".dxf\"")){

Should work but I have no way to test it…

1 Like

Flawless! And I have learned some syntax as a bonus! :slight_smile: Thank you so much!

Any way to recompute it when a new file is loaded?