/// <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)
        {
            Rhino.Geometry.Mesh mesh = new Rhino.Geometry.Mesh();
            if (!DA.GetData(0, ref mesh))
            {
                return;
            }
            if (!mesh.IsValid)
            {
                return;
            }

            // call the cpp function to solve the adjacency list
            var res = IGLRhinoCommon.Utils.getAdjacencyLst(ref mesh);

            // construct the index & pt tree from the adjacency list
            Grasshopper.DataTree <int>     treeArray = new Grasshopper.DataTree <int>();
            Grasshopper.DataTree <Point3d> ptArray   = new Grasshopper.DataTree <Point3d>();
            for (int i = 0; i < res.Count; i++)
            {
                var path = new Grasshopper.Kernel.Data.GH_Path(i);
                treeArray.AddRange(res[i], path);

                foreach (var id in res[i])
                {
                    ptArray.Add(mesh.Vertices[id], path);
                }
            }

            // assign to the output
            DA.SetDataTree(0, treeArray);
            DA.SetDataTree(1, ptArray);
        }
Exemplo n.º 2
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            string        p      = "";
            bool          b      = false;
            List <string> sheets = new List <string>();

            if (!DA.GetData(0, ref p))
            {
                return;
            }
            if (!DA.GetDataList(1, sheets))
            {
                return;
            }
            if (!DA.GetData(2, ref b))
            {
                return;
            }

            if (b)
            {
                // Get raw data
                List <string[, ]> data = new List <string[, ]>();

                if (sheets.Count == 1 && sheets[0] == _allSheets)
                {
                    data = ExcelTools.ExcelUtilities.GetAllData2(p);
                }
                else
                {
                    data = ExcelTools.ExcelUtilities.GetAllData2(p, sheets);
                }

                // Sort data into tree structure
                Grasshopper.DataTree <string>   dt = new Grasshopper.DataTree <string>();
                Grasshopper.Kernel.Data.GH_Path pth;

                int pCount = 0;
                foreach (string[,] dataSheet in data)
                {
                    for (int i = 0; i < dataSheet.GetLength(0); i++)
                    {
                        pth = new Grasshopper.Kernel.Data.GH_Path(pCount, i);

                        for (int j = 0; j < dataSheet.GetLength(1); j++)
                        {
                            dt.Add(dataSheet[i, j], pth);
                        }
                    }

                    pCount++;
                }

                DA.SetDataTree(0, dt);
            }
        }
Exemplo n.º 3
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)
        {
            // INPUT
            // declaration
            string        _XML     = String.Empty;
            List <string> _tagName = new List <string>();

            DA.GetData(0, ref _XML);
            DA.GetDataList(1, _tagName);

            // actions
            XmlDocument xmlDoc = new XmlDocument();

            //declare tree
            Grasshopper.DataTree <string> intTree = new Grasshopper.DataTree <string>();

            xmlDoc.LoadXml(_XML);

            if (_tagName != null)
            {
                try
                {
                    //set up tree
                    for (int i = 0; i < _tagName.Count; i++)
                    {
                        Grasshopper.Kernel.Data.GH_Path pth = new Grasshopper.Kernel.Data.GH_Path(i);

                        var innerTextData = xmlDoc.GetElementsByTagName(_tagName[i])[0].InnerText;
                        intTree.Add(innerTextData, pth);
                    }

                    // output
                    DA.SetDataTree(0, intTree);
                }
                catch
                {
                    this.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Please provide a valid keyword.");
                    return;
                }
            }
        }
Exemplo n.º 4
0
        void AddObjectToTree(Object o, List <int> path, ref Grasshopper.DataTree <Object> output)
        {
            ICollection <Object> innerList = o as ICollection <Object>;

            if (innerList != null)
            {
                List <int> newPath = new List <int>(path);
                newPath.Add(0);
                int i = 0;
                foreach (Object innerO in innerList)
                {
                    List <int> newPath2 = new List <int>(path);
                    newPath2.Add(i);
                    AddObjectToTree(innerO, newPath2, ref output);
                    ++i;
                }
            }
            else
            {
                output.Add(o, ListToTreePath(path));
            }
        }
        /// <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)
        {
            Rhino.Geometry.Mesh mesh    = new Rhino.Geometry.Mesh();
            List <int>          con_idx = new List <int>();
            List <double>       con_val = new List <double>();
            int divN = 1;

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

            if (!DA.GetDataList(1, con_idx))
            {
                return;
            }
            if (!DA.GetDataList(2, con_val))
            {
                return;
            }
            if (!(con_idx.Count > 0) || !(con_val.Count > 0))
            {
                return;
            }
            if (con_idx.Count != con_val.Count)
            {
                return;
            }
            // TODO: add warning message
            if (!DA.GetData(3, ref divN))
            {
                return;
            }

            // call the cpp function to solve the adjacency list
            var res = IGLRhinoCommon.Utils.getIsolinePts(ref mesh, ref con_idx, ref con_val, divN);

            // construct the index & pt tree from the adjacency list
            Grasshopper.DataTree <Point3d> ptTree  = new Grasshopper.DataTree <Point3d>();
            Grasshopper.DataTree <Curve>   crvTree = new Grasshopper.DataTree <Curve>();
            for (int i = 0; i < res.Count; i++)
            {
                var path = new Grasshopper.Kernel.Data.GH_Path(i);
                ptTree.AddRange(res[i], path);

                // if has move than 2 pts, interpolate curves
                if (res[i].Count > 3)
                {
                    var crv = Curve.CreateInterpolatedCurve(res[i], 3);
                    crvTree.Add(crv, path);
                }
                else if (res[i].Count > 2)
                {
                    var crv = Curve.CreateInterpolatedCurve(res[i], 2);
                    crvTree.Add(crv, path);
                }
            }

            // assign to the output
            DA.SetDataTree(0, crvTree);
            DA.SetDataTree(1, ptTree);
        }
