-export command line options?

Hi! I’m making an alias to help automate exporting files for solidworks, rotating the model etc. when using “-export” there is an option to specify rhino version, but not file type in the command line. If I want to do a STP file. Is there any way to specify stp in the command line? Right now I’m just using browse and selecting step in the drop down, but it would be nice to not have to hunt for it every time.

Thanks!

Yes, in order to do this you need to use at least a filename plus extension

So for example, on running _-Export, you can type “yourfilename.stp” to export selected objects to the last used folder in .step format.

If you want to specify the directory where the file is to be saved, you either need to go through the Browse dialog anyway, or type the full path+filename+extension to automatically save it there. Don’t forget to use quotes if the filename or file path you type has a space in it…

Ah interesting! Thanks, that did the trick!

I am doing this same kind of thing, except exporting to Iges files. Is there some kind of rhino variable I could use so that my exported file has the same name as the Rhino file, just with a different path and file extension?

I would like to capture just the file name, not the extension or path of the Rhino file.

can I make my button somehow execute _-Export “c:\another folder*filename*.igs” where file name is filled in from my Rhino document name?

So if my Rhino file is:
“c:\somefiles\test.3DM” my export would go to “c:\another folder\test.igs”

Hi @James_Richters,

This can be done with a Python script. Here is a place to start:

import System
import Rhino
import scriptcontext as sc

def test_export_iges():
    path = sc.doc.Path
    if path:
        path = System.IO.Path.ChangeExtension(path, ".igs")
    else:
        folder = Rhino.ApplicationSettings.FileSettings.WorkingFolder
        path = System.IO.Path.Combine(folder, "Untitled.igs")
        
    script = "_-Export _Pause \"{0}\" _Enter".format(path)
    Rhino.RhinoApp.RunScript(script, True)

if __name__ == "__main__":
    test_export_iges()

– Dale

1 Like

Hi @dale,

Thank you very much for the script! I’ve been struggling with to get started with Python scripts in Rhino.
This was very helpful! I haven’t been able to figure out how to run Rhino commands from the script, but now I can see how it’s done. I have a lot of operations I need to do before the export, but they are always exactly the same, so I can just stack them all up in front of the export.

I want to save the file in a different folder than the one where the Rhino drawing is, so I was able to modify your script to save the .Igs file to a hard coded path, which is fine for my needs.

Here’s what I came up with… after some struggle with the hard coded backslashes :slight_smile:

import System
import Rhino
import rhinoscriptsyntax as rs

def test_export_iges():
    folder = r'M:\CNC_Data\Struts\Drill\2x4'
    name = rs.DocumentName()
    extension = ".igs"
    path = System.IO.Path.Combine(folder, name)
    path = System.IO.Path.ChangeExtension(path, extension)
    print path
    script = "_-Export _Pause \"{0}\" _Enter".format(path)
    Rhino.RhinoApp.RunScript(script, True)

if __name__ == "__main__":
    test_export_iges()

I have my python script working fine from the python editor… but when I try to put it into a button, it doesn’t work. I must be doing something wrong.

Here is what I get running it from the editor:

but when I try to put it into a button, I just get it on the command line, but it doesn’t run… and it doesn’t export the file.

For running a Python script in a button, remove the if name==main part and just leave the main definition name with the parentheses (and out-dent it).

1 Like

Thank you, that worked!

I noticed that if name == “main:” Doesn’t seem to be needed for the Python Editor either. Is it ever needed in Rhino?

It’s easier if I can fix up my scripts in the editor and test them, then just copy and paste the whole thing into a button.

No, not really, if your scripts are independent and you don’t call them as modules from other scripts. I never use it personally.

Good to know, Thanks! I would rather keep it un-cluttered as well.

I have another issue with my Python Script… nothing after the script runs. Is there some way to make further button commands work after the Python script?

I am trying to run Undo after the script to undo the explodes in the script. because if I run it inside the script it says there is nothing to undo… but I noticed I can just do one undo after the script and all the explodes in the script get undone.

