Ejemplo n.º 1
0
        public override bool CastTo <Q>(ref Q target)
        {
            // This function is called when Grasshopper needs to convert this
            // instance of GsaSection into some other type Q.


            if (typeof(Q).IsAssignableFrom(typeof(GsaSection)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    target = (Q)(object)Value;
                }
                return(true);
            }

            if (typeof(Q).IsAssignableFrom(typeof(Section)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    target = (Q)(object)Value;
                }
                return(true);
            }

            if (typeof(Q).IsAssignableFrom(typeof(GH_Integer)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    GH_Integer ghint = new GH_Integer();
                    if (GH_Convert.ToGHInteger(Value.ID, GH_Conversion.Both, ref ghint))
                    {
                        target = (Q)(object)ghint;
                    }
                    else
                    {
                        target = default;
                    }
                }
                return(true);
            }

            target = default;
            return(false);
        }
Ejemplo n.º 2
0
        public override bool CastTo <Q>(out Q target)
        {
            // This function is called when Grasshopper needs to convert this
            // instance of GsaMember into some other type Q.


            if (typeof(Q).IsAssignableFrom(typeof(GsaMember3d)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    target = (Q)(object)Value.Duplicate();
                }
                return(true);
            }

            if (typeof(Q).IsAssignableFrom(typeof(Member)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    target = (Q)(object)Value.Member;
                }
                return(true);
            }

            //Cast to Mesh
            if (typeof(Q).IsAssignableFrom(typeof(Mesh)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    target = (Q)(object)Value.SolidMesh;
                }
                return(true);
            }
            if (typeof(Q).IsAssignableFrom(typeof(GH_Mesh)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    target = (Q)(object)new GH_Mesh(Value.SolidMesh);
                    if (Value.SolidMesh == null)
                    {
                        return(false);
                    }
                }

                return(true);
            }

            if (typeof(Q).IsAssignableFrom(typeof(GH_Integer)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    GH_Integer ghint = new GH_Integer();
                    if (GH_Convert.ToGHInteger(Value.ID, GH_Conversion.Both, ref ghint))
                    {
                        target = (Q)(object)ghint;
                    }
                    else
                    {
                        target = default;
                    }
                }
                return(true);
            }

            target = default;
            return(false);
        }
Ejemplo n.º 3
0
        SolveResults Compute(string fileLoc, List <Color> colors, int tskId)
        {
            var  rc           = new SolveResults();
            bool filterColors = colors.Any();

            List <GH_Colour>        topCols     = new List <GH_Colour>();
            List <GH_Integer>       colCount    = new List <GH_Integer>();
            GH_Structure <GH_Point> colLocation = new GH_Structure <GH_Point>();


            try
            {
                using (Bitmap bitmap = new Bitmap(fileLoc))
                {
                    GH_Integer pixCount = new GH_Integer();
                    GH_Convert.ToGHInteger(bitmap.Height * bitmap.Width, 0, ref pixCount);
                    rc.PixCount = pixCount;

                    ///https://www.grasshopper3d.com/forum/topics/unsafe?page=1&commentId=2985220%3AComment%3A808291&x=1#2985220Comment808291
                    GH_MemoryBitmap sampler = new GH_MemoryBitmap(bitmap);

                    Color col = Color.Transparent;
                    for (int x = 0; x < bitmap.Width; x++)
                    {
                        for (int y = 0; y < bitmap.Height; y++)
                        {
                            ///GH_MemoryBitmap Sample is faster than GetPixel
                            //col = bitmap.GetPixel(x, y);
                            if (sampler.Sample(x, y, ref col))
                            {
                                if (colors.Contains(col))
                                {
                                    GH_Path path = new GH_Path(tskId, colors.IndexOf(col));
                                    colLocation.Append(new GH_Point(new Point3d(x, y, 0)), path);
                                }
                                else if (!filterColors)
                                {
                                    colors.Add(col);
                                    GH_Path path = new GH_Path(tskId, colors.IndexOf(col));
                                    colLocation.Append(new GH_Point(new Point3d(x, y, 0)), path);
                                }
                            }
                        }
                    }

                    sampler.Release(false);
                    bitmap.Dispose();
                }
            }

            catch
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Could not load image from file path: " + fileLoc);
            }

            List <GH_Colour> ghColors = new List <GH_Colour>();

            foreach (var c in colors)
            {
                ghColors.Add(new GH_Colour(c));
            }

            rc.TopColors     = ghColors;
            rc.ColorLocation = colLocation;

            return(rc);
        }
Ejemplo n.º 4
0
        SolveResults Compute(string fileLoc, int numColors)
        {
            var rc = new SolveResults();
            Dictionary <Color, int> dictColors = new Dictionary <Color, int>();
            Dictionary <Color, List <GH_Point> > dictColorLocation = new Dictionary <Color, List <GH_Point> >();
            List <GH_Colour>        topCols     = new List <GH_Colour>();
            List <GH_Integer>       colCount    = new List <GH_Integer>();
            GH_Structure <GH_Point> colLocation = new GH_Structure <GH_Point>();


            try
            {
                using (Bitmap bitmap = new Bitmap(fileLoc))
                {
                    GH_Integer pixCount = new GH_Integer();
                    GH_Convert.ToGHInteger(bitmap.Height * bitmap.Width, 0, ref pixCount);
                    rc.PixCount = pixCount;

                    ///https://www.grasshopper3d.com/forum/topics/unsafe?page=1&commentId=2985220%3AComment%3A808291&x=1#2985220Comment808291
                    GH_MemoryBitmap sampler = new GH_MemoryBitmap(bitmap);

                    Color col = Color.Transparent;
                    for (int x = 0; x < bitmap.Width; x++)
                    {
                        for (int y = 0; y < bitmap.Height; y++)
                        {
                            ///GH_MemoryBitmap Sample is faster than GetPixel
                            //col = bitmap.GetPixel(x, y);
                            if (sampler.Sample(x, y, ref col))
                            {
                                if (!dictColors.ContainsKey(col))
                                {
                                    dictColors.Add(col, 1);
                                    //dictColorLocation.Add(col, new List<GH_Point> { new GH_Point(new Point3d(x,y,0)) });
                                }
                                else
                                {
                                    dictColors[col]++;
                                    //dictColorLocation[col].Add(new GH_Point(new Point3d(x,y,0)));
                                }
                            }
                        }
                    }

                    if (numColors > dictColors.Count || numColors <= 0)
                    {
                        numColors = dictColors.Count;
                    }

                    var sortedColorDict = (from entry in dictColors orderby entry.Value descending select entry)
                                          .Take(numColors)
                                          .ToDictionary(pair => pair.Key, pair => pair.Value);

                    //var sortedColorLocation = (from entry in dictColorLocation orderby entry.Value.Count descending select entry)
                    //    .Take(numColors)
                    //   .ToDictionary(pair => pair.Key, pair => pair.Value);

                    foreach (var clr in sortedColorDict)
                    {
                        GH_Colour gh_Col = new GH_Colour();
                        GH_Convert.ToGHColour(clr.Key, 0, ref gh_Col);
                        topCols.Add(gh_Col);

                        GH_Integer gh_Count = new GH_Integer();
                        GH_Convert.ToGHInteger(clr.Value, 0, ref gh_Count);
                        colCount.Add(gh_Count);
                    }

                    /*int i = 0;
                     * foreach (var clr in sortedColorLocation)
                     * {
                     *  GH_Path path = new GH_Path(i);
                     *  colLocation.AppendRange(clr.Value, path);
                     *  i++;
                     * }
                     */

                    sampler.Release(false);
                    bitmap.Dispose();
                }
            }

            catch
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Could not load image from file path: " + fileLoc);
            }

            rc.TopColors  = topCols;
            rc.ColorCount = colCount;
            //rc.ColorLocation = colLocation;

            return(rc);
        }
Ejemplo n.º 5
0
        public override bool CastTo <Q>(out Q target)
        {
            // This function is called when Grasshopper needs to convert this
            // instance of GsaElement3D into some other type Q.


            if (typeof(Q).IsAssignableFrom(typeof(GsaElement3d)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    target = (Q)(object)Value.Duplicate();
                }
                return(true);
            }

            if (typeof(Q).IsAssignableFrom(typeof(Element)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    target = (Q)(object)Value.Elements[0];
                }
                return(true);
            }

            //Cast to Mesh
            if (typeof(Q).IsAssignableFrom(typeof(Mesh)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    target = (Q)(object)Value.DisplayMesh;
                }
                return(true);
            }

            if (typeof(Q).IsAssignableFrom(typeof(GH_Mesh)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    target = (Q)(object)new GH_Mesh(Value.DisplayMesh);
                }

                return(true);
            }

            if (typeof(Q).IsAssignableFrom(typeof(List <GH_Integer>)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    List <GH_Integer> ints = new List <GH_Integer>();

                    for (int i = 0; i < Value.ID.Count; i++)
                    {
                        GH_Integer ghint = new GH_Integer();
                        if (GH_Convert.ToGHInteger(Value.ID, GH_Conversion.Both, ref ghint))
                        {
                            ints.Add(ghint);
                        }
                    }
                    target = (Q)(object)ints;
                }
                return(true);
            }

            target = default;
            return(false);
        }
