Пример #1
0
        public static bool SampleForNextPoint(List <Basis> bases, Point3d point, Basis start,
                                              GH_Vector lastVec, StaticSettings settings,
                                              out Basis outBasis)
        {
            var tolerance = settings.tolerance;
            var radius    = settings.avgRadius;
            var lineCont  = settings.lineCont;
            var tensorDir = settings.tensorDir;

            outBasis = new Basis();

            GH_Vector ghkv = GetInterpolatedVector(bases, lastVec, lineCont, tensorDir, ref point, tolerance, radius);

            if (ghkv == null)
            {
                return(false);
            }

            var kv = ghkv.Value;

            // po = pt + kv
            outBasis.Vector = new GH_Vector(kv);
            outBasis.Point  = new GH_Point(point + kv);

            return(true);
        }
Пример #2
0
        private List <Vector> GetVectorsFromTree <T>(GH_Structure <T> tree) where T : IGH_Goo
        {
            var output = new List <Vector>();

            for (int i = 0; i < tree.Branches.Count; i++)
            {
                var branchItems = tree[i].Count * 3;
                output.Add(new Vector(branchItems));

                for (int j = 0; j < branchItems; j++)
                {
                    if (tree[i][j / 3] is GH_Point)
                    {
                        GH_Point p = tree[i][j / 3] as GH_Point;
                        output[i][j] = p.Value[j % 3];
                    }
                    else if (tree[i][j / 3] is GH_Vector)
                    {
                        GH_Vector p = tree[i][j / 3] as GH_Vector;
                        output[i][j] = p.Value[j % 3];
                    }

                    output[i].SetMagnitude();
                }
            }

            return(output);
        }
Пример #3
0
        /// <summary>
        /// Provides an user-friendly description of a <see cref="Slot"/>.
        /// Required by Grasshopper.
        /// </summary>
        /// <returns>A string.</returns>
        public override string ToString( )
        {
            if (!IsValid)
            {
                return(IsValidWhyNot);
            }
            var pt          = new GH_Point(AbsoluteCenter);
            var diagonal    = new GH_Vector(Diagonal);
            var plane       = new GH_Plane(BasePlane);
            var containment = "";

            if (AllowsAnyModule)
            {
                containment = "all Modules";
            }
            if (IsContradictory)
            {
                containment = "no Modules";
            }
            if (!IsContradictory && !AllowsAnyModule)
            {
                var count = AllowedModuleNames.Count;
                if (count == 1)
                {
                    containment = "Module '" + AllowedModuleNames[0] + "'";
                }
                else
                {
                    containment = count + " Modules";
                }
            }
            return("Slot allows placement of " + containment + ". Slot dimensions are " + diagonal +
                   ", center is at " + pt + ", base plane is " + plane + ".");
        }
Пример #4
0
        private bool MeshTolVecErrorCheck(GH_Mesh msh, GH_Number tol, GH_Vector vec)
        {
            bool flag = true;

            if (msh == null || tol == null || vec == null)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Null Input! Skipping branch.");
                return(false);
            }
            if (!msh.Value.IsValid || !(msh.Value.Vertices.Count > 2))
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Invalid mesh! Skipping branch.");
                flag = false;
            }
            if (tol.Value < Rhino.RhinoMath.ZeroTolerance)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Tolerance too small! Skipping branch.");
                flag = false;
            }
            if (vec.Value.IsZero)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Cannot flow with zero-length vector! Skipping branch.");
                flag = false;
            }
            return(flag);
        }
Пример #5
0
        /// <summary>
        /// Casts the <see cref="Slot"/> to a <see cref="GH_Point"/> (from
        /// <see cref="AbsoluteCenter"/>, to a <see cref="Box"/> (from
        /// <see cref="Cage"/>) or to a <see cref="Brep"/> (from
        /// <see cref="Cage"/>). Required by Grasshopper for automatic type
        /// casting.
        /// </summary>
        /// <param name="target">The target Grasshopper geometry.</param>
        /// <returns>True when successful.</returns>
        public bool CastTo <T>(out T target)
        {
            if (typeof(T) == typeof(GH_Point))
            {
                var absoluteCenter = new GH_Point(AbsoluteCenter);
                target = (T)absoluteCenter.Duplicate();
                return(true);
            }

            if (typeof(T) == typeof(GH_Vector))
            {
                var diagonal = new GH_Vector(Diagonal);
                target = (T)diagonal.Duplicate();
                return(true);
            }

            if (typeof(T) == typeof(GH_Box))
            {
                var box = new GH_Box(Cage);
                target = (T)box.Duplicate();
                return(true);
            }

            if (typeof(T) == typeof(GH_Brep))
            {
                var boxBrep = new GH_Brep(Cage.ToBrep());
                target = (T)boxBrep.Duplicate();
                return(true);
            }

            target = default;
            return(false);
        }
Пример #6
0
        private void ClearAcceleration(List <IDynamic> dynamics, List <int> idxToClear, List <int> idxToRemove)
        {
            string accelKey = "accelerationVectors";

            foreach (var d in dynamics)
            {
                if (d.Accelerated)
                {
                    if (d.Param.Contains(accelKey))
                    {
                        // clear accel vector at idx's
                        var a = d.Param[accelKey] as List <GH_Vector>;
                        d.Param.Remove(accelKey);

                        // clear some vectors
                        foreach (var i in idxToClear)
                        {
                            a[i] = new GH_Vector(Vector3d.Unset);
                        }

                        // remove others for resync
                        for (int i = idxToRemove.Count - 1; i >= 0; i--)
                        {
                            a.RemoveAt(idxToRemove[i]);
                        }

                        d.Param[accelKey] = a;
                    }
                }
            }
        }
Пример #7
0
        // ===============================================================================================
        // method to display output to grasshopper with some optimization in conversion
        // ===============================================================================================
        public void DisplayToGrasshopper()
        {
            if (Containment[0].Label == 's')
            {
                _container = (SurfaceContainment)Containment[0];

                var nu = NumberOperations.remap(XMin, XMax, _container.Surface.Domain(0).T0,
                                                _container.Surface.Domain(0).T1, Position.X);
                var nv = NumberOperations.remap(YMin, YMax, _container.Surface.Domain(1).T0,
                                                _container.Surface.Domain(1).T1, Position.Y);

                var vu = NumberOperations.remap(XMin, XMax, _container.Surface.Domain(0).T0,
                                                _container.Surface.Domain(0).T1, Velocity.X);
                var vv = NumberOperations.remap(YMin, YMax, _container.Surface.Domain(1).T0,
                                                _container.Surface.Domain(1).T1, Velocity.Y);

                GHPosition = new GH_Point(_container.Surface.PointAt(nu, nv));
                GHVelocity = new GH_Vector(new Vector3d(_container.Surface.PointAt(vu, vv)));
            }
            else
            {
                GHPosition = new GH_Point(Position);
                GHVelocity = new GH_Vector(Velocity);
            }
        }
Пример #8
0
 private void InitVectorField()
 {
     Vectors = new GH_Vector[3];
     for (int i = 0; i < 3; i++)
     {
         Vectors[i] = new GH_Vector();
     }
 }
Пример #9
0
        public void GetPointsVectors(out GH_Point[] pts, out GH_Vector[] vecs)
        {
            pts  = new GH_Point[Particles.Count];
            vecs = new GH_Vector[Particles.Count];

            for (int i = 0; i < Particles.Count; i++)
            {
                pts[i]  = new GH_Point(Particles[i].pos);
                vecs[i] = new GH_Vector(Particles[i].vel);
            }
        }
Пример #10
0
        public void GetPointsVectors(out GH_Point[] pts, out GH_Vector[] vecs)
        {
            pts  = new GH_Point[Agents.Count];
            vecs = new GH_Vector[Agents.Count];

            for (int i = 0; i < Agents.Count; i++)
            {
                pts[i]  = new GH_Point(Agents[i].position);
                vecs[i] = new GH_Vector(Agents[i].velocity);
            }
        }
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            GsaGravityLoad gravityLoad = new GsaGravityLoad();

            //Load case
            int        lc    = 1;
            GH_Integer gh_lc = new GH_Integer();

            if (DA.GetData(0, ref gh_lc))
            {
                GH_Convert.ToInt32(gh_lc, out lc, GH_Conversion.Both);
            }
            gravityLoad.GravityLoad.Case = lc;

            //element/beam list
            string    beamList = "all";
            GH_String gh_bl    = new GH_String();

            if (DA.GetData(1, ref gh_bl))
            {
                GH_Convert.ToString(gh_bl, out beamList, GH_Conversion.Both);
            }
            gravityLoad.GravityLoad.Elements = beamList;

            // 2 Name
            string    name    = "";
            GH_String gh_name = new GH_String();

            if (DA.GetData(2, ref gh_name))
            {
                if (GH_Convert.ToString(gh_name, out name, GH_Conversion.Both))
                {
                    gravityLoad.GravityLoad.Name = name;
                }
            }

            //factor
            Vector3   factor    = new Vector3();
            Vector3d  vect      = new Vector3d(0, 0, -1);
            GH_Vector gh_factor = new GH_Vector();

            if (DA.GetData(3, ref gh_factor))
            {
                GH_Convert.ToVector3d(gh_factor, ref vect, GH_Conversion.Both);
            }
            factor.X = vect.X; factor.Y = vect.Y; factor.Z = vect.Z;
            gravityLoad.GravityLoad.Factor = factor;

            GsaLoad gsaLoad = new GsaLoad(gravityLoad);

            DA.SetData(0, new GsaLoadGoo(gsaLoad));
        }