Here is what I have my button defined to do:

! _-RunPythonScript (
import System
import Rhino
import rhinoscriptsyntax as rs

def Export_Cut():
    script = "_-Explode"
    Rhino.RhinoApp.RunScript(script, True)
    Rhino.RhinoApp.RunScript(script, True)
    Rhino.RhinoApp.RunScript(script, True)
    folder = r'M:\CNC_Data\Cut\2x4'
    name = rs.DocumentName()
    extension = ".igs"
    path = System.IO.Path.Combine(folder, name)
    path = System.IO.Path.ChangeExtension(path, extension)
    print path
    script = "_-Export _Pause \"{0}\" _Enter".format(path)
    Rhino.RhinoApp.RunScript(script, True)

Export_Cut()
)
Undo

Yes, Undo outside the script takes the whole script operation back. The script is effectlively like executing one command. So running Undo inside doesn’t work. Basically you need to keep track of stuff inside the script, for example if you need to ‘undo’ explode, you need to first explode a copy of the object being exploded, then delete the results after, leaving the original intact.

That’s what I’m trying to do… undo the entire script right after I run it… because my exported file will still be there.

But my Undo command that I put after the _-RunPythonScript () command never runs… nothing I put after the script ever runs when I push the button. Is there some way to continue executing commands in the button after the script finishes? I thought maybe I need to give some command to make it wait for the script but I can’t guess how I would do that.

Can you try to save your script and then put this in button instead:

-_RunPythonScript (
"path\to\script.py"
)
Undo

Here that seems to work

I can’t get it to work… maybe If your test script it tiny, then it would seem to work… but it’s just not taking enough time. because the commands after the script don’t come in right away. Here is what I get:

Here’s how I have the button… if you save the script with the Python Editor in it’s default location, you don’t need to bother with the path

! _-RunPythonScript (“Export Wall Notch to Drill Folder”)
Undo

here is the output:

Command: _-RunPythonScript
Python Script ( ResetEngine ): ("Export Wall Notch to Drill Folder")
Python Script ( ResetEngine ): _-SelLayer
Layers to select ( Pick ): "Drill Alignment"
1 curve, 3 surfaces added to selection.
Command: _Enter
Command: Undo    <-----   This Undo is NOT in the script.. it's coming from the button
Nothing to undo.
Command: _-Explode
Cannot explode single curve segments.
Cannot explode 3 single surfaces.
Exploded 2 instances into 4 objects.
Command: _-Explode
Cannot explode single curve segments.
Cannot explode 3 single surfaces.
Exploded 2 instances into 24 objects.
Command: _-Explode
Cannot explode single curve segments.
Cannot explode 3 single surfaces.
Exploded 4 polysurfaces into 50 surfaces.
Exploded 2 instances into 1368 objects.
Exploded 8 annotation objects into 32 curves.
M:\CNC_Data\Drill\2x4\Strut 2x4 C1C Upside Down test.igs
Command: _-Export
Command: _Pause
Save file name ( Version=7  SaveSmall=No  GeometryOnly=No  SaveTextures=Yes  SaveNotes=No  SavePlugInData=Yes  Browse ): "M:\CNC_Data\Drill\2x4\Strut 2x4 C1C Upside Down test.igs"
IGES Export Type: Default ( ListTypes  UnitSystem=Inches  StringType=Unicode  Tolerance=1e-14 ): _Enter
IGES Export Type: Default ( ListTypes  UnitSystem=Inches  StringType=Unicode  Tolerance=1e-14 )
Saved M:\CNC_Data\Drill\2x4\Strut 2x4 C1C Upside Down test.igs.
File successfully saved as M:\CNC_Data\Drill\2x4\Strut 2x4 C1C Upside Down test.igs.

If I put something after the script that requires a selection and start it without a selection like BlockEdit, then you get this:

Command: _-RunPythonScript
Python Script <C:\Users\james\AppData\Roaming\McNeel\Rhinoceros\7.0\scripts\Export Wall Notch to Drill Folder.py> ( ResetEngine ): ("Export Wall Notch to Drill Folder")
Python Script <C:\Users\james\AppData\Roaming\McNeel\Rhinoceros\7.0\scripts\Export Wall Notch to Drill Folder.py> ( ResetEngine ): _-SelLayer
Layers to select ( Pick ): "Drill Alignment"
1 curve, 3 surfaces added to selection.
Command: _Enter
Command: BlockEdit    <---- BlockEdit came after the script.
Select block to edit
Block Editing part "Block 01"  
Command: _-Explode    <--- After I select a block to edit and it puts it in block edit mode, the script continues

OR Maybe… … having things like this in the script are causing this…

    script = "_-Explode"
    Rhino.RhinoApp.RunScript(script, True)

maybe I need smaller python scripts to just to the python part, then the Rhino commands, just issue outside the python script… but unfortunately the one thing I really wanted is to capture the file name and use it for the export… but the export can’t be done any other way than calling Rhino.RhinoApp.RunScript() so I guess even if it is the Rhino.RhinoApp.RunScript() causing the things after the script to run… it still won’t matter

Yes, that is what it is… I just tried a button with this:

! _-RunPythonScript (
import System
import Rhino
import rhinoscriptsyntax as rs

def Export_Drill():
    #Restore Layer state
    plugin = rs.GetPlugInObject("Rhino Bonus Tools")
    if plugin is not None:
        plugin.RestoreLayerState("Wall Notch")
    rs.ObjectsByLayer("Drill Alignment", True)
Export_Drill()
)
_-Explode
_-Explode
_-Explode

 _-RunPythonScript (
import System
import Rhino
import rhinoscriptsyntax as rs

def Export_Drill():
    folder = r'M:\CNC_Data\ Drill\2x4'
    name = rs.DocumentName()
    name = name.replace('Strut 2x4 ', '')
    name = name.replace('Strut 2x6 ', '')
    extension = ".igs"
    path = System.IO.Path.Combine(folder, name)
    path = System.IO.Path.ChangeExtension(path, extension)
    print path
    script = "_-Export _Pause \"{0}\" _Enter".format(path)
Export_Drill()
)
_-Undo
_-Undo
_-Undo
_-Undo

and the results are in the correct order:

Command: _-RunPythonScript
Python Script <import System (
import System
import Rhino
import rhinoscriptsyntax as rs

def Export_Drill():
    #Restore Layer state
    plugin = rs.GetPlugInObject("Rhino Bonus Tools")
    if plugin is not None:
        plugin.RestoreLayerState("Wall Notch")
    rs.ObjectsByLayer("Drill Alignment", True)
Export_Drill()
)
Command: _-Explode
Cannot explode single curve segments.
Cannot explode 3 single surfaces.
Exploded an instance into 2 objects.
Command: _-Explode
Cannot explode single curve segments.
Cannot explode 3 single surfaces.
Exploded an instance into 12 objects.
Command: _-Explode
Cannot explode single curve segments.
Cannot explode 3 single surfaces.
Exploded 2 polysurfaces into 25 surfaces.
Exploded an instance into 684 objects.
Exploded 4 annotation objects into 16 curves.
Command: _-RunPythonScript
Python Script <import System (
import System
import Rhino
import rhinoscriptsyntax as rs

def Export_Drill():
    folder = r'M:\CNC_Data\Drill\2x4'
    name = rs.DocumentName()
    name = name.replace('Strut 2x4 ', '')
    name = name.replace('Strut 2x6 ', '')
    extension = ".igs"
    path = System.IO.Path.Combine(folder, name)
    path = System.IO.Path.ChangeExtension(path, extension)
    print path
    script = "_-Export _Pause \"{0}\" _Enter".format(path)
Export_Drill()
)
M:\CNC_Data\Drill\2x4\C1C Upside Down test.igs
Command: _-Undo
Undoing Explode
Command: _-Undo
Undoing Explode
Command: _-Undo
Undoing Explode
Command: _-Undo
Undoing RunPythonScript