Ejemplo n.º 6
0
        public override bool CastTo <Q>(out Q target)
        {
            // This function is called when Grasshopper needs to convert this
            // instance of GsaMember into some other type Q.


            if (typeof(Q).IsAssignableFrom(typeof(GsaMember2d)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    target = (Q)(object)Value.Duplicate();
                }
                return(true);
            }

            if (typeof(Q).IsAssignableFrom(typeof(Member)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    target = (Q)(object)Value.Member;
                }
                return(true);
            }

            //Cast to Curve
            if (typeof(Q).IsAssignableFrom(typeof(Curve)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    target = (Q)(object)Value.PolyCurve;
                }
                return(true);
            }
            if (typeof(Q).IsAssignableFrom(typeof(GH_Curve)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    target = (Q)(object)new GH_Curve(Value.PolyCurve);
                    if (Value.PolyCurve == null)
                    {
                        return(false);
                    }
                }

                return(true);
            }

            if (typeof(Q).IsAssignableFrom(typeof(PolyCurve)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    target = (Q)(object)Value.PolyCurve;
                    if (Value.PolyCurve == null)
                    {
                        return(false);
                    }
                }

                return(true);
            }

            if (typeof(Q).IsAssignableFrom(typeof(Polyline)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    target = (Q)(object)Value.PolyCurve;
                    if (Value.PolyCurve == null)
                    {
                        return(false);
                    }
                }

                return(true);
            }
            if (typeof(Q).IsAssignableFrom(typeof(Line)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    target = (Q)(object)Value.PolyCurve.ToPolyline(0.05, 5, 0, 0);
                    if (Value.PolyCurve == null)
                    {
                        return(false);
                    }
                }

                return(true);
            }

            //Cast to Brep
            if (typeof(Q).IsAssignableFrom(typeof(Brep)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    target = (Q)(object)Value.Brep;
                    if (Value.Brep == null)
                    {
                        return(false);
                    }
                }

                return(true);
            }
            if (typeof(Q).IsAssignableFrom(typeof(GH_Brep)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    target = (Q)(object)new GH_Brep(Value.Brep);
                    if (Value.Brep == null)
                    {
                        return(false);
                    }
                }
                return(true);
            }

            //Cast to Points
            if (typeof(Q).IsAssignableFrom(typeof(List <Point3d>)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    target = (Q)(object)Value.Topology;
                }
                return(true);
            }
            if (typeof(Q).IsAssignableFrom(typeof(List <GH_Point>)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    target = (Q)(object)Value.Topology;
                }
                return(true);
            }

            if (typeof(Q).IsAssignableFrom(typeof(GH_Integer)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    GH_Integer ghint = new GH_Integer();
                    if (GH_Convert.ToGHInteger(Value.ID, GH_Conversion.Both, ref ghint))
                    {
                        target = (Q)(object)ghint;
                    }
                    else
                    {
                        target = default;
                    }
                }
                return(true);
            }

            target = default;
            return(false);
        }
Ejemplo n.º 7
0
        /*******************************************/

        public static bool CastToGoo(object value, ref GH_Integer target)
        {
            return(GH_Convert.ToGHInteger(value, GH_Conversion.Both, ref target));
        }
Ejemplo n.º 8
0
        public override bool CastTo <Q>(out Q target)
        {
            // This function is called when Grasshopper needs to convert this
            // instance of GsaElement into some other type Q.


            if (typeof(Q).IsAssignableFrom(typeof(GsaElement1d)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    target = (Q)(object)Value.Duplicate();
                }
                return(true);
            }

            if (typeof(Q).IsAssignableFrom(typeof(Element)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    target = (Q)(object)Value.Element;
                }
                return(true);
            }

            //Cast to Curve
            if (typeof(Q).IsAssignableFrom(typeof(Line)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    target = (Q)(object)Value.Line;
                }
                return(true);
            }
            if (typeof(Q).IsAssignableFrom(typeof(GH_Line)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    GH_Line ghLine = new GH_Line();
                    GH_Convert.ToGHLine(Value.Line, GH_Conversion.Both, ref ghLine);
                    target = (Q)(object)ghLine;
                }

                return(true);
            }
            if (typeof(Q).IsAssignableFrom(typeof(Curve)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    target = (Q)(object)Value.Line;
                }
                return(true);
            }
            if (typeof(Q).IsAssignableFrom(typeof(GH_Curve)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    target = (Q)(object)new GH_Curve(Value.Line);
                }

                return(true);
            }

            if (typeof(Q).IsAssignableFrom(typeof(GH_Integer)))
            {
                if (Value == null)
                {
                    target = default;
                }
                else
                {
                    GH_Integer ghint = new GH_Integer();
                    if (GH_Convert.ToGHInteger(Value.ID, GH_Conversion.Both, ref ghint))
                    {
                        target = (Q)(object)ghint;
                    }
                    else
                    {
                        target = default;
                    }
                }
                return(true);
            }


            target = default;
            return(false);
        }