Пример #1
0
        private void PrepareSimulation()
        {
            RoboDK RDK = new RoboDK("127.0.0.1");

            RoboDK.Item frame_pallet = RDK.GetItem("PalletA", RoboDK.ITEM_TYPE_FRAME);

            // 获取参数
            string SizeBox    = RDK.GetParam("SizeBox");                                                // 150,200,150  箱子大小
            string SizePallet = RDK.GetParam("SizePallet");                                             // 5,3,3  横纵列

            float[] SizeBoxXyz    = SizeBox.Split(',').Select((m) => float.Parse(m.Trim())).ToArray();  // [150,200,150]
            int[]   SizePalletXyz = SizePallet.Split(',').Select((m) => int.Parse(m.Trim())).ToArray(); // [5,3,3]

            float SizeBoxZ = SizeBoxXyz[2];                                                             // 箱子的高度很重要,用来计算位置和碰撞检测的


            RDK.Render(false);

            CleanUp(RDK.GetItemList(RoboDK.ITEM_TYPE_OBJECT), "Part ");
            CleanUp(RDK.GetItemList(RoboDK.ITEM_TYPE_TOOL), "TCP ");

            RDK.GetItem("box100mm").Copy();
            Random random = new Random();

            for (int i = 0; i < SizePalletXyz[0]; i++)
            {
                for (int j = 0; j < SizePalletXyz[1]; j++)
                {
                    for (int k = 0; k < SizePalletXyz[2]; k++)
                    {
                        // 计算位置
                        float location_x = SizeBoxXyz[0] / 2 + i * SizeBoxXyz[0];
                        float location_y = SizeBoxXyz[1] / 2 + j * SizeBoxXyz[1];
                        float location_z = SizeBoxXyz[2] / 2 + k * SizeBoxXyz[2];

                        // 复制新的对象
                        RoboDK.Item newPart = frame_pallet.Paste();
                        newPart.Scale(new double[] { SizeBoxXyz[0] / 100, SizeBoxXyz[1] / 100, SizeBoxXyz[2] / 100 });
                        newPart.SetName("Part " + i.ToString() + j.ToString() + k.ToString());
                        newPart.SetPose(Matrix.Transl(location_x, location_y, location_z));
                        newPart.SetVisible(true, 0);
                        newPart.SetColor(new double[] { random.NextDouble(), random.NextDouble(), random.NextDouble(), 1 });
                    }
                }
            }


            RDK.SetParam("SENSOR", "0");
            RDK.Render(true);
            RDK.Disconnect();

            PartsClear();
        }