public void Process(List <GH_Point> points, List <GH_Vector> vectors, GH_Surface surface)
        {
            var planes = Param["Pl"] as List <GH_Plane>;
            var k1     = (double)Param["k1"];
            var k2     = (double)Param["k2"];

            for (int i = 0; i < points.Count; i++)
            {
                foreach (var pl in planes)
                {
                    Point3d remap;
                    pl.Value.RemapToPlaneSpace(points[i].Value, out remap);

                    var     denom     = Math.Sqrt(Math.Pow(remap.X, 2) + Math.Pow(remap.Y, 2));
                    Point3d direction = new Point3d(remap.X / denom,
                                                    remap.Y / denom,
                                                    (-Math.Sign(remap.Z) * Math.Abs(remap.Z)) / Math.Pow(denom, k2));
                    direction = pl.Value.PointAt(direction.X, direction.Y, direction.Z);

                    var movement = direction - pl.Value.Origin;
                    movement         *= k1;
                    vectors[i].Value += movement;
                }
            }
        }
Exemple #2
0
        public bool ToGH_Surface <T>(ref T target)
        {
            object obj = new GH_Surface(ToBrep());

            target = (T)obj;
            return(true);
        }
Exemple #3
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            var surface = new GH_Surface();

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

            var output = new List <GH_Point>();

            Continuity c = Continuity.C0_continuous | Continuity.C1_continuous | Continuity.C2_continuous |
                           Continuity.C0_locus_continuous | Continuity.C1_locus_continuous | Continuity.C2_locus_continuous |
                           Continuity.Cinfinity_continuous;

            // u direction
            var u = 0.0d;
            var v = 0.0d;

            surface.Face.GetNextDiscontinuity(1, c, 0, 1, out u);
            output.Add(new GH_Point(surface.Face.PointAt(u, 0)));
            output.Add(new GH_Point(surface.Face.PointAt(u, 1)));

            DA.SetDataList(0, output);
        }
Exemple #4
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            var points          = new List <GH_Point>();
            var vectors         = new List <GH_Vector>();
            var surface         = new GH_Surface();
            var dynamicsWrapped = new List <GH_ObjectWrapper>();

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

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

            // if vec field is empty, create parallel list of 0 vectors
            if (vectors.Count == 0)
            {
                for (int i = 0; i < points.Count; i++)
                {
                    vectors.Add(new GH_Vector());
                }
            }

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

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

            var dynamics = new List <IDynamic>();

            foreach (var d in dynamicsWrapped)
            {
                dynamics.Add(d.Value as IDynamic);
            }
            Algos.SortDynamicsByPriority(dynamics);

            Algos.ClearDynamics(dynamics);

            foreach (var d in dynamics)
            {
                Algos.ProcessDynamics(d, points, vectors, surface);
            }

            Algos.RealignAccelerationVectors(dynamics, vectors);

            DA.SetDataList(0, points);
            DA.SetDataList(1, vectors);
        }
Exemple #5
0
        public void Process(List <GH_Point> points, List <GH_Vector> vectors, GH_Surface surface)
        {
            var v = (GH_Vector)Param["V"];

            for (int i = 0; i < points.Count; i++)
            {
                vectors[i].Value += v.Value;
            }
        }
Exemple #6
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            GH_Surface surface    = new GH_Surface();
            GH_String  lvlbtm     = new GH_String("");
            GH_String  style      = new GH_String("");
            GH_String  layer      = new GH_String("");
            GH_Number  taperAng   = new GH_Number(0);
            GH_Number  height     = new GH_Number();
            GH_Point   stop       = new GH_Point();
            GH_Point   sbtm       = new GH_Point();
            GH_Boolean structural = new GH_Boolean(true);

            List <Parameter> param = new List <Parameter>();

            if (!DA.GetDataList <Parameter>("Parameters", param))
            {
                param = new List <Parameter>();
            }
            DA.GetData <GH_Surface>("Surface", ref surface);

            DA.GetData <GH_String>("Layer", ref layer);
            //DA.GetData<GH_String>("Style", ref style);
            DA.GetData <GH_Number>("taperAngle", ref taperAng);
            DA.GetData <GH_Number>("height", ref height);

            Slab s = new Slab();

            s.FamilyOrStyle   = style.Value;
            s.TypeOrLayer     = layer.Value;
            s.levelbottom     = lvlbtm.Value;
            s.structural      = structural.Value;
            s.surface         = new Profile();
            s.surface.profile = new List <Loop>();
            Loop loop = new Loop()
            {
                outline = new List <Component>()
            };

            foreach (Rhino.Geometry.BrepEdge be in surface.Value.Edges)
            {
                loop.outline.Add(be.ToNurbsCurve().ToGrevitCurve());
            }
            s.surface.profile.Add(loop);


            s.height     = height.Value;
            s.parameters = param;
            //s.top = ComponentUtilities.GHPoint2Point(stop);
            //s.bottom = ComponentUtilities.GHPoint2Point(sbtm);
            s.slope = taperAng.Value;
            s.GID   = this.InstanceGuid.ToString();


            //SetPreview(s.GID, surface.Value);
            DA.SetData("GrevitComponent", s);
        }