Exemplo n.º 6
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            Mesh mshin = new Mesh();

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

            CResults results = null;

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

            int outputType = 0;

            if (!DA.GetData(2, ref outputType))
            {
                outputType = 0;
            }


            Grasshopper.DataTree <double> avgI = new Grasshopper.DataTree <double>();

            List <double> areas = new List <double>();

            MeshFaceList mshfaces = mshin.Faces;


            for (int m = 0; m < mshfaces.Count; m++)
            {
                MeshFace fc = mshfaces[m];

                int    spA       = fc.A;
                int    spB       = fc.B;
                int    spC       = fc.C;
                int    spD       = fc.D;
                int    intvert   = 3;
                double quadmulti = 0;
                if (fc.IsQuad)
                {
                    intvert   = 4;
                    quadmulti = 1;
                }

                Grasshopper.Kernel.Data.GH_Path ghpath = new Grasshopper.Kernel.Data.GH_Path(m);

                List <double> sp_I = new List <double>();

                switch (outputType)
                {
                case 0:
                    avgI.Add((results.I_total[spA] + results.I_total[spB] + results.I_total[spC] + (results.I_total[spD] * quadmulti)) / intvert, ghpath);
                    break;

                case 1:
                    avgI.Add((results.Ib_total[spA] + results.Ib_total[spB] + results.Ib_total[spC] + (results.Ib_total[spD] * quadmulti)) / intvert, ghpath);
                    break;

                case 2:
                    avgI.Add((results.Id_total[spA] + results.Id_total[spB] + results.Id_total[spC] + (results.Id_total[spD] * quadmulti)) / intvert, ghpath);
                    break;

                case 3:
                    for (int t = 0; t < results.I_hourly.ColumnCount; t++)
                    {
                        avgI.Add((results.I_hourly[spA, t] + results.I_hourly[spB, t] + results.I_hourly[spC, t] + (results.I_hourly[spD, t] * quadmulti)) / intvert, ghpath);
                    }
                    break;

                case 4:
                    for (int t = 0; t < results.Ib_hourly.ColumnCount; t++)
                    {
                        avgI.Add((results.Ib_hourly[spA, t] + results.Ib_hourly[spB, t] + results.Ib_hourly[spC, t] + (results.Ib_hourly[spD, t] * quadmulti)) / intvert, ghpath);
                    }
                    break;

                case 5:
                    for (int t = 0; t < results.Id_hourly.ColumnCount; t++)
                    {
                        avgI.Add((results.Id_hourly[spA, t] + results.Id_hourly[spB, t] + results.Id_hourly[spC, t] + (results.Id_hourly[spD, t] * quadmulti)) / intvert, ghpath);
                    }
                    break;

                case 6:
                    for (int t = 0; t < results.Ib_hourly.ColumnCount; t++)
                    {
                        int shdwcount = 0;
                        if (results.Ib_hourly[spA, t] == 0)
                        {
                            shdwcount++;
                        }
                        if (results.Ib_hourly[spB, t] == 0)
                        {
                            shdwcount++;
                        }
                        if (results.Ib_hourly[spC, t] == 0)
                        {
                            shdwcount++;
                        }
                        if (results.Ib_hourly[spD, t] == 0)
                        {
                            shdwcount++;
                        }
                        if (shdwcount > 1)
                        {
                            avgI.Add(0, ghpath);
                        }
                        else
                        {
                            avgI.Add(1, ghpath);
                        }
                    }
                    break;
                }
                areas.Add(CMisc.getMeshFaceArea(m, mshin));
            }



            DA.SetDataTree(0, avgI);
            DA.SetDataList(1, areas);
        }
Exemplo n.º 7
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            Grasshopper.DataTree <Polyline> P0 = new Grasshopper.DataTree <Polyline>();
            Grasshopper.DataTree <Polyline> P1 = new Grasshopper.DataTree <Polyline>();

            GH_Structure <GH_Curve> C0 = new GH_Structure <GH_Curve>();
            GH_Structure <GH_Curve> C1 = new GH_Structure <GH_Curve>();
            bool b = true;

            DA.GetDataTree(0, out C0);
            DA.GetDataTree(1, out C1);
            DA.GetData(2, ref b);

            if (C0.DataCount == C1.DataCount)
            {
                for (int i = 0; i < C0.Branches.Count; i++)
                {
                    if (C0.Branches[i].Count == 1 && C1.Branches[i].Count == 1)
                    {
                        Polyline poly0 = new Polyline();
                        Polyline poly1 = new Polyline();
                        bool     f0    = C0.Branches[i][0].Value.TryGetPolyline(out poly0);
                        bool     f1    = C1.Branches[i][0].Value.TryGetPolyline(out poly1);



                        if (poly0.Count == poly1.Count)
                        {
                            P0.Add(poly0, C0.Paths[i]);
                            P1.Add(poly1, C1.Paths[i]);
                        }
                    }
                }

                if (C0 != null && C1 != null)
                {
                    Grasshopper.DataTree <Mesh> mesh = MeshUtil.LoftMeshFast(P0, P1, b);
                    Mesh meshDispay = new Mesh();
                    foreach (Mesh mm in mesh.AllData())
                    {
                        meshDispay.Append(mm);
                    }

                    PreparePreview(meshDispay, DA.Iteration);
                    DA.SetDataTree(0, mesh);
                }
            }
            else if (C0.DataCount > 0 && C1.DataCount == 0)
            {
                for (int i = 0; i < C0.Branches.Count; i++)
                {
                    if (C0.Branches[i].Count == 2)
                    {
                        Polyline poly0 = new Polyline();
                        Polyline poly1 = new Polyline();
                        bool     f0    = C0.Branches[i][0].Value.TryGetPolyline(out poly0);
                        bool     f1    = C0.Branches[i][1].Value.TryGetPolyline(out poly1);


                        if (poly0.Count == poly1.Count)
                        {
                            P0.Add(poly0, C0.Paths[i]);
                            P1.Add(poly1, C0.Paths[i]);
                        }
                    }
                    else if (C0.Branches[i].Count % 2 == 0)
                    {
                        for (int j = 0; j < C0.Branches[i].Count; j += 2)
                        {
                            Polyline poly0 = new Polyline();
                            Polyline poly1 = new Polyline();
                            bool     f0    = C0.Branches[i][j].Value.TryGetPolyline(out poly0);
                            bool     f1    = C0.Branches[i][j + 1].Value.TryGetPolyline(out poly1);


                            if (poly0.Count == poly1.Count)
                            {
                                P0.Add(poly0, new GH_Path(i, (int)(j * 0.5)));
                                P1.Add(poly1, new GH_Path(i, (int)(j * 0.5)));
                            }
                        }
                    }
                }


                Grasshopper.DataTree <Mesh> mesh = MeshUtil.LoftMeshFast(P0, P1, b);
                Mesh meshDispay = new Mesh();
                foreach (Mesh mm in mesh.AllData())
                {
                    meshDispay.Append(mm);
                }

                PreparePreview(meshDispay, DA.Iteration);
                DA.SetDataTree(0, mesh);
            }

            //Curve C0 = null;
            //Curve C1 = null;
            //DA.GetData(0, ref C0);
            //DA.GetData(1, ref C1);

            //int N= 2;
            //DA.GetData(2, ref N);

            //bool E = true;
            //DA.GetData(3, ref E);



            //if(C0 != null && C1 != null) {
            //   Mesh mesh = PolylineUtil.LoftTwoCurves(C0, C1, N, E);
            //    PreparePreview(mesh, DA.Iteration);
            //    DA.SetData(0, mesh);
            //}
        }
Exemplo n.º 8
0
        /// <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)
        {
            // *********************************************************************************
            // get info from grasshopper
            // *********************************************************************************
            List <double> xyzsize = new List <double>();

            if (!DA.GetDataList(0, xyzsize))
            {
                return;
            }
            ;

            List <int> Nxyz = new List <int>();

            if (!DA.GetDataList(1, Nxyz))
            {
                return;
            }
            ;
            int Nx = Nxyz[0];
            int Ny = Nxyz[1];
            int Nz = Nxyz[2];


            List <double[]> geom = new List <double[]>();

            if (!DA.GetDataList(2, geom))
            {
                return;
            }
            ;


            // time step
            double dt = 0.1;

            if (!DA.GetData(3, ref dt))
            {
                return;
            }


            // wind speed
            double Vmet = 10;

            if (!DA.GetData(4, ref Vmet))
            {
                return;
            }

            //terrain type
            int terrain = 0;

            if (!DA.GetData(5, ref terrain))
            {
                return;
            }


            bool run = false;

            if (!DA.GetData(6, ref run))
            {
                return;
            }



            //List<Mesh> mshCp = new List<Mesh>();
            //DA.GetDataList(10, mshCp);
            bool writeresults = false;

            DA.GetData(7, ref writeresults);


            DA.GetData(9, ref resetFFD);

            double b = 0;

            if (!DA.GetData(10, ref b))
            {
                return;
            }
            ;

            Point3d Pb = new Point3d();

            if (!DA.GetData(11, ref Pb))
            {
                return;
            }

            Point3d origin = new Point3d();

            if (!DA.GetData(12, ref origin))
            {
                return;
            }


            double nu = 1.511e-5;       // increase viscosity to impose turbulence. the higher velocity, the higher visc., 1e-3

            DA.GetData(13, ref nu);

            // *********************************************************************************
            //from Lukas
            // *********************************************************************************


            // Set initial velocity conditions
            double[, ,] u0 = new double[Nx + 1, Ny + 2, Nz + 2];
            double[, ,] v0 = new double[Nx + 2, Ny + 1, Nz + 2];
            double[, ,] w0 = new double[Nx + 2, Ny + 2, Nz + 1];

            // Create empty arrays for body forces
            double[, ,] f_x = new double[Nx + 1, Ny + 2, Nz + 2];
            double[, ,] f_y = new double[Nx + 2, Ny + 1, Nz + 2];
            double[, ,] f_z = new double[Nx + 2, Ny + 2, Nz + 1];

            // Create structure for solver parameters
            FluidSolver.solver_struct solver_prams = new FluidSolver.solver_struct();
            solver_prams.tol             = 1e-4;
            solver_prams.min_iter        = 1;
            solver_prams.max_iter        = 30;
            solver_prams.verbose         = false;
            solver_prams.backtrace_order = 2;
            solver_prams.mass_correction = false;
            solver_prams.mass_corr_alpha = 0.7;


            // Create FFD solver and domain
            if (ffd == null)
            {
                omega = new WindInflowWentao(Nx + 2, Ny + 2, Nz + 2, xyzsize[0], xyzsize[1], xyzsize[2]);
                foreach (double[] geo in geom)
                {
                    omega.add_obstacle(geo[0], geo[1], geo[2], geo[3], geo[4], geo[5]);
                }

                ffd = new FluidSolver(omega, dt, nu, u0, v0, w0, solver_prams);
                de  = new DataExtractor(omega, ffd);
                t   = 0;
            }

            //reset FFD solver and domain
            if (resetFFD)
            {
                omega = new WindInflowWentao(Nx + 2, Ny + 2, Nz + 2, xyzsize[0], xyzsize[1], xyzsize[2]);
                foreach (double[] geo in geom)
                {
                    omega.add_obstacle(geo[0], geo[1], geo[2], geo[3], geo[4], geo[5]);
                }

                ffd      = new FluidSolver(omega, dt, nu, u0, v0, w0, solver_prams);
                de       = new DataExtractor(omega, ffd);
                t        = 0;
                resetFFD = false;
            }



            //run solver. the solving-loop (new timestep) is executed in Grasshopper with a timer-component.
            if (run)
            {
                ffd.time_step(f_x, f_y, f_z);
            }



            // *******************************************************************************************
            // *********************************     Output Results       ********************************
            double[, ,] p  = new double[Nx, Ny, Nz];
            double[, ,] vu = new double[Nx, Ny, Nz];
            double[, ,] vv = new double[Nx, Ny, Nz];
            double[, ,] vw = new double[Nx, Ny, Nz];

            double[, ,] pstag  = new double[Nx + 1, Ny + 1, Nz + 1];
            double[, ,] vustag = new double[Nx + 1, Ny + 1, Nz + 1];
            double[, ,] vvstag = new double[Nx + 1, Ny + 1, Nz + 1];
            double[, ,] vwstag = new double[Nx + 1, Ny + 1, Nz + 1];

            for (int i = 0; i < Nx; i++)
            {
                for (int j = 0; j < Ny; j++)
                {
                    for (int k = 0; k < Nz; k++)
                    {
                        if (omega.obstacle_cells[i + 1, j + 1, k + 1] != 1)
                        {
                            p[i, j, k] = de.get_pressure(i * omega.hx + 0.5 * omega.hx, j * omega.hy + 0.5 * omega.hy, k * omega.hz + 0.5 * omega.hz);
                            double[] vel = de.get_velocity(i * omega.hx + 0.5 * omega.hx, j * omega.hy + 0.5 * omega.hy, k * omega.hz + 0.5 * omega.hz);
                            vu[i, j, k] = vel[0];
                            vv[i, j, k] = vel[1];
                            vw[i, j, k] = vel[2];

                            pstag[i, j, k] = de.get_pressure(i * omega.hx, j * omega.hy, k * omega.hz);
                            double[] velcen = de.get_velocity(i * omega.hx, j * omega.hy, k * omega.hz);
                            vustag[i, j, k] = velcen[0];
                            vvstag[i, j, k] = velcen[1];
                            vwstag[i, k, k] = velcen[2];
                        }
                        else
                        {
                            p[i, j, k]  = 0;
                            vu[i, j, k] = 0;
                            vv[i, j, k] = 0;
                            vw[i, j, k] = 0;

                            //pstag[i, j, k] = 0;
                            //vustag[i, j, k] = 0;
                            //vvstag[i, j, k] = 0;
                            //vwstag[i, k, k] = 0;

                            pstag[i, j, k] = de.get_pressure(i * omega.hx, j * omega.hy, k * omega.hz);
                            double[] velcen = de.get_velocity(i * omega.hx, j * omega.hy, k * omega.hz);
                            vustag[i, j, k] = velcen[0];
                            vvstag[i, j, k] = velcen[1];
                            vwstag[i, k, k] = velcen[2];
                        }
                    }
                }
            }

            //last x slice
            for (int j = 0; j < Ny + 1; j++)
            {
                for (int k = 0; k < Nz + 1; k++)
                {
                    pstag[Nx, j, k] = de.get_pressure((Nx) * omega.hx, j * omega.hy, k * omega.hz);
                    double[] vcen = de.get_velocity((Nx) * omega.hx, j * omega.hy, k * omega.hz);
                    vustag[Nx, j, k] = vcen[0];
                    vvstag[Nx, j, k] = vcen[1];
                    vwstag[Nx, j, k] = vcen[2];
                }
            }

            //last y slice
            for (int i = 0; i < Nx + 1; i++)
            {
                for (int k = 0; k < Nz + 1; k++)
                {
                    pstag[i, Ny, k] = de.get_pressure(i * omega.hx, (Ny) * omega.hy, k * omega.hz);
                    double[] vcen = de.get_velocity(i * omega.hx, (Ny) * omega.hy, k * omega.hz);
                    vustag[i, Ny, k] = vcen[0];
                    vvstag[i, Ny, k] = vcen[1];
                    vwstag[i, Ny, k] = vcen[2];
                }
            }

            //last z slice
            for (int i = 0; i < Nx + 1; i++)
            {
                for (int j = 0; j < Ny + 1; j++)
                {
                    pstag[i, j, Nz] = de.get_pressure(i * omega.hx, j * omega.hy, (Nz) * omega.hz);
                    double[] vcen = de.get_velocity(i * omega.hx, j * omega.hy, (Nz) * omega.hz);
                    vustag[i, j, Nz] = vcen[0];
                    vvstag[i, j, Nz] = vcen[1];
                    vwstag[i, j, Nz] = vcen[2];
                }
            }

            List <double[, , ]> veloutCen = new List <double[, , ]> {
            };

            veloutCen.Add(vu);
            veloutCen.Add(vv);
            veloutCen.Add(vw);

            List <double[, , ]> veloutStag = new List <double[, , ]> {
            };

            veloutStag.Add(vustag);
            veloutStag.Add(vvstag);
            veloutStag.Add(vwstag);


            DA.SetDataList(0, veloutCen);
            DA.SetData(1, p);
            DA.SetDataList(2, veloutStag);
            DA.SetData(3, pstag);



            Grasshopper.DataTree <double> utree = new Grasshopper.DataTree <double>();
            Grasshopper.DataTree <double> vtree = new Grasshopper.DataTree <double>();
            Grasshopper.DataTree <double> wtree = new Grasshopper.DataTree <double>();

            double [] xpos = new double[] { -0.75 * b, -0.5 * b, -0.25 * b, 0, 0.5 * b, 0.75 * b, 1.25 * b, 2 * b, 3.25 * b };
            for (int i = 0; i < xpos.Length; i++)
            {
                xpos[i] = xpos[i] + Pb[0] - origin[0];
            }

            //(Ny+1) / 2
            int branch = 0;

            foreach (double xpo in xpos)
            {
                for (int k = 0; k < Nz + 1; k++)
                {
                    double[] vcen = de.get_velocity(xpo, (Ny / 2) * omega.hy + 0.5 * omega.hy, k * omega.hz);

                    utree.Add(vcen[0], new Grasshopper.Kernel.Data.GH_Path(branch));
                    vtree.Add(vcen[1], new Grasshopper.Kernel.Data.GH_Path(branch));
                    wtree.Add(vcen[2], new Grasshopper.Kernel.Data.GH_Path(branch));
                }
                branch++;
            }



            DA.SetDataTree(5, utree);
            DA.SetDataTree(6, vtree);
            DA.SetDataTree(7, wtree);

            // *******************************************************************************************
            // *******************************      Output Cp values on Surfaces   ***********************
            //if (mshCp.Count > 0)
            if (writeresults)
            {
                ////generate list of objects
                ////  each item contains:
                ////      - 2d matrix of Cp values for each node of the analysis mesh
                ////      - mesh itself
                //List<object[]> mshCpOUT = new List<object[]>();
                //foreach (Mesh msh in mshCp)
                //{
                //    object[] _mshCpout = new object[2] ;
                //    _mshCpout[0] = msh;
                //    //_mshCpout[1]
                //     double [] Cps = new double[msh.Vertices.Count];        //this will be the Cp values. size of array corresponds to mesh vertices
                //    for (int u = 0; u < msh.Vertices.Count; u++)
                //    {
                //        double pref = de.get_pressure(0, msh.Vertices[u].Y, msh.Vertices[u].Z);     //!!! adjust msh to origin and discretisationj
                //        double cp = de.get_pressure(msh.Vertices[u].X, msh.Vertices[u].Y, msh.Vertices[u].Z);
                //        Cps[u] = 1;
                //    }

                //}
                //DA.SetDataList(4, mshCp);


                DA.SetData(4, de);
            }



            // _____________________________________________________________________________________
            // // THIS SHOWS HOW TO ADD A FORM
            //if (f == null)
            //{
            //    f = new LEGACY_Form1();
            //    f.Show(Grasshopper.Instances.DocumentEditor);
            //    Grasshopper.Instances.DocumentEditor.FormShepard.RegisterForm(f);
            //    f.checkBox1.Text = "run the solver";
            //}



            // _____________________________________________________________________________________
            // // THIS SHOWS HOW TO DYNAMICALLY USE GRASSHOPPER SLIDER INPUTS
            //List<Grasshopper.Kernel.Special.GH_NumberSlider> sliders = new List<Grasshopper.Kernel.Special.GH_NumberSlider>();
            //foreach (IGH_Param param in Component.Params.Input)
            //{
            //    Grasshopper.Kernel.Special.GH_NumberSlider slider = param.Sources[0] as Grasshopper.Kernel.Special.GH_NumberSlider;
            //    if (slider != null)
            //    {
            //        sliders.Add(slider);
            //        x = (int)slider.CurrentValue;
            //    }
            //}

            //while (terminate != true)
            //{
            //    doc.NewSolution(false);

            //    fx = x * 10;
            //    maximize(x, fx);
            //    sliders[0].TickValue = xNew;
            //    x = xNew;
            //}
        }
