apply function error

apply function error

etilley327KA
Advocate Advocate
613 Views
7 Replies
Message 1 of 8

apply function error

etilley327KA
Advocate
Advocate

Im getting this error when trying to get the max of my list of numbers. What am I missing?

 

etilley327KA_1-1673287664154.png

(defun c:TEST (/ MFF 1stk 2stk 3stk 4stk toc hpd b18 c18 d18 e18 f18 j3 j6 j9)
 (setq MFF (getreal "Enter MFF :"))
 (setq 1stk (car (entsel "\nSelect Front Left Stake: ")))
 (setq 1stk (atof (getpropertyvalue 1stk "ELEV2")))
 (setq 2stk (car (entsel "\nSelect Back Left Stake: ")))
 (setq 2stk (atof (getpropertyvalue 2stk "ELEV2")))
 (setq 3stk (car (entsel "\nSelect Back Right Stake: ")))
 (setq 3stk (atof (getpropertyvalue 3stk "ELEV2")))
 (setq 4stk (car (entsel "\nSelect Front Right Stake: ")))
 (setq 4stk (atof (getpropertyvalue 4stk "ELEV2")))
 (setq toc (car (entsel "\nSelect High TOC: ")))
 (setq toc (atof (getpropertyvalue toc "ELEV2")))
 (setq hpd (cons (list 1stk 2stk 3stk 4stk) hpd))
 (setq hpd (apply 'max hpd))
 (setq b18 (- MF 100))
 (setq c18 (- MF toc))
 (setq e18 (- hpd toc))
 (setq f18 (+ e18 0.8))
 (setq j3 (cons (list f18 b18 c18) j3))
 (setq j3 (apply 'max j3))
 (setq d18 (+ j3 100))
 (setq j6 (+ toc j3))
 (setq j9 (- j6 0.8))
 (princ j3)
 (princ j6)
 (princ j9)
)

 

0 Likes
Accepted solutions (1)
614 Views
7 Replies
Replies (7)
Message 2 of 8

john.kaulB9QW2
Advocate
Advocate
Accepted solution

Quick look: I believe line 13 should be: 

 (setq hpd (list 1stk 2stk 3stk 4stk))
another swamper
Message 3 of 8

hak_vz
Advisor
Advisor

Try this. I hope you can figure out the reason for getting error with your code.

I would need test dwg for your code, but obviously cons is not needed. It create list ((a b c d)) and this is unnecessary to look for max value inside list. 

 

 

(defun c:TEST (/ MFF 1stk 2stk 3stk 4stk toc hpd b18 c18 d18 e18 f18 j3 j6 j9)
 (setq MFF (getreal "Enter MFF :"))
 (setq 1stk (car (entsel "\nSelect Front Left Stake: ")))
 (setq 1stk (atof (getpropertyvalue 1stk "ELEV2")))
 (setq 2stk (car (entsel "\nSelect Back Left Stake: ")))
 (setq 2stk (atof (getpropertyvalue 2stk "ELEV2")))
 (setq 3stk (car (entsel "\nSelect Back Right Stake: ")))
 (setq 3stk (atof (getpropertyvalue 3stk "ELEV2")))
 (setq 4stk (car (entsel "\nSelect Front Right Stake: ")))
 (setq 4stk (atof (getpropertyvalue 4stk "ELEV2")))
 (setq toc (car (entsel "\nSelect High TOC: ")))
 (setq toc (atof (getpropertyvalue toc "ELEV2")))
 (setq hpd (list 1stk 2stk 3stk 4stk))
 (setq hpd (apply 'max hpd))
 (setq b18 (- MF 100))
 (setq c18 (- MF toc))
 (setq e18 (- hpd toc))
 (setq f18 (+ e18 0.8))
 (setq j3 (list f18 b18 c18))
 (setq j3 (apply 'max j3))
 (setq d18 (+ j3 100))
 (setq j6 (+ toc j3))
 (setq j9 (- j6 0.8))
 (princ j3)
 (princ j6)
 (princ j9)
)

 

Add some testing procedure to make your code bulletproof.

Also check

 (setq b18 (- MF 100))

I guess it should be:

 (setq b18 (- MFF 100))

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 4 of 8

etilley327KA
Advocate
Advocate
Thanks
0 Likes
Message 5 of 8

etilley327KA
Advocate
Advocate
That was it, thanks
Message 6 of 8

hak_vz
Advisor
Advisor

@john.kaulB9QW2 

Also check this helper function. It assures that asked entity is selected before processing.

 

(defun get_selection (msg)
	(setq e (car(entsel msg)))
	(if (and (not e) (= (getvar 'Errno) 7)) (get_selection msg) e)
)

Usage:

(setq e (get_selection "\nPick it >"))

 

As long as you don't pick some entity i.e function  receives nil it recursively calls itself. With some variations this is standard include in my autolisp codes.

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 7 of 8

Kent1Cooper
Consultant
Consultant

Since you don't use that list for anything else, there's no need to make it into a list variable first, and then apply (max) to it and replace the value in the variable, for example like this:

 (setq hpd (list 1stk 2stk 3stk 4stk))
 (setq hpd (apply 'max hpd))

You can just get the maximum of those values directly, without the middle-man of the list variable:

 (setq hpd (max 1stk 2stk 3stk 4stk))

 

Kent Cooper, AIA
Message 8 of 8

etilley327KA
Advocate
Advocate

Thanks, ill be adding more.

0 Likes