GH: Extract/Dump Script component's code to clipboard or file

rhino5

#1

I wonder if there’s any property on the C# or VB.NET Script Components (not compiled components) that can be used to retrieve the code.

Why? I’d like to extract the code from the components and dump it to .cs / .vb files for version control etc.

// Rolf


#2

@DavidRutten, I’ve seen examples of ScriptComponents exposing SourceCode but it seems that GrassHopper.GUI.Script.GH_ScriptEditor doesn’t expose them anymore?

Fig1. Example :
bild


Edit: OK, the property GetSourceCode do exist, but, I can’t cast the variable to a known valid type.


I have also seen examples of using a library named

using ScriptComponents;

… which seems to contain a type ("Component_CSNET_Script")which I cannot access without that library. Another example:

var scriptComponent = (Component_CSNET_Script)Grasshopper.Instances.ActiveCanvas.Document.Find… ;

Difficult this for a newbie on .NET. So how can I retrieve the source code from a (C# or VB.Script) Script Component? I understand that I could retrieve the serialized xml for the component, but I don’t know how to get myself a valid reader for the call comp.Read(reader).

This is what I’ve so far at a breakpoint (a valid script component “comp” dynamic cast to “c”, but the type shown in the debog panels is not available in code, so I can’t access the ScriptSource property… ) :


Edit:
Another example of that I have a paused execution on the correct type of (script) component, which I unfortunately cannot cast properly :frowning: :

// Rolf


(David Rutten) #3

The actual implementations of the script component (i.e. the VB and C# components that you use) are defined in ScriptLibrary.gha. To access this stuff you’d need to reference that assembly.

However you can use Reflection to just access the fields without importing the assembly. Have a look at this file: scriptsource.gh (8.2 KB)

ps. The Grasshopper.Utility class I’m using is basically just a thin wrapper around Reflection functionality which takes care of a lot of annoying boilerplate code.


#4

Aha, a two-step reflection into Object type. I just tried the first step comp.GetMethod("ScriptSource").ToString(), but that didn’t do the trick. ScriptSource.gh looks more promising though.


EDIT:
- Perfetto! :+1:

// Rolf