Exemple #7
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)
        {
            List <IGH_GeometricGoo> geometry = new List <IGH_GeometricGoo>();
            Curve curve = null;

            if (!DA.GetDataList(0, geometry) || !DA.GetData(1, ref curve))
            {
                return;
            }
            ////////////////将于指定曲线线有关系的物件选出(Brep,Curve,Mesh)strict 选出完全重合,some 局部重合 ,all 相关联所有
            List <IGH_GeometricGoo> collect = new List <IGH_GeometricGoo>();
            List <int> indexes = new List <int>();

            for (int i = 0; i < geometry.Count; i++)
            {
                IGH_GeometricGoo obj = geometry[i];
                if (obj is GH_Curve)
                {
                    GH_Curve ghcurve = (GH_Curve)obj;
                    if (CurveAndCurve(ghcurve.Value, curve, strict, some, all))
                    {
                        indexes.Add(i);
                        collect.Add(obj);
                    }
                }
                else if (obj is GH_Brep)
                {
                    GH_Brep ghbrep = (GH_Brep)obj;
                    if (CurveAndBrep(ghbrep.Value, curve, strict, some, all))
                    {
                        indexes.Add(i);
                        collect.Add(obj);
                    }
                }
                else if (obj is GH_Surface)
                {
                    GH_Surface ghsurface = (GH_Surface)obj;
                    if (CurveAndBrep(ghsurface.Value, curve, strict, some, all))
                    {
                        indexes.Add(i);
                        collect.Add(obj);
                    }
                }
                else if (obj is GH_Mesh)
                {
                    GH_Mesh ghmesh = (GH_Mesh)obj;
                    if (CurveAndMesh(ghmesh.Value, curve, strict, some, all))
                    {
                        indexes.Add(i);
                        collect.Add(obj);
                    }
                }
            }
            DA.SetDataList(0, collect);
            DA.SetDataList(1, indexes);
        }
 public bool ToGH_Surface <T>(ref T target)
 {
     if (Polygon.Count == 1)
     {
         object obj = new GH_Surface(Polygon[0]);
         target = (T)obj;
         return(true);
     }
     return(false);
 }
Exemple #9
0
 public bool ToGH_Brep <T>(ref T target)
 {
     if (IsPlanar())
     {
         object obj = new GH_Surface(ToPlanarBrep());
         target = (T)obj;
         return(true);
     }
     return(false);
 }
Exemple #10
0
        public void Process(List <GH_Point> points, List <GH_Vector> vectors, GH_Surface surface)
        {
            var surfaces  = Param["Surfs"] as List <GH_Surface>;
            var alignList = new List <int>();

            for (int i = 0; i < points.Count; i++)
            {
                var point  = points[i].Value;
                var vector = vectors[i].Value;

                foreach (var surf in surfaces)
                {
                    var       curve = Curve.CreateControlPointCurve(new Point3d[] { point, point + vector });
                    Curve[]   overlaps;
                    Point3d[] intersections;
                    Intersection.CurveBrep(curve, surf.Value, 0.001d, out overlaps, out intersections);

                    if (intersections.Length > 0)
                    {
                        var intersect = intersections[0];

                        // get uv coordiantes of intersection
                        double u, v;
                        surf.Face.ClosestPoint(intersect, out u, out v);
                        var surfNormal = surf.Face.NormalAt(u, v);

                        // unitize manually, apply dot product
                        surfNormal *= 1 / surfNormal.Length;
                        surfNormal *= Algos.DotProduct(vector, surfNormal);

                        // black magic
                        vector -= 2 * surfNormal;

                        vectors[i].Value = vector;

                        alignList.Add(i);
                    }
                }
            }

            if (alignList.Count > 0)
            {
                Param["alignAccVectors"] = alignList;
            }
            else
            {
                Param.Remove("alignAccVectors");
            }
        }
        // we set up some defaults here to make it easier for the components
        // they will use these defaults if they are not passed a paramaters object
        public DynamicSettings()
        {
            respawn  = true;
            newSpawn = 0;
            rndSpawn = 0;

            avgRadius = 0.0d;
            snapTol   = 0.0d;
            snapAngle = 0.0d;
            windAngle = 0.0d;
            lineCont  = false;

            surface = new GH_Surface();
            bounds  = new GH_Brep();
        }
Exemple #12
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            GH_Point point = new GH_Point();

            Grevit.Types.Wall wall = null;


            GH_String layer = new GH_String("");
            GH_String style = new GH_String("");

            List <Parameter> param = new List <Parameter>();

            if (!DA.GetDataList <Parameter>("Parameters", param))
            {
                param = new List <Parameter>();
            }

            DA.GetData <GH_Point>("Point", ref point);
            DA.GetData <GH_String>("Layer", ref layer);
            DA.GetData <GH_String>("Style", ref style);
            DA.GetData <Wall>("wall", ref wall);


            Door d = new Door();

            SetGID(d);
            d.stalledForReference = true;
            d.TypeOrLayer         = layer.Value;
            d.FamilyOrStyle       = style.Value;
            d.locationPoint       = point.ToGrevitPoint();
            d.parameters          = param;
            SetGID(d);



            Rhino.Geometry.Circle  c   = new Rhino.Geometry.Circle(point.Value, 0.5);
            Rhino.Geometry.Surface srf = Rhino.Geometry.NurbsSurface.CreateExtrusion(c.ToNurbsCurve(), new Rhino.Geometry.Vector3d(0, 0, 2));



            SetPreview(d.GID, srf.ToBrep());


            GH_Surface ghb = new GH_Surface(srf);

            DA.SetData("GrevitComponent", d);
        }