Exemplo n.º 9
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)
        {
            Point3d origin = Point3d.Unset;

            if (!DA.GetData(0, ref origin))
            {
                origin = new Point3d(0, 0, 0);
            }

            List <Mesh> mshP = new List <Mesh>();

            if (!DA.GetDataList(1, mshP))
            {
                return;
            }

            DataExtractor de = null;

            if (!DA.GetData(2, ref de))
            {
                return;
            }

            //double roh = 1.2041;
            //DA.GetData(3, ref roh);

            //double min = -1;
            //double max = 1;
            //DA.GetData(4, ref min);
            //DA.GetData(5, ref max);

            int colourSheme = 1;

            DA.GetData(3, ref colourSheme);

            double fontsize = 0.5;

            DA.GetData(4, ref fontsize);

            Point3d zrefp = new Point3d();

            if (!DA.GetData(5, ref zrefp))
            {
                return;
            }

            string face    = "Baskerville";
            bool   bold    = false;
            bool   italics = true;


            List <Mesh> mshPOUT = new List <Mesh>();

            Grasshopper.DataTree <double> Ptree = new Grasshopper.DataTree <double>();
            int branch = 0;

            //vref and pdyn should be measured at the building height...
            //double[] vref = de.get_velocity(0, zrefp[1] - origin[1], zrefp[2] - origin[2]);
            //Vector3d vrefv = new Vector3d(vref[0], vref[1], vref[2]);
            //double vrefl = vrefv.Length;
            //double pref = de.get_pressure(0, zrefp[1] - origin[1], zrefp[2] - origin[2]);
            //double pdyn = 0.5 * roh * Math.Pow(vrefl, 2);

            foreach (Mesh msh in mshP)
            {
                double[]     Press  = new double[msh.Faces.Count];
                Color[]      Cols   = new Color[msh.Vertices.Count];
                Mesh         mshcol = new Mesh();
                List <Curve> lst    = new List <Curve>();

                for (int u = 0; u < msh.Faces.Count; u++)
                {
                    //double[] vref = de.get_velocity(0 - origin[0], msh.Vertices[u].Y - origin[1], msh.Vertices[u].Z - origin[2]);
                    //Vector3d vrefv = new Vector3d(vref[0], vref[1], vref[2]);
                    //double vrefl = vrefv.Length;
                    double pref = de.get_pressure(zrefp[0] - origin[0], zrefp[1] - origin[1], zrefp[2] - origin[2]);
                    //double pdyn = 0.5 * roh * Math.Pow(vrefl, 2);

                    //Press[u] = de.get_pressure(msh.Vertices[u].X - origin[0], msh.Vertices[u].Y - origin[1], msh.Vertices[u].Z - origin[2]);

                    MeshFace mFace = msh.Faces[u];
                    Point3d  pt1   = msh.Vertices[mFace.A];
                    Point3d  pt2   = msh.Vertices[mFace.B];
                    Point3d  pt3   = msh.Vertices[mFace.C];
                    Point3d  pt4   = msh.Vertices[mFace.D];

                    NurbsSurface faceSrf    = NurbsSurface.CreateFromCorners(pt1, pt2, pt3, pt4);
                    Point3d      faceCenter = AreaMassProperties.Compute(faceSrf).Centroid;
                    double       uCoord;
                    double       vCoord;

                    faceSrf.ClosestPoint(faceCenter, out uCoord, out vCoord);
                    Vector3d faceNormal = faceSrf.NormalAt(uCoord, vCoord);

                    faceNormal.Unitize();
                    faceNormal *= 0.01;

                    faceCenter += faceNormal;

                    double pFace = de.get_pressure(faceCenter.X - origin[0], faceCenter.Y - origin[1], faceCenter.Z - origin[2]);

                    Press[u] = pFace - pref;

                    //double[] U = de.get_velocity(faceCenter.X - origin[0], faceCenter.Y - origin[1], faceCenter.Z - origin[2]);
                    //Vector3d Uvec = new Vector3d(U[0], U[1], U[2]);
                    //double velocity = Uvec.Length;
                    //Press[u] = 0.5 * 1.225 * Math.Pow(velocity, 2);

                    //Cp[u] = (px - pref) / pdyn;
                    Ptree.Add(Press[u], new Grasshopper.Kernel.Data.GH_Path(branch));
                    //Cols[u] = Utilities.GetRGB(colourSheme, Cp[u], max, min);
                    //Cols[u] = Utilities.GetAbsoluteRGB(colourSheme, Press[u]);
                    //mshcol.Vertices.Add(msh.Vertices[u]);
                    //mshcol.VertexColors.SetColor(u, Cols[u]);

                    //string strval = Math.Round(Cp[u], 2).ToString();
                    string   strval = Math.Round(Press[u], 2).ToString();
                    Point3d  plp    = new Point3d(faceCenter.X, faceCenter.Y, faceCenter.Z);
                    Vector3d vec    = new Vector3d(-1, 0, 0);
                    Plane    pl     = new Plane(plp, vec);

                    var te = Rhino.RhinoDoc.ActiveDoc.Objects.AddText(strval, pl, fontsize, face, bold, italics);
                    Rhino.DocObjects.TextObject txt = Rhino.RhinoDoc.ActiveDoc.Objects.Find(te) as Rhino.DocObjects.TextObject;

                    if (txt != null)
                    {
                        var     tt = txt.Geometry as Rhino.Geometry.TextEntity;
                        Curve[] A  = tt.Explode();

                        foreach (Curve crv in A)
                        {
                            lst.Add(crv);
                        }
                    }

                    Rhino.RhinoDoc.ActiveDoc.Objects.Delete(te, true);
                }
                branch++;

                for (int j = 0; j < msh.Faces.Count; j++)
                {
                    mshcol.Faces.AddFace(msh.Faces[j].A, msh.Faces[j].B, msh.Faces[j].C, msh.Faces[j].D);
                }

                for (int i = 0; i < msh.Vertices.Count; i++)
                {
                    double vertexPress = de.get_pressure(msh.Vertices[i].X - origin[0], msh.Vertices[i].Y - origin[1], msh.Vertices[i].Z - origin[2]);
                    Cols[i] = Utilities.GetAbsoluteRGB(colourSheme, vertexPress);
                    mshcol.Vertices.Add(msh.Vertices[i]);
                    mshcol.VertexColors.SetColor(i, Cols[i]);
                }


                // output Pressure numbers as text into rhino viewport
                DA.SetDataList(1, lst);

                // output coloured meshes
                mshPOUT.Add(mshcol);
            }

            DA.SetDataTree(2, Ptree);
            DA.SetDataList(0, mshPOUT);



            //THIS IS FROM GIULIO PIACENTINO's page... txtlines component
            //            private void RunScript(string face, bool bold, bool italics, double size, string content, Plane pl, ref object A)
            //{

            //  if(size == 0)
            //    size = 1;

            //  if(!string.IsNullOrEmpty(face) && size > 0 && !string.IsNullOrEmpty(content) &&
            //    pl.IsValid)

            //    var te = RhinoDoc.ActiveDoc.Objects.AddText(content, pl, size, face, bold, italics);
            //    Rhino.DocObjects.TextObject txt = RhinoDoc.ActiveDoc.Objects.Find(te) as Rhino.DocObjects.TextObject;

            //    if(txt != null)
            //    {
            //      var tt = txt.Geometry as Rhino.Geometry.TextEntity;
            //      A = tt.Explode();
            //    }

            //    RhinoDoc.ActiveDoc.Objects.Delete(txt, true);
            //    RhinoDoc.ActiveDoc.Objects.Delete(te, true);
            //  }

            //}
        }
