Export/Import. Path and file name to clipboard

Hello.
I often work in Rhino_8 and Rhino_5 at the same time.
Unfortunately, it is impossible to paste objects to Rhino5 from the clipboard Rhino8.
Therefore I have to Export or Save as Rhino 5.
For convenience, I use this script by Mitch:

"""
Exports selected objects as Rhino version V5. Any SubD objects are converted
to NURBS for export.  Script by Mitch Heynick 11.01.22
"""

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino

def ConvertSubDToNurbs(subd_obj):
    subd=rs.coercegeometry(subd_obj)
    brep=subd.ToBrep(Rhino.Geometry.SubDToBrepOptions.DefaultPacked)
    return brep

def ExportAsV5ConvPackedSubD():
    msg="Select objects to export as V5"
    obj_ids=rs.GetObjects(msg,preselect=True,select=True)
    if not obj_ids: return
    
    rs.EnableRedraw(False)
    subd_count=0
    subd_exp_error=0
    subd_obj_ids=[]
    conv_brep_objs=[]
    for obj_id in obj_ids:
        if rs.ObjectType(obj_id)==262144:
            #object is a sub-d
            brep=ConvertSubDToNurbs(obj_id)
            if brep:
                subd_obj_ids.append(obj_id)
                rs.UnselectObject(obj_id)
                conv_brep_obj=sc.doc.Objects.AddBrep(brep)
                conv_brep_objs.append(conv_brep_obj)
                rs.SelectObject(conv_brep_obj)
                subd_count+=1
            else:
                subd_exp_error+=1
                
    filename=rs.SaveFileName("Save V5 file","Rhino Files|.3dm||")
    if filename:
        export_count=len(rs.SelectedObjects())
        comm_str='_-Export _Version=5 "{}"'.format(filename)
        rs.Command(comm_str)
    if conv_brep_objs: rs.DeleteObjects(conv_brep_objs)
    if subd_obj_ids: rs.SelectObjects(subd_obj_ids)
    msg="{} objects exported as V5".format(export_count)
    if subd_count:
        msg+=" including {} SubD objects".format(subd_count)
    if subd_exp_error:
        msg+=". {} SubD objects unable to be exported".format(subd_exp_error)
    msg+="."
    print msg
        
ExportAsV5ConvPackedSubD()

Is it possible when Export done, copy the path and file name to clipboard in the text from Rhino8, to quickly find it in Rhino_5 and import this file without question?

import Rhino
import scriptcontext as sc
import rhinoscriptsyntax as rs
import_path = rs.OpenFileName("file to import")
if import_path:
    FileReadOptions = Rhino.FileIO.FileReadOptions()
    FileReadOptions.ScaleGeometry = True
    FileData = Rhino.RhinoDoc.ReadFile(import_path, FileReadOptions)



Something like that. Copy to clipboard Path and DocumentName.

import rhinoscriptsyntax as rs
import os

filepath=rs.DocumentPath()
if filepath:
    fullpath=os.path.join(filepath,rs.DocumentName())
    rs.ClipboardText(fullpath)
    print ("File path+name copied to clipboard")
else:
    print ("File has not been saved, no document path available")

Thanks.

It should be possible. From IronPython 2 components or RhinoPython, .Net provides:

There’s no guarantee it will work from PythonNet without trying it, but in Grasshopper, a CPython 3 component could import pyperclip. pyperclip · PyPI

There’s a C++ API too, but that’s trickier to use from Rhino.

1 Like

To the above export script, maybe you can just add a line that copies the filepath to the clipboard:

ExportAsV5ConvPackedSubD.py (2.0 KB)

Then in the import file, pull the text from the clipboard, check to make sure it’s a .3dm file and then open it.

import rhinoscriptsyntax as rs
import scriptcontext as sc

filepath=rs.ClipboardText()
if filepath.endswith(".3dm"):
    sc.doc.Open(rs.ClipboardText())
else:
    print("Clipboard text is not a path to a Rhino .3dm file!")

Hi Mitch
Both works great in R8, thanks!

But in R5 it does not open or import.
I get a Message: ‘RhinoDoc’ object has no attribute ‘Open’.

Ah, I assumed that that was available in V5 as it was so simple… Didn’t test, sorry. Let me look and see what can be done there… OK, that was easy - the command name changed, it V5 it was OpenFile not just OpenOpenFile also works in later versions.

import rhinoscriptsyntax as rs
import scriptcontext as sc

filepath=rs.ClipboardText()
if filepath.endswith(".3dm"):
    sc.doc.OpenFile(rs.ClipboardText())
else:
    print("Clipboard text is not a path to a Rhino .3dm file!")

Hi Mitch,
I сhanged open to import. I need “import”, to add an object to the current scene.
It works well in R8, but with R5 doesn’t work.

import rhinoscriptsyntax as rs
import scriptcontext as sc

filepath=rs.ClipboardText()
if filepath.endswith(".3dm"):
    sc.doc.Import(rs.ClipboardText())
else:
    print("Clipboard text is not a path to a Rhino .3dm file!")

For R5, I tried “sc.doc.ImportFile(rs.ClipboardText())”
does not work.

In general, where do you look for these parameters that should work in Python with R5?

Yes, there is sc.doc.Import, but it was only added in V7.

I look here:
https://developer.rhino3d.com/api/rhinocommon/
and then do a search for the term I am looking for.

The documentation will tell you which versions it applies to. One other thing I did is just open V5, open the script editor, import scriptcontext as sc and start typing sc.doc... This represents the active Rhino document and the autocomplete will show you the methods available. In V5, there is no Import method in the list, but there is ReadFile… It’s a bit more complicated as you need to script some options to use import mode…

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino

fr_opts=Rhino.FileIO.FileReadOptions()
fr_opts.ImportMode=True
fr_opts.ScaleGeometry=True
fr_opts.UseScaleGeometry=True

filepath=rs.ClipboardText()
if filepath.endswith(".3dm"):
    sc.doc.ReadFile(rs.ClipboardText(),fr_opts)
else:
    print("Clipboard text is not a path to a Rhino .3dm file!")
1 Like

Magic! :man_mage:
Many thanks Mitch!

By the way, in the script with export.
When saved in the folder, Rhino files are not displayed.
This is not very convenient, since sometimes it is better to see which of the old files need to overwrite without changing the name.

I tried to do it myself, Rhino files while saving are displayed, but the filename is recorded on the clipboard without a 3dm extension.

...
filename=rs.SaveFileName("Save V5 file","All Files" "Rhino Files" ".3dm")
    if filename:
...

Dunno, the extension seems to be there for me using rs.SaveFileName():

import rhinoscriptsyntax as rs

filter = "Rhino Files (*.3dm)|*.3dm||"
filename=rs.SaveFileName("Save V5 file", filter, extension=".3dm")
rs.ClipboardText(filename)

print(rs.ClipboardText())

https://developer.rhino3d.com/api/RhinoScriptSyntax/#userinterface-SaveFileName

1 Like

Improved a little. If success, then will be added a text on the command line with the name of the imported file.

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino

fr_opts=Rhino.FileIO.FileReadOptions()
fr_opts.ImportMode=True
fr_opts.ScaleGeometry=True
fr_opts.UseScaleGeometry=True

filepath=rs.ClipboardText()
if filepath.endswith(".3dm"):
    sc.doc.ReadFile(rs.ClipboardText(),fr_opts)
    print(rs.ClipboardText())
else:
    print("Clipboard text is not a path to a Rhino .3dm file!")




It works well, and thank you again so much, Mitch! :handshake: