Ejemplo n.º 1
0
        public void UpdateGrasshopperGeometryAndGetResults([NotNull] NlOpt_Point inSolPoint)
        {
            if (inSolPoint == null)
            {
                throw new ArgumentNullException(nameof(inSolPoint));
            }

            Stopwatch sw = Stopwatch.StartNew();

            // Writes the data in the files and updated the Grasshopper Geometry
            UpdateGrasshopperGeometry(inSolPoint);

            // Reads the geometry into the solution point
            GrasshopperAllEmasaOutputWrapper_AsRhino3dm outputDefs = RhinoModel.RM.Grasshopper_GetAllEmasaOutputs();

            foreach (GhGeom_ParamDefBase output_ParamDefBase in GeometryDefs)
            {
                try
                {
                    switch (output_ParamDefBase)
                    {
                    case DoubleList_GhGeom_ParamDef doubleList_Output_ParamDef:
                        List <double> doubles = outputDefs.DoubleLists[doubleList_Output_ParamDef.Name];
                        inSolPoint.GhGeom_Values[output_ParamDefBase] = doubles;
                        break;

                    case LineList_GhGeom_ParamDef lineList_Output_ParamDef:
                        List <Line> lines = outputDefs.LineLists[lineList_Output_ParamDef.Name];
                        inSolPoint.GhGeom_Values[output_ParamDefBase] = lines;
                        break;

                    case PointList_GhGeom_ParamDef pointList_Output_ParamDef:
                        List <Point3d> points = outputDefs.PointLists[pointList_Output_ParamDef.Name];
                        inSolPoint.GhGeom_Values[output_ParamDefBase] = points;
                        break;

                    default:
                        throw new ArgumentOutOfRangeException(nameof(output_ParamDefBase));
                    }
                }
                catch (Exception e)
                {
                    throw new Exception($"Could not read the Grasshopper geometry {output_ParamDefBase.Name}.", e);
                }
            }

            /*
             * foreach (GhGeom_ParamDefBase output_ParamDefBase in GeometryDefs)
             * {
             *  string geometryVarFilePath = Path.Combine(GhGeometryDirPath, $"{output_ParamDefBase.Name}.{output_ParamDefBase.TypeName}");
             *
             *  try
             *  {
             *      string[] fileLines = File.ReadAllLines(geometryVarFilePath);
             *      switch (output_ParamDefBase)
             *      {
             *          case DoubleList_GhGeom_ParamDef doubleList_Output_ParamDef:
             *              List<double> doubles = fileLines.Select(Convert.ToDouble).ToList();
             *
             *              inSolPoint.GhGeom_Values[output_ParamDefBase] = doubles;
             *              break;
             *
             *          case LineList_GhGeom_ParamDef lineList_Output_ParamDef:
             *              List<Line> lines = fileLines.Select(a =>
             *              {
             *                  if (!RhinoStaticMethods.TryParseLine(a, out Line l)) throw new InvalidCastException($"Could not convert {a} to a Line.");
             *                  return l;
             *              }).ToList();
             *
             *              inSolPoint.GhGeom_Values[output_ParamDefBase] = lines;
             *              break;
             *
             *          case PointList_GhGeom_ParamDef pointList_Output_ParamDef:
             *              List<Point3d> points = fileLines.Select(a =>
             *              {
             *                  if (!RhinoStaticMethods.TryParsePoint3d(a, out Point3d p)) throw new InvalidCastException($"Could not convert {a} to a Point3d.");
             *                  return p;
             *              }).ToList();
             *
             *              inSolPoint.GhGeom_Values[output_ParamDefBase] = points;
             *              break;
             *
             *          default:
             *              throw new ArgumentOutOfRangeException(nameof(output_ParamDefBase));
             *      }
             *  }
             *  catch (Exception e)
             *  {
             *      throw new Exception($"Could not read the Grasshopper geometry {output_ParamDefBase.Name}.", e);
             *  }
             * }
             */

            // Obtains the Rhino Screenshots
            try
            {
                List <(string dir, Image image)> rhinoScreenshots = RhinoModel.RM.GetScreenshots(AppSS.I.ScreenShotOpt.ImageCapture_ViewDirectionsEnumerable.Select(inEnum => inEnum.ToString()).ToArray());
                foreach ((string dir, Image image)rhinoScreenshot in rhinoScreenshots)
                {
                    if (!Enum.TryParse(rhinoScreenshot.dir, out ImageCaptureViewDirectionEnum dirEnum))
                    {
                        throw new Exception($"Could not get back the direction enumerate value from its string representation. {rhinoScreenshot.dir}.");
                    }

                    inSolPoint.ScreenShots.Add(new NlOpt_Point_ScreenShot(
                                                   AppSS.I.ScreenShotOpt.SpecialRhinoDisplayScreenshotInstance,
                                                   dirEnum,
                                                   rhinoScreenshot.image));
                }
            }
            catch (Exception e)
            {
                throw new COMException("Could not get the RhinoScreenshots.", e);
            }

            sw.Stop();
            inSolPoint.GhUpdateTimeSpan = sw.Elapsed;
        }
Ejemplo n.º 2
0
        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
        }