Exemplo n.º 10
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)
        {
            Point3d origin = Point3d.Unset;

            if (!DA.GetData(0, ref origin))
            {
                origin = new Point3d(0, 0, 0);
            }

            List <Mesh> mshCp = new List <Mesh>();

            if (!DA.GetDataList(1, mshCp))
            {
                return;
            }

            DataExtractor de = null;

            if (!DA.GetData(2, ref de))
            {
                return;
            }

            double roh = 1.2041;

            DA.GetData(3, ref roh);

            double min = -1;
            double max = 1;

            DA.GetData(4, ref min);
            DA.GetData(5, ref max);

            int colourSheme = 1;

            DA.GetData(6, ref colourSheme);

            double fontsize = 0.5;

            DA.GetData(7, ref fontsize);

            Point3d zrefp = new Point3d();

            if (!DA.GetData(8, ref zrefp))
            {
                return;
            }

            string face    = "Baskerville";
            bool   bold    = false;
            bool   italics = true;


            List <Mesh> mshCpOUT = new List <Mesh>();

            Grasshopper.DataTree <double> cptree = new Grasshopper.DataTree <double>();
            int branch = 0;

            //vref and pdyn should be measured at the building height...
            double[] vref  = de.get_velocity(0, zrefp[1] - origin[1], zrefp[2] - origin[2]);
            Vector3d vrefv = new Vector3d(vref[0], vref[1], vref[2]);
            double   vrefl = vrefv.Length;
            double   pref  = de.get_pressure(0, zrefp[1] - origin[1], zrefp[2] - origin[2]);
            double   pdyn  = 0.5 * roh * Math.Pow(vrefl, 2);

            foreach (Mesh msh in mshCp)
            {
                double[]     Cp     = new double[msh.Vertices.Count];
                Color[]      Cols   = new Color[msh.Vertices.Count];
                Mesh         mshcol = new Mesh();
                List <Curve> lst    = new List <Curve>();

                for (int u = 0; u < msh.Vertices.Count; u++)
                {
                    //double[] vref = de.get_velocity(0 - origin[0], msh.Vertices[u].Y - origin[1], msh.Vertices[u].Z - origin[2]);
                    //Vector3d vrefv = new Vector3d(vref[0], vref[1], vref[2]);
                    //double vrefl = vrefv.Length;
                    //double pref = de.get_pressure(0 - origin[0], msh.Vertices[u].Y - origin[1], msh.Vertices[u].Z - origin[2]);
                    //double pdyn = 0.5 * roh * Math.Pow(vrefl, 2);
                    double px = de.get_pressure(msh.Vertices[u].X - origin[0], msh.Vertices[u].Y - origin[1], msh.Vertices[u].Z - origin[2]);


                    Cp[u] = (px - pref) / pdyn;
                    cptree.Add(Cp[u], new Grasshopper.Kernel.Data.GH_Path(branch));
                    Cols[u] = Utilities.GetRGB(colourSheme, Cp[u], max, min);
                    mshcol.Vertices.Add(msh.Vertices[u]);
                    mshcol.VertexColors.SetColor(u, Cols[u]);

                    string   strval = Math.Round(Cp[u], 2).ToString();
                    Point3d  plp    = new Point3d(msh.Vertices[u].X, msh.Vertices[u].Y, msh.Vertices[u].Z);
                    Vector3d vec    = new Vector3d(-1, 0, 0);
                    Plane    pl     = new Plane(plp, vec);

                    var te = Rhino.RhinoDoc.ActiveDoc.Objects.AddText(strval, pl, fontsize, face, bold, italics);
                    Rhino.DocObjects.TextObject txt = Rhino.RhinoDoc.ActiveDoc.Objects.Find(te) as Rhino.DocObjects.TextObject;

                    if (txt != null)
                    {
                        var     tt = txt.Geometry as Rhino.Geometry.TextEntity;
                        Curve[] A  = tt.Explode();

                        foreach (Curve crv in A)
                        {
                            lst.Add(crv);
                        }
                    }

                    Rhino.RhinoDoc.ActiveDoc.Objects.Delete(te, true);
                }
                branch++;

                for (int j = 0; j < msh.Faces.Count; j++)
                {
                    mshcol.Faces.AddFace(msh.Faces[j].A, msh.Faces[j].B, msh.Faces[j].C, msh.Faces[j].D);
                }


                // output Cp numbers as text into rhino viewport
                DA.SetDataList(1, lst);

                // output coloured meshes
                mshCpOUT.Add(mshcol);
            }

            DA.SetDataTree(2, cptree);
            DA.SetDataList(0, mshCpOUT);



            //THIS IS FROM GIULIO PIACENTINO's page... txtlines component
            //            private void RunScript(string face, bool bold, bool italics, double size, string content, Plane pl, ref object A)
            //{

            //  if(size == 0)
            //    size = 1;

            //  if(!string.IsNullOrEmpty(face) && size > 0 && !string.IsNullOrEmpty(content) &&
            //    pl.IsValid)

            //    var te = RhinoDoc.ActiveDoc.Objects.AddText(content, pl, size, face, bold, italics);
            //    Rhino.DocObjects.TextObject txt = RhinoDoc.ActiveDoc.Objects.Find(te) as Rhino.DocObjects.TextObject;

            //    if(txt != null)
            //    {
            //      var tt = txt.Geometry as Rhino.Geometry.TextEntity;
            //      A = tt.Explode();
            //    }

            //    RhinoDoc.ActiveDoc.Objects.Delete(txt, true);
            //    RhinoDoc.ActiveDoc.Objects.Delete(te, true);
            //  }

            //}
        }
