Set name of objects as filename input?

I am trying to adapt a .rvb script to convert from IGES to 3DM files automatically, and I do need to set the name of the objects to be the same as the imported file name and I get stuck at:

Rhino.ObjectName arrObjects, Chr(34), 0

How I can name the objects as the input filename?

‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’’
’ BatchConvert_IGS_to_3DM script for Rhinoceros
’ Robert McNeel & Associates
www.rhino3d.com
‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’’

Option Explicit

‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’’
’ BatchConvert_IGS_3DM
‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’’
Sub BatchConvert_IGS_3DM()

’ Make sure RhinoScript does not reinitialize when opening models,
’ otherwise this script will only process one file.
Rhino.Command “_-Options _RhinoScript _Reinitialize=_No _Enter _Enter”, 0

’ Allow the user to interactively pick a folder
Dim sFolder
sFolder = Rhino.BrowseForFolder(, “Select folder to process”, “Batch Convert IGS_to_3DM”)
If VarType(sFolder) <> vbString Then Exit Sub

’ Create a file system object
Dim oFSO
Set oFSO = CreateObject(“Scripting.FileSystemObject”)

’ Get a folder object based on the selected folder
Dim oFolder
Set oFolder = oFSO.GetFolder(sFolder)

’ Process the folder
ProcessFolder oFSO, oFolder

’ Release the objects
Set oFolder = Nothing
Set oFSO = Nothing

’ Close the last file processed
Rhino.DocumentModified False
Rhino.Command “_-New _None”, 0

End Sub

‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’’
’ ProcessFolder
‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’’
Sub ProcessFolder(oFSO, oFolder)

’ Process all .igs files in the selected folder
Dim oFile, strOpen, strSave
For Each oFile In oFolder.Files
If LCase(oFSO.GetExtensionName(oFile.Path)) = “igs” Then
strOpen = LCase(oFile.Path)
strSave = LCase(Replace(strOpen, “.igs”, “.3dm”, 1, -1, 1))
ProcessFile strOpen, strSave
End If
Next

’ Comment out the following lines if you do not
’ want to recursively process the selected folder.
Dim oSubFolder
For Each oSubFolder In oFolder.SubFolders
ProcessFolder oFSO, oSubFolder
Next

End Sub

‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’
’ ProcessFile
‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’
Sub ProcessFile(strOpen, strSave)
Rhino.DocumentModified False
Rhino.Command "-Open " & Chr(34) & strOpen & Chr(34), 0
Rhino.Command "
-Zoom _All Extents", 0
Rhino.Command "
-SetActiveView Top", 0
Rhino.Command "
-SetDisplayMode _Viewport=_All _Mode=Ghosted", 0
Rhino.Command "
-Purge _BloackDefinitions=Yes _AnnotationStyles=Yes _Groups=Yes _HatchPatterns=Yes _Layers=Yes _Linetype=Yes _Materials=Yes _Textures=Yes Environments=Yes Bitmaps=Yes Enter", 0
Rhino.Command "
-SelAll", 0
Rhino.ObjectName arrObjects, Chr(34), 0
Rhino.Command "
-Join", 0
Rhino.RenameLayer “IGES level 0”, “Fittings”
Rhino.LayerColor “Fittings”, RGB(0, 255, 0)
Rhino.Command "
-SaveAs Version=6 " & Chr(34) & strSave & Chr(34), 0
End Sub

This is not a vb Rhinoscript, but you can try running this Python script and see if it works for you.

BatchConvertIGSTo3dm.py (1.9 KB)

To run the script, save it somewhere, then use _RunPythonScript and browse to the script file. For more permanent script installation via aliases or toolbars, have a look at this wiki page.
https://wiki.mcneel.com/rhino/macroscriptsetup

1 Like

Hi Helvetosaur. Thank you for the script, I just tested and it is working, but the .rvb modified script have more functions that it is doing. The only thing, I don’t know how to pass the filename as variable to be used as name for the objects.

The sixth line from bottom to top:

Rhino.ObjectName arrObjects, Chr(34), 0

I also tried:
Rhino.Command "_-Properties _Object _Name (strSave) _EnterEnd", 0

and a lot of variations, but I can’t manage to make that part to work.

That line doesn’t make any sense to me and is probably wrong. Chr(34) is a single double quote and in theory that should produce an error… Either you need to feed it a real string to assign a name to the objects or give it two double quotes (an “empty string”) to remove any names from the objects.

Plus, in principle, if you supply arrObjects (an array of objects) and not a single strObject as the first argument, the method only wants the name (string) as the second argument. So I don’t understand Chr(34), 0 - what’s the 0 for?

In any case I get this if I try to run it:

image

So try replacing the part Chr(34),0 with some real string representing the name to be applied or just two double quotes "" if you are trying to remove the name(s).

I don’t understand where this script stores the variable for name, because it is using the filename to write the same filename with different extension, and after that I need to use that variable to pass into the:

Rhino.Command "_-Properties _Object _Name

to set the name of the objects?

I do found here Rhino - 2 RhinoScript Essentials (rhino3d.com):

Rhino.Command "-_Properties _Object _Name Profile _Enter _Enter"

and I think that I only need to replace the “Profile” with the variable where this script stores the values for the name of the file.

oFile appears to be the file name being opened
strOpen appears to be the full filepath to the file being opened
strSave appears to be the full filepath for the file to be saved

So I guess you might want to use oFile to name the objects…

And how I can pass that variable to set the name of the objects?

Rhino.Command "_-Properties _Object _Name (oFile) _EnterEnd", 0

Rhino.Command "_-Properties _Object _Name [oFile] _EnterEnd", 0

don’t works. If I use:

Rhino.Command "_-Properties _Object _Name oFile _EnterEnd", 0
then all the object will have the name oFile instead of the variable content.

