コード例 #1
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            //1. Get Nesting Settings
            GetNestingSettings();

            //2. Get Sheets
            List <Polyline> sheets = GetSheets();

            if (sheets == null)
            {
                return(Result.Failure);
            }
            if (sheets.Count == 0)
            {
                return(Result.Failure);
            }

            //3.1 Get Objects to Nest
            Tuple <Brep[], Dictionary <int, List <Guid> > > data = GetObjectsToNest();


            //3.2 Get planar brep outlines for nesting
            Polyline[][] outlines = new Polyline[this.n][];
            for (int i = 0; i < data.Item1.Length; i++)
            {
                outlines[i] = OpenNestUtil.BrepLoops(data.Item1[i]);
            }


            //4. Nest
            System.Threading.Tasks.Task.Run(() => {
                Nest(sheets, outlines, ref data);
            });

            Rhino.RhinoApp.WriteLine("OpenNest: Nesting... Keep working while the nesting finishes.");



            return(Rhino.Commands.Result.Success);
        }
コード例 #2
0
        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");
        }