Exemplo n.º 11
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)
        {
            // See if gdal and ogr are working first.
            string output = "";

            try
            {
                GdalConfiguration.ConfigureOgr();

                GdalConfiguration.ConfigureGdal();

                output = "It works!";
            }

            catch (Exception e)
            {
                output = "{0} Exception caught. " + e;
            }

            string input = "";

            DA.GetData(0, ref input);

            var driver = OSGeo.OGR.Ogr.GetDriverByName("ESRI Shapefile");

            if (driver == null)
            {
                output = "Driver is null";
            }

            else
            {
                var ds = driver.Open(input, 0);

                if (ds == null)
                {
                    output = "DataSource is null";
                }

                else
                {
                    Grasshopper.DataTree <double> latitudesOut = new Grasshopper.DataTree <double>(), longitudesOut = new Grasshopper.DataTree <double>(), altitudesOut = new Grasshopper.DataTree <double>();
                    Grasshopper.DataTree <bool>   cullPattern = new Grasshopper.DataTree <bool>();

                    long numberOfFeatures = 0;

                    var layer = ds.GetLayerByIndex(0);

                    string geoType = layer.GetGeomType().ToString();

                    numberOfFeatures = unchecked ((int)layer.GetFeatureCount(0));

                    bool parallel = false;

                    DA.GetData(1, ref parallel);

                    GH_Path path = new GH_Path();

                    OSGeo.OGR.Feature  feature = null;
                    OSGeo.OGR.Geometry geo = null, ring = null;

                    double[] pointList = { 0, 0, 0 };

                    if (parallel == false)
                    {
                        if (geoType == "wkbPolygon")
                        {
                            for (int i = 0; i < numberOfFeatures; ++i)
                            {
                                path    = new GH_Path(i);
                                feature = layer.GetFeature(i);
                                geo     = feature.GetGeometryRef();
                                ring    = geo.GetGeometryRef(0);
                                int pointCount = ring.GetPointCount();

                                cullPattern.Add(pointCount > 0 ? true : false, path);

                                for (int j = 0; j < pointCount; ++j)
                                {
                                    ring.GetPoint(j, pointList);

                                    latitudesOut.Add(pointList[1], path);
                                    longitudesOut.Add(pointList[0], path);
                                    altitudesOut.Add(pointList[2], path);
                                }
                            }
                        }

                        else if (geoType == "wkbLineString")
                        {
                            for (int i = 0; i < numberOfFeatures; ++i)
                            {
                                path    = new GH_Path(i);
                                feature = layer.GetFeature(i);
                                geo     = feature.GetGeometryRef();
                                int pointCount = geo.GetPointCount();

                                cullPattern.Add(pointCount > 0 ? true : false, path);

                                for (int j = 0; j < pointCount; ++j)
                                {
                                    geo.GetPoint(j, pointList);

                                    latitudesOut.Add(pointList[1], path);
                                    longitudesOut.Add(pointList[0], path);
                                    altitudesOut.Add(pointList[2], path);
                                }
                            }
                        }

                        else if (geoType == "wkbPoint" || geoType == "wkbPoint25D")
                        {
                            // TODO add reprojections if needed.
                            //OSGeo.OSR.CoordinateTransformation.TransformPoint(  )

                            for (int i = 0; i < numberOfFeatures; ++i)
                            {
                                path = new GH_Path(i);

                                try
                                {
                                    feature = layer.GetFeature(i);

                                    geo = feature.GetGeometryRef();

                                    geo.GetPoint(0, pointList);

                                    latitudesOut.Add(pointList[1], path);
                                    longitudesOut.Add(pointList[0], path);
                                    altitudesOut.Add(pointList[2], path);
                                }

                                catch (Exception e)
                                {
                                    output = "{0} Exception caught. " + e;
                                }
                            }
                        }

                        else
                        {
                            output = "Geometry type not implemented yet";
                        }
                    }

                    else
                    {
                        if (geoType == "wkbPolygon")
                        {
                            double[][] latMat = new double[numberOfFeatures][];
                            double[][] lonMat = new double[numberOfFeatures][];
                            double[][] altMat = new double[numberOfFeatures][];

                            for (int i = 0; i < numberOfFeatures; ++i)
                            {
                                path    = new GH_Path(i);
                                feature = layer.GetFeature(i);
                                geo     = feature.GetGeometryRef();
                                ring    = geo.GetGeometryRef(0);
                                int pointCount = ring.GetPointCount();

                                latMat[i] = new double[pointCount];
                                lonMat[i] = new double[pointCount];
                                altMat[i] = new double[pointCount];

                                System.Threading.Tasks.Parallel.For(0, pointCount, j =>
                                {
                                    layer.GetFeature(i).GetGeometryRef().GetPoint(j, pointList);

                                    latMat[i][j] = pointList[1];
                                    lonMat[i][j] = pointList[0];
                                    altMat[i][j] = pointList[2];
                                });

                                if (pointCount > 0)
                                {
                                    latitudesOut.AddRange(latMat[i], path);
                                    longitudesOut.AddRange(lonMat[i], path);
                                    altitudesOut.AddRange(altMat[i], path);
                                    cullPattern.Add(true, path);
                                }

                                else
                                {
                                    cullPattern.Add(false, path);
                                }
                            }
                        }

                        else if (geoType == "wkbLineString")
                        {
                            double[][]           latMat = new double[numberOfFeatures][];
                            double[][]           lonMat = new double[numberOfFeatures][];
                            double[][]           altMat = new double[numberOfFeatures][];
                            OSGeo.OGR.Geometry[] g      = new OSGeo.OGR.Geometry[numberOfFeatures];

                            for (int i = 0; i < numberOfFeatures; ++i)
                            {
                                path    = new GH_Path(i);
                                feature = layer.GetFeature(i);
                                geo     = feature.GetGeometryRef();

                                g[i] = geo;

                                int pointCount = geo.GetPointCount();

                                latMat[i] = new double[pointCount];
                                lonMat[i] = new double[pointCount];
                                altMat[i] = new double[pointCount];

                                System.Threading.Tasks.Parallel.For(0, pointCount, j =>
                                {
                                    g[i].GetPoint(j, pointList);



                                    latMat[i][j] = pointList[1];
                                    lonMat[i][j] = pointList[0];
                                    altMat[i][j] = pointList[2];
                                });

                                if (pointCount > 0)
                                {
                                    latitudesOut.AddRange(latMat[i], path);
                                    longitudesOut.AddRange(lonMat[i], path);
                                    altitudesOut.AddRange(altMat[i], path);
                                    cullPattern.Add(true, path);
                                }

                                else
                                {
                                    cullPattern.Add(false, path);
                                }
                            }
                        }

                        else if (geoType == "wkbPoint" || geoType == "wkbPoint25D")
                        {
                            double[] latMat = new double[numberOfFeatures];
                            double[] lonMat = new double[numberOfFeatures];
                            double[] altMat = new double[numberOfFeatures];

                            System.Threading.Tasks.Parallel.For(0, numberOfFeatures, i =>
                            {
                                //feature = layer.GetFeature(i);

                                //geo = feature.GetGeometryRef();
                                layer.GetFeature(i).GetGeometryRef().GetPoint(0, pointList);

                                latMat[i] = pointList[1];
                                lonMat[i] = pointList[0];
                                altMat[i] = pointList[2];
                            });

                            for (int i = 0; i < numberOfFeatures; ++i)
                            {
                                path = new GH_Path(i);

                                latitudesOut.Add(latMat[i], path);
                                longitudesOut.Add(lonMat[i], path);
                                altitudesOut.Add(altMat[i], path);
                            }
                        }

                        else
                        {
                            output = "Geometry type not implemented yet";
                        }
                    }

                    DA.SetDataTree(1, latitudesOut);
                    DA.SetDataTree(2, longitudesOut);
                    DA.SetDataTree(3, altitudesOut);
                    DA.SetDataTree(4, cullPattern);
                }
            }

            DA.SetData(0, output);
        }