I there a RhinoScript equivalent to what in C would be "Include" or in Delphi "Uses" (where you can reference functions and such from another source file)?

 

I want to build a library of common functions that can be used from within other scripts, but I can't see how to reference an external file that would contain this stuff. 

 

So far, the only solutions I could find seem a bit like a hack, and rely on absolute paths (eg "C:\library") which does not work for me because I may not know the absolute path when the script and library is installed on someone else's machine.

 

Thanks


Carl.

Tags: #include, external, functions, include, library, references, uses

Views: 309

Replies to This Discussion

Dale,

 

Thanks for the reply.  What you have is something close to what I have already tried.

 

This The code in your first link (posted below for reference) works the first time it is run.   However if I execute my script, then open a new file in Rhino, then execute the script again, it gives me "File not found" error at the point where it would be doing the "include".  The exact sequence to repeat is:

 

1 - Open the rhino script editor, and cut and paste the code below into a new script, and write a line of code to call it to include a unit without using absolute paths (for example, call Include("mylibrary.rvb"))

 

2 - Run the script (I used the "run the script" button in the RhinoScript Editor, if that matters.)

 

3 - Observe that it works.

 

4 - Load a part into Rhino from some folder other than the folder with the library that is included.

 

5 - Go to step 2, and observe the file not found error.

 

I believe what is happening behind the scenes is that the path is changing when the Open dialog in Rhino is used, so the second time it runs it is looking for the script library in the folder that contains the part, instead of the folder that contains the code.

 

Thanks,


Carl.

 

' ---------------------------------------------------------------------------
' Subroutine:  Include
' Purpose:     Includes, or loads, other RhinoScript files
' Argument:    A script file name to include
' Example:     Include "C:\Scripts\MyScriptFile.rvb"
' ---------------------------------------------------------------------------
Sub Include(strScriptName)
 Dim objFSO, objFile
 Set objFSO = CreateObject("Scripting.FileSystemObject")
 Set objFile = objFSO.OpenTextFile(strScriptName)
 ExecuteGlobal objFile.ReadAll()
 objFile.Close
End Sub

Hi Carl,

When you do this:

  Call Include("mylibrary.rvb")

you are not providing much information in regards to where "mylibrary.rvb" is located. Thus, my suggestion of putting commonly used libraries in RhinoScript's list of script to load when Rhino loads. If you don't want to do this, then you might take a look at RhinoScript's FindFile function and make sure "mylibrary.rvb" is located on one of these folders. Then, you can so something like this:

  Call Include(Rhino.FindFile("mylibrary.rvb"))

 

-- Dale

 

Yea, I guess that's what I'll have to do.   I was kind of hoping there was some kind of rule that says, "If path not specified, assume the path to be the same folder as the current source being executed", so you could do things like say:

 

call Include ("MyLibrary.rvb") 

...to grab MyLibrary from same folder as the source that is executing.

 

or

 

call Include ("..\Library\MyLibrary.rvb") 

...to jump back one folder and enter the "library" folder to grab it.

 

or

 

call Include ("MyLibrary\MyLibrary.rvb") 

...to jump forward into the MyLibrary folder and grab it.

 

or of course

call Include ("C:\MyLibrary\MyLibrary.rvb") 

...to grab it from an absolute path (the way it works presently, which for my purposes is quite awkward.)

 

Thanks for your help on this.  It is very helpful in that I now know what I'm up against so that I can choose the appropriate strategy.

 

Thanks!


Carl.

Rhino.FindFile will to this kind of searching for you...

 

-- Dale

Sort of...provided the file is within Rhino's search path, if I understand Rhino.FindFile right.

 

I'll keep playing.  I think I now have enough info to work things out.

 

Thanks,


Carl.

You can add to Rhino's file search path, of couse. From Rhino, pick Tools -> Options -> Files.

 

-- Dale

RSS

Translate

© 2013   Created by McNeel Admin.

Badges  |  Report an Issue  |  Terms of Service