[rhinocommon] Getting the name or path of an opened non-3dm file


I’d like to suggest a filename in the SaveFileDialog based on the filename of the current document.
When the file is already saved as a 3dm file I can use ActiveDoc.Name or Path. If the file is new, both Path and Name are empty and I suggest “Untitled” in analogy with Rhino SaveAs. So far, so good.
However, when I open an IGS or a file format of our own, the Doc.Name and Path are also empty, but the Rhino Titlebar displays the imported file name (without the extension). This is actually the data I need to suggest a filename.
The TemplateFileUsed property also does not reveal this information.

Anyone a suggestion how to get the name of a non-3dm file after it was opened?

Kind Regards,

-import get filename import
(Karol Wierzbicki) #2

RhinoDoc provides nice functionality i.e.

    // Summary:
    //     Returns the name of the currently loaded Rhino document (3DM file).
    public string Name { get; }


I wish it was that trivial. As long as I haven’t saved the file as 3dm, the Name property provides me with an empty string. At my desk which is running Rhino 5.7.

I’m testing using the following code in a command:

    protected override Result RunCommand(RhinoDoc doc, RunMode mode)

        // todo: how to detect whether a file was new or imported. (And more important, which file was imported.)

        RhinoApp.WriteLine("Doc file info: Template file used '{0}', Doc path '{1}', Doc Name '{2}'", doc.TemplateFileUsed, doc.Path, doc.Name);

        FileInfo fi = String.IsNullOrWhiteSpace(RhinoDoc.ActiveDoc.Path) ? new FileInfo(Path.Combine(Environment.CurrentDirectory, "untitled")) : new FileInfo(RhinoDoc.ActiveDoc.Path);

        SaveFileDialog d = new SaveFileDialog();
        d.InitialDirectory = fi.DirectoryName;
        d.FileName = Path.GetFileNameWithoutExtension(fi.Name);
        d.DefaultExt = ".lis";


        RhinoApp.WriteLine("Filename: {0}", d.FileName);
        return Result.Success;

(Dale Fugier) #4

Rhino does not save this (filename).


Hi Gerco, one alternative would be to parse the reversed command history text. If a file has been opened or imported successfully it is reported in the command history similar like this:

File "D:\Temp\FileName.obj" successfully read

I´ve hacked this together in python which seems to work, note that it reports the last opened file (eg. opened via drag & drop). If multiple files are opened without saving as 3dm, the title text remains at the one imported first.

import Rhino

def LastFileSuccessfullyRead():

    # get command history text
    ch_text = Rhino.RhinoApp.CommandHistoryWindowText
    if not ch_text: return None

    # no file yet
    file = None

    # command history text as line list
    lines = [line for line in (ch_text.split('\n'))]

    # reverse lines in text to search backwards

    # find "File ..... successfully read" string
    for line in lines:
        a = line.find("File") 
        b = line.find("successfully read")
        if a > -1 and b > 5:
            file = line[a+6:b-2]

    return file

if __name__=="__main__":
    file = LastFileSuccessfullyRead()
    if file: 
        print "Last imported file:", file
        print "Error getting last imported file"

it is a workaround :smile:


Document name DXF

Hi Clement, that workaround could work for me, thanks.