Пример #1
0
        public void UpdateGrasshopperGeometry([NotNull] NlOpt_Point inSolPoint)
        {
            if (inSolPoint == null)
            {
                throw new ArgumentNullException(nameof(inSolPoint));
            }

            // Data that will be sent to Grasshopper
            Dictionary <string, object> inValuePairs = new Dictionary <string, object>();

            // Writes the data to the config files
            foreach (Integer_GhConfig_ParamDef intGhConfig in ConfigDefs.OfType <Integer_GhConfig_ParamDef>())
            {
                inValuePairs.Add(intGhConfig.Name, inSolPoint.Owner.GetGhIntegerConfig(intGhConfig));
                //string inputVarFilePath = Path.Combine(GhInputDirPath, $"{intGhConfig.Name}.{intGhConfig.TypeName}");

                //try
                //{
                //    // Finds the configuration value

                //    //object configValue = AppSS.I.SolveMgr.CurrentCalculatingProblemConfig.GetGhIntegerConfig(intGhConfig);
                //    object configValue = inSolPoint.Owner.GetGhIntegerConfig(intGhConfig);
                //    File.WriteAllText(inputVarFilePath, configValue.ToString());
                //}
                //catch (Exception e)
                //{
                //    throw new Exception($"Could not write the Grasshopper configuration input {intGhConfig.Name} to {inputVarFilePath}.", e);
                //}
            }

            // Writes the data to the input files
            foreach (Input_ParamDefBase input_ParamDefBase in InputDefs)
            {
                inValuePairs.Add(input_ParamDefBase.Name, inSolPoint.GhInput_Values[input_ParamDefBase]);
                //string inputVarFilePath = Path.Combine(GhInputDirPath, $"{input_ParamDefBase.Name}.{input_ParamDefBase.TypeName}");
                //try
                //{
                //    object solPointValue = inSolPoint.GhInput_Values[input_ParamDefBase];
                //    File.WriteAllText(inputVarFilePath, solPointValue.ToString());
                //}
                //catch (Exception e)
                //{
                //    throw new Exception($"Could not write the Grasshopper input {input_ParamDefBase.Name} to {inputVarFilePath}.", e);
                //}
            }

            try
            {
                // Updates the input and Runs Grasshopper
                //RhinoModel.RM.SolveGrasshopper();
                RhinoModel.RM.Grasshopper_UpdateEmasaInputs(inValuePairs, true);
            }
            catch (Exception e)
            {
                throw new COMException("Could not update the input parameters and solve the Grasshopper Algorithm. Error in the COM interface.", e);
            }

            // Checks if the Grasshopper result has anything to say
            string[,] ghMessages = RhinoModel.RM.Grasshopper_GetDocumentMessages();
            if (ghMessages != null)
            {
                string errors = string.Empty;

                // Adds the message to the buffer
                for (int i = 0; i < ghMessages.GetLength(0); i++)
                {
                    switch (ghMessages[i, 0])
                    {
                    case "Error":
                        errors += $"{ghMessages[i, 1]}{Environment.NewLine}";
                        break;

                    case "Warning":
                        inSolPoint.RuntimeWarningMessages.Add(new NlOpt_Point_Message(ghMessages[i, 1], NlOpt_Point_MessageSourceEnum.Grasshopper, NlOpt_Point_MessageLevelEnum.Warning));
                        break;

                    case "Remark":
                        inSolPoint.RuntimeWarningMessages.Add(new NlOpt_Point_Message(ghMessages[i, 1], NlOpt_Point_MessageSourceEnum.Grasshopper, NlOpt_Point_MessageLevelEnum.Remark));
                        break;

                    default:
                        errors += $"A grasshopper message type was unexpected: {ghMessages[i, 0]}. Message: {ghMessages[i, 1]}{Environment.NewLine}";
                        break;
                    }
                }

                // There was an error, so we must throw
                if (!string.IsNullOrWhiteSpace(errors))
                {
                    throw new Exception($"Grasshopper errors. {Environment.NewLine}{errors}");
                }
            }
        }