Пример #12
0
        // Implement `Solveinstance` as usual
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            //this.ClearRuntimeMessages();

            object obj = null;

            if (!DA.GetData(0, ref obj))
            {
                return;
            }
            if (obj == null)
            {
                return;
            }

            // Be verbose about data types
            if (obj is GH_Point)
            {
                if (this.Relative)
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "WARNING: using Point in Relative mode, did you mean to work in Absolute mode instead? This Move action will take the Point as a Vector for relative motion.");
                }
                GH_Point p = obj as GH_Point;
                DA.SetData(0, new ActionTranslation(new MVector(p.Value.X, p.Value.Y, p.Value.Z), this.Relative));
            }

            else if (obj is GH_Plane)
            {
                if (this.Relative)
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "WARNING: using Plane in Relative mode, did you mean to work in Absolute mode instead? This Move action will take the Plane's origin Point as a Vector for relative motion.");
                }
                GH_Plane p = obj as GH_Plane;
                DA.SetData(0, new ActionTranslation(new MVector(p.Value.OriginX, p.Value.OriginY, p.Value.OriginZ), this.Relative));
            }

            else if (obj is GH_Vector)
            {
                if (!this.Relative)
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "WARNING: using Vector in Absolute mode, did you mean to work in Relative mode instead? This Move action will take the Vector's coordinates as the target Point for absolute motion.");
                }
                GH_Vector p = obj as GH_Vector;
                DA.SetData(0, new ActionTranslation(new MVector(p.Value.X, p.Value.Y, p.Value.Z), this.Relative));
            }

            else
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"ERROR: Can't take {obj.GetType()} as argument for a Move action; please use Point, Vector or Plane.");
                //DA.SetData(0, null);  // not necessary
            }
        }
Пример #13
0
        public static void ProcessDynamics(IDynamic dyn, List <GH_Point> points, List <GH_Vector> vectors, GH_Surface surface)
        {
            dyn.Process(points, vectors, surface);

            if (dyn.Accelerated)
            {
                // account for acceleration, if necessary
                var output = new List <GH_Vector>();
                for (int i = 0; i < vectors.Count; i++)
                {
                    output.Add(new GH_Vector());
                }

                if (dyn.Param.Contains("accelerationVectors"))
                {
                    var v0 = dyn.Param["accelerationVectors"] as List <GH_Vector>;

                    double drag = 1.0d;
                    if (dyn.Param.Contains("Dg"))
                    {
                        drag = (dyn.Param["Dg"] as GH_Number).Value;
                    }

                    // if vectors have been added (say via new particles) we need to
                    // add the difference in new vectors (unset)
                    if (v0.Count < vectors.Count)
                    {
                        for (int i = vectors.Count - v0.Count; i >= 0; i--)
                        {
                            v0.Add(new GH_Vector(Vector3d.Unset));
                        }
                    }

                    for (int i = 0; i < vectors.Count; i++)
                    {
                        // if v0 is unset its because we want to reset this accel vector externally
                        if (v0[i].Value == Vector3d.Unset)
                        {
                            output[i] = new GH_Vector();
                        }
                        else
                        {
                            output[i]        = new GH_Vector(v0[i].Value + (vectors[i].Value * drag));
                            vectors[i].Value = v0[i].Value + ((vectors[i].Value * drag) / 2);
                        }
                    }
                }

                dyn.Param["accelerationVectors"] = output;
            }
        }
Пример #14
0
        /*******************************************/

        public static bool CastToGoo(object value, ref IGH_QuickCast target)
        {
            try
            {
                if (value is bool)
                {
                    target = new GH_Boolean((bool)value);
                }
                else if (value is Color)
                {
                    target = new GH_Colour((Color)value);
                }
                else if (value is Complex)
                {
                    target = new GH_ComplexNumber((Complex)value);
                }
                else if (value is int)
                {
                    target = new GH_Integer((int)value);
                }
                else if (value is Interval)
                {
                    target = new GH_Interval((Interval)value);
                }
                else if (value is Matrix)
                {
                    target = new GH_Matrix((Matrix)value);
                }
                else if (value is double || value is float)
                {
                    target = new GH_Number((double)value);
                }
                else if (value is Point3d)
                {
                    target = new GH_Point((Point3d)value);
                }
                else if (value is string)
                {
                    target = new GH_String(value.ToString());
                }
                else if (value is Vector3d)
                {
                    target = new GH_Vector((Vector3d)value);
                }
                return(true);
            }
            catch
            {
                return(false);
            }
        }
Пример #15
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            object initialSpeed       = null;
            double maxSpeed           = new double();
            double maxForce           = new double();
            double velocityMultiplier = new double();

            if (!DA.GetData(0, ref initialSpeed))
            {
                return;
            }
            if (!DA.GetData(1, ref maxSpeed))
            {
                return;
            }
            if (!DA.GetData(2, ref maxForce))
            {
                return;
            }
            if (!DA.GetData(3, ref velocityMultiplier))
            {
                return;
            }

            ArrayList myAL       = new ArrayList();
            string    objectType = initialSpeed.GetType().Name.ToString();

            if (!(initialSpeed.GetType() == typeof(GH_Vector)) && !(initialSpeed.GetType() == typeof(GH_Number)))
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Initial Speed input must either be a vector or a double value and not " + objectType);
            }
            else
            {
                if (initialSpeed.GetType() == typeof(GH_Vector))
                {
                    GH_Vector moveVector = (GH_Vector)initialSpeed;
                    myAL.Add(initialSpeed);
                }
                else
                {
                    GH_Number moveValue = (GH_Number)initialSpeed;
                    myAL.Add(moveValue);
                }
            }
            myAL.Add(maxSpeed);
            myAL.Add(maxForce);
            myAL.Add(velocityMultiplier);

            DA.SetDataList(0, myAL);
        }
Пример #16
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            var v = new GH_Vector();

            if (DA.GetData(0, ref v) && v == null)
            {
                this.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid vector. Operation canceled.");
                return;
            }

            var dynamic = new VectorAdditionDynamic();

            dynamic.Param["V"] = v;

            DA.SetData(0, new GH_ObjectWrapper(dynamic));
        }
Пример #17
0
        private static GH_Vector GetNextInterpolatedVector(List <Basis> bases, List <int> vectorIdxs, GH_Vector lastVec, bool lineCont, int tensorDir, ref Point3d point, double radius)
        {
            var next = new GH_Vector();
            int n    = vectorIdxs.Count;

            double sumWeight    = 0.0d;
            var    weightedVecs = new Vector3d[n];

            for (int i = 0; i < n; i++)
            {
                int idx = vectorIdxs[i];

                double d = bases[idx].Point.Value.DistanceTo(point);

                // if d is 0 we are directly on top of a vector, its weight will overrule
                // any other vectors in the area, use it exclusively
                if (d == 0)
                {
                    return(bases[idx].Vector);
                }

                var w = Math.Pow((radius - d) / (radius * d), 2);

                var vec = bases[idx].Vector.Value;

                if (tensorDir != -1)
                {
                    vec = bases[idx].Vectors[tensorDir].Value;
                }

                if (lineCont && !lastVec.Value.IsZero)
                {
                    vec = bases[idx].GetBestContinuousVector(lastVec.Value);
                }

                weightedVecs[i] = vec * w;
                sumWeight      += w;
            }

            foreach (var v in weightedVecs)
            {
                next.Value += v / sumWeight;
            }

            return(next);
        }
Пример #18
0
        public void GetPtsVecs(out GH_Point[] pts, out GH_Vector[] vecs)
        {
            pts  = new GH_Point[Agents.Count];
            vecs = new GH_Vector[Agents.Count];

            for (int i = 0; i < Agents.Count; i++)
            {
                pts[i]  = new GH_Point(Agents[i].Position);
                vecs[i] = new GH_Vector(Agents[i].Velocity);
            }

            // parallel for loop:
            //Parallel.For(0, Agents.Count, i =>
            //{
            //    // parallel loop operations
            //}
            //);
        }
