comcu 发表于 2022-7-5 17:44:58

(nesting)optimize bar lengths

Hi,
 
 
Does anyone know any code that would allow me to optimize bar lengths?
 
I have bar lengths that are 6m long and I have to cut 20 allsorted lengths from the 6m lengths.
 
What code would you write to perform this task?
 
Thanks
 
Col

CAB 发表于 2022-7-5 18:06:36

Related subjects. I thought there was another one but could not find it.
 
http://cadtutor.net/forum/viewtopic.php?t=2257&highlight=
 
http://cadtutor.net/forum/viewtopic.php?t=6690&highlight=

comcu 发表于 2022-7-5 18:18:37

Those are kinda what im looking for but the seem overally complicated.
 
All I need to be able to do is 1D optimizing and it doesnt need to be drawn out, could use Text boxes for input & output
 
any thoughts?
 
cheers

CAB 发表于 2022-7-5 18:34:42

This is something I was playing with some time back. Perhaps you can make use of it.
 

(defun c:test(/ lst maxlen) (setq lst '(144 35 23 86 99 12 230 12 12 14 132 189 6 3 99)) (setq maxlen 240.0) (get_cutlist lst maxlen));;result ((99) (99 132) (86 144) (12 12 12 14 23 35 189) (3 6 230));;CAB 03-10-06(defun get_cutlist (lst maxlen / cutlst itm lst ptr tl x finallst remove-at) (defun remove-at (lst pos / head) ; Tony Tanzillo   (repeat pos   (setq head (cons (car lst) head)         lst(cdr lst)   )   )   (append (reverse head) (cdr lst)) ) (setq lst (mapcar '(lambda (x) (nth x lst)) (vl-sort-i lst '>))) ;;step through lst (while lst   (setq cutlst (list (car lst)) ; start new cutlist w/ first item         lst    (cdr lst) ; remove first item         ptr    (1- (length lst)) ; point to end of list         tl   (apply '+ cutlst) ; total length so far   )   ;; build the cutlst   (while (and lst cutlst)   ;; find largest next cut   ;; exit conditions ptr < 0 or itm length exceeds max   (while (and (< (+ tl (setq itm (nth ptr lst))) maxlen)               (> ptr 0))       (setq ptr (1- ptr))   )   (if (> ptr -1)       (if (= ptr (1- (length lst)))         ;;no more cuts fit, go to next         (setq finallst (cons cutlst finallst)               cutlst   nil         )         (setq cutlst (cons (nth (1+ ptr) lst) cutlst)               lst    (remove-at lst (1+ ptr))               tl   (apply '+ cutlst) ; new total         )       )       ;; else exausted pointer       (setq finallst (cons cutlst finallst)             cutlst   nil       )   )    )) (if cutlst   (cons cutlst finallst)   finallst ))

Migz 发表于 2022-7-5 18:48:46

Hi Guys,
 
I was playing around with the code above and noticed that it wouldn't work in a real world situation when I make;
 
(setq maxlen 6500.0) and (setq lst '(1120 2705 2725 2715)).... I get back ((2715) (1120 2705 2725)) = 6550? this should be (2725 2715)(2705 1120)
 
(setq maxlen 6500.0) and (setq lst '(2715 1050 2725 2705)).... i get back ((2715) (1050 2705 2725)) where as this should optimize to (2725 2715 1050)(2705)
 
 
Can someone please help me fix this code?
 
Cheers
Mike
页: [1]
查看完整版本: (nesting)optimize bar lengths