如何将切割清单零件放置在2D切割清单的正确位置

时间:2019-07-04 11:03:08

标签: php algorithm codeigniter

面板是2400 * 1200,我将得到清单列表输入。也许有更好的方法可以做到这一点,但是我正在使用这个简单的概念来做到这一点。

  1. 最初,我根据面积将更大的一部分放在切割清单中。

  2. 然后我检查可用零件中的长度或宽度是否相近。

  3. 然后我决定将其放置在右侧或底部。

  4. 在所有部分放好之前,我都会这样做。

问题是我无法保持所放置零件的当前长度和宽度

这是我到目前为止尝试过的

public function first_dynamic(){
       $panel_length = 2400; //length of the panel
       $panel_width = 1200; // width of the panel
       $kerf_saw = 5; // cutting saw width
       $cutlist = array (
           array(
                'length' => 97,
                'width' => 657,
                'quantity' => 6,
                'rotatable' => 1
            ),
           array(
               'length' => 1031,
               'width' => 261,
               'quantity' => 1,
               'rotatable' => 1
           ),
           array(
               'length' => 337,
               'width' => 297,
               'quantity' => 2,
               'rotatable' => 1
           )
           );
       $parts = $this->getparts($cutlist);

       $x1 = 0;//stating point of x 
       $y1 = 0;//starting point of y
       $curlength = 0;
       $curwidth = 0;
       $left = 0;
       $bottom = 0;
       $col = 0;
       $row = 0;
       //image creation
       header ("Content-type: image/png");
       $handle = ImageCreate ($panel_width, $panel_length) or die ("Cannot Create image");
       $bg_color = ImageColorAllocate ($handle, 255, 255, 255);
       $txt_color = ImageColorAllocate ($handle, 0, 0, 0);
       $blue = imagecolorallocate ($handle, 56, 196, 228);
       $i=1;
       //loop through parts
       goto domagic;

       domagic:{
           if(!empty($parts)){
               $length = $parts[0]['length'];
               $width = $parts[0]['width'];

               if($i==1){
                  $curlength = $length;
                  $curwidth = $width;
                  $row = 1;
                  $column = 1;
               }

               if($bottom){

               }
               if($left){
                   $col++;
                   $curwidth = $curwidth+$kerf_saw;
                   $x1 = $curwidth;
                   $y1=0;
                   if($curwidth+$width > $panel_width){
                       $x1 = $curwidth-$prewidth-$kerf_saw;
                       $y1 = $prelength+$kerf_saw;
                   }

               }

               imagefilledrectangle($handle, $x1, $y1, $x1+$width, $y1+$length, $blue);
               ImageString($handle, 20, $x1+10, $y1+10, "L = $length, W = $width", $txt_color);

               $x1 = $x1;
               $y1 = $y1+$length+$kerf_saw;

               unset($parts[0]);
               $parts = array_values($parts);

               if($length > $width){
                   //to check length to insert on right side
                   $left = 1;
                   $bottom = 0;
                   $parts = $this->getsimilar_length($parts,$length);
                   //have to set current width & height
                   if($i!=1){
                   $curwidth = $curwidth + $width;
                   }
               }
               if($width > $length){
                   //to check width to insert on bottom side
                   $bottom = 1;
                   $left = 0;
                   $parts = $this->getsimilar_width($parts,$width);
               }
               $prewidth = $width;
               $prelength = $length;
//               echo $i.'. Curwidth='.$curwidth.' Curlength='.$curlength.'<br/>';

               $i++;
                if($i<5){
                   goto domagic;
                }
           }
       }

       ImagePng ($handle);

   }
   public function getsimilar_width($parts,$width){
       $dum = array();
       foreach($parts as $data){
           $data['diff'] = abs($width-$data['width']);
           array_push($dum,$data);
       }
       if(!function_exists('sortdiffwid')){
       function sortdiffwid($x, $y) {
            return $x['diff'] - $y['diff'];
       }
       }
       usort($dum, 'sortdiffwid');
       return $dum;
   }
   public function getsimilar_length($parts,$length){
       $dum = array();
       foreach($parts as $data){
           $data['diff'] = abs($length-$data['length']);
           array_push($dum,$data);
       }
       if(!function_exists('sortdifflen')){
       function sortdifflen($x, $y) {
            return $x['diff'] - $y['diff'];
       }
       }
       usort($dum, 'sortdifflen');
       return $dum;
   }
   public function getparts($cutlist){
       $length = count($cutlist);
       $parts = array();
       for($i=0;$i<$length;$i++){
           $quantity = $cutlist[$i]['quantity'];
           $subpart = array();
           for($j=0;$j<$quantity;$j++){
               $subpart['length'] = $cutlist[$i]['length'];
               $subpart['width'] = $cutlist[$i]['width'];
               $subpart['rotatable'] = $cutlist[$i]['rotatable'];
               $subpart['area'] = $cutlist[$i]['length'] * $cutlist[$i]['width'];
               array_push($parts,$subpart);
           }
       }
       function sortById($x, $y) {
            return $y['area'] - $x['area'];
       }
       usort($parts, 'sortById');
       return $parts;
   }

这是我得到的

但是第四部分实际上位置错误,应该在第二行

图片-https://ibb.co/H22wPtY

0 个答案:

没有答案