If I try:

Rhino.Command "_-Properties _Object _Name" + oFile + "_EnterEnd", 0
I get an error: Variable is undefined: 'oFile'.

The script file is compartmentalized. There is the main section Sub BatchConvert_IGS_3DM() that opens the selected folder and looks for .igs files, and then calls a subroutine Sub ProcessFile(strOpen, strSave) for each .igs file it finds in the folder. You would need to pass the oFile variable to the subroutine for each file - it could be re-derived from strOpen, but that’s a mess.

So it would end up being something like this for calling the sub
ProcessFile strOpen, strSave, oFile

And the subroutine modified to look like this:
Sub ProcessFile(strOpen, strSave, oFile)

Then you will have access to the variable oFile inside the subroutine.

Then to name your objects inside the subroutine, it would be:

Rhino.ObjectName arrObjects, oFile

Now I do get this error: Variable is undefined: 'arrObjects'

Yes, because it hasn’t been defined nor has anything been assigned to it.

You would need to define it inside the subroutine
dim arrObjects

and then get all the objects in the file you just imported and store them in arrObjects.

That could be
arrObjects=Rhino.AllObjects (gets everything, even locked or hidden)
or
arrObjects=Rhino.NormalObjects (gets only selectable objects)

If you are wanting to adapt existing script samples to your needs, you do need some basic knowledge of how scripts work…

BTW, there is a typo here:

1 Like

I tried making the script work. Reminded me of how much I hate working in VB Rhinoscript… :face_vomiting: All the hoops you need to jump through to make simple stuff work.

There were numerous typos and other errors, now corrected. Getting the import file name to name the objects was a little more complicated than I remembered because oFile is actually a file system object, not the file name.

If there is nothing in the imported file that can be joined, the script will hang there until you hit Enter.
If the layer IGES level 0 does not exist in the imported file the script would also fail there, so I enclosed it in an “If” clause.


Option Explicit

Call BatchConvert_IGS_3DM()

Sub BatchConvert_IGS_3DM()

	' Make sure RhinoScript does not reinitialize when opening models,
	' otherwise this script will only process one file.
	Rhino.Command "_ - Options _RhinoScript _Reinitialize=_No _Enter _Enter", 0

	' Allow the user To interactively pick a folder
	Dim sFolder
	sFolder = Rhino.BrowseForFolder(, " Select folder To process", " Batch Convert IGS_to_3DM")
	If VarType(sFolder) <> vbString Then Exit Sub

	' Create a file system object
	Dim oFSO
	Set oFSO = CreateObject("Scripting.FileSystemObject")

	' Get a folder object based On the selected folder
	Dim oFolder
	Set oFolder = oFSO.GetFolder(sFolder)

	' Process the folder
	ProcessFolder oFSO, oFolder

	' Release the objects
	Set oFolder = Nothing
	Set oFSO = Nothing

	' Close the last file processed
	Rhino.DocumentModified False
	Rhino.Command "_ - New _None", 0

End Sub

'''''''''''''''''''''''''''''''''''''
' ProcessFolder
'''''''''''''''''''''''''''''''''''''
Sub ProcessFolder(oFSO, oFolder)

	' Process all.igs files In the selected folder
	Dim oFile, strOpen, strSave, fileName
	For Each oFile In oFolder.Files		
		If LCase(oFSO.GetExtensionName(oFile.Path)) = "igs" Then
			'get file name from file object less extension
			fileName = left(oFile.Name, Len(oFile.Name) - 4)
			strOpen = LCase(oFile.Path)
			strSave = LCase(Replace(strOpen, " .igs", " .3dm", 1, -1, 1))
			'process the file
			ProcessFile strOpen, strSave, fileName
			
		End If
	Next

	' Comment out the following lines If you do not
	' want To recursively process the selected folder.
	Dim oSubFolder
	For Each oSubFolder In oFolder.SubFolders
		ProcessFolder oFSO, oSubFolder
	Next

End Sub

	''''''''''''''''''''''''''''''''''''''''''''''
	' ProcessFile
''''''''''''''''''''''''''''''''''''''''''''''
Sub ProcessFile(strOpen, strSave, fileName)
	Dim strComm, arrObjects
	
	Rhino.DocumentModified False
	Rhino.Command "-Open " & Chr(34) & strOpen & Chr(34), False
	Rhino.Command "-Zoom _All Extents", False
	Rhino.Command "-SetActiveView Top", False
	Rhino.Command "-SetDisplayMode _Viewport=_All _Mode=Ghosted", 0
	'create your long command string
	strComm = "-Purge _BlockDefinitions=_Yes _AnnotationStyles=_Yes _Groups=_Yes"
	strComm = strComm & " _HatchPatterns=_Yes _Layers=_Yes _Linetypes=_Yes"
	strComm = strComm & " _Materials=_Yes _Textures=_Yes Environments=_Yes"
	strComm = strComm & " _Bitmaps=_Yes _Enter"
	'run the long command string
	Rhino.Command strComm, False
	
	arrObjects = Rhino.AllObjects(True) 'selects all objects
	Rhino.ObjectName arrObjects, fileName
	''''''''''''''''''''''''''''''
	'This will cause problems if nothing can be joined
	Rhino.Command "_Join", False
	''''''''''''''''''''''''''''''
	'This would potentially cause problems if the layer doesn't exist!
	If Rhino.IsLayer("IGES level 0") Then
		Rhino.RenameLayer "IGES level 0", "Fittings"
		Rhino.LayerColor "Fittings", RGB(0, 255, 0)
	End If
	
	Rhino.Command "-SaveAs Version=7 " & Chr(34) & strSave & Chr(34) & " _Enter", False
End Sub
1 Like