VBA to copy and paste string to rhino textbox

I thought maybe if i could grab the second line of the log that states the full path has opened successfully and copy and paste that. But i haven’t found any Rhino commands to be able to script that.

Could you give me a quick example of what you’re saying?

This is what i have so far. Just missing that filename!

Option Explicit

Sub Location
On Error Resume Next
Dim FileLocation
FileLocation = FileName

End Sub

Function FileName

	Dim strPath, arrPoint

	StrPath = Rhino.WorkingFolder & Rhino.DocumentName 
	
		If IsNull (Rhino.DocumentName) Then
		
			arrPoint = Rhino.GetPoint("Pick Insertion Point")
			IsArray (arrPoint)
			Rhino.AddText strPath & "Main STP File", arrPoint, 5,,1
			
			Else
		
		End If	
	

			arrPoint = Rhino.GetPoint ("Pick Insertion Point")
			IsArray(arrPoint)
			Rhino.AddText strPath, arrPoint, 5,,1 
			MsgBox "Disclaimer: If this is an Untitled Rhino file. Your directory will be WRONG until you save it!", vbOkOnly
		
						
End Function

Rhino.AddAlias “Location”, “_NoEcho _-Runscript Location”

Rhino.AddStartupScript Rhino.LastLoadedScriptFile

The IsNull is a quick workaround i do not believe is going to fly with the boss’s.

Hi Joseph - if you open your Step file with some code like this

Call Main()
Sub Main()
	Dim OpenFile: OpenFile = Rhino.OpenFileName()
	If isNull(OpenFile) Then
		Exit Sub
	End If
	
	Rhino.Command("Open " & chr(34) & OpenFile & chr(34) & " Enter")
	If Rhino.LastCommandResult() = 0 Then
		Rhino.SetDocumentUserText "FileIOpened", OpenFile
	Else 
		Exit Sub
	End If
	Rhino.Print("The file I opened is " & Rhino.GetDocumentUserText("FileIOpened"))
End Sub

In your later script you can retrieve the file name with Rhino.GetDocumentUserText(“FileIOpened”)

Does that make sense?

-Pascal

I see what the code is doing. I’m just not sure where i would need to put it to run. on the .EXE?

Or would that have to be a seperate “open” button within rhino to properly run the code?

Hi Joseph - I was thinking that when you open your file, run a script like what I have above on a special file opening button or alias instead of the normal Open or Import commands. Is that what you mean?

-Pascal

Yes. That’s what i mean. I will paste the script into a new button and see if i can get the original filename trapped with your DocumentUserText.

Hi Joseph - use

! _-Runscrpt (

Code

) 

if you want to use the code directly in a button.

-Pascal

That works!!! However, every time i hit the button it prompts me to choose the main subroutine.

Is ‘Call Main()’ in there?

    Call Main() 
    Sub Main() 
       Dim OpenFile: etc etc 
   End Sub

-Pascal

Yes it is.

!_runscript

(Call Main()
Sub Main()
Dim OpenFile: OpenFile = Rhino.OpenFileName()
If isNull(OpenFile) Then
Exit Sub
End If

Rhino.Command("Open " & chr(34) & OpenFile & chr(34) & " Enter")
If Rhino.LastCommandResult() = 0 Then
	Rhino.SetDocumentUserText "FileIOpened", OpenFile
Else 
	Exit Sub
End If
Rhino.Print("The file I opened is " & Rhino.GetDocumentUserText("FileIOpened"))

End Sub)

I forgot the - in runscript! My apologies!

1 Like

I have it fully working now.

@pascal
@JavierG

You guys are life savers! Thanks so much!

Great, I’m glad it worked out!

-Pascal

@pascal

Rhino.Command(“Open " & chr(34) & OpenFile & chr(34) & " Enter”)

Could you give me a rundown of how this line of code works? I understand the rest of it.

When you have a free moment!

I understand the initial open and openfile

I’m just not sure what the chr is? Are you setting a character limit?
Why does enter need to be pushed?

Hi Joseph - chr(34) is the " character so that the path is surrounded in double-quotes.

If the Rhino command line hits a space in a string like a file path and there are no double-quotes, then it will assume that space is ‘Enter’ and stop there. The Enter at the end is like hitting Enter after hand typing in the path.

-Pascal

Thanks @pascal

Quick question. Is there a command that will return a true or false as to whether the document has EVER been saved?

Thanks again!

Hi Joseph - you can check with a function like this:

Call Main()

Function IsSaved ()
	Dim x: x = Rhino.DocumentName()
	If isNull(x) Then
		IsSaved = False
		Exit Function
	Else
		IsSaved = True
		
	End If
End Function

Sub Main()
	Rhino.Print IsSaved()
End Sub

-Pascal