示例#1
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            var formula = DA.GetData <string>(0);
            var s       = DA.GetData <double>(1);

            if (string.IsNullOrEmpty(formula))
            {
                return;
            }

            var parser = new GH_ExpressionParser();

            parser.AddVariable("S", s);
            parser.AddVariable("Height", 1.0);

            var result = parser.Evaluate(formula);

            if (result != null && result.IsNumeric)
            {
                var t = result.Type;

                double res = result.Data <double>();

                DA.SetData(0, res);
            }
        }
示例#2
0
            //public void SetVariables( List<Variable> variables )
            //{
            //   foreach( var v in variables )
            //   {
            //      _parser.AddVariable(v.Name, v.Value);
            //   }
            //}

            public List <Point3d> EvaluatePoints(List <Point> qpoints)
            {
                var gpoints = new List <Point3d>();

                foreach (var pi in qpoints)
                {
                    var v = new Vector3d(0, 0, 0);

                    // evaluate formula
                    var result = _parser.Evaluate(pi.X);
                    if (result?.IsNumeric == true)
                    {
                        v.X = result.Data <double>();
                    }

                    result = _parser.Evaluate(pi.Y);
                    if (result?.IsNumeric == true)
                    {
                        v.Y = result.Data <double>();
                    }

                    Point3d p0 = Point3d.Origin;
                    Point3d p1 = Point3d.Origin;
                    Point3d p2 = Point3d.Origin;
                    Point3d p3 = Point3d.Origin;

                    int kind = 0;

                    if (!string.IsNullOrEmpty(pi.Refp))
                    {
                        kind += 1000;
                        p1    = _geo_points[pi.Refp];
                    }

                    if (!string.IsNullOrEmpty(pi.Refd)) // polar reference
                    {
                        if (pi.Refd[0] == '~')
                        {
                            kind   += 100;
                            pi.Refd = pi.Refd.Substring(1);
                        }
                        else if (pi.Refd[0] == '+')
                        {
                            kind   += 200;
                            pi.Refd = pi.Refd.Substring(1);
                        }
                        else if (pi.Refd[0] == '*')
                        {
                            kind   += 300;
                            pi.Refd = pi.Refd.Substring(1);
                        }
                        else
                        {
                            kind += 400;
                        }

                        p2 = _geo_points[pi.Refd];
                    }

                    // check for references
                    if (!string.IsNullOrEmpty(pi.Refs)) // constr. reference
                    {
                        if (pi.Refs[0] == '>')
                        {
                            kind   += 10;
                            pi.Refs = pi.Refs.Substring(1);
                        }
                        else if (pi.Refs[0] == '^')
                        {
                            kind   += 20;
                            pi.Refs = pi.Refs.Substring(1);
                        }
                        else
                        {
                            kind += 30;
                        }

                        p3 = _geo_points[pi.Refs];
                    }

                    // evaluate references
                    if (kind == 0) // default
                    {
                        p0 = Point3d.Origin + v;
                    }
                    else if (kind == 1000) // only refd
                    {
                        p0 = p1 + v;
                    }
                    else if (kind == 1400) // y z
                    {
                        p0.X = p1.X + v.X;
                        p0.Y = p2.Y + v.Y;
                    }
                    else if (kind / 10 == 0) // polar: absolute
                    {
                        Vector3d va  = p2 - p1;
                        double   len = va.Length;
                        if (len > 1.0E-6)
                        {
                            double xix = kind == 1410 ? 1.0 / len : 1.0;
                            double xiy = kind != 1430 ? 1.0 / len : 1.0;

                            p0 = p1 + va * v.X * xix + new Vector3d(-va.Y, va.X, 0) * v.Y * xiy;
                        }
                        else
                        {
                            p0 = p1;
                        }
                    }
                    else if (kind == 1410) // constructional in x
                    {
                        double dx = p2.X - p1.X;
                        if (Math.Abs(dx) > 1.0E-6)
                        {
                            double xi = (p3.X - p1.X) / dx;
                            p0 = (1.0 - xi) * p1 + xi * p2;
                        }
                        else
                        {
                            p0 = p1;
                        }
                    }
                    else if (kind == 1420) // constructional in y
                    {
                        double dy = p2.Y - p1.Y;
                        if (Math.Abs(dy) > 1.0E-6)
                        {
                            double xi = (p3.Y - p1.Y) / dy;
                            p0 = (1.0 - xi) * p1 + xi * p2;
                        }
                        else
                        {
                            p0 = p1;
                        }
                    }
                    else if (kind == 1430) // perpendicular point
                    {
                        var ln = new Line(p1, p2);
                        p0 = ln.ClosestPoint(p3, false);
                    }
                    else // fallback
                    {
                        p0 = Point3d.Origin + v;
                    }

                    if (!string.IsNullOrEmpty(pi.Id)) // add to internal dictionary
                    {
                        _geo_points.Add(pi.Id, p0);
                    }

                    gpoints.Add(p0); // add to resulting list
                }
                return(gpoints);
            }