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.
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…
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”
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
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.
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.
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…
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
Export can only be done with Rhino.RhinoApp.RunScript()
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.
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?