TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Jeroen on April 15, 2019, 07:31:17 AM

Title: Combine lists for desk layout and placing blocks
Post by: Jeroen on April 15, 2019, 07:31:17 AM
I have a Lisp that creates two lists throug user-input. These lists are measurements of  two different parts.
The lists can change in length. For example (top one in screenshot):

A (2 1 2) -> white-rectangles with "100+" in it
B (3 2) -> red dashed with value)

What I need is a list with points to put in 2 different blocks. (see screenshot, block A and block B)

The start and end-block I got covered.

List A only contains values of 1or 2
List B contains values of 1, 2, 3or 4

It should work in every combination where the total of the sum of the values in the lists are equal. (two more examples in screenshot)

I have been breaking my mind for hours and I cannot figure it out...

Kind regards,
Jeroen Berkers
Title: Re: Combine lists for desk layout and placing blocks
Post by: roy_043 on April 15, 2019, 08:35:07 AM
Your problem is hard to understand.
If both list A and B are based on user input what should the Lisp in fact do?
If only list B is given and you accept a list A with only 1's (as per your image) the algorithm is rather simple.

Code: [Select]
(defun B_To_A (lstB)
  (apply
    'append
    (mapcar
      '(lambda (itm / ret) (repeat itm (setq ret (cons 1 ret))))
      lstB
    )
  )
)
Title: Re: Combine lists for desk layout and placing blocks
Post by: ribarm on April 15, 2019, 09:16:11 AM
Here you are :

Code - Auto/Visual Lisp: [Select]
  1. (defun c:const-st-a-b-en-lst ( / lst1 lst2 lst3 l l1 l2 lst )
  2.   (initget 1)
  3.   (setq lst1 (read (strcat "(" (getstring t "\nSpecify first list with numbers - integers : ") ")")))
  4.   (initget 1)
  5.   (setq lst2 (read (strcat "(" (getstring t "\nSpecify second list with numbers - integers : ") ")")))
  6.   (if (and lst1 lst2)
  7.     (progn
  8.       (repeat (apply '+ lst1)
  9.         (setq lst3 (cons 1 lst3))
  10.       )
  11.       (setq lst (cons "st" lst))
  12.       (setq l 0 l1 0 l2 0)
  13.       (foreach x lst3
  14.         (setq l (+ l x))
  15.         (if (= l (apply '+ lst3))
  16.           (setq lst (append lst (list "en")))
  17.           (if (= l (+ (car lst1) l1))
  18.             (if (= l (+ (car lst2) l2))
  19.               (progn
  20.                 (setq lst (append lst (list "b")))
  21.                 (setq l2 (+ l2 (car lst2)))
  22.                 (setq l1 (+ l1 (car lst1)))
  23.                 (setq lst2 (cdr lst2))
  24.                 (setq lst1 (cdr lst1))
  25.               )
  26.               (progn
  27.                 (setq lst (append lst (list "a")))
  28.                 (setq l1 (+ l1 (car lst1)))
  29.                 (setq lst1 (cdr lst1))
  30.               )
  31.             )
  32.           )
  33.         )
  34.       )
  35.     )
  36.   )
  37.   (princ lst)
  38.   (princ)
  39. )
  40.  
  41. ;|
  42. Command: CONST-ST-A-B-EN-LST
  43. Specify first list with numbers - integers : 2 1 2
  44. Specify second list with numbers - integers : 3 2
  45. (st a b en)
  46. Command:
  47. Command: CONST-ST-A-B-EN-LST
  48. Specify first list with numbers - integers : 1 1 1 1 1 1
  49. Specify second list with numbers - integers : 2 2 2
  50. (st a b a b a en)
  51. Command:
  52. Command: CONST-ST-A-B-EN-LST
  53. Specify first list with numbers - integers : 1 1 1 1 1
  54. Specify second list with numbers - integers : 2 3
  55. (st a b a a en)
  56. |;
  57.  

HTH., M.R.
Title: Re: Combine lists for desk layout and placing blocks
Post by: Jeroen on April 15, 2019, 09:35:53 AM
Thanks Marko, it does exactly what I was looking for.

Jeroen