Пример #19
0
        private static GH_Vector GetNextVector(List <Basis> bases, GH_Vector lastVec, bool lineCont, int tensorDir, ref Point3d point, double tolerance, double radius)
        {
            GH_Vector next = null;

            // 0 radius means don't interpolate -- use nearest point
            if (radius == 0.0d)
            {
                var idx = GetNearestSamplePointIndex(bases, ref point, tolerance);

                if (idx == -1)
                {
                    return(null);
                }

                if (lineCont && !lastVec.Value.IsZero)
                {
                    next = new GH_Vector(bases[idx].GetBestContinuousVector(lastVec.Value));
                }
                else
                {
                    if (tensorDir != -1)
                    {
                        next = bases[idx].Vectors[tensorDir];
                    }
                    else
                    {
                        next = bases[idx].Vector;
                    }
                }
            }
            else
            {
                // use interpolation
                var sampleIdxs = GetNearestVectorsWithin(bases, ref point, tolerance, radius);

                // if samples is null, all vectors gathered were out of the tolerance range
                if (sampleIdxs != null)
                {
                    next = GetNextInterpolatedVector(bases, sampleIdxs, lastVec, lineCont, tensorDir, ref point, radius);
                }
            }

            return(next);
        }
Пример #20
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            Mesh mesh = createTetrahedronMesh();

            var colors = new List <GH_Colour>();

            DA.GetDataList(2, colors);
            setVertexColorsOfMesh(mesh, colors);

            var axis = new GH_Vector();

            DA.GetData(1, ref axis);
            orientMeshAlongAxis(mesh, axis);

            var point = new GH_Point();

            DA.GetData(0, ref point);
            moveMeshToPoint(mesh, point);

            DA.SetData(0, mesh);
        }
Пример #21
0
        /// <inheritdoc />
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            GH_Vector scale = null;

            if (!DA.GetData(0, ref scale))
            {
                return;
            }

            GH_Vector offset = null;

            if (!DA.GetData(1, ref offset))
            {
                return;
            }

            switch (_type)
            {
            case NoiseType.Perlin:
            {
                var f = new PerlinNoise3d(scale.Value, offset.Value);
                DA.SetData(0, new GH_ObjectWrapper(f));
                break;
            }

            case NoiseType.Simplex:
            {
                var f = new SimplexNoise3d(scale.Value, offset.Value);
                DA.SetData(0, new GH_ObjectWrapper(f));
                break;
            }

            default:
            {
                throw new ArgumentException("The specified noise type is not supported.");
            }
            }
        }
Пример #22
0
        private static GH_Vector GetInterpolatedVector(List <Basis> bases, GH_Vector lastVec, bool lineCont, int tensorDir, ref Point3d point, double tolerance, double radius)
        {
            var k1 = GetNextVector(bases, lastVec, lineCont, tensorDir, ref point, tolerance, radius);

            if (k1 == null)
            {
                return(null);
            }

            var tmpTrvlr = point + (k1.Value / 2);
            var k2       = GetNextVector(bases, lastVec, lineCont, tensorDir, ref tmpTrvlr, tolerance, radius);

            if (k2 == null)
            {
                return(null);
            }

            tmpTrvlr = point + (k2.Value / 2);
            var k3 = GetNextVector(bases, lastVec, lineCont, tensorDir, ref tmpTrvlr, tolerance, radius);

            if (k3 == null)
            {
                return(null);
            }

            tmpTrvlr = point + k3.Value;
            var k4 = GetNextVector(bases, lastVec, lineCont, tensorDir, ref tmpTrvlr, tolerance, radius);

            if (k4 == null)
            {
                return(null);
            }

            // kv = ((1/6) * (k1 + 2k2 + 2k3 + k4))
            return(new GH_Vector((1d / 6d) * (k1.Value + ((2 * k2.Value) + (2 * k3.Value) + k4.Value))));
        }
        public void SetInputs(List <DataTree <ResthopperObject> > values)
        {
            foreach (var tree in values)
            {
                if (!_input.TryGetValue(tree.ParamName, out var inputGroup))
                {
                    continue;
                }

                if (inputGroup.AlreadySet(tree))
                {
                    Console.WriteLine("Skipping input tree... same input");
                    continue;
                }

                inputGroup.CacheTree(tree);
                inputGroup.Param.VolatileData.Clear();
                inputGroup.Param.ExpireSolution(true);

                if (inputGroup.Param is Param_Point)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject       restobj = entree.Value[i];
                            Rhino.Geometry.Point3d rPt     = JsonConvert.DeserializeObject <Rhino.Geometry.Point3d>(restobj.Data);
                            GH_Point data = new GH_Point(rPt);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Vector)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject        restobj  = entree.Value[i];
                            Rhino.Geometry.Vector3d rhVector = JsonConvert.DeserializeObject <Rhino.Geometry.Vector3d>(restobj.Data);
                            GH_Vector data = new GH_Vector(rhVector);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Integer)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj = entree.Value[i];
                            int        rhinoInt      = JsonConvert.DeserializeObject <int>(restobj.Data);
                            GH_Integer data          = new GH_Integer(rhinoInt);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Number)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj  = entree.Value[i];
                            double           rhNumber = JsonConvert.DeserializeObject <double>(restobj.Data);
                            GH_Number        data     = new GH_Number(rhNumber);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_String)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj  = entree.Value[i];
                            string           rhString = restobj.Data;
                            GH_String        data     = new GH_String(rhString);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Line)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject    restobj = entree.Value[i];
                            Rhino.Geometry.Line rhLine  = JsonConvert.DeserializeObject <Rhino.Geometry.Line>(restobj.Data);
                            GH_Line             data    = new GH_Line(rhLine);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Curve)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj = entree.Value[i];
                            GH_Curve         ghCurve;
                            try
                            {
                                Rhino.Geometry.Polyline data = JsonConvert.DeserializeObject <Rhino.Geometry.Polyline>(restobj.Data);
                                Rhino.Geometry.Curve    c    = new Rhino.Geometry.PolylineCurve(data);
                                ghCurve = new GH_Curve(c);
                            }
                            catch
                            {
                                var dict = JsonConvert.DeserializeObject <Dictionary <string, string> >(restobj.Data);
                                var c    = (Rhino.Geometry.Curve)Rhino.Runtime.CommonObject.FromJSON(dict);
                                ghCurve = new GH_Curve(c);
                            }
                            inputGroup.Param.AddVolatileData(path, i, ghCurve);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Circle)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject      restobj  = entree.Value[i];
                            Rhino.Geometry.Circle rhCircle = JsonConvert.DeserializeObject <Rhino.Geometry.Circle>(restobj.Data);
                            GH_Circle             data     = new GH_Circle(rhCircle);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Plane)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject     restobj = entree.Value[i];
                            Rhino.Geometry.Plane rhPlane = JsonConvert.DeserializeObject <Rhino.Geometry.Plane>(restobj.Data);
                            GH_Plane             data    = new GH_Plane(rhPlane);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Rectangle)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject           restobj     = entree.Value[i];
                            Rhino.Geometry.Rectangle3d rhRectangle = JsonConvert.DeserializeObject <Rhino.Geometry.Rectangle3d>(restobj.Data);
                            GH_Rectangle data = new GH_Rectangle(rhRectangle);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Box)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject   restobj = entree.Value[i];
                            Rhino.Geometry.Box rhBox   = JsonConvert.DeserializeObject <Rhino.Geometry.Box>(restobj.Data);
                            GH_Box             data    = new GH_Box(rhBox);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Surface)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject       restobj   = entree.Value[i];
                            Rhino.Geometry.Surface rhSurface = JsonConvert.DeserializeObject <Rhino.Geometry.Surface>(restobj.Data);
                            GH_Surface             data      = new GH_Surface(rhSurface);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Brep)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject    restobj = entree.Value[i];
                            Rhino.Geometry.Brep rhBrep  = JsonConvert.DeserializeObject <Rhino.Geometry.Brep>(restobj.Data);
                            GH_Brep             data    = new GH_Brep(rhBrep);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Mesh)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject    restobj = entree.Value[i];
                            Rhino.Geometry.Mesh rhMesh  = JsonConvert.DeserializeObject <Rhino.Geometry.Mesh>(restobj.Data);
                            GH_Mesh             data    = new GH_Mesh(rhMesh);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is GH_NumberSlider)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj  = entree.Value[i];
                            double           rhNumber = JsonConvert.DeserializeObject <double>(restobj.Data);
                            GH_Number        data     = new GH_Number(rhNumber);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Boolean || inputGroup.Param is GH_BooleanToggle)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj = entree.Value[i];
                            bool             boolean = JsonConvert.DeserializeObject <bool>(restobj.Data);
                            GH_Boolean       data    = new GH_Boolean(boolean);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is GH_Panel)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj  = entree.Value[i];
                            string           rhString = JsonConvert.DeserializeObject <string>(restobj.Data);
                            GH_String        data     = new GH_String(rhString);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }
            }
        }