Exemple #13
0
        public void Process(List <GH_Point> points, List <GH_Vector> vectors, GH_Surface surface)
        {
            var dynamicsWrapped = Param["D"] as List <GH_ObjectWrapper>;

            var dynamics = new List <IDynamic>();

            foreach (var d in dynamicsWrapped)
            {
                dynamics.Add(d.Value as IDynamic);
            }
            Algos.SortDynamicsByPriority(dynamics);

            foreach (var d in dynamics)
            {
                Algos.ProcessDynamics(d, points, vectors, surface);
            }
        }
        public override List <SpeckleObjectProperties> getObjectProperties(IEnumerable <object> objects)
        {
            var propertiesList = new List <SpeckleObjectProperties>();
            var simpleProps    = new List <ArchivableDictionary>();
            int k = 0;

            foreach (object o in objects)
            {
                CommonObject myObj = null;

                GH_Brep brep = o as GH_Brep;
                if (brep != null)
                {
                    myObj = brep.Value;
                }

                GH_Surface srf = o as GH_Surface;
                if (srf != null)
                {
                    myObj = srf.Value;
                }

                GH_Mesh mesh = o as GH_Mesh;
                if (mesh != null)
                {
                    myObj = mesh.Value;
                }

                GH_Curve crv = o as GH_Curve;
                if (crv != null)
                {
                    myObj = crv.Value;
                }

                if (myObj != null)
                {
                    if (myObj.UserDictionary.Keys.Length > 0)
                    {
                        propertiesList.Add(new SpeckleObjectProperties(k, myObj.UserDictionary));
                    }
                }

                k++;
            }
            return(propertiesList);
        }
        // we set up some defaults here to make it easier for the components
        // they will use these defaults if they are not passed a paramaters object
        public StaticSettings()
        {
            steps     = 0;
            tolerance = 10.0d;
            stop      = true;
            avgRadius = 0.0d;
            snapTol   = 0.0d;
            snapAngle = 0.0d;
            windAngle = 0.0d;
            tensor    = false;
            tensorDir = 0;
            lineCont  = false;

            tensorAxes = new List <int>(new [] { 0, 1 });

            surface = new GH_Surface();
            bounds  = new GH_Brep();
        }
Exemple #16
0
        public void Process(List <GH_Point> points, List <GH_Vector> vectors, GH_Surface surface)
        {
            var sf = ((GH_Number)Param["S"]).Value;
            var a  = ((GH_Number)Param["F"]).Value;

            foreach (var v in vectors)
            {
                var x = v.Value.Length;

                if (x > a)
                {
                    x        = a + Math.Log(x + 1 - a);
                    v.Value *= x / v.Value.Length;
                }

                v.Value *= sf;
            }
        }
Exemple #17
0
        public static bool CheckIfOffSurface(Basis point, GH_Surface surface)
        {
            // surface constraint check
            if (surface.IsValid)
            {
                double u, v;
                surface.Face.ClosestPoint(point.Point.Value, out u, out v);

                point.Point = new GH_Point(surface.Face.PointAt(u, v));

                if (surface.Face.IsPointOnFace(u, v) == PointFaceRelation.Boundary)
                {
                    return(true);
                }
            }

            return(false);
        }
Exemple #18
0
        public static GH_Surface ProjectSurfaceToTopoFast(Mesh topoMesh, Surface featureSurface)
        {
            GH_Surface   ghSurface = new GH_Surface();
            NurbsSurface surface   = featureSurface.ToNurbsSurface();

            ///Move patch verts to topo
            for (int u = 0; u < surface.Points.CountU; u++)
            {
                for (int v = 0; v < surface.Points.CountV; v++)
                {
                    Point3d controlPt;
                    surface.Points.GetPoint(u, v, out controlPt);
                    Ray3d  ray = new Ray3d(controlPt, moveDir);
                    double t   = Rhino.Geometry.Intersect.Intersection.MeshRay(topoMesh, ray);

                    if (t >= 0.0)
                    {
                        surface.Points.SetPoint(u, v, ray.PointAt(t));
                    }
                    else
                    {
                        Ray3d  rayOpp = new Ray3d(controlPt, -moveDir);
                        double tOpp   = Rhino.Geometry.Intersect.Intersection.MeshRay(topoMesh, rayOpp);
                        if (tOpp >= 0.0)
                        {
                            surface.Points.SetPoint(u, v, rayOpp.PointAt(t));
                        }
                        else
                        {
                            //return null;
                        }
                    }
                }
            }
            if (surface.IsValid)
            {
                GH_Convert.ToGHSurface(surface, GH_Conversion.Primary, ref ghSurface);
                return(ghSurface);
            }
            else
            {
                return(null);
            }
        }
Exemple #19
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            Types.Assembly assembly = new Types.Assembly();
            DA.GetData <Types.Assembly>("Material", ref assembly);

            GH_Number  area    = new GH_Number(0);
            GH_Surface surface = new GH_Surface();

            if (!DA.GetData <GH_Number>("Area", ref area))
            {
                area = new GH_Number(0);
            }
            if (!DA.GetData <GH_Surface>("Surface", ref surface))
            {
                surface = new GH_Surface();
            }

            double calculationArea = area.Value;

            if (surface.Value != null)
            {
                calculationArea += surface.Value.GetArea();
            }

            Types.Result result = new Types.Result()
            {
                GlobalWarmingPotential     = new Types.UnitDouble <Types.LCA.CO2e>(assembly.GlobalWarmingPotential.Value * calculationArea),
                Acidification              = new Types.UnitDouble <Types.LCA.kgSO2>(assembly.Acidification.Value * calculationArea),
                DepletionOfNonrenewbles    = new Types.UnitDouble <Types.LCA.MJ>(assembly.DepletionOfNonrenewbles.Value * calculationArea),
                DepletionOfOzoneLayer      = new Types.UnitDouble <Types.LCA.kgCFC11>(assembly.DepletionOfOzoneLayer.Value * calculationArea),
                Eutrophication             = new Types.UnitDouble <Types.LCA.kgPhostphate>(assembly.Eutrophication.Value * calculationArea),
                FormationTroposphericOzone = new Types.UnitDouble <Types.LCA.kgNOx>(assembly.FormationTroposphericOzone.Value * calculationArea)
            };



            DA.SetData("LCA Result", result);
        }
