示例#1
0
            public static NestingContext Get(PropertyHandler handler, int nestingLevel)
            {
                var result = new NestingContext {
                    m_Handler = handler, m_NestingLevel = nestingLevel
                };

                result.Open();
                return(result);
            }
示例#2
0
 public NestingContext IncrementNestingContext()
 {
     return(NestingContext.Get(this, m_NestingLevel + 1));
 }
        private void Nest(List <Polyline> sheets, Polyline[][] outlines, ref Tuple <Brep[], Dictionary <int, List <Guid> > > data)
        {
            this.scalesInv   = new Transform[0];
            this.scales      = new Transform[0];
            this.orient      = new Transform[0];
            this.sheetsRhino = new Polyline[0];
            this.id          = new int[0];
            this.transforms  = new Transform[0];

            //Scale
            Transform scale    = Transform.Scale(Point3d.Origin, 1 / tolerance);
            Transform scaleInv = Transform.Scale(Point3d.Origin, tolerance);

            for (int i = 0; i < sheets.Count; i++)
            {
                //sheets[i] = new Rectangle3d(sheets[i].Plane, sheets[i].Width * 1 / tolerance, sheets[i].Height * 1 / tolerance);
                Polyline polyline = new Polyline(sheets[i]);
                polyline.Transform(Transform.Scale(Point3d.Origin, 1 / tolerance));
                sheets[i] = polyline;
            }

            //Scale polylines and holes by tolerance
            scalesInv = new Transform[n];
            scales    = new Transform[n];

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < outlines[i].Length; j++)
                {
                    outlines[i][j].Transform(scale);
                }

                scalesInv[i] = scaleInv;
                scales[i]    = scale;
            }



            //Translation
            orient = new Transform[n];


            for (int i = 0; i < n; i++)
            {
                int last = (outlines[i].Length - 1) % outlines[i].Length;
                Tuple <Polyline, Transform> projectedPolyline = OpenNestUtil.FitPolylineToPlane(new Polyline(outlines[i][last]));



                //Rhino.RhinoDoc.ActiveDoc.Objects.AddPolyline(projectedPolyline.Item1);

                for (int j = 0; j < outlines[i].Length; j++)
                {
                    outlines[i][j].Transform(projectedPolyline.Item2);
                }

                //foreach (var pp in outlines[i][0])
                //    Rhino.RhinoDoc.ActiveDoc.Objects.AddPoint(pp);

                orient[i] = projectedPolyline.Item2;
            }


            ////////////Run Solver////////////
            //OpenNestSolver sample = new OpenNestSolver();
            //sample.Context.LoadSampleData(sheets, outlines);
            //var output = sample.Run(spacing, iterations, placementType, rotations);

            //Context = new NestingContext(this.seed);
            Context = new NestingContext();
            Context.LoadSampleData(sheets, outlines);


            //Rhino.RhinoApp.WriteLine("outlines " + polyline[0].Count.ToString());



            Background.UseParallel = true;


            SvgNest.Config.placementType = (PlacementTypeEnum)(placementType % 3);
            //Rhino.RhinoApp.WriteLine(((PlacementTypeEnum)(placementType % 3)).ToString());
            SvgNest.Config.spacing        = this.spacing;
            SvgNest.Config.rotations      = this.rotations;
            SvgNest.Config.seed           = this.seed;
            SvgNest.Config.clipperScale   = 1e7;
            SvgNest.Config.simplify       = (placementType == 4);
            SvgNest.Config.exploreConcave = (placementType == 5);
            SvgNest.Config.mergeLines     = false;
            //SvgNest.Config.useHoles = false;

            Context.StartNest();

            if (iterations > 0)
            {
                for (int i = 0; i < iterations; i++)
                {
                    Context.NestIterate();
                }
            }

            if (Context.SheetsNotUsed != -1)
            {
                int sheetCount = Context.Sheets.Count - Context.SheetsNotUsed;
                this.sheetsRhino = new Polyline[sheetCount];
                for (int i = 0; i < sheetCount; i++)
                {
                    //Rhino.RhinoApp.WriteLine(Context.Sheets[i].id.ToString());
                    Polyline sheetPoly = Context.Sheets[i].ToPolyline();
                    sheetPoly.Transform(Transform.Translation(new Vector3d(this.x * i, 0, 0)));
                    this.sheetsRhino[i] = sheetPoly;
                }
            }
            else
            {
                this.sheetsRhino = Context.Sheets.ToPolylines();
            }



            this.id = Context.Polygons.ToIDArray();
            //Context.Polygons[i].sheet.Id;
            Transform[] polygonsTransforms = Context.Polygons.GetTransforms();



            this.transforms = new Transform[scalesInv.Length];

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

            for (int i = 0; i < scalesInv.Length; i++)
            {
                if (Context.Polygons[i].fitted)
                {
                    this.transforms[i] = scalesInv[i] * Transform.Translation(new Vector3d(this.x * Context.Polygons[i].sheet.id, 0, 0)) * polygonsTransforms[i] * orient[i] * scales[i];
                    polygonIDinSheet.Add(Context.Polygons[i].sheet.id);
                }
                else
                {
                    this.transforms[i] = Transform.Translation(new Vector3d(0, 0, 0));
                    //this.transforms[i] = scalesInv[i] * polygonsTransforms[i] * orient[i] * scales[i];
                    polygonIDinSheet.Add(-1);
                }
            }

            //Rhino.RhinoApp.WriteLine("OpenNest: " + n.ToString() + "  " + data.Item2.ToString());
            for (int i = 0; i < n; i++)
            {
                var goo = data.Item1[i].DuplicateBrep(); //if not duplicated grasshopper will change original ones
                goo.Transform(transforms[i]);
                data.Item1[i] = goo;

                //Rhino.RhinoDoc.ActiveDoc.Objects.AddBrep(goo);

                foreach (Guid guid in data.Item2[i])
                {
                    Rhino.RhinoDoc.ActiveDoc.Objects.Transform(guid, transforms[i], false);//bake objects
                }
            }

            for (int i = 0; i < sheetsRhino.Length; i++)
            {
                sheetsRhino[i].Transform(Transform.Scale(Point3d.Origin, tolerance));
            }



            //Rhino.RhinoApp.WriteLine("Hi");
        }