Пример #24
0
    public override bool CastFrom(object source)
    {
        var switchExpr = source.GetType();

        switch (switchExpr)
        {
        case var @case when @case == typeof(Tensor):
        {
            this.Value = (Tensor)source;
            return(true);
        }

        case var case1 when case1 == typeof(TensorSet):
        {
            TensorSet ts = (TensorSet)source;
            if (ts.Count != 1)
            {
                return(false);
            }
            this.Value = ts[0];
            return(true);
        }

        case var case2 when case2 == typeof(Point3d):
        {
            Point3d asp = source;
            this.Value = new Tensor(new[] { asp.X, asp.Y, asp.Z });
            return(true);
        }

        case var case3 when case3 == typeof(GH_Point):
        {
            GH_Point asp = source;
            this.Value = new Tensor(new[] { asp.Value.X, asp.Value.Y, asp.Value.Z });
            return(true);
        }

        case var case4 when case4 == typeof(GH_Vector):
        {
            GH_Vector asp = source;
            this.Value = new Tensor(new[] { asp.Value.X, asp.Value.Y, asp.Value.Z });
            return(true);
        }

        case var case5 when case5 == typeof(Vector3d):
        {
            Vector3d asp = source;
            this.Value = new Tensor(new[] { asp.X, asp.Y, asp.Z });
            return(true);
        }

        case var case6 when case6 == typeof(Color):
        {
            Color asp = (Color)source;
            this.Value = new Tensor(new[] { Convert.ToDouble(asp.A), Convert.ToDouble(asp.R), Convert.ToDouble(asp.G), Convert.ToDouble(asp.B) });
            return(true);
        }

        case var case7 when case7 == typeof(GH_Colour):
        {
            GH_Colour asp = source;
            this.Value = new Tensor(new[] { Convert.ToDouble(asp.Value.A), Convert.ToDouble(asp.Value.R), Convert.ToDouble(asp.Value.G), Convert.ToDouble(asp.Value.B) });
            return(true);
        }

        case var case8 when case8 == typeof(Line):
        {
            Line asp = source;
            this.Value = new Tensor(new[] { asp.From.X, asp.From.Y, asp.From.Z, asp.To.X, asp.To.Y, asp.To.Z });
            return(true);
        }

        case var case9 when case9 == typeof(GH_Line):
        {
            GH_Line thisv = source;
            Line    asp   = thisv.Value;
            this.Value = new Tensor(new[] { asp.From.X, asp.From.Y, asp.From.Z, asp.To.X, asp.To.Y, asp.To.Z });
            return(true);
        }
        }

        return(false);
    }