Exemple #20
0
        /*************************************/

        private static T FromGoo <T>(this GH_Surface goo, IGH_TypeHint hint = null)
        {
            Brep brep = goo.ScriptVariable() as Brep;

            oM.Geometry.IGeometry geometry = null;
            if (brep.IsSurface)
            {
                geometry = BH.Engine.Rhinoceros.Convert.IFromRhino(brep.Faces[0].UnderlyingSurface());
            }
            else
            {
                geometry = BH.Engine.Rhinoceros.Convert.IFromRhino(brep);
            }

            try
            {
                return((T)(geometry as dynamic));
            }
            catch
            {
                Engine.Base.Compute.RecordError("Failed to cast " + geometry.GetType().IToText() + " into " + typeof(T).IToText());
                return(default(T));
            }
        }
Exemple #21
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 ghGeometryBase = null;
            double tolerance      = 0.0001;

            if (!DA.GetData(0, ref ghGeometryBase))
            {
                return;
            }
            if (!DA.GetData(1, ref tolerance))
            {
                return;
            }

            if (ghGeometryBase == null)
            {
                return;
            }
            Type type = ghGeometryBase.GetType();

            Topologic.Topology topology = null;
            GH_Point           ghPoint  = ghGeometryBase as GH_Point;

            if (ghPoint != null)
            {
                topology = ByPoint(ghPoint.Value);
                DA.SetData(0, topology);
                return;
            }

            GH_Line ghLine = ghGeometryBase as GH_Line;

            if (ghLine != null)
            {
                topology = ByLine(ghLine.Value);
                DA.SetData(0, topology);
                return;
            }

            GH_Curve ghCurve = ghGeometryBase as GH_Curve;

            if (ghCurve != null)
            {
                topology = ByCurve(ghCurve.Value);
                DA.SetData(0, topology);
                return;
            }

            GH_Surface ghSurface = ghGeometryBase as GH_Surface;

            if (ghSurface != null)
            {
                //topology = ByBrep(ghSurface.Value.Faces[0].ToBrep(), tolerance);
                //topology = ByBrepFace(ghSurface.Value.Faces[0]);
                topology = ByBrep(ghSurface.Value, tolerance);
                DA.SetData(0, topology);
                return;
            }

            GH_Brep ghBrep = ghGeometryBase as GH_Brep;

            if (ghBrep != null)
            {
                topology = ByBrep(ghBrep.Value, tolerance);
                DA.SetData(0, topology);
                return;
            }

            GH_Box ghBox = ghGeometryBase as GH_Box;

            if (ghBox != null)
            {
                topology = ByBox(ghBox.Value);
                DA.SetData(0, topology);
                return;
            }

            GH_Mesh ghMesh = ghGeometryBase as GH_Mesh;

            if (ghMesh != null)
            {
                topology = ByMesh(ghMesh.Value);
                DA.SetData(0, topology);
                return;
            }

            //BrepLoop ghBrepLoop = ghGeometryBase as BrepLoop;
            //if (ghBrepLoop != null)
            //{
            //    topology = ByBrepLoop(ghBrepLoop);
            //    DA.SetData(0, topology);
            //    return;
            //}

            throw new Exception("This type of geometry is not yet supported.");
        }
