示例#1
0
        private PointValueRange ReadPointValueRange(string inFileName)
        {
            try
            {
                string[] fileLines = File.ReadAllLines(inFileName);
                if (fileLines.Length != 2)
                {
                    throw new Exception();
                }

                if (!RhinoStaticMethods.TryParsePoint3d(fileLines[0], out Point3d min))
                {
                    throw new Exception();
                }
                if (!RhinoStaticMethods.TryParsePoint3d(fileLines[1], out Point3d max))
                {
                    throw new Exception();
                }

                return(new PointValueRange(min, max));
            }
            catch (Exception e)
            {
                throw new IOException($"{MethodBase.GetCurrentMethod().Name}: Could not read from file {inFileName}.", e);
            }
        }
示例#2
0
 private Point3d ReadPoint(string inFileName)
 {
     try
     {
         if (!RhinoStaticMethods.TryParsePoint3d(File.ReadAllText(inFileName), out Point3d p))
         {
             throw new Exception();
         }
         return(p);
     }
     catch (Exception e)
     {
         throw new IOException($"{MethodBase.GetCurrentMethod().Name}: Could not read from file {inFileName}.", e);
     }
 }
示例#3
0
 private List <Point3d> ReadPoints(string inFileName)
 {
     try
     {
         string[] fileLines = File.ReadAllLines(inFileName);
         return(fileLines.Select(a =>
         {
             if (!RhinoStaticMethods.TryParsePoint3d(a, out Point3d p))
             {
                 throw new Exception();
             }
             return p;
         }).ToList());
     }
     catch (Exception e)
     {
         throw new IOException($"{MethodBase.GetCurrentMethod().Name}: Could not read from file {inFileName}.", e);
     }
 }
示例#4
0
        public override async void SetOrReset()
        {
            try
            {
                // Puts the active Rhino Instance in the Singleton
                RhinoModel.Initialize();
                RhinoModel.RM.RhinoVisible = true;

                // Gets the current GH file. Will throw if not available
                GrasshopperFullFileName = RhinoModel.RM.GrasshopperFullFileName;

                // Adding a list of Problems that have been implemented
                _possibleProblems = new List <ProblemBase>()
                {
                    new FindTriangleProblem(),
                    new BestArchProblem()
                };
            }
            catch (Exception ex)
            {
                ExceptionViewer.Show(ex, "Rhino/Grasshopper Initialization Issue.");
                Application.Current.Shutdown(1);
            }

            try
            {
                OnBeginCommand();

                void lf_Work()
                {
                    CustomOverlayBindings.I.Title = "Getting the Grasshopper Input and Output variable list.";

                    #region Managing the GH Parameters
                    // First, gets a list of the input variables and their types from the GH definition
                    List <(string Name, string Type)> tmpFolderInputVars = new List <(string Name, string Type)>();
                    string inputVarFolder = RhinoStaticMethods.GH_Auto_InputVariableFolder(RhinoModel.RM.GrasshopperFullFileName);
                    foreach (string inputFile in Directory.GetFiles(inputVarFolder))
                    {
                        string name      = Path.GetFileNameWithoutExtension(inputFile);
                        string extension = Path.GetExtension(inputFile).Trim(new[] { '.' });

                        tmpFolderInputVars.Add((name, extension));
                    }
                    if (tmpFolderInputVars.Count == 0)
                    {
                        throw new Exception($"Could not find input definitions for the current Grasshopper file.");
                    }

                    // First, gets a list of the output variables and their types from the GH definition
                    List <(string Name, string Type)> tmpFolderOutputVars = new List <(string Name, string Type)>();
                    string outputVarFolder = RhinoStaticMethods.GH_Auto_OutputVariableFolder(RhinoModel.RM.GrasshopperFullFileName);
                    foreach (string outputFile in Directory.GetFiles(outputVarFolder))
                    {
                        string name      = Path.GetFileNameWithoutExtension(outputFile);
                        string extension = Path.GetExtension(outputFile).Trim(new[] { '.' });

                        tmpFolderOutputVars.Add((name, extension));
                    }
                    if (tmpFolderOutputVars.Count == 0)
                    {
                        throw new Exception($"Could not find intermediate (Grasshopper outputs) definitions for the current Grasshopper file.");
                    }

                    CustomOverlayBindings.I.Title = "Finding, in the problem library, a problem that can solve this Grasshopper file.";

                    // Marks the problems that match the current GH file
                    int countValid = 0;
                    foreach (ProblemBase problem in _possibleProblems)
                    {
                        if (!problem.ObjectiveFunction.InputDefs.All(a =>
                                                                     tmpFolderInputVars.Any(b =>
                                                                                            b.Name == a.Name && b.Type == a.TypeName)))
                        {
                            continue;
                        }

                        if (!problem.ObjectiveFunction.IntermediateDefs.All(a =>
                                                                            tmpFolderOutputVars.Any(b =>
                                                                                                    b.Name == a.Name && b.Type == a.TypeName)))
                        {
                            continue;
                        }

                        problem.SolvesCurrentGHFile = true;
                        countValid++;
                    }

                    // Checks the number of valid problems
                    if (countValid == 0)
                    {
                        throw new Exception($"Could not find any problem that has the variables matching the ones given in the Grasshopper file.{Environment.NewLine}Please select a valid Grasshopper file or write the problem to this geometry.");
                    }

                    if (countValid > 1)
                    {
                        throw new Exception($"Found two or more possible problems for this Grasshopper file. This is currently not supported.");
                    }

                    // We only found one possible Problem, thus we are good to go.


                    // Finds a file that has been saved and loads the current status
                    if (File.Exists(RhinoStaticMethods.GH_Auto_SavedStateFileFull()))
                    {
                        throw new NotImplementedException($"Does not support Loading - yet.");
                    }
                    else // Could not find any problem that has been saved - Sets the default one as the current
                    {
                        CurrentProblem = _possibleProblems.First(a => a.SolvesCurrentGHFile);
                    }
                    #endregion

                    // Asks the main window to come back to the front
                    EventAggregatorSingleton.I.GetEvent <BindGenericCommandEvent>().Publish(new BindCommandEventArgs(this, "ActivateWindow"));
                }

                // Runs the job async
                Task task = new Task(lf_Work);
                task.Start();
                await task;
            }
            catch (Exception ex)
            {
                ExceptionViewer.Show(ex);
                Application.Current.Shutdown(1);
            }
            finally
            {
                OnEndCommand();
            }
        }