Пример #25
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            if (GH_Document.IsEscapeKeyDown())
            {
                GH_Document GHDocument = OnPingDocument();
                GHDocument.RequestAbortSolution();
                return;
            }

            ikvm.runtime.Startup.addBootClassPathAssemby(Assembly.Load("culebra"));
            ikvm.runtime.Startup.addBootClassPathAssemby(Assembly.Load("IKVM.OpenJDK.Core"));

            bool           reset           = new bool();
            int            iterations      = new int();
            List <object>  init_Settings   = new List <object>();
            List <object>  move_Settings   = new List <object>();
            IGH_VisualData visual_Settings = null;

            object behavioral_Settings = null;

            if (!DA.GetDataList(0, init_Settings) || init_Settings.Count == 0 || init_Settings == null)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "No Init Settings Detected, please connect Init Settings to enable the component");
                return;
            }
            if (!DA.GetDataList(1, move_Settings) || move_Settings.Count == 0 || move_Settings == null)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "No Move Settings Detected, please connect Move Settings to enable the component");
                return;
            }
            if (!DA.GetData(3, ref visual_Settings) || visual_Settings == null)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "No Visual Settings Detected, please connect Visual Settings to enable the component");
                return;
            }
            if (!DA.GetData(4, ref iterations))
            {
                return;
            }
            if (!DA.GetData(5, ref reset))
            {
                return;
            }
            Random rnd = new Random();

            if (!DA.GetData(2, ref behavioral_Settings) || behavioral_Settings == null)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Input Object is Null");
                return;
            }
            string objtype = behavioral_Settings.GetType().Name.ToString();

            if (!(behavioral_Settings.GetType() == typeof(IGH_BehaviorData)))
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "You did not input a Behavior Data Object, please ensure input is Behavior Data Object and not " + objtype);
                return;
            }
            else
            {
                #region Initialize / Data Parse
                //------------------------Init Settings--------------------------
                if (init_Settings.Count != 0)
                {
                    String init_Convert = "";
                    if (init_Settings[0].GetType() == typeof(GH_String))
                    {
                        GH_String value = (GH_String)init_Settings[0];
                        init_Convert = value.Value;
                    }
                    if (init_Convert == "Box")
                    {
                        this.spawnData = "box";
                        GH_Convert.ToBox_Primary(init_Settings[3], ref this.box);
                        GH_Convert.ToInt32(init_Settings[4], out this.spawnType, GH_Conversion.Primary);
                        GH_Convert.ToInt32(init_Settings[5], out this.pointCount, GH_Conversion.Primary);
                        GH_Convert.ToInt32(init_Settings[1], out this.dimensions, GH_Conversion.Primary);
                    }
                    else if (init_Convert == "Points")
                    {
                        this.spawnData = "Points";
                        var wrapperToGoo = GH_Convert.ToGoo(init_Settings[3]);
                        wrapperToGoo.CastTo <List <Point3d> >(out this.ptList);
                        GH_Convert.ToInt32(init_Settings[1], out this.dimensions, GH_Conversion.Primary);
                        GH_Convert.ToBox_Primary(init_Settings[4], ref this.box);
                    }
                    GH_Convert.ToInt32(init_Settings[2], out this.bounds, GH_Conversion.Primary);
                }
                //------------------------Move Settings--------------------------
                Vector3d initialVector = new Vector3d();
                if (move_Settings.Count != 0)
                {
                    if (move_Settings[0].GetType() == typeof(GH_Vector))
                    {
                        GH_Vector value = (GH_Vector)move_Settings[0];
                        initialVector = value.Value;
                    }
                    else if (move_Settings[0].GetType() == typeof(GH_Number))
                    {
                        GH_Number value = (GH_Number)move_Settings[0];
                        this.initialSpeed = value.Value;
                    }
                    GH_Convert.ToDouble(move_Settings[1], out this.maxSpeed, GH_Conversion.Primary);
                    GH_Convert.ToDouble(move_Settings[2], out this.maxForce, GH_Conversion.Primary);
                    GH_Convert.ToDouble(move_Settings[3], out this.velMultiplier, GH_Conversion.Primary);
                }
                //------------------------Visual Settings--------------------------
                TrailData td = visual_Settings.Value.trailData;
                ColorData cd = visual_Settings.Value.colorData;
                this.trail           = td.createTrail;
                this.displayMode     = visual_Settings.Value.displayMode;
                this.trailStep       = td.trailStep;
                this.maxTrailSize    = td.maxTrailSize;
                this.particleTexture = cd.particleTexture;
                this.graphicType     = cd.colorDataType;
                this.useTexture      = visual_Settings.Value.useTexture;
                if (cd.colorDataType == "Gradient")
                {
                    this.maxthick       = cd.maxThickness;
                    this.minthick       = cd.minThickness;
                    this.redValues[0]   = cd.redChannel[0];
                    this.redValues[1]   = cd.redChannel[1];
                    this.greenValues[0] = cd.greenChannel[0];
                    this.greenValues[1] = cd.greenChannel[1];
                    this.blueValues[0]  = cd.blueChannel[0];
                    this.blueValues[1]  = cd.blueChannel[1];
                }
                else if (cd.colorDataType == "GraphicPolyline")
                {
                    this.polylineColor = cd.color;
                    this.dotted        = cd.dotted;
                    this.maxthick      = cd.maxThickness;
                }
                else if (cd.colorDataType == "Disco")
                {
                    this.maxthick = cd.maxThickness;
                    this.minthick = cd.minThickness;
                }
                else if (cd.colorDataType == "Base")
                {
                    this.maxthick = 3;
                    this.minthick = 1;
                }
                //-----------------------------------------------------------------
                IGH_PreviewObject comp = (IGH_PreviewObject)this;
                if (comp.Hidden && (this.displayMode == 0))
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Component preview must be enabled to see Graphic Mode on Canvas, right click on component and set preview on");
                }
                #endregion
                #region Pre Simulation Code
                //------------------------RESET STARTS HERE--------------------------
                if (reset)
                { //We are using the reset to reinitialize all the variables and positions
                    //-----------------------------------------------------------------
                    this.bb = new BoundingBox();
                    int  loopCount = new int();
                    bool create    = new bool();
                    if (this.spawnData == "box")
                    {
                        this.bb   = this.box.BoundingBox;
                        loopCount = this.pointCount;
                        create    = true;
                    }
                    else if (this.spawnData == "Points")
                    {
                        loopCount = this.ptList.Count;
                        create    = false;
                        this.bb   = this.box.BoundingBox;
                    }
                    //-----------------------------------------------------------------
                    this.moveList       = new List <Vector3d>();
                    this.startList      = new List <Vector3d>();
                    this.creepList      = new List <CulebraObject>();
                    this.currentPosList = new List <Point3d>();
                    this.networkList    = new List <Line>();
                    flattenedTrails     = new List <Vector3d>();

                    for (int i = 0; i < loopCount; i++)
                    {
                        if (this.dimensions == 0)
                        { //If we want 2D
                            General.setViewport("Top", "Shaded");
                            if (create)
                            { //If are creating random points inside bbox
                                if (this.spawnType == 0 || this.spawnType == 2)
                                {
                                    this.startPos = new Vector3d((int)bb.Min[0], rnd.Next((int)bb.Min[1], (int)bb.Max[1]), 0); //spawn along the y axis of the bounding area
                                }
                                else if (this.spawnType == 1 || this.spawnType == 3)
                                {
                                    this.startPos = new Vector3d(rnd.Next((int)bb.Min[0], (int)bb.Max[0]), rnd.Next((int)bb.Min[1], (int)bb.Max[1]), 0); //spawn randomly inside the bounding area
                                }
                                if (initialVector.Length > 0)
                                {
                                    this.moveVec = initialVector; //move in the user specified direction
                                }
                                else
                                {
                                    this.moveVec = new Vector3d(rnd.Next(-1, 2) * initialSpeed, rnd.Next(-1, 2) * initialSpeed, 0); //move randomly in any direction 2d
                                }
                            }
                            else
                            { //If we are using user defined points
                                this.startPos = (Vector3d)this.ptList[i];
                                if (initialVector.Length > 0)
                                {
                                    this.moveVec = initialVector; //move in the user specified direction
                                }
                                else
                                {
                                    this.moveVec = new Vector3d(rnd.Next(-1, 2) * initialSpeed, rnd.Next(-1, 2) * initialSpeed, 0); //move randomly in any direction 2d
                                }
                            }
                            this.creep = new Creeper(this.startPos, this.moveVec, true, false);
                            this.creepList.Add(this.creep);
                        }
                        else
                        { //If we want 3D
                            General.setViewport("Perspective", "Shaded");
                            if (create)
                            { //If are creating random points inside bbox
                                if (this.spawnType == 0 || this.spawnType == 2)
                                {
                                    this.startPos = new Vector3d(rnd.Next((int)bb.Min[0], (int)bb.Max[0]), rnd.Next((int)bb.Min[1], (int)bb.Max[1]), (int)bb.Min[2]); //start randomly on the lowest plane of the 3d bounds
                                    if (initialVector.Length > 0)
                                    {
                                        this.moveVec = initialVector; //move in the user specified direction
                                    }
                                    else
                                    {
                                        this.moveVec = new Vector3d(rnd.Next(-2, 2) * initialSpeed, rnd.Next(-2, 2) * initialSpeed, 1 * initialSpeed); //move randomly in the xy axis and up in the z axis
                                    }
                                }
                                else if (this.spawnType == 1 || this.spawnType == 3)
                                {
                                    this.startPos = new Vector3d(rnd.Next((int)bb.Min[0], (int)bb.Max[0]), rnd.Next((int)bb.Min[1], (int)bb.Max[1]), rnd.Next((int)bb.Min[2], (int)bb.Max[2])); //start randomly inside the 3d bounds
                                    if (initialVector.Length > 0)
                                    {
                                        this.moveVec = initialVector; //move in the user specified direction
                                    }
                                    else
                                    {
                                        this.moveVec = new Vector3d(rnd.Next(-2, 2) * initialSpeed, rnd.Next(-2, 2) * initialSpeed, rnd.Next(-2, 2) * initialSpeed); //move randomly in any direction 3d
                                    }
                                }
                            }
                            else
                            { //If we are using user defined points
                                this.startPos = (Vector3d)this.ptList[i];
                                if (initialVector.Length > 0)
                                {
                                    this.moveVec = initialVector; //move in the user specified direction
                                }
                                else
                                {
                                    this.moveVec = new Vector3d(rnd.Next(-2, 2) * initialSpeed, rnd.Next(-2, 2) * initialSpeed, rnd.Next(-2, 2) * initialSpeed); //move randomly in any direction 3d
                                }
                            }
                            this.creep = new Creeper(this.startPos, this.moveVec, true, true);
                            this.creepList.Add(this.creep);
                        }
                        this.startList.Add(this.startPos); //add the initial starting positions to the list to pass once we start running
                        this.moveList.Add(this.moveVec);   //add the initial move vectors to the list to pass once we start running
                    }
                    #endregion
                    #region Simulation Code
                    this.trailTree    = new DataTree <Point3d>();
                    this.globalEngine = new Engine_Global();
                    for (int z = 0; z < iterations; z++)
                    {
                        this.particleSet    = new DataTree <Point3d>();
                        this.currentPosList = new List <Point3d>();

                        this.trailTree.Clear();
                        this.networkTree.Clear();
                        this.trailTree.TrimExcess();
                        this.networkTree.TrimExcess();

                        if (this.moveList == null)
                        {
                            AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Please Reset the CreepyCrawlers Component"); return;
                        }
                        try
                        {
                            globalEngine.Action(this.creepList, this.dimensions, behavioral_Settings, this.displayMode, this.networkList,
                                                this.maxSpeed, this.maxForce, this.velMultiplier, this.flattenedTrails, this.particleList, this.particleSet, networkTree, trailStep, maxTrailSize, bounds, bb, currentPosList, trail, trailTree);
                        }
                        catch (Exception e)
                        {
                            AddRuntimeMessage(GH_RuntimeMessageLevel.Error, e.Message.ToString());
                            return;
                        }

                        this.flattenedTrails.Clear();
                        this.flattenedTrails.TrimExcess();

                        #endregion
                    }
                    DA.SetDataList(0, this.currentPosList);
                    DA.SetDataTree(2, networkTree);
                    if (this.displayMode == 1 && this.trail)
                    {
                        DA.SetDataTree(1, trailTree);
                    }
                }
            }
        }
Пример #26
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            // inputs
            var points      = new List <GH_Point>();
            var vectors     = new List <GH_Vector>();
            var startPoints = new List <GH_Point>();
            var reset       = new GH_Boolean();

            var settings = new GH_ObjectWrapper();

            if (DA.GetDataList(0, points) && points == null)
            {
                this.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid points list. Operation canceled.");
                return;
            }

            if (DA.GetDataList(1, vectors) && vectors == null)
            {
                this.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid vector list. Operation canceled.");
                return;
            }

            if (vectors.Count != points.Count)
            {
                this.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Vector list size mismatch with points list, they must be equal in length. Operation canceled.");
                return;
            }

            if (DA.GetDataList(2, startPoints) && startPoints == null)
            {
                this.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid travelling points. Operation canceled.");
                return;
            }

            // spm parameters component is optional, we use its defaults if it is not available
            SPM_Parameters spm_settings = new SPM_Parameters();

            if (DA.GetData(3, ref settings))
            {
                // if getdata succeeded but the settings var is null we had bad input
                if (settings == null)
                {
                    this.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid settings input. Operation canceled.");
                    return;
                }

                // otherwise cast from gh_objectwrapper and continue
                spm_settings = (SPM_Parameters)settings.Value;
            }

            if (DA.GetData(4, ref reset) && reset == null)
            {
                this.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid reset input. Operation canceled.");
                return;
            }

            if (startBasis == null || reset.Value)
            {
                var count = startPoints.Count;

                startBasis = new Basis[count];
                lastVecs   = new GH_Vector[count];

                xy = new double[count];
                yz = new double[count];
                xz = new double[count];

                for (int i = 0; i < count; i++)
                {
                    startBasis[i] = new Basis(startPoints[i]);
                    lastVecs[i]   = new GH_Vector();
                    xy[i]         = 0;
                    yz[i]         = 0;
                    xz[i]         = 0;
                }
            }

            if (moving == null || reset.Value)
            {
                moving = startPoints;
                return;
            }

            int steps = spm_settings.steps;

            if (steps == 0)
            {
                steps = 1;
            }

            var bases = new List <Basis>();

            for (int i = 0; i < points.Count; i++)
            {
                bases.Add(new Basis(points[i], vectors[i]));
            }

            // find each next point based on an averaging formula and iterate

            for (int i = 0; i < startPoints.Count; i++)
            {
                for (int j = 0; j < steps; j++)
                {
                    bool add = false;

                    var outBasis = new Basis();

                    bool working = Algos.SampleForNextPoint(bases, moving[i].Value, startBasis[i], lastVecs[i], spm_settings, out outBasis, out add);

                    if (spm_settings.stop && spm_settings.windAngle != 0.0d)
                    {
                        if (!lastVecs[i].Value.IsZero)
                        {
                            double cxy = xy[i];
                            double cyz = yz[i];
                            double cxz = xz[i];

                            if (Algos.IsWoundPast(outBasis.Vector.Value, lastVecs[i].Value, spm_settings.windAngle, ref cxy, ref cyz, ref cxz))
                            {
                                break;
                            }

                            xy[i] = cxy;
                            yz[i] = cyz;
                            xz[i] = cxz;
                        }
                    }

                    lastVecs[i] = outBasis.Vector;

                    if (working && startBasis[i].Vector.Value.IsZero)
                    {
                        startBasis[i].Vector.Value = moving[i].Value - startBasis[i].Point.Value;
                    }

                    if (add)
                    {
                        moving[i] = outBasis.Point;
                    }

                    if (!working)
                    {
                        moving[i] = startPoints[i];
                    }
                }
            }

            DA.SetDataList(0, moving);
        }