Exemple #22
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            Types.Assembly assembly = new Types.Assembly();
            DA.GetData<Types.Assembly>("Material", ref assembly);

            GH_Number area = new GH_Number(0);
            GH_Surface surface = new GH_Surface();

            if (!DA.GetData<GH_Number>("Area", ref area)) area = new GH_Number(0);
            if (!DA.GetData<GH_Surface>("Surface", ref surface)) surface = new GH_Surface();

            double calculationArea = area.Value;
            if (surface.Value != null) calculationArea += surface.Value.GetArea();

            Types.Result result = new Types.Result()
            {
                GlobalWarmingPotential = new Types.UnitDouble<Types.LCA.CO2e>(assembly.GlobalWarmingPotential.Value * calculationArea),
                Acidification = new Types.UnitDouble<Types.LCA.kgSO2>(assembly.Acidification.Value * calculationArea),
                DepletionOfNonrenewbles = new Types.UnitDouble<Types.LCA.MJ>(assembly.DepletionOfNonrenewbles.Value * calculationArea),
                DepletionOfOzoneLayer = new Types.UnitDouble<Types.LCA.kgCFC11>(assembly.DepletionOfOzoneLayer.Value * calculationArea),
                Eutrophication = new Types.UnitDouble<Types.LCA.kgPhostphate>(assembly.Eutrophication.Value * calculationArea),
                FormationTroposphericOzone = new Types.UnitDouble<Types.LCA.kgNOx>(assembly.FormationTroposphericOzone.Value * calculationArea)
            };

            DA.SetData("LCA Result", result);
        }
        /// <summary>
        /// Determines object type and calls the appropriate conversion call.
        /// </summary>
        /// <param name="o">Object to convert.</param>
        /// <param name="getEncoded">If set to true, will return a base64 encoded value of more complex objects (ie, breps).</param>
        /// <returns></returns>
        private static SpeckleObject fromGhRhObject(object o, bool getEncoded = false, bool getAbstract = true)
        {
            GH_Interval int1d = o as GH_Interval;

            if (int1d != null)
            {
                return(GhRhConveter.fromInterval(int1d.Value));
            }
            if (o is Interval)
            {
                return(GhRhConveter.fromInterval((Interval)o));
            }

            GH_Interval2D int2d = o as GH_Interval2D;

            if (int2d != null)
            {
                return(GhRhConveter.fromInterval2d(int2d.Value));
            }
            if (o is UVInterval)
            {
                return(GhRhConveter.fromInterval2d((UVInterval)o));
            }

            // basic stuff
            GH_Number num = o as GH_Number;

            if (num != null)
            {
                return(SpeckleConverter.fromNumber(num.Value));
            }
            if (o is double || o is int)
            {
                return(SpeckleConverter.fromNumber((double)o));
            }

            GH_Boolean bul = o as GH_Boolean;

            if (bul != null)
            {
                return(SpeckleConverter.fromBoolean(bul.Value));
            }
            if (o is Boolean)
            {
                return(GhRhConveter.fromBoolean((bool)o));
            }

            GH_String str = o as GH_String;

            if (str != null)
            {
                return(SpeckleConverter.fromString(str.Value));
            }
            if (o is string)
            {
                return(SpeckleConverter.fromString((string)o));
            }

            // simple geometry
            GH_Point point = o as GH_Point;

            if (point != null)
            {
                return(GhRhConveter.fromPoint(point.Value));
            }
            if (o is Point3d)
            {
                return(GhRhConveter.fromPoint((Point3d)o));
            }
            // added because when we assign user data to points they get converted to points.
            // the above comment does makes sense. check the sdk.
            if (o is Rhino.Geometry.Point)
            {
                return(GhRhConveter.fromPoint(((Rhino.Geometry.Point)o).Location));
            }

            GH_Vector vector = o as GH_Vector;

            if (vector != null)
            {
                return(GhRhConveter.fromVector(vector.Value));
            }
            if (o is Vector3d)
            {
                return(GhRhConveter.fromVector((Vector3d)o));
            }

            GH_Plane plane = o as GH_Plane;

            if (plane != null)
            {
                return(GhRhConveter.fromPlane(plane.Value));
            }
            if (o is Plane)
            {
                return(GhRhConveter.fromPlane((Plane)o));
            }

            GH_Line line = o as GH_Line;

            if (line != null)
            {
                return(GhRhConveter.fromLine(line.Value));
            }
            if (o is Line)
            {
                return(GhRhConveter.fromLine((Line)o));
            }

            GH_Arc arc = o as GH_Arc;

            if (arc != null)
            {
                return(GhRhConveter.fromArc(arc.Value));
            }
            if (o is Arc)
            {
                return(GhRhConveter.fromArc((Arc)o));
            }

            GH_Circle circle = o as GH_Circle;

            if (circle != null)
            {
                return(GhRhConveter.fromCircle(circle.Value));
            }
            if (o is Circle)
            {
                return(GhRhConveter.fromCircle((Circle)o));
            }

            GH_Rectangle rectangle = o as GH_Rectangle;

            if (rectangle != null)
            {
                return(GhRhConveter.fromRectangle(rectangle.Value));
            }
            if (o is Rectangle3d)
            {
                return(GhRhConveter.fromRectangle((Rectangle3d)o));
            }

            GH_Box box = o as GH_Box;

            if (box != null)
            {
                return(GhRhConveter.fromBox(box.Value));
            }
            if (o is Box)
            {
                return(GhRhConveter.fromBox((Box)o));
            }

            // getting complex
            GH_Curve curve = o as GH_Curve;

            if (curve != null)
            {
                Polyline poly;
                if (curve.Value.TryGetPolyline(out poly))
                {
                    return(GhRhConveter.fromPolyline(poly));
                }
                return(GhRhConveter.fromCurve(curve.Value, getEncoded, getAbstract));
            }

            if (o is Polyline)
            {
                return(GhRhConveter.fromPolyline((Polyline)o));
            }
            if (o is Curve)
            {
                return(GhRhConveter.fromCurve((Curve)o, getEncoded, getAbstract));
            }

            GH_Surface surface = o as GH_Surface;

            if (surface != null)
            {
                return(GhRhConveter.fromBrep(surface.Value, getEncoded, getAbstract));
            }

            GH_Brep brep = o as GH_Brep;

            if (brep != null)
            {
                return(GhRhConveter.fromBrep(brep.Value, getEncoded, getAbstract));
            }

            if (o is Brep)
            {
                return(GhRhConveter.fromBrep((Brep)o, getEncoded, getAbstract));
            }

            GH_Mesh mesh = o as GH_Mesh;

            if (mesh != null)
            {
                return(GhRhConveter.fromMesh(mesh.Value));
            }
            if (o is Mesh)
            {
                return(GhRhConveter.fromMesh((Mesh)o));
            }

            return(new SpeckleObject()
            {
                hash = "404", value = "Type not supported", type = "404"
            });
        }
        /// <summary>
        /// Updates the geometry for an input chromosome
        /// </summary>
        /// <param name="chromo">The chromosome used to get geometry from the gh canvas</param>
        /// <returns></returns>
        public int GetGeometry(Chromosome chromo)
        {
            // Collect the object at the current instance
            List <object> localObjs = new List <object>();

            // Thank you Dimitrie :)
            foreach (IGH_Param param in Params.Input[1].Sources)
            {
                foreach (Object myObj in param.VolatileData.AllData(true)) // AllData flattens the tree
                {
                    localObjs.Add(myObj);
                }
            }

            // Gets lists of different geometry
            List <Mesh>          meshGeometry = new List <Mesh>();
            List <PolylineCurve> polyGeometry = new List <PolylineCurve>();

            // Get only mesh geometry from the object list
            for (int i = 0; i < localObjs.Count; i++)
            {
                // Need to replace with a Switch

                if (localObjs[i] is GH_Mesh)
                {
                    GH_Mesh myGHMesh = new GH_Mesh();
                    myGHMesh = (GH_Mesh)localObjs[i];
                    Mesh myLocalMesh = new Mesh();
                    GH_Convert.ToMesh(myGHMesh, ref myLocalMesh, GH_Conversion.Primary);
                    myLocalMesh.Faces.ConvertQuadsToTriangles();

                    meshGeometry.Add(myLocalMesh);

                    //Mesh joinedMesh = new Mesh();
                    //joinedMesh.Append(myLocalMesh);
                }

                if (localObjs[i] is GH_Brep)
                {
                    GH_Brep myBrep = new GH_Brep();
                    myBrep = (GH_Brep)localObjs[i];

                    Mesh[] meshes = Mesh.CreateFromBrep(myBrep.Value, MeshingParameters.FastRenderMesh);

                    Mesh mesh = new Mesh();
                    mesh.Append(meshes);
                    mesh.Faces.ConvertQuadsToTriangles();

                    meshGeometry.Add(mesh);
                }

                if (localObjs[i] is GH_Box)
                {
                    GH_Box myBox  = new GH_Box((GH_Box)localObjs[i]);
                    Mesh[] meshes = Mesh.CreateFromBrep(myBox.Brep(), MeshingParameters.FastRenderMesh);

                    Mesh mesh = new Mesh();
                    mesh.Append(meshes);
                    mesh.Faces.ConvertQuadsToTriangles();

                    meshGeometry.Add(mesh);
                }

                if (localObjs[i] is GH_Surface)
                {
                    GH_Surface mySurface = (GH_Surface)localObjs[i];

                    Mesh[] meshes = Mesh.CreateFromBrep(mySurface.Value, MeshingParameters.FastRenderMesh);

                    Mesh mesh = new Mesh();
                    mesh.Append(meshes);
                    mesh.Faces.ConvertQuadsToTriangles();

                    meshGeometry.Add(mesh);
                }

                if (localObjs[i] is GH_Curve)
                {
                    GH_Curve      myGHCurve = (GH_Curve)localObjs[i];
                    PolylineCurve myPoly    = myGHCurve.Value.ToPolyline(0, 0, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, true);
                    polyGeometry.Add(myPoly);
                }

                if (localObjs[i] is GH_Arc)
                {
                    GH_Arc        myGHArc = (GH_Arc)localObjs[i];
                    PolylineCurve myPoly  = myGHArc.Value.ToNurbsCurve().ToPolyline(0, 0, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, true);
                    polyGeometry.Add(myPoly);
                }

                if (localObjs[i] is GH_Line)
                {
                    GH_Line        myGHLine = (GH_Line)localObjs[i];
                    List <Point3d> pts      = new List <Point3d> {
                        myGHLine.Value.From, myGHLine.Value.To
                    };
                    PolylineCurve myPoly = new PolylineCurve(pts);
                    polyGeometry.Add(myPoly);
                }
            }

            // Get performance data
            List <double> performas = new List <double>();
            List <string> criteria  = new List <string>();

            // Cap at eight criteria max.
            int pCount        = 0;
            int repeatCounter = 1;

            foreach (IGH_Param param in Params.Input[2].Sources)
            {
                foreach (Object myObj in param.VolatileData.AllData(true))
                {
                    if (myObj is GH_Number && pCount < 8)
                    {
                        GH_Number temp = (GH_Number)myObj;
                        performas.Add(temp.Value);

                        if (!criteria.Contains(param.NickName))
                        {
                            criteria.Add(param.NickName);
                        }

                        else
                        {
                            criteria.Add(param.NickName + " (" + repeatCounter + ")");
                            repeatCounter++;
                        }

                        pCount++;
                    }

                    else if (myObj is GH_Integer && pCount < 8)
                    {
                        GH_Integer temp = (GH_Integer)myObj;
                        performas.Add((double)temp.Value);

                        if (!criteria.Contains(param.NickName))
                        {
                            criteria.Add(param.NickName);
                        }

                        else
                        {
                            criteria.Add(param.NickName + " (" + repeatCounter + ")");
                            repeatCounter++;
                        }

                        pCount++;
                    }
                }
            }

            // Set the phenotype within the chromosome class
            chromo.SetPhenotype(meshGeometry, polyGeometry, performas, criteria);

            // Return the number of performance criteria
            return(performas.Count);
        }