Notice I left the actual Rhino.RhinoApp.RunScript(script, True) out… and then the undo’s work…
Rhino.RhinoApp.RunScript(script, True) in the python script is what breaks everything.

If I put Rhino.RhinoApp.RunScript(script, True) to actually run the Export, then I get:

Command: _-RunPythonScript
Python Script <import System (
import System
import Rhino
import rhinoscriptsyntax as rs

def Export_Drill():
    #Restore Layer state
    plugin = rs.GetPlugInObject("Rhino Bonus Tools")
    if plugin is not None:
        plugin.RestoreLayerState("Wall Notch")
    rs.ObjectsByLayer("Drill Alignment", True)
Export_Drill()
)
Command: _-Explode
Exploded an instance into 2 objects.
Command: _-Explode
Exploded an instance into 12 objects.
Command: _-Explode
Exploded 2 polysurfaces into 25 surfaces.
Exploded an instance into 684 objects.
Exploded 4 annotation objects into 16 curves.
Command: _-RunPythonScript
Python Script <import System (
import System
import Rhino
import rhinoscriptsyntax as rs

def Export_Drill():
    folder = r'M:\CNC_Data\Drill\2x4'
    name = rs.DocumentName()
    name = name.replace('Strut 2x4 ', '')
    name = name.replace('Strut 2x6 ', '')
    extension = ".igs"
    path = System.IO.Path.Combine(folder, name)
    path = System.IO.Path.ChangeExtension(path, extension)
    print path
    script = "_-Export _Pause \"{0}\" _Enter".format(path)
    Rhino.RhinoApp.RunScript(script, True)
Export_Drill()
)
M:\CNC_Data\Drill\2x4\C1C Upside Down test.igs
Python Script <import System _-Export
Python Script <import System _Pause
Save file name ( Version=7  SaveSmall=No  GeometryOnly=No  SaveTextures=Yes  SaveNotes=No  SavePlugInData=Yes  Browse ): "M:\CNC_Data\Drill\2x4\C1C Upside Down test.igs"
IGES Export Type: Default ( ListTypes  UnitSystem=Inches  StringType=Unicode  Tolerance=1e-14 ): _Enter
IGES Export Type: Default ( ListTypes  UnitSystem=Inches  StringType=Unicode  Tolerance=1e-14 )
Saved M:\CNC_Data\Drill\2x4\C1C Upside Down test.igs.
File successfully saved as M:\CNC_Data\Drill\2x4\C1C Upside Down test.igs.
Command: _-Undo   <--- these undo's all ran while it was saving the file.. the script wasn't done yet
Nothing to undo.
Command: _-Undo
Nothing to undo.
Command: _-Undo
Nothing to undo.
Command: _-Undo
Nothing to undo.
Command: _-Undo
Nothing to undo.

If I run another undo on my own, it does undo the script.

So… I think that given that

  1. Export can only be done with Rhino.RhinoApp.RunScript()
  2. Rhino.RhinoApp.RunScript() causes commands following the script to run too early

It will be impossible to get it to work that way… so that leaves me making copies and deleting the copies… I have got that to work… it’s just that I prefer to undo the entire script automatically because if things get more complicated then it’s more difficult to make sure I don’t delete something I shouldn’t.

I have a new idea:

  1. Save the Rhino Drawing
  2. do all this exporting stuff
  3. Close the Rhino Drawing without saving it
  4. Open the previously saved Drawing

Then no matter how convoluted my scheme is and if I make a silly mistake in the script, nothing in the drawing can possibly change because of the script. not as elegant as running a script and undoing it, but if the undo can’t be automated, this would be far safer.

can someone please point me in the right direction on how my python script might save, close without saving and opening the file? Are those things all possible from a python script?