Python inside c# Visual Studio

Hello,

I have a lot of python scripts with many custom modules.py, as I’m diving into python I fill the limitations in relation to rhino,

The first one is the compiler+custom modules that alredy solve with .dll compile.

The second is the custom dockable panel, that I not found solution, the only solution
is make this panel on visual studio C# (twhich is a language I do not understand).

So,after thinking a little I pondered on a solution that can solve both, compilation and dockable panels.

There is possible insert Pyhton script into C#?

I know I can do a C # command to call a pyhton script, or even a compiled python script, but that’s not it.

I say integrate the python script inside of C # as a module?

Thanks

Any idea?

Maybe you just need the script compiler?

– dale

Thank you dale,

I use the scripter compile for scripts and dll custom modules, but the problem is the Dockable forms that is not
possible do in python,

So the ideia (because my C# is terrible), is make only the form on Visual studio and all the functions on Python.

Than I can join all-in-one plugin whit visual studio, maybe convert all Python scrit in .dll, and import to visual studio?,

I now that is possible import .dll reference for Visual studio, but , how call the .dll functions in visual studio?

This is possible??

Thank you

maybe take a look at this: https://www.red-gate.com/simple-talk/dotnet/net-framework/dynamic-language-integration-in-a-c-world/

Thank you

I was thinking something:

Put the .py file on visual studio reference or some other place that will be possible call and edit only by visual studio.
TextFile1.PY (80 Bytes)

And for call :
Rhino.RhinoApp.RunScript("TextFile1.PY ", false);

So all the files could be encrypted, and all installed at the same time by the pugin.

This is tecnical possible??

Thanks

Yes this is technically possible. You would create a C# based command and inside the command have some code that looked like

var script = Rhino.Runtime.PythonScript.Create();
script.ExecuteScript("my script goes here");

Your python scripts could be embedded resources in your plug-in. I always wanted to make an easy to use system for this, but other work always got in the way.

Hello Steve,

I embedded the “RunScritTeste.py” on Resources and follow your example:

    private void button4_Click(object sender, EventArgs e)

    {
        var script = Rhino.Runtime.PythonScript.Create();
     
        script.ExecuteScript("RunScritTeste.py");

    }

But by the error, I have to define the “RunScritTeste.py”, how I do that?

Thanks

OK,

I found the point of this command,

var script = Rhino.Runtime.PythonScript.Create();

script.ExecuteScript(“a=10\n”+“print (a)”);

This mode will be very painful, it will not be possible to change all my scripts to run this way :joy:

And if embedded python compile rhi on visual studio plugin? there is any solution to call then?

Maybe;
Rhino.RhinoApp.RunScript("-_RunPythonScript (resources.RunTestFile.py) ", false);

If you read a file, it will automatically have the \n (newline characters).

So you could probably do something like:

var script = File.ReadAllLines(@"C:\Path\To\Your\PythonFile.py");
script.ExecuteScript(yourScript);

I’m not 100% sure what @stevebaer means by embeddings scripts as a resource, but I imagine it would mean including them as a file in your resources.

If you make sure that you set the public resource to public you could use it something like this:

var script = Properties.Resources.NameYouGaveThePythonFile.ToString()
script.ExecuteScript(script);

(See the second reply for setting resources to public)

Ok this work:

        var assembly = Assembly.GetExecutingAssembly();
        var oscript = "SampleCsDockBar.Resources.RunScritTeste.py";
        using (Stream stream = assembly.GetManifestResourceStream(oscript)) 
        using (StreamReader reader = new StreamReader(stream)) 


        {
            string result = reader.ReadToEnd();

            var script = Rhino.Runtime.PythonScript.Create();
            script.ExecuteScript(result);

        }

Now the problem is going to be another (custom python modules).