Exemple #25
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;
                }
            }
        }
Exemple #26
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="dataAccess">
        /// The DA object is used to retrieve from inputs and store in outputs.
        /// </param>
        protected override void SolveInstance(IGH_DataAccess dataAccess)
        {
            object @object   = null;
            double tolerance = 0.0001;

            if (!dataAccess.GetData(0, ref @object))
            {
                return;
            }

            if (!dataAccess.GetData(1, ref tolerance))
            {
                return;
            }

            if (@object == null)
            {
                return;
            }

            Type type = @object.GetType();

            global::Topologic.Topology topology = null;
            GH_Point ghPoint = @object as GH_Point;

            if (ghPoint != null)
            {
                topology = ghPoint.Value.ToTopologic();
                dataAccess.SetData(0, topology);
                return;
            }

            GH_Line ghLine = @object as GH_Line;

            if (ghLine != null)
            {
                topology = ghLine.Value.ToTopologic();
                dataAccess.SetData(0, topology);
                return;
            }

            GH_Curve ghCurve = @object as GH_Curve;

            if (ghCurve != null)
            {
                topology = ghCurve.Value.ToTopologic();
                dataAccess.SetData(0, topology);
                return;
            }

            GH_Surface ghSurface = @object as GH_Surface;

            if (ghSurface != null)
            {
                topology = ghSurface.Value.ToTopologic(tolerance);
                dataAccess.SetData(0, topology);
                return;
            }

            GH_Brep ghBrep = @object as GH_Brep;

            if (ghBrep != null)
            {
                topology = ghBrep.Value.ToTopologic(tolerance);
                dataAccess.SetData(0, topology);
                return;
            }

            GH_Box ghBox = @object as GH_Box;

            if (ghBox != null)
            {
                topology = ghBox.Value.ToTopologic();
                dataAccess.SetData(0, topology);
                return;
            }

            GH_Mesh ghMesh = @object as GH_Mesh;

            if (ghMesh != null)
            {
                topology = ghMesh.Value.ToTopologic();
                dataAccess.SetData(0, topology);
                return;
            }

            throw new Exception("Cannot convert geometry.");
        }
        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);
        }
