Ejemplo n.º 1
0
        protected override void Initialize()
        {
            base.Initialize();

            if (Doc != null)
            {
                Doc.SolutionEnd += OnDocSolutionEnd;
            }

            m_py = PythonScript.Create();
            if (m_py != null)
            {
                SetScriptTransientGlobals();
                m_py.Output = m_py_output.Write;
                m_py.SetVariable("__name__", "__main__");
                m_env = new PythonEnvironment(this, m_py);

                m_py.SetVariable(PARENT_ENVIRONMENT_NAME, m_env);
                m_py.SetIntellisenseVariable(PARENT_ENVIRONMENT_NAME, m_env);

                m_py.ContextId = 2;                                      // 2 is Grasshopper

                m_env.LoadAssembly(typeof(GH_Component).Assembly);       //add Grasshopper.dll reference
                m_env.LoadAssembly(typeof(ZuiPythonComponent).Assembly); //add GHPython.dll reference

                UnpackScriptResources();

                m_env.AddGhPythonPackage();
            }
        }
        protected override void Initialize()
        {
            base.Initialize();

            if (Doc != null)
            {
                Doc.SolutionEnd += OnDocSolutionEnd;
            }

            // ksteinfe
            _py = PythonScript.Create();
            if (_py != null)
            {
                SetScriptTransientGlobals();
                _py.Output = m_py_output.Write;
                _py.SetVariable("__name__", "__main__");
                _env = new PythonEnvironment(this, _py);
                _py.SetVariable(PARENT_ENVIRONMENT_NAME, _env);
                _py.SetIntellisenseVariable(PARENT_ENVIRONMENT_NAME, _env);

                _py.ContextId = 2; // 2 is Grasshopper
            }
        }
Ejemplo n.º 3
0
        protected override void SafeSolveInstance(IGH_DataAccess da)
        {
            m_env.DataAccessManager = da;

            if (m_py == null)
            {
                da.SetData(0, "No Python engine available. This component needs Rhino v5");
                return;
            }

            if (!HiddenOutOutput)
            {
                da.DisableGapLogic(0);
            }

            m_py_output.Reset();

            var rhdoc       = RhinoDoc.ActiveDoc;
            var prevEnabled = (rhdoc != null) && rhdoc.Views.RedrawEnabled;

            try
            {
                // set output variables to "None"
                for (int i = HiddenOutOutput ? 0 : 1; i < Params.Output.Count; i++)
                {
                    string varname = Params.Output[i].NickName;
                    m_py.SetVariable(varname, null);
                }

                // caching variable to keep things as fast as possible
                bool showing_code_input = !HiddenCodeInput;
                // Set all input variables. Even null variables may be used in the
                // script, so do not attempt to skip these for optimization purposes.
                // Skip "Code" input parameter
                // Please pay attention to the input data structure type
                for (int i = showing_code_input ? 1 : 0; i < Params.Input.Count; i++)
                {
                    string varname = Params.Input[i].NickName;
                    object o       = m_marshal.GetInput(da, i);
                    m_py.SetVariable(varname, o);
                    m_py.SetIntellisenseVariable(varname, o);
                }

                // the "code" string could be embedded in the component itself
                if (showing_code_input || m_compiled_py == null)
                {
                    string script;
                    if (!showing_code_input)
                    {
                        script = Code;
                    }
                    else
                    {
                        script = null;
                        da.GetData(0, ref script);
                    }

                    if (string.IsNullOrWhiteSpace(script))
                    {
                        AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "No script to execute");
                        return;
                    }

                    if (m_compiled_py == null ||
                        string.Compare(script, m_previousRunCode, StringComparison.InvariantCulture) != 0)
                    {
                        if (!(m_inDocStringsMode = DocStringUtils.FindApplyDocString(script, this)))
                        {
                            ResetAllDescriptions();
                        }
                        m_compiled_py     = m_py.Compile(script);
                        m_previousRunCode = script;
                    }
                }

                if (m_compiled_py != null)
                {
                    string localPath;
                    bool   added = AddLocalPath(out localPath);

                    m_compiled_py.Execute(m_py);

                    if (added)
                    {
                        RemoveLocalPath(localPath);
                    }

                    // Python script completed, attempt to set all of the
                    // output paramerers
                    for (int i = HiddenOutOutput ? 0 : 1; i < Params.Output.Count; i++)
                    {
                        string varname = Params.Output[i].NickName;
                        object o       = m_py.GetVariable(varname);
                        m_marshal.SetOutput(o, da, i);
                    }
                }
                else
                {
                    m_py_output.Write("There was a permanent error parsing this script. Please report to [email protected].");
                }
            }
            catch (Exception ex)
            {
                AddErrorNicely(m_py_output, ex);
                SetFormErrorOrClearIt(da, m_py_output);
                throw;
            }
            finally
            {
                if (rhdoc != null && prevEnabled != rhdoc.Views.RedrawEnabled)
                {
                    rhdoc.Views.RedrawEnabled = true;
                }
            }
            SetFormErrorOrClearIt(da, m_py_output);
        }