public GhAlgorithm() { // In the constructor, we will build the input files RhinoModel.Initialize(); // Gets the current GH file. Will throw if not available GrasshopperFullFileName = RhinoModel.RM.GrasshopperFullFileName; WpfGrasshopperFileDescription = RhinoModel.RM.GrasshopperDescription; #region Gets the Grasshopper Input List GrasshopperAllEmasaInputDefsWrapper_AsRhino3dm inputDefs = RhinoModel.RM.Grasshopper_GetAllEmasaInputDefs(); // Integers foreach (string integerInput in inputDefs.IntegerInputs) { Integer_GhConfig_ParamDef intParam = new Integer_GhConfig_ParamDef(inName: integerInput); ConfigDefs.Add(intParam); } // Doubles foreach (var doubleInput in inputDefs.DoubleInputs) { Double_Input_ParamDef bdlInputParam = new Double_Input_ParamDef(inName: doubleInput.Key, inRange: new DoubleValueRange(doubleInput.Value.Item2, doubleInput.Value.Item3)); bdlInputParam.Start = doubleInput.Value.Item1; AddParameterToInputs(bdlInputParam); } // Points foreach (var pointInputs in inputDefs.PointInputs) { Point_Input_ParamDef pntParam = new Point_Input_ParamDef(inName: pointInputs.Key, inRange: new PointValueRange(pointInputs.Value.Item2, pointInputs.Value.Item3)); pntParam.Start = pointInputs.Value.Item1; AddParameterToInputs(pntParam); break; } #endregion #region Gets the Grasshopper Output List GrasshopperAllEmasaOutputWrapper_AsRhino3dm outputDefs = RhinoModel.RM.Grasshopper_GetAllEmasaOutputs(); // Line Lists foreach (var lineList in outputDefs.LineLists) { GeometryDefs.Add(new LineList_GhGeom_ParamDef(lineList.Key)); } // Double Lists foreach (var doubleList in outputDefs.DoubleLists) { DoubleList_GhGeom_ParamDef dlParam = new DoubleList_GhGeom_ParamDef(doubleList.Key); GeometryDefs.Add(dlParam); } // Point Lists foreach (var pointList in outputDefs.PointLists) { GeometryDefs.Add(new PointList_GhGeom_ParamDef(pointList.Key)); } #endregion #region Setting the WPF collection views InputDefs_View = CollectionViewSource.GetDefaultView(InputDefs); InputDefs_View.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending)); InputDefs_View.GroupDescriptions.Add(new PropertyGroupDescription("TypeName")); ConfigDefs_View = CollectionViewSource.GetDefaultView(ConfigDefs); ConfigDefs_View.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending)); CollectionViewSource configDefs_Integer_Cvs = new CollectionViewSource() { Source = ConfigDefs }; ConfigDefs_Integer_View = configDefs_Integer_Cvs.View; ConfigDefs_Integer_View.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending)); ConfigDefs_Integer_View.Filter += inO => inO is Integer_GhConfig_ParamDef; HasConfigDefs_Integer = ConfigDefs_Integer_View.IsEmpty ? Visibility.Collapsed : Visibility.Visible; CollectionViewSource geometryDefs_PointList_Cvs = new CollectionViewSource() { Source = GeometryDefs }; GeometryDefs_PointList_View = geometryDefs_PointList_Cvs.View; GeometryDefs_PointList_View.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending)); //GeometryDefs_PointList_View.GroupDescriptions.Add(new PropertyGroupDescription("TypeName")); GeometryDefs_PointList_View.Filter += inO => inO is PointList_GhGeom_ParamDef; HasGeometryDef_PointList = GeometryDefs_PointList_View.IsEmpty ? Visibility.Collapsed : Visibility.Visible; GeometryDefs_LineList_View = (new CollectionViewSource() { Source = GeometryDefs }).View; GeometryDefs_LineList_View.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending)); //GeometryDefs_LineList_View.GroupDescriptions.Add(new PropertyGroupDescription("TypeName")); GeometryDefs_LineList_View.Filter += inO => inO is LineList_GhGeom_ParamDef; HasGeometryDef_LineList = GeometryDefs_LineList_View.IsEmpty ? Visibility.Collapsed : Visibility.Visible; GeometryDefs_DoubleList_View = (new CollectionViewSource() { Source = GeometryDefs }).View; GeometryDefs_DoubleList_View.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending)); //GeometryDefs_DoubleList_View.GroupDescriptions.Add(new PropertyGroupDescription("TypeName")); GeometryDefs_DoubleList_View.Filter += inO => inO is DoubleList_GhGeom_ParamDef; HasGeometryDef_DoubleList = GeometryDefs_DoubleList_View.IsEmpty ? Visibility.Collapsed : Visibility.Visible; GeometryDefs_PointLineListBundle_View = (new CollectionViewSource() { Source = GeometryDefs }).View; GeometryDefs_PointLineListBundle_View.SortDescriptions.Add(new SortDescription("TypeName", ListSortDirection.Ascending)); GeometryDefs_PointLineListBundle_View.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending)); GeometryDefs_PointLineListBundle_View.Filter += inO => inO is LineList_GhGeom_ParamDef || inO is PointList_GhGeom_ParamDef; #endregion }
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(); } }