Exemple #28
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            GH_String name = new GH_String();
            DA.GetData<GH_String>("Name", ref name);
            GH_String view = new GH_String();
            DA.GetData<GH_String>("View", ref view);
            GH_Surface srf = new GH_Surface();
            DA.GetData<GH_Surface>("Plane", ref srf);
            List<Parameter> parameters = new List<Parameter>();
            if (!DA.GetDataList<Parameter>("Parameters", parameters)) parameters = new List<Parameter>();

            Rhino.Geometry.Point3d[] pkts = srf.Value.GetBoundingBox(true).GetCorners();

            Grevit.Types.ReferencePlane referencePlane = new ReferencePlane();
            referencePlane.Name = name.Value;
            referencePlane.EndA = pkts[0].ToGrevitPoint();
            referencePlane.EndB = pkts[1].ToGrevitPoint();
            referencePlane.cutVector = srf.Value.GetBoundingBox(true).Center.ToGrevitPoint();
            referencePlane.View = view.Value;
            SetGID(referencePlane);

            DA.SetData("GrevitComponent", referencePlane);
        }
Exemple #29
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            GH_Surface surface = new GH_Surface();
            GH_String level = new GH_String();
            GH_String family = new GH_String("");
            GH_String type = new GH_String("");

            GH_Boolean structural = new GH_Boolean(false); 
            GH_Number slope = new GH_Number(0.0);
            List<Parameter> parameters = new List<Parameter>();
            if (!DA.GetDataList<Parameter>("Parameters", parameters)) parameters = new List<Parameter>();
            DA.GetData<GH_Surface>("Surface", ref surface);

            GH_Point slopeTopPoint = new GH_Point(surface.Value.Edges[0].PointAtStart);
            GH_Point slopeBottomPoint = new GH_Point(surface.Value.Edges[0].PointAtEnd);

            //DA.GetData<GH_String>("Family", ref family);
            //DA.GetData<GH_String>("Type", ref type);
            DA.GetData<GH_String>("Levelbottom", ref level);
            DA.GetData<GH_Boolean>("Structural", ref structural);
            DA.GetData<GH_Point>("SlopeTopPoint", ref slopeTopPoint);
            DA.GetData<GH_Point>("SlopeBottomPoint", ref slopeBottomPoint);
            DA.GetData<GH_Number>("Slope", ref slope);

            

            Slab slab = new Slab();
            slab.FamilyOrStyle = family.Value;
            slab.TypeOrLayer = type.Value;
            slab.levelbottom = level.Value;
            slab.structural = structural.Value;
            slab.surface = new Profile();
            slab.surface.profile = new List<Loop>();
            
            Loop loop = new Loop();
            loop.outline = new List<Component>();

            foreach (Rhino.Geometry.BrepEdge be in surface.Value.Edges)
            {
                loop.outline.Add(be.ToNurbsCurve().ToGrevitCurve());
            }

            slab.surface.profile.Add(loop);

            slab.parameters = parameters;
            slab.top = slopeTopPoint.ToGrevitPoint();
            slab.bottom = slopeBottomPoint.ToGrevitPoint();
            slab.slope = slope.Value;
            slab.GID = this.InstanceGuid.ToString();



            DA.SetData("GrevitComponent", slab);
        }
Exemple #30
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 o = null;

            DA.GetData(0, ref o);
            if (o == null)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Failed to get object"); return;
            }

            CommonObject myObj = null;

            GH_Mesh mesh = o as GH_Mesh;

            if (mesh != null)
            {
                myObj = mesh.Value;
            }

            GH_Brep brep = o as GH_Brep;

            if (brep != null)
            {
                myObj = brep.Value;
            }

            GH_Surface srf = o as GH_Surface;

            if (srf != null)
            {
                myObj = srf.Value;
            }

            GH_Curve crv = o as GH_Curve;

            if (crv != null)
            {
                myObj = crv.Value;
            }

            Point pt = o as Rhino.Geometry.Point;

            if (pt != null)
            {
                myObj = pt;
            }

            if (myObj == null)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Failed to get object");
                DA.SetData(0, null);
                return;
            }

            if (myObj.UserDictionary == null)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Object has no user dictionary.");
                DA.SetData(0, null);
                return;
            }

            DA.SetData(0, myObj.UserDictionary);
        }