Пример #27
0
        private void orientMeshAlongAxis(Mesh mesh, GH_Vector axis)
        {
            var rotation = Transform.Rotation(Vector3d.ZAxis, axis.Value, Point3d.Origin);

            mesh.Transform(rotation);
        }
Пример #28
0
        public void SetInputs(List <DataTree <ResthopperObject> > values)
        {
            foreach (var tree in values)
            {
                if (!_input.TryGetValue(tree.ParamName, out var inputGroup))
                {
                    continue;
                }

                if (inputGroup.AlreadySet(tree))
                {
                    LogDebug("Skipping input tree... same input");
                    continue;
                }

                inputGroup.CacheTree(tree);

                IGH_ContextualParameter contextualParameter = inputGroup.Param as IGH_ContextualParameter;
                if (contextualParameter != null)
                {
                    switch (ParamTypeName(inputGroup.Param))
                    {
                    case "Number":
                    {
                        foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                        {
                            double[] doubles = new double[entree.Value.Count];
                            for (int i = 0; i < doubles.Length; i++)
                            {
                                ResthopperObject restobj = entree.Value[i];
                                doubles[i] = JsonConvert.DeserializeObject <double>(restobj.Data);
                            }
                            contextualParameter.AssignContextualData(doubles);
                            break;
                        }
                    }
                    break;

                    case "Integer":
                    {
                        foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                        {
                            int[] integers = new int[entree.Value.Count];
                            for (int i = 0; i < integers.Length; i++)
                            {
                                ResthopperObject restobj = entree.Value[i];
                                integers[i] = JsonConvert.DeserializeObject <int>(restobj.Data);
                            }
                            contextualParameter.AssignContextualData(integers);
                            break;
                        }
                    }
                    break;

                    case "Point":
                    {
                        foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                        {
                            Point3d[] points = new Point3d[entree.Value.Count];
                            for (int i = 0; i < entree.Value.Count; i++)
                            {
                                ResthopperObject restobj = entree.Value[i];
                                points[i] = JsonConvert.DeserializeObject <Rhino.Geometry.Point3d>(restobj.Data);
                            }
                            contextualParameter.AssignContextualData(points);
                            break;
                        }
                    }
                    break;

                    case "Line":
                    {
                        foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                        {
                            Line[] lines = new Line[entree.Value.Count];
                            for (int i = 0; i < entree.Value.Count; i++)
                            {
                                ResthopperObject restobj = entree.Value[i];
                                lines[i] = JsonConvert.DeserializeObject <Rhino.Geometry.Line>(restobj.Data);
                            }
                            contextualParameter.AssignContextualData(lines);
                            break;
                        }
                    }
                    break;

                    case "Text":
                    {
                        foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                        {
                            string[] strings = new string[entree.Value.Count];
                            for (int i = 0; i < entree.Value.Count; i++)
                            {
                                ResthopperObject restobj = entree.Value[i];
                                strings[i] = restobj.Data.Trim(new char[] { '"' });
                            }
                            contextualParameter.AssignContextualData(strings);
                            break;
                        }
                    }
                    break;

                    case "Geometry":
                    {
                        foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                        {
                            GeometryBase[] geometries = new GeometryBase[entree.Value.Count];
                            for (int i = 0; i < entree.Value.Count; i++)
                            {
                                ResthopperObject restobj = entree.Value[i];
                                var dict = JsonConvert.DeserializeObject <Dictionary <string, string> >(restobj.Data);
                                geometries[i] = Rhino.Runtime.CommonObject.FromJSON(dict) as GeometryBase;
                            }
                            contextualParameter.AssignContextualData(geometries);
                            break;
                        }
                    }
                    break;
                    }
                    continue;
                }

                inputGroup.Param.VolatileData.Clear();
                inputGroup.Param.ExpireSolution(false); // mark param as expired but don't recompute just yet!

                if (inputGroup.Param is Param_Point)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject       restobj = entree.Value[i];
                            Rhino.Geometry.Point3d rPt     = JsonConvert.DeserializeObject <Rhino.Geometry.Point3d>(restobj.Data);
                            GH_Point data = new GH_Point(rPt);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Vector)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject        restobj  = entree.Value[i];
                            Rhino.Geometry.Vector3d rhVector = JsonConvert.DeserializeObject <Rhino.Geometry.Vector3d>(restobj.Data);
                            GH_Vector data = new GH_Vector(rhVector);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Integer)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj = entree.Value[i];
                            int        rhinoInt      = JsonConvert.DeserializeObject <int>(restobj.Data);
                            GH_Integer data          = new GH_Integer(rhinoInt);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Number)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj  = entree.Value[i];
                            double           rhNumber = JsonConvert.DeserializeObject <double>(restobj.Data);
                            GH_Number        data     = new GH_Number(rhNumber);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_String)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj  = entree.Value[i];
                            string           rhString = restobj.Data;
                            GH_String        data     = new GH_String(rhString);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Line)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject    restobj = entree.Value[i];
                            Rhino.Geometry.Line rhLine  = JsonConvert.DeserializeObject <Rhino.Geometry.Line>(restobj.Data);
                            GH_Line             data    = new GH_Line(rhLine);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Curve)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj = entree.Value[i];
                            GH_Curve         ghCurve;
                            try
                            {
                                Rhino.Geometry.Polyline data = JsonConvert.DeserializeObject <Rhino.Geometry.Polyline>(restobj.Data);
                                Rhino.Geometry.Curve    c    = new Rhino.Geometry.PolylineCurve(data);
                                ghCurve = new GH_Curve(c);
                            }
                            catch
                            {
                                var dict = JsonConvert.DeserializeObject <Dictionary <string, string> >(restobj.Data);
                                var c    = (Rhino.Geometry.Curve)Rhino.Runtime.CommonObject.FromJSON(dict);
                                ghCurve = new GH_Curve(c);
                            }
                            inputGroup.Param.AddVolatileData(path, i, ghCurve);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Circle)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject      restobj  = entree.Value[i];
                            Rhino.Geometry.Circle rhCircle = JsonConvert.DeserializeObject <Rhino.Geometry.Circle>(restobj.Data);
                            GH_Circle             data     = new GH_Circle(rhCircle);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Plane)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject     restobj = entree.Value[i];
                            Rhino.Geometry.Plane rhPlane = JsonConvert.DeserializeObject <Rhino.Geometry.Plane>(restobj.Data);
                            GH_Plane             data    = new GH_Plane(rhPlane);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Rectangle)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject           restobj     = entree.Value[i];
                            Rhino.Geometry.Rectangle3d rhRectangle = JsonConvert.DeserializeObject <Rhino.Geometry.Rectangle3d>(restobj.Data);
                            GH_Rectangle data = new GH_Rectangle(rhRectangle);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Box)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject   restobj = entree.Value[i];
                            Rhino.Geometry.Box rhBox   = JsonConvert.DeserializeObject <Rhino.Geometry.Box>(restobj.Data);
                            GH_Box             data    = new GH_Box(rhBox);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Surface)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject       restobj   = entree.Value[i];
                            Rhino.Geometry.Surface rhSurface = JsonConvert.DeserializeObject <Rhino.Geometry.Surface>(restobj.Data);
                            GH_Surface             data      = new GH_Surface(rhSurface);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Brep)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject    restobj = entree.Value[i];
                            Rhino.Geometry.Brep rhBrep  = JsonConvert.DeserializeObject <Rhino.Geometry.Brep>(restobj.Data);
                            GH_Brep             data    = new GH_Brep(rhBrep);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Mesh)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject    restobj = entree.Value[i];
                            Rhino.Geometry.Mesh rhMesh  = JsonConvert.DeserializeObject <Rhino.Geometry.Mesh>(restobj.Data);
                            GH_Mesh             data    = new GH_Mesh(rhMesh);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is GH_NumberSlider)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj  = entree.Value[i];
                            double           rhNumber = JsonConvert.DeserializeObject <double>(restobj.Data);
                            GH_Number        data     = new GH_Number(rhNumber);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Boolean || inputGroup.Param is GH_BooleanToggle)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj = entree.Value[i];
                            bool             boolean = JsonConvert.DeserializeObject <bool>(restobj.Data);
                            GH_Boolean       data    = new GH_Boolean(boolean);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is GH_Panel)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj  = entree.Value[i];
                            string           rhString = JsonConvert.DeserializeObject <string>(restobj.Data);
                            GH_String        data     = new GH_String(rhString);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }
            }
        }
