Is it possible to compare the current documentPath with Rhino.Options.DefaultFileDialogDirectories.ExportDirectory in Rhino Python script?
I mean, if documentPath is not equal to Rhino.Options.DefaultFileDialogDirectories.ExportDirectory (parent path, it could be interesting to detect\ in order to extract a subpath to compare) then use documentPath to export files, else use the last used, stored in defaultFileDialogDirectories…
See: Original post
The goal is to optimize teh exporting path and avoid mistakes
import Rhino
import scriptcontext
import rhinoscriptsyntax as rs
import System
def GetLastExportDirectory():
try:
rh_id = Rhino.RhinoApp.CurrentRhinoId
settings = Rhino.PlugIns.PlugIn.GetPluginSettings(rh_id, False)
s = settings.GetChild("Options").GetChild("DefaultFileDialogDirectories")
d = s.GetString("ExportDirectory")
# only return path if it exists
if System.IO.Directory.Exists(d):
return d
except Exception as ex:
print ex
def DoSomething():
print "ExportDirectory:", GetLastExportDirectory()
if __name__ == "__main__":
DoSomething()
to get the document path for comparison you might use: rs.DocumentPath().Note that the document must be saved before or it returns None.
It seems to work only when I open a new rhino file, but if I export, for instance, a DXF file, this register seems not to update. In fact, if I use export selected curves from rhino menu, it opens the last route where I saved the last file… Is there a solution for that?
But now I am in a trouble, importing re library and test using re.split (or similar) I am trying to get substrings of the path, counting “\” special character and comparing with rs.DocumentPath(). If I try to find \ it does not work, but if I try to find other character it works.
How could I get my desired substring from GetLastExportDirectory() counting \ special character in order to extract the main path?
Example from your code, where directorio is “\\Nasduyba\produccion\4\772.015\plantilla”
and rs.DocumentPath() is "\\Nasduyba\produccion\4\772.015"
In this case, for me, comparision is true (I would like to count \ and verify that excluding plantilla folder is the same path). This sublevel will be the same “\\Nasduyba\produccion\x\y.…”
def DoSomething():
directorio=GetLastExportDirectory()
res =re.split("\\",directorio) #error
rs.MessageBox (res)
Another option is after exporting a DXF file to save a txt in a specific path storing the string of the path, and every time I use the script compare the string included inside the txt file, but I think that this could be solved an easier way…
I think for what you’re trying to accomplish the best way would be to script the _Export command and feed it with a file path name you’re gathering from your own python script. By using rs.SaveFileName which allows to feed all required parameters, eg the folder it opens at to ask the user for the file name:
You might then store the last used folder once a file name has been accepted by the function. On the next run, you can compare the last used folder with the document path and make your comparison to adjust the folder used as argument of rs.SaveFileName.
You may not need regular expressions to get the parent folder of another folder. There are two possibilities eg:
import System
folder = "C:\\Alpha\\Beta"
parent = System.IO.Directory.GetParent(folder)
print parent.FullName
or, use the os.path module:
import os
folder = "C:\\Alpha\\Beta"
parent = os.path.dirname(folder)
print parent
Thank you so much for sharing and helping me with your knowledge.
Finally I solved it by creating a TXT file and saving the last exporting path, comparing every time with Rhino working file the parent one.
I summarize a lot because the purpose is a little bit more complicated, but I can say that using this method we can export files using a predefined dxf exporting schema for a cutting laser machine and save our files in a correct path avoiding mistakes.