示例#4
0
 public NestingContext ApplyNestingContext(int nestingLevel)
 {
     return(NestingContext.Get(this, nestingLevel));
 }
示例#5
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            try {
                this.reset         = DA.Fetch <bool>("Reset");
                this.run           = DA.Fetch <bool>("Run");
                this.iterations    = DA.Fetch <int>("Iterations");
                this.spacing       = DA.Fetch <double>("Spacing");
                this.placementType = DA.Fetch <int>("Placement");
                this.tolerance     = DA.Fetch <double>("Tolerance");
                this.rotations     = DA.Fetch <int>("Rotations");
                this.seed          = DA.Fetch <int>("Seed");
                this.spacing      *= (1 / tolerance);

                //Rhino.RhinoApp.WriteLine("ITER" + iterations.ToString());

                if (((this.reset) || this.scales.Length == 0) || (this.iterations > 0))
                {
                    //Input

                    //Rhino.RhinoApp.WriteLine("setup" + rotations.ToString());

                    List <Polyline> sheets = DA.FetchList <Curve>("Sheets").ToPolylines(true);

                    if (sheets.Count == 1)
                    {
                        Polyline sheetCopy = new Polyline(sheets[0]);
                        sheets.Clear();

                        Point3d  p0  = sheetCopy.BoundingBox.PointAt(0, 0, 0);
                        Point3d  p1  = sheetCopy.BoundingBox.PointAt(1, 0, 0);
                        Vector3d vec = p1 - p0;
                        x = (p1.X - p0.X + this.spacing) / this.tolerance;
                        for (int i = 0; i < 499; i++)
                        {
                            //Polyline sheetMoved = new Polyline(sheetCopy);
                            //sheetMoved.Transform(Transform.Translation(vec * i));
                            sheets.Add(sheetCopy);
                        }
                    }
                    else
                    {
                        this.x = 0;
                    }


                    List <IGH_GeometricGoo> geo_ = DA.FetchList <IGH_GeometricGoo>("Geo");

                    Polyline[][] outlines = GooToOutlines(geo_);



                    ////////////Solution////////////



                    base.Message = "Setup";

                    this.scalesInv   = new Transform[0];
                    this.scales      = new Transform[0];
                    this.orient      = new Transform[0];
                    this.sheetsRhino = new Polyline[0];
                    this.id          = new int[0];
                    this.transforms  = new Transform[0];



                    //Scale
                    Transform scale    = Transform.Scale(Point3d.Origin, 1 / tolerance);
                    Transform scaleInv = Transform.Scale(Point3d.Origin, tolerance);

                    for (int i = 0; i < sheets.Count; i++)
                    {
                        //sheets[i] = new Rectangle3d(sheets[i].Plane, sheets[i].Width * 1 / tolerance, sheets[i].Height * 1 / tolerance);
                        Polyline polyline = new Polyline(sheets[i]);
                        polyline.Transform(Transform.Scale(Point3d.Origin, 1 / tolerance));
                        sheets[i] = polyline;
                    }

                    //Scale polylines and holes by tolerance
                    scalesInv = new Transform[n];
                    scales    = new Transform[n];

                    for (int i = 0; i < n; i++)
                    {
                        for (int j = 0; j < outlines[i].Length; j++)
                        {
                            outlines[i][j].Transform(scale);
                        }

                        scalesInv[i] = scaleInv;
                        scales[i]    = scale;
                    }



                    //Translation
                    orient = new Transform[n];


                    for (int i = 0; i < n; i++)
                    {
                        int last = (outlines[i].Length - 1) % outlines[i].Length;
                        Tuple <Polyline, Transform> projectedPolyline = OpenNestUtil.FitPolylineToPlane(new Polyline(outlines[i][last]));



                        //Rhino.RhinoDoc.ActiveDoc.Objects.AddPolyline(projectedPolyline.Item1);

                        for (int j = 0; j < outlines[i].Length; j++)
                        {
                            outlines[i][j].Transform(projectedPolyline.Item2);
                        }

                        //foreach (var pp in outlines[i][0])
                        //    Rhino.RhinoDoc.ActiveDoc.Objects.AddPoint(pp);

                        orient[i] = projectedPolyline.Item2;
                    }


                    ////////////Run Solver////////////
                    //OpenNestSolver sample = new OpenNestSolver();
                    //sample.Context.LoadSampleData(sheets, outlines);
                    //var output = sample.Run(spacing, iterations, placementType, rotations);

                    //Context = new NestingContext(this.seed);
                    Context = new NestingContext();
                    Context.LoadSampleData(sheets, outlines);


                    //Rhino.RhinoApp.WriteLine("outlines " + polyline[0].Count.ToString());



                    Background.UseParallel = true;


                    ONest.Config.placementType = (PlacementTypeEnum)(placementType % 3);
                    //Rhino.RhinoApp.WriteLine(((PlacementTypeEnum)(placementType % 3)).ToString());
                    ONest.Config.spacing        = this.spacing;
                    ONest.Config.rotations      = this.rotations;
                    ONest.Config.seed           = this.seed;
                    ONest.Config.clipperScale   = 1e7;
                    ONest.Config.simplify       = (placementType == 4);
                    ONest.Config.exploreConcave = (placementType == 5);
                    ONest.Config.mergeLines     = false;
                    //ONest.Config.useHoles = false;

                    Context.StartNest();
                }



                if ((run || this.scales.Length == 0) && iterations == 0)
                {
                    Context.NestIterate();
                    base.Message = "Run " + Context.Iterations.ToString() + " \n Current Best: " + Context.Current.fitness;
                }

                if (iterations > 0)
                {
                    for (int i = 0; i < iterations; i++)
                    {
                        Context.NestIterate();
                    }
                    base.Message = "Static Solver " + Context.Iterations.ToString() + " \n Current Best: " + Context.Current.fitness;
                }



                if (Context.SheetsNotUsed != -1)
                {
                    int sheetCount = Context.Sheets.Count - Context.SheetsNotUsed;
                    this.sheetsRhino = new Polyline[sheetCount];
                    for (int i = 0; i < sheetCount; i++)
                    {
                        //Rhino.RhinoApp.WriteLine(Context.Sheets[i].id.ToString());
                        Polyline sheetPoly = Context.Sheets[i].ToPolyline();
                        sheetPoly.Transform(Transform.Translation(new Vector3d(this.x * i, 0, 0)));
                        this.sheetsRhino[i] = sheetPoly;
                    }
                }
                else
                {
                    this.sheetsRhino = Context.Sheets.ToPolylines();
                }



                this.id = Context.Polygons.ToIDArray();
                //Context.Polygons[i].sheet.Id;
                Transform[] polygonsTransforms = Context.Polygons.GetTransforms();



                this.transforms = new Transform[scalesInv.Length];

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

                for (int i = 0; i < scalesInv.Length; i++)
                {
                    if (Context.Polygons[i].fitted)
                    {
                        this.transforms[i] = scalesInv[i] * Transform.Translation(new Vector3d(this.x * Context.Polygons[i].sheet.id, 0, 0)) * polygonsTransforms[i] * orient[i] * scales[i];
                        polygonIDinSheet.Add(Context.Polygons[i].sheet.id);
                    }
                    else
                    {
                        this.transforms[i] = Transform.Translation(new Vector3d(0, 0, 0));
                        //this.transforms[i] = scalesInv[i] * polygonsTransforms[i] * orient[i] * scales[i];
                        polygonIDinSheet.Add(-1);
                    }
                }


                for (int i = 0; i < n; i++)
                {
                    IGH_GeometricGoo goo = geo_Original[i].DuplicateGeometry(); //if not duplicated grasshopper will change original ones
                    goo.Transform(transforms[i]);
                    geo[i] = goo;
                }

                for (int i = 0; i < sheetsRhino.Length; i++)
                {
                    sheetsRhino[i].Transform(Transform.Scale(Point3d.Origin, tolerance));
                }



                ////////////Output////////////
                DA.SetDataList(0, sheetsRhino); //Sheets
                DA.SetDataList(1, geo);         //Surfaces or outlines
                DA.SetDataList(2, id);          //ID
                DA.SetDataList(3, transforms);  //transformations
                DA.SetDataList(4, polygonIDinSheet);
            } catch (Exception e) {
                base.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, e.ToString());
            }
        }