Пример #29
0
        ///<summary>
        /// Verify validity of each input set with each error handled w/ separate message
        ///</summary>
        public bool FullErrorCheck(GH_Point pt, GH_Mesh msh, GH_Number tol, GH_Integer step, GH_Vector vec)
        {
            bool flag = true;

            flag = flag && MeshTolVecErrorCheck(msh, tol, vec);
            flag = flag && PointStepErrorCheck(pt, step);
            return(flag);
        }
Пример #30
0
        public static Response Grasshopper(NancyContext ctx)
        {
            // load grasshopper file
            var archive = new GH_Archive();
            // TODO: stream to string
            var body = ctx.Request.Body.ToString();

            string json = string.Empty;

            using (var reader = new StreamReader(ctx.Request.Body))
            {
                json = reader.ReadToEnd();
            }

            //GrasshopperInput input = Newtonsoft.Json.JsonConvert.DeserializeObject<GrasshopperInput>(json);
            //JsonSerializerSettings settings = new JsonSerializerSettings();
            //settings.ContractResolver = new DictionaryAsArrayResolver();
            Schema input = JsonConvert.DeserializeObject <Schema>(json);

            string grasshopperXml = string.Empty;

            if (input.Algo != null)
            {
                // If request contains markup
                byte[] byteArray = Convert.FromBase64String(input.Algo);
                grasshopperXml = System.Text.Encoding.UTF8.GetString(byteArray);
            }
            else
            {
                // If request contains pointer
                string pointer = input.Pointer;
                grasshopperXml = GetGhxFromPointer(pointer);
            }
            if (!archive.Deserialize_Xml(grasshopperXml))
            {
                throw new Exception();
            }

            var definition = new GH_Document();

            if (!archive.ExtractObject(definition, "Definition"))
            {
                throw new Exception();
            }

            // Set input params
            foreach (var obj in definition.Objects)
            {
                var group = obj as GH_Group;
                if (group == null)
                {
                    continue;
                }

                if (group.NickName.Contains("RH_IN"))
                {
                    // It is a RestHopper input group!
                    GHTypeCodes code  = (GHTypeCodes)Int32.Parse(group.NickName.Split(':')[1]);
                    var         param = group.Objects()[0];
                    //GH_Param<IGH_Goo> goo = obj as GH_Param<IGH_Goo>;

                    // SetData
                    foreach (Resthopper.IO.DataTree <ResthopperObject> tree in input.Values)
                    {
                        string paramname = tree.ParamName;
                        if (group.NickName == paramname)
                        {
                            switch (code)
                            {
                            case GHTypeCodes.Boolean:
                                //PopulateParam<GH_Boolean>(goo, tree);
                                Param_Boolean boolParam = param as Param_Boolean;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path           path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Boolean> objectList = new List <GH_Boolean>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject restobj = entree.Value[i];
                                        bool             boolean = JsonConvert.DeserializeObject <bool>(restobj.Data);
                                        GH_Boolean       data    = new GH_Boolean(boolean);
                                        boolParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Point:
                                //PopulateParam<GH_Point>(goo, tree);
                                Param_Point ptParam = param as Param_Point;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path         path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Point> objectList = new List <GH_Point>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject       restobj = entree.Value[i];
                                        Rhino.Geometry.Point3d rPt     = JsonConvert.DeserializeObject <Rhino.Geometry.Point3d>(restobj.Data);
                                        GH_Point data = new GH_Point(rPt);
                                        ptParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Vector:
                                //PopulateParam<GH_Vector>(goo, tree);
                                Param_Vector vectorParam = param as Param_Vector;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path          path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Vector> objectList = new List <GH_Vector>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject        restobj  = entree.Value[i];
                                        Rhino.Geometry.Vector3d rhVector = JsonConvert.DeserializeObject <Rhino.Geometry.Vector3d>(restobj.Data);
                                        GH_Vector data = new GH_Vector(rhVector);
                                        vectorParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Integer:
                                //PopulateParam<GH_Integer>(goo, tree);
                                Param_Integer integerParam = param as Param_Integer;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path           path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Integer> objectList = new List <GH_Integer>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject restobj = entree.Value[i];
                                        int        rhinoInt      = JsonConvert.DeserializeObject <int>(restobj.Data);
                                        GH_Integer data          = new GH_Integer(rhinoInt);
                                        integerParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Number:
                                //PopulateParam<GH_Number>(goo, tree);
                                Param_Number numberParam = param as Param_Number;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path          path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Number> objectList = new List <GH_Number>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject restobj  = entree.Value[i];
                                        double           rhNumber = JsonConvert.DeserializeObject <double>(restobj.Data);
                                        GH_Number        data     = new GH_Number(rhNumber);
                                        numberParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Text:
                                //PopulateParam<GH_String>(goo, tree);
                                Param_String stringParam = param as Param_String;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path          path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_String> objectList = new List <GH_String>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject restobj  = entree.Value[i];
                                        string           rhString = JsonConvert.DeserializeObject <string>(restobj.Data);
                                        GH_String        data     = new GH_String(rhString);
                                        stringParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Line:
                                //PopulateParam<GH_Line>(goo, tree);
                                Param_Line lineParam = param as Param_Line;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path        path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Line> objectList = new List <GH_Line>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject    restobj = entree.Value[i];
                                        Rhino.Geometry.Line rhLine  = JsonConvert.DeserializeObject <Rhino.Geometry.Line>(restobj.Data);
                                        GH_Line             data    = new GH_Line(rhLine);
                                        lineParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Curve:
                                //PopulateParam<GH_Curve>(goo, tree);
                                Param_Curve curveParam = param as Param_Curve;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path         path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Curve> objectList = new List <GH_Curve>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject restobj = entree.Value[i];
                                        GH_Curve         ghCurve;
                                        try
                                        {
                                            Rhino.Geometry.Polyline data = JsonConvert.DeserializeObject <Rhino.Geometry.Polyline>(restobj.Data);
                                            Rhino.Geometry.Curve    c    = new Rhino.Geometry.PolylineCurve(data);
                                            ghCurve = new GH_Curve(c);
                                        }
                                        catch
                                        {
                                            Rhino.Geometry.NurbsCurve data = JsonConvert.DeserializeObject <Rhino.Geometry.NurbsCurve>(restobj.Data);
                                            Rhino.Geometry.Curve      c    = new Rhino.Geometry.NurbsCurve(data);
                                            ghCurve = new GH_Curve(c);
                                        }
                                        curveParam.AddVolatileData(path, i, ghCurve);
                                    }
                                }
                                break;

                            case GHTypeCodes.Circle:
                                //PopulateParam<GH_Circle>(goo, tree);
                                Param_Circle circleParam = param as Param_Circle;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path          path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Circle> objectList = new List <GH_Circle>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject      restobj  = entree.Value[i];
                                        Rhino.Geometry.Circle rhCircle = JsonConvert.DeserializeObject <Rhino.Geometry.Circle>(restobj.Data);
                                        GH_Circle             data     = new GH_Circle(rhCircle);
                                        circleParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.PLane:
                                //PopulateParam<GH_Plane>(goo, tree);
                                Param_Plane planeParam = param as Param_Plane;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path         path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Plane> objectList = new List <GH_Plane>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject     restobj = entree.Value[i];
                                        Rhino.Geometry.Plane rhPlane = JsonConvert.DeserializeObject <Rhino.Geometry.Plane>(restobj.Data);
                                        GH_Plane             data    = new GH_Plane(rhPlane);
                                        planeParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Rectangle:
                                //PopulateParam<GH_Rectangle>(goo, tree);
                                Param_Rectangle rectangleParam = param as Param_Rectangle;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path             path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Rectangle> objectList = new List <GH_Rectangle>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject           restobj     = entree.Value[i];
                                        Rhino.Geometry.Rectangle3d rhRectangle = JsonConvert.DeserializeObject <Rhino.Geometry.Rectangle3d>(restobj.Data);
                                        GH_Rectangle data = new GH_Rectangle(rhRectangle);
                                        rectangleParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Box:
                                //PopulateParam<GH_Box>(goo, tree);
                                Param_Box boxParam = param as Param_Box;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path       path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Box> objectList = new List <GH_Box>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject   restobj = entree.Value[i];
                                        Rhino.Geometry.Box rhBox   = JsonConvert.DeserializeObject <Rhino.Geometry.Box>(restobj.Data);
                                        GH_Box             data    = new GH_Box(rhBox);
                                        boxParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Surface:
                                //PopulateParam<GH_Surface>(goo, tree);
                                Param_Surface surfaceParam = param as Param_Surface;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path           path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Surface> objectList = new List <GH_Surface>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject       restobj   = entree.Value[i];
                                        Rhino.Geometry.Surface rhSurface = JsonConvert.DeserializeObject <Rhino.Geometry.Surface>(restobj.Data);
                                        GH_Surface             data      = new GH_Surface(rhSurface);
                                        surfaceParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Brep:
                                //PopulateParam<GH_Brep>(goo, tree);
                                Param_Brep brepParam = param as Param_Brep;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path        path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Brep> objectList = new List <GH_Brep>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject    restobj = entree.Value[i];
                                        Rhino.Geometry.Brep rhBrep  = JsonConvert.DeserializeObject <Rhino.Geometry.Brep>(restobj.Data);
                                        GH_Brep             data    = new GH_Brep(rhBrep);
                                        brepParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Mesh:
                                //PopulateParam<GH_Mesh>(goo, tree);
                                Param_Mesh meshParam = param as Param_Mesh;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path        path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Mesh> objectList = new List <GH_Mesh>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject    restobj = entree.Value[i];
                                        Rhino.Geometry.Mesh rhMesh  = JsonConvert.DeserializeObject <Rhino.Geometry.Mesh>(restobj.Data);
                                        GH_Mesh             data    = new GH_Mesh(rhMesh);
                                        meshParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Slider:
                                //PopulateParam<GH_Number>(goo, tree);
                                GH_NumberSlider sliderParam = param as GH_NumberSlider;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path          path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Number> objectList = new List <GH_Number>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject restobj  = entree.Value[i];
                                        double           rhNumber = JsonConvert.DeserializeObject <double>(restobj.Data);
                                        GH_Number        data     = new GH_Number(rhNumber);
                                        sliderParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.BooleanToggle:
                                //PopulateParam<GH_Boolean>(goo, tree);
                                GH_BooleanToggle toggleParam = param as GH_BooleanToggle;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path           path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Boolean> objectList = new List <GH_Boolean>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject restobj   = entree.Value[i];
                                        bool             rhBoolean = JsonConvert.DeserializeObject <bool>(restobj.Data);
                                        GH_Boolean       data      = new GH_Boolean(rhBoolean);
                                        toggleParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Panel:
                                //PopulateParam<GH_String>(goo, tree);
                                GH_Panel panelParam = param as GH_Panel;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path         path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Panel> objectList = new List <GH_Panel>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject restobj  = entree.Value[i];
                                        string           rhString = JsonConvert.DeserializeObject <string>(restobj.Data);
                                        GH_String        data     = new GH_String(rhString);
                                        panelParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
            }

            Schema OutputSchema = new Schema();

            OutputSchema.Algo = Utils.Base64Encode(string.Empty);

            // Parse output params
            foreach (var obj in definition.Objects)
            {
                var group = obj as GH_Group;
                if (group == null)
                {
                    continue;
                }

                if (group.NickName.Contains("RH_OUT"))
                {
                    // It is a RestHopper output group!
                    GHTypeCodes code  = (GHTypeCodes)Int32.Parse(group.NickName.Split(':')[1]);
                    var         param = group.Objects()[0] as IGH_Param;
                    if (param == null)
                    {
                        continue;
                    }

                    try
                    {
                        param.CollectData();
                        param.ComputeData();
                    }
                    catch (Exception)
                    {
                        param.Phase = GH_SolutionPhase.Failed;
                        // TODO: throw something better
                        throw;
                    }

                    // Get data
                    Resthopper.IO.DataTree <ResthopperObject> OutputTree = new Resthopper.IO.DataTree <ResthopperObject>();
                    OutputTree.ParamName = group.NickName;

                    var volatileData = param.VolatileData;
                    for (int p = 0; p < volatileData.PathCount; p++)
                    {
                        List <ResthopperObject> ResthopperObjectList = new List <ResthopperObject>();
                        foreach (var goo in volatileData.get_Branch(p))
                        {
                            if (goo == null)
                            {
                                continue;
                            }
                            else if (goo.GetType() == typeof(GH_Boolean))
                            {
                                GH_Boolean ghValue = goo as GH_Boolean;
                                bool       rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <bool>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_Point))
                            {
                                GH_Point ghValue = goo as GH_Point;
                                Point3d  rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <Point3d>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_Vector))
                            {
                                GH_Vector ghValue = goo as GH_Vector;
                                Vector3d  rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <Vector3d>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_Integer))
                            {
                                GH_Integer ghValue = goo as GH_Integer;
                                int        rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <int>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_Number))
                            {
                                GH_Number ghValue = goo as GH_Number;
                                double    rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <double>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_String))
                            {
                                GH_String ghValue = goo as GH_String;
                                string    rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <string>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_Line))
                            {
                                GH_Line ghValue = goo as GH_Line;
                                Line    rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <Line>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_Curve))
                            {
                                GH_Curve ghValue = goo as GH_Curve;
                                Curve    rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <Curve>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_Circle))
                            {
                                GH_Circle ghValue = goo as GH_Circle;
                                Circle    rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <Circle>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_Plane))
                            {
                                GH_Plane ghValue = goo as GH_Plane;
                                Plane    rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <Plane>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_Rectangle))
                            {
                                GH_Rectangle ghValue = goo as GH_Rectangle;
                                Rectangle3d  rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <Rectangle3d>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_Box))
                            {
                                GH_Box ghValue = goo as GH_Box;
                                Box    rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <Box>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_Surface))
                            {
                                GH_Surface ghValue = goo as GH_Surface;
                                Brep       rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <Brep>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_Brep))
                            {
                                GH_Brep ghValue = goo as GH_Brep;
                                Brep    rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <Brep>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_Mesh))
                            {
                                GH_Mesh ghValue = goo as GH_Mesh;
                                Mesh    rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <Mesh>(rhValue));
                            }
                        }

                        GhPath path = new GhPath(new int[] { p });
                        OutputTree.Add(path.ToString(), ResthopperObjectList);
                    }

                    OutputSchema.Values.Add(OutputTree);
                }
            }


            if (OutputSchema.Values.Count < 1)
            {
                throw new System.Exceptions.PayAttentionException("Looks like you've missed something..."); // TODO
            }
            string returnJson = JsonConvert.SerializeObject(OutputSchema);

            return(returnJson);
        }
