Exemple #1
0
        /// <summary>
        /// This function is resposible for writing python files after
        /// gathering all inputs and outputs in a python-syntax form.
        /// </summary>
        /// <param name="WinForm"></param>
        public void writeReadPythonFile(GH_Component ThisComponent,
                                        PythonShell WinForm,
                                        IGH_DataAccess DA,
                                        int index,
                                        string path)
        {
            string variablesAre = "";
            string foot         = string.Empty;

            /// Section 1
            /// Initiate temporary Python file name that will be executed as well as the temporary folder.
            string name = "PythonFileWritten_" + index.ToString();

            try
            {
                variablesAre = ""; // Collecting the input variables here.

                /// Section 2
                /// Add the output variables' names and initiate them as None.
                for (int i = 0; i < ThisComponent.Params.Output.Count; i++)
                {
                    variablesAre += ThisComponent.Params.Output[i].NickName + @" = None\n";
                }

                /// Section 3.
                /// Collect input data names, and values then initiate them in a python syntax form as : "variableName = varibaleValue \n"
                for (int i = 0; i < ThisComponent.Params.Input.Count; i++)
                {
                    string datahere  = "";
                    string paramType = "";
                    try
                    {
                        paramType = ThisComponent.Params.Input[i].Sources[0].Type.ToString();
                    }
                    catch
                    {
                        paramType = ThisComponent.Params.Input[i].Type.ToString();
                    }

                    // GET input type at first
                    try
                    {
                        if (ThisComponent.Params.Input[i].Access == GH_ParamAccess.list /* &&
                                                                                         * (paramType == "Grasshopper.Kernel.Types.GH_Integer" ||
                                                                                         * paramType == "Grasshopper.Kernel.Types.GH_Number" ||
                                                                                         * paramType == "Grasshopper.Kernel.Types.GH_Boolean")*/)
                        {
                            //MessageBox.Show(ThisComponent.Params.Input[i].VolatileData.DataDescription(false, false));
                            string thisInputString = ThisComponent.Params.Input[i].VolatileData.DataDescription(false, false).
                                                     Trim().Replace(System.Environment.NewLine + System.Environment.NewLine, "],[").Replace(System.Environment.NewLine, ",");

                            datahere = recomposeInputString(thisInputString);

                            /*string thisInputString = ThisComponent.Params.Input[i].VolatileData.DataDescription(false, false).Trim().Replace(System.Environment.NewLine, ",");
                             * datahere = recomposeInputString(thisInputString);*/
                        }
                        else if (ThisComponent.Params.Input[i].Access == GH_ParamAccess.tree)
                        {
                            string thisInputString = ThisComponent.Params.Input[i].VolatileData.DataDescription(false, false).
                                                     Trim().Replace(System.Environment.NewLine + System.Environment.NewLine, "],[").Replace(System.Environment.NewLine, ",");
                            thisInputString = "[" + thisInputString + "]";

                            datahere = recomposeInputStringTree(thisInputString);
                            //MessageBox.Show(ThisComponent.Params.Input[i].VolatileData.DataDescription(false, false));
                        }
                        else
                        {
                            datahere += getInputs(ThisComponent, DA, i);
                        }
                    }
                    catch
                    {
                        datahere += "None";
                    }
                    if (datahere == "")
                    {
                        datahere = "None";
                    }
                    variablesAre += ThisComponent.Params.Input[i].NickName + " = " + datahere + @" \n";
                }
                foot = Resources.SavedPythonFile.savingFile;
                string thisOutputData = "";


                for (int i = 0; i < ThisComponent.Params.Output.Count; i++)
                {
                    if (i < ThisComponent.Params.Output.Count - 1)
                    {
                        thisOutputData += "\"" + ThisComponent.Params.Output[i].NickName + "\":" + ThisComponent.Params.Output[i].NickName + ", ";
                    }
                    else
                    {
                        thisOutputData += "\"" + ThisComponent.Params.Output[i].NickName + "\":" + ThisComponent.Params.Output[i].NickName;
                    }
                }
                foot = foot.Replace("##data##", thisOutputData);
                foot = foot.Replace("##fileName##", path.Replace(@"\", "/") + @"_PythonExecutionOrder_" + index.ToString() + @".xml");
            }
            catch (Exception exep)
            {
                //MessageBox.Show(exep.ToString());
            }

            /// Section 5.
            /// Save all data as a python file that will be run when the component is expired.
            string thisfilePath = "None";
            string thisfileName = "None";

            if (ThisComponent.OnPingDocument().IsFilePathDefined)
            {
                thisfilePath = ThisComponent.OnPingDocument().FilePath;
                thisfileName = ThisComponent.OnPingDocument().DisplayName;
            }
            else
            {
                thisfilePath = @"C:\GH_CPython";
                thisfileName = "unname";
            }



            string envVars = Resources.SavedPythonFile.initghEnv.Replace(Environment.NewLine, @"\n").Replace("##filePath##", thisfilePath.Replace(@"\", "/"));

            envVars      = envVars.Replace("##fileName##", thisfileName.Replace("*", ""));
            variablesAre = Resources.SavedPythonFile.initVars.Replace("##InitVars##", variablesAre.Replace("'", @"\'")).Replace("##initGHENV##", envVars);
            System.IO.File.WriteAllText(path + name + ".py", variablesAre + "\n" + WinForm.PythonCanvas.Text + "\n" + foot);
        }
Exemple #2
0
        /// <summary>
        /// Constructor
        /// </summary>
        public Gh_CPythonComponent()
            : base("GH_CPython", "GH_CPython",
                   "a python IDE interface",
                   "Maths", "Script")
        {
            initfunc = new InitFunctions();

            //1- Look for default GH_CPython path i.e.'C:\GH_CPython\'
            path = initfunc.isPythonFloderExists(@"C:\GH_CPython\");

            //2- Look for and set Python.exe interpreter if not set
            StartFileName = initfunc.getPythonInterpretere(@"C:\GH_CPython\interpreter.dat");

            //3- Add latest version of required python modules if not existed
            initfunc.addGrasshopperPyModule(@"C:\GH_CPython\Grasshopper.py", pythonVersion);

            // initiate python IDE instance
            PythonIDE = new PythonShell();

            PythonIDE.TopMost = true;


            PreviewExpired += Gh_CPythonComponent_PreviewExpired;

            thisIndex = Globals.index;

            name = "PythonFileWritten_" + thisIndex.ToString();

            Globals.fileName.Add(thisIndex, "_PythonExecutionOrder_" + thisIndex.ToString());
            Globals.index += 1;
            Globals.OpenThisShell.Add(thisIndex, false);

            ///Initiate Python process options.
            ///Don't show Shell - Redirect Standard output - Redirect Standard error - Hide Shell
            RunningPythonProcess.StartInfo.UseShellExecute        = false;
            RunningPythonProcess.StartInfo.RedirectStandardOutput = true;
            RunningPythonProcess.StartInfo.RedirectStandardError  = true;
            RunningPythonProcess.StartInfo.WindowStyle            = ProcessWindowStyle.Hidden;
            RunningPythonProcess.StartInfo.UseShellExecute        = false;
            RunningPythonProcess.StartInfo.CreateNoWindow         = true;


            /// Initiate Python IDE Editor text, it should be like so: - Change if you wish-
            //# -*- coding: utf-8 -*-
            //"""
            //Python Script
            //Created on  Tuesday August 2017 12:22:25
            //@author:  UserName
            //"""

            at = DateTime.Now.ToString("dddd MMMM yyyy hh:mm:ss");
            string Name = System.Environment.UserName;

            InitialPythonText = Resources.SavedPythonFile.Shellinit.Replace("##CreatedBy##", Name);
            InitialPythonText = InitialPythonText.Replace("##at##", at);

            try
            {
                /// Initiate Console data as follows
                /// "Hi UserName, How are you ?"
                PythonIDE.console.Text = "Hi " + Name + ", How are you ?";


                /// retrievedData are the data that are saved just after closing the Form (either by clicking x or close)
                /// They are saved here and then retrieved just after reopening the form again.
                if (writtenText != "")
                {
                    PythonIDE.PythonCanvas.Text = writtenText;
                    retrievedData = writtenText;
                }
                else if (retrievedData != "")
                {
                    PythonIDE.PythonCanvas.Text = retrievedData;
                    writtenText = retrievedData;
                }
                else
                {
                    PythonIDE.PythonCanvas.Text = InitialPythonText;
                    retrievedData = InitialPythonText;
                    writtenText   = InitialPythonText;
                }

                /// This function reads all the input data, then initiates it in python syntax
                /// this refrers to the present winForm i.e. the python IDE.
                /// writeReaadPythonFile function needs a lot of work to handle different inputs in a proper way

                /// EventHandler of the Form Closing
                PythonIDE.FormClosing += Ps_FormClosing;

                PythonIDE.manageDataItem.Click += manageDataItem_Click;
                /// Handleing Test button click.
                PythonIDE.Test.Click += (se, ev) =>
                {
                    AddNamesAndDescriptions();
                    //writeReadPythonFile(this);
                    ExpireSolution(true);
                };

                PythonIDE.PythonCanvas.KeyDown += PythonCanvas_KeyDown;
                PythonIDE.close.Click          += (se, ev) =>
                {
                    InitialPythonText = PythonIDE.PythonCanvas.Text;
                    shellOpened       = false;
                    PythonIDE.Hide();
                    AddNamesAndDescriptions();
                    Globals.OpenThisShell[thisIndex] = false;
                    Grasshopper.Instances.RedrawCanvas();
                };

                Grasshopper.Instances.RedrawCanvas();
            }
            catch { }

            AddNamesAndDescriptions();
        }
Exemple #3
0
        /// <summary>
        /// Constructor
        /// </summary>
        public Gh_CPythonComponent()
            : base("GH_CPython", "GH_CPython",
                   "a python IDE interface",
                   "Maths", "Script")
        {
            PythonIDE         = new PythonShell();
            PythonIDE.TopMost = true;

            PreviewExpired += Gh_CPythonComponent_PreviewExpired;

            thisIndex = Globals.index;

            name = "PythonFileWritten_" + thisIndex.ToString();



            Globals.fileName.Add(thisIndex, "_PythonExecutionOrder_" + thisIndex.ToString());
            Globals.index += 1;
            Globals.OpenThisShell.Add(thisIndex, false);


            // Verify that Python is installed on the machine properly.

            if (File.Exists(@"C:\Python27\python.exe"))
            {
                StartFileName = @"C:\Python27\python.exe";
            }
            else if (File.Exists(@"C:\Anaconda\python.exe"))
            {
                StartFileName = @"C:\Anaconda\python.exe";
            }
            else if (File.Exists(@"C:\Python34\python.exe"))
            {
                StartFileName = @"C:\Python34\python.exe";
            }
            else if (File.Exists(@"C:\Python35\python.exe"))
            {
                StartFileName = @"C:\Python35\python.exe";
            }
            else if (File.Exists(@"C:\Python36\python.exe"))
            {
                StartFileName = @"C:\Python36\python.exe";
            }

            else if (File.Exists(@"C:\Program Files (x86)\Python27\python.exe"))
            {
                StartFileName = @"C:\Program Files (x86)\Python27\python.exe";
            }
            else if (File.Exists(@"C:\Program Files (x86)\Python34\python.exe"))
            {
                StartFileName = @"C:\Program Files (x86)\Python34\python.exe";
            }
            else if (File.Exists(@"C:\Program Files (x86)\Python35\python.exe"))
            {
                StartFileName = @"C:\Program Files (x86)\Python35\python.exe";
            }
            else if (File.Exists(@"C:\Program Files (x86)\Python36\python.exe"))
            {
                StartFileName = @"C:\Program Files (x86)\Python36\python.exe";
            }

            else if (File.Exists(@"C:\Program Files\Python27\python.exe"))
            {
                StartFileName = @"C:\Program Files\Python27\python.exe";
            }
            else if (File.Exists(@"C:\Program Files\Python34\python.exe"))
            {
                StartFileName = @"C:\Program Files\Python34\python.exe";
            }
            else if (File.Exists(@"C:\Program Files\Python35\python.exe"))
            {
                StartFileName = @"C:\Program Files\Python35\python.exe";
            }
            else if (File.Exists(@"C:\Program Files\Python36\python.exe"))
            {
                StartFileName = @"C:\Program Files\Python36\python.exe";
            }

            else
            {
                MessageBox.Show("Sorry, We can't find Python installed on your machine, If you have already installed it, would you contact Mahmoud Abdlerahman via this e-mail \n [email protected]\n Thanks.");
            }


            ///Initiate Python process options.
            ///Don't show Shell - Redirect Standard output - Redirect Standard error - Hide Shell
            RunningPythonProcess.StartInfo.UseShellExecute        = false;
            RunningPythonProcess.StartInfo.RedirectStandardOutput = true;
            RunningPythonProcess.StartInfo.RedirectStandardError  = true;
            RunningPythonProcess.StartInfo.WindowStyle            = ProcessWindowStyle.Hidden;
            RunningPythonProcess.StartInfo.UseShellExecute        = false;
            RunningPythonProcess.StartInfo.CreateNoWindow         = true;

            /// Initiate Python IDE Editor text, it should be like so: - Change if you wish-
            //# -*- coding: utf-8 -*-
            //"""
            //Python Script
            //Created on  Tuesday August 2017 12:22:25
            //@author:  UserName
            //"""

            at = DateTime.Now.ToString("dddd MMMM yyyy hh:mm:ss");
            string Name = System.Environment.UserName;

            InitialPythonText = Resources.SavedPythonFile.Shellinit.Replace("##CreatedBy##", Name);
            InitialPythonText = InitialPythonText.Replace("##at##", at);

            try
            {
                /// Initiate Console data as follows
                /// "Hi UserName, How are you ? Are you ready to Change the world ?"
                PythonIDE.console.Text = "Hi " + Name + ", How are you ? Are you ready to Change the world ?";


                /// retrievedData are the data that are saved just after closing the Form (either by clicking x or close)
                /// They are saved here and then retrieved just after reopening the form again.
                if (writtenText != "")
                {
                    PythonIDE.PythonCanvas.Text = writtenText;
                    retrievedData = writtenText;
                }
                else if (retrievedData != "")
                {
                    PythonIDE.PythonCanvas.Text = retrievedData;
                    writtenText = retrievedData;
                }
                else
                {
                    PythonIDE.PythonCanvas.Text = InitialPythonText;
                    retrievedData = InitialPythonText;
                    writtenText   = InitialPythonText;
                }

                /// This function reads all the input data, then initiates it in python syntax
                /// this refrers to the present winForm i.e. the python IDE.
                /// writeReaadPythonFile function needs a lot of work to handle different inputs in a proper way
                writeReadPythonFile(this);

                /// EventHandler of the Form Closing
                PythonIDE.FormClosing += Ps_FormClosing;

                /// Handleing Test button click.
                PythonIDE.Test.Click += (se, ev) =>
                {
                    AddNamesAndDescriptions();
                    //writeReadPythonFile(this);
                    ExpireSolution(true);
                };

                PythonIDE.close.Click += (se, ev) =>
                {
                    InitialPythonText = PythonIDE.PythonCanvas.Text;
                    shellOpened       = false;
                    PythonIDE.Hide();
                    AddNamesAndDescriptions();
                    Globals.OpenThisShell[thisIndex] = false;
                    Grasshopper.Instances.RedrawCanvas();
                };

                Grasshopper.Instances.RedrawCanvas();
            }
            catch (Exception erx)
            {
                this.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, erx.ToString());
                //MessageBox.Show(erx.ToString());
            }
            AddNamesAndDescriptions();
        }
Exemple #4
0
        /// <summary>
        /// Constructor
        /// </summary>
        public PythonInterfaceComponent()
            : base("GH_CPython", "GH_CPython",
                   "a python IDE interface",
                   "Maths", "Script")
        {
            PythonIDE         = new PythonShell();
            PythonIDE.TopMost = true;
            thisIndex         = Globals.index;

            name = "PythonFileWritten_" + thisIndex.ToString();

            Globals.fileName.Add(thisIndex, "_PythonExecutionOrder_" + thisIndex.ToString());
            Globals.index += 1;
            Globals.OpenThisShell.Add(thisIndex, false);


            ///Initiate Python process options.
            ///Don't show Shell - Redirect Standard output - Redirect Standard error - Hide Shell
            RunningPythonProcess.StartInfo.UseShellExecute        = false;
            RunningPythonProcess.StartInfo.RedirectStandardOutput = true;
            RunningPythonProcess.StartInfo.RedirectStandardError  = true;
            RunningPythonProcess.StartInfo.WindowStyle            = ProcessWindowStyle.Hidden;
            RunningPythonProcess.StartInfo.UseShellExecute        = false;
            //RunningPythonProcess.StartInfo.CreateNoWindow = true;



            /// This adds Python folder to Windows Environment Paths variables.
            /// if it exists, set python as the process file name , else, set python folders and then set python as the process filename
            if (ExistsOnPath("python.exe"))
            {
                RunningPythonProcess.StartInfo.FileName = "python.exe";
            }
            else
            {
                string pp    = System.Environment.GetEnvironmentVariable("PATH");
                string path_ = pp + @";C:\Python27;C:\Python27\DLLs;C:\Python27\Scripts";
                System.Environment.SetEnvironmentVariable("PATH", path_);

                RunningPythonProcess.StartInfo.FileName = "python.exe";
            }


            /// Initiate Python IDE Editor text, it should be like so: - Change if you wish-

            /*  # -*- coding: utf-8 -*-
             *  """
             *  Python Script
             *  Created on  Tuesday August 2017 12:22:25
             *  @author:  UserName
             *  """
             */


            at = DateTime.Now.ToString("dddd MMMM yyyy hh:mm:ss");
            string Name = System.Environment.UserName;

            InitialPythonText = Resources.SavedPythonFile.Shellinit.Replace("##CreatedBy##", Name);
            InitialPythonText = InitialPythonText.Replace("##at##", at);



            try
            {
                /// Initiate Console data as follows
                /// "Hi UserName, How are you ? Are you ready to Change the world ?"
                PythonIDE.console.Text = "Hi " + Name + ", How are you ? Are you ready to Change the world ?";


                /// retrievedData are the data that are saved just after closing the Form (either by clicking x or close)
                /// They are saved here and then retrieved just after reopening the form again.
                if (writtenText != "")
                {
                    PythonIDE.PythonCanvas.Text = writtenText;
                    retrievedData = writtenText;
                }
                else if (retrievedData != "")
                {
                    PythonIDE.PythonCanvas.Text = retrievedData;
                    writtenText = retrievedData;
                }
                else
                {
                    PythonIDE.PythonCanvas.Text = InitialPythonText;
                    retrievedData = InitialPythonText;
                    writtenText   = InitialPythonText;
                }

                /// This function reads all the input data, then initiates it in python syntax
                /// this refrers to the present winForm i.e. the python IDE.
                /// writeReaadPythonFile function needs a lot of work to handle different inputs in a proper way
                writeReadPythonFile(this);

                /// EventHandler of the Form Closing
                PythonIDE.FormClosing += Ps_FormClosing;

                /// Handleing Test button click.
                PythonIDE.Test.Click += (se, ev) =>
                {
                    //writeReadPythonFile(this);
                    ExpireSolution(true);
                };

                PythonIDE.close.Click += (se, ev) =>
                {
                    InitialPythonText = PythonIDE.PythonCanvas.Text;
                    shellOpened       = false;
                    PythonIDE.Hide();

                    Globals.OpenThisShell[thisIndex] = false;
                    Grasshopper.Instances.RedrawCanvas();
                };

                Grasshopper.Instances.RedrawCanvas();
            }
            catch (Exception erx)
            {
                MessageBox.Show(erx.ToString());
            }
        }