public NlOpt_Point_ProblemQuantity_Output([NotNull] NlOpt_Point inOwnerNlOptPoint, [NotNull] ProblemQuantity inQuantity)
        {
            _ownerNlOptPoint = inOwnerNlOptPoint ?? throw new ArgumentNullException(nameof(inOwnerNlOptPoint));
            Quantity         = inQuantity ?? throw new ArgumentNullException(nameof(inQuantity));

            // Gets and saves a reference to the Raw Data Table
            RawDataTable = inOwnerNlOptPoint.GetRawOutputTable(inQuantity.QuantitySource);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates the abstraction of the FeModel.
        /// </summary>
        /// <param name="inSolPoint">The solution point that contain the Gh Geometry that will define the model. Usually the model is then saved into the its FeModel parameter</param>
        public FeModel([NotNull] NlOpt_Point inSolPoint)
        {
            try
            {
                if (inSolPoint == null)
                {
                    throw new ArgumentNullException(nameof(inSolPoint));
                }

                Owner = inSolPoint;

                // Generates the model
                //////////////////////////

                // For each point list GH Geometry Parameter - Adds the joint
                foreach (PointList_GhGeom_ParamDef pointList_Output_ParamDef in inSolPoint.GhGeom_Values.Keys.OfType <PointList_GhGeom_ParamDef>())
                {
                    FeGroup grp = AddNewOrGet_Group(pointList_Output_ParamDef.FeGroupNameHelper);

                    List <Point3d> pPoints = (List <Point3d>)inSolPoint.GhGeom_Values[pointList_Output_ParamDef];

                    foreach (Point3d p in pPoints)
                    {
                        FeJoint j = AddNewOrGet_JointByCoordinate(p);
                        j.Restraint.IncorporateRestraint(pointList_Output_ParamDef.Restraint);
                        grp.AddElement(j);
                    }
                }

                // For each line list GH Geometry Parameter - Adds the joints and a frame
                foreach (LineList_GhGeom_ParamDef lineList_Output_ParamDef in inSolPoint.GhGeom_Values.Keys.OfType <LineList_GhGeom_ParamDef>())
                {
                    FeGroup grp = AddNewOrGet_Group(lineList_Output_ParamDef.FeGroupNameHelper);

                    List <Line> pLines = (List <Line>)inSolPoint.GhGeom_Values[lineList_Output_ParamDef];

                    foreach (Line l in pLines)
                    {
                        // Adds the From joint
                        FeJoint jFrom = AddNewOrGet_JointByCoordinate(l.From);
                        jFrom.Restraint.IncorporateRestraint(lineList_Output_ParamDef.Restraint);
                        grp.AddElement(jFrom);

                        // Adds the To joint
                        FeJoint jTo = AddNewOrGet_JointByCoordinate(l.To);
                        jTo.Restraint.IncorporateRestraint(lineList_Output_ParamDef.Restraint);
                        grp.AddElement(jTo);

                        // Adds the Frame
                        FeSection s = Owner.Owner.GetGhLineListSection(lineList_Output_ParamDef);
                        FeFrame   f = AddNewOrGet_LineByCoordinate(jFrom, jTo, s);
                        grp.AddElement(f);
                    }
                }

                // Adds the gravity loads
                if (AppSS.I.FeOpt.Gravity_IsLoad)
                {
                    FeLoad_Inertial gravity = FeLoad_Inertial.GetStandardGravity(AppSS.I.FeOpt.Gravity_Multiplier);
                    // Sets the direction based on the options
                    switch (AppSS.I.FeOpt.Gravity_DirectionEnum_Selected)
                    {
                    case MainAxisDirectionEnum.xPos:
                        gravity.Direction = Vector3d.XAxis;
                        break;

                    case MainAxisDirectionEnum.xNeg:
                        gravity.Direction = -Vector3d.XAxis;
                        break;

                    case MainAxisDirectionEnum.yPos:
                        gravity.Direction = Vector3d.YAxis;
                        break;

                    case MainAxisDirectionEnum.yNeg:
                        gravity.Direction = -Vector3d.YAxis;
                        break;

                    case MainAxisDirectionEnum.zPos:
                        gravity.Direction = Vector3d.ZAxis;
                        break;

                    case MainAxisDirectionEnum.zNeg:
                        gravity.Direction = -Vector3d.ZAxis;
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                    Loads.Add(gravity);
                }

                // Adds the point loads
                if (AppSS.I.FeOpt.PointLoads.Count > 0)
                {
                    Loads.AddRange(AppSS.I.FeOpt.PointLoads);
                }
            }
            catch (Exception e)
            {
                throw new Exception($"Error defining the FeModel internal class.", e);
            }
        }
Exemplo n.º 3
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;
        }
Exemplo n.º 4
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}");
                }
            }
        }