Пример #31
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            GH_Curve baseline = new GH_Curve();
            GH_Vector vector = new GH_Vector();
            GH_String family = new GH_String("");
            GH_String type = new GH_String("");
            List<Parameter> parameters = new List<Parameter>();
            if (!DA.GetDataList<Parameter>("Parameters", parameters)) parameters = new List<Parameter>();

            
            DA.GetData<GH_Vector>("Vector", ref vector);
            DA.GetData<GH_Curve>("Outline", ref baseline);

            if (baseline.Value.IsPolyline() && baseline.Value.IsClosed)
            {
                


                Rhino.Geometry.Polyline pline;
                if (baseline.Value.TryGetPolyline(out pline))
                {
                    PLine plineToExtrude = new PLine();
                    plineToExtrude.points = new List<Grevit.Types.Point>();
                    foreach (Rhino.Geometry.Point3d pkt in pline)
                    {
                        plineToExtrude.points.Add(pkt.ToGrevitPoint());
                    }
                    plineToExtrude.closed = pline.IsClosed;
                    plineToExtrude.GID = baseline.ReferenceID.ToString();


                    Grevit.Types.Point extrusionVector = new Grevit.Types.Point()
                    {
                        x = vector.Value.X,
                        y = vector.Value.Y,
                        z = vector.Value.Z
                    };

                    SimpleExtrusion extrusion = new SimpleExtrusion(family.Value, type.Value, parameters, plineToExtrude, extrusionVector);

                    Rhino.Geometry.Surface srf = Rhino.Geometry.Surface.CreateExtrusion(baseline.Value, vector.Value);
                    
                    SetGID(extrusion);
                    SetPreview(extrusion.GID, srf.ToBrep());

                    

                    DA.SetData("GrevitComponent",extrusion);
                }
            }
        }