Exemple #31
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            GH_Surface surface = new GH_Surface();
            GH_String lvlbtm = new GH_String("");
            GH_String style = new GH_String("");
            GH_String layer = new GH_String("");
            GH_Number taperAng = new GH_Number(0);
            GH_Number height = new GH_Number();
            GH_Point stop = new GH_Point();
            GH_Point sbtm = new GH_Point();
            GH_Boolean structural = new GH_Boolean(true);

            List<Parameter> param = new List<Parameter>();
            if (!DA.GetDataList<Parameter>("Parameters", param)) param = new List<Parameter>();
            DA.GetData<GH_Surface>("Surface", ref surface);

            DA.GetData<GH_String>("Layer", ref layer);
            //DA.GetData<GH_String>("Style", ref style);
            DA.GetData<GH_Number>("taperAngle", ref taperAng);
            DA.GetData<GH_Number>("height", ref height);

            Slab s = new Slab();
            s.FamilyOrStyle = style.Value;
            s.TypeOrLayer = layer.Value;
            s.levelbottom = lvlbtm.Value;
            s.structural = structural.Value;
            s.surface = new Surface();
            s.surface.outline = new List<Component>();

            foreach (Rhino.Geometry.BrepEdge be in surface.Value.Edges)
            {
                s.surface.outline.Add(be.ToNurbsCurve().ToGrevitCurve());
            }

            s.height = height.Value;
            s.parameters = param;
            //s.top = ComponentUtilities.GHPoint2Point(stop);
            //s.bottom = ComponentUtilities.GHPoint2Point(sbtm);
            s.slope = taperAng.Value;
            s.GID = this.InstanceGuid.ToString();

            //SetPreview(s.GID, surface.Value);
            DA.SetData("GrevitComponent", s);
        }
Exemple #32
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            GH_Point point = new GH_Point();
            Grevit.Types.Wall wall = null;

            GH_String layer = new GH_String("");
            GH_String style = new GH_String("");

            List<Parameter> param = new List<Parameter>();
            if (!DA.GetDataList<Parameter>("Parameters", param)) param = new List<Parameter>();

            DA.GetData<GH_Point>("Point", ref point);
            DA.GetData<GH_String>("Layer", ref layer);
            DA.GetData<GH_String>("Style", ref style);
            DA.GetData<Wall>("wall", ref wall);

            Door d = new Door();
            SetGID(d);
            d.stalledForReference = true;
            d.TypeOrLayer = layer.Value;
            d.FamilyOrStyle = style.Value;
            d.locationPoint = point.ToGrevitPoint();
            d.parameters = param;
            SetGID(d);

               Rhino.Geometry.Circle c = new Rhino.Geometry.Circle(point.Value, 0.5);
               Rhino.Geometry.Surface srf = Rhino.Geometry.NurbsSurface.CreateExtrusion(c.ToNurbsCurve(), new Rhino.Geometry.Vector3d(0,0, 2));

            SetPreview(d.GID, srf.ToBrep());

            GH_Surface ghb = new GH_Surface(srf);

            DA.SetData("GrevitComponent", d);
        }
Exemple #33
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;
            }
        }
        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;
                }
            }
        }
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object can be used to retrieve data from input parameters and
        /// to store data in output parameters.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            object obj = null;

            DA.GetData(0, ref obj);

            // Remarks:
            // 1) why lines, rectangles or *any* rhinocommon object can't have user dicts?
            // 2) @David: i hate grasshopper typecasting and the hassle below
            // (why isn't there a GH_DefaultType, where i can access the .Value regardless of type...?)

            GeometryBase myObj = null;

            GH_Mesh mesh = obj as GH_Mesh;

            if (mesh != null)
            {
                myObj = mesh.Value;
            }

            GH_Brep brep = obj as GH_Brep;

            if (brep != null)
            {
                myObj = brep.Value;
            }

            GH_Surface srf = obj as GH_Surface;

            if (srf != null)
            {
                myObj = srf.Value;
            }

            GH_Box box = obj as GH_Box;

            if (box != null)
            {
                myObj = box.Value.ToBrep();
            }

            GH_Curve crv = obj as GH_Curve;

            if (crv != null)
            {
                myObj = crv.Value;
            }

            GH_Line line = obj as GH_Line;

            if (line != null)
            {
                myObj = line.Value.ToNurbsCurve();
            }

            GH_Rectangle rect = obj as GH_Rectangle;

            if (rect != null)
            {
                myObj = rect.Value.ToNurbsCurve();
            }

            GH_Circle circle = obj as GH_Circle;

            if (circle != null)
            {
                myObj = circle.Value.ToNurbsCurve();
            }

            GH_Arc arc = obj as GH_Arc;

            if (arc != null)
            {
                myObj = arc.Value.ToNurbsCurve();
            }

            GH_Point pt = obj as GH_Point;

            if (pt != null)
            {
                myObj = new Point(pt.Value);
            }

            if (myObj == null)
            {
                // get the object out
                DA.SetData(0, obj);
                this.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Failed to set user dictionary to object. Probably an unsupported type.");
                return;
            }

            myObj.UserDictionary.Clear();

            object value = null;

            DA.GetData(1, ref value);

            GH_ObjectWrapper temp = value as GH_ObjectWrapper;

            if (temp == null)
            {
                DA.SetData(0, obj);
                this.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Could not cast object to GH_ObjectWrapper.");
                return;
            }

            ArchivableDictionary dict = ((GH_ObjectWrapper)value).Value as ArchivableDictionary;

            if (dict == null)
            {
                DA.SetData(0, obj);
                this.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Could not cast object to Dictionary.");
                return;
            }

            myObj.UserDictionary.ReplaceContentsWith(dict);

            DA.SetData(0, myObj);
        }