Jump to content
TunzaGibbo

"IF"

Recommended Posts

TunzaGibbo

(defun c:www (/ P1 P2 Dist1 Dist2 Ang1 hole1a hole1b) 
   (savevartoold)
     (setq P1 (getpoint "\n Select a Point  : "))
     (setq P2 (getpoint P1 "\n Select a Point  : "))
     (setq Dist1 (distance P1 P2))
     (setq Dist2 (- Dist1 40.0))
     (setq Ang1 (angle P1 P2))
     (setvar "osmode" 0)
   (command "_.circle" (polar P1 (+ ang1 (dtr 0)) 20.0) "D" 3.2 "" ) 
     (setq hole1a (entlast))
   (command "_.circle" (polar P1 (+ ang1 (dtr 0)) (- Dist1 20.0)) "D" 3.2 "" ) 
     (setq hole1b (entlast))

 (if 
     (and (> (- Dist1 40) 200) (/> (- Dist1 40) 400)) 
     (setq NewDist (/ (- Dist1 40) 2)))
     (command "_.copy" hole1a "" (polar P1 (+ ang1 (dtr 0)) 20)  NewDist) "")

   (resetoldvar)
 (princ)   
)

 

It all works perfect until I get to the "if"

 

What I'm trying to say is that if Dist1 minus 40 is greater than 200 or Dist1 minus 40 is not greater than 400 then set NewDist to Dist1 minus 40 divided by 2.

I hope I have explained that well enough

 

Regards

Share this post


Link to post
Share on other sites
Aftertouch

The /> wont work.

 

Its either:

> larger than...

=> larger or equal

=

 

So... /> 400 had to be '

Share this post


Link to post
Share on other sites
TunzaGibbo

thanks for the advice

 

I need to write a series of conditions for example

>200 but not > than 400

>200 but not > than 600

>200 but not > than 800

and so on up to 3400

Share this post


Link to post
Share on other sites
Aftertouch

i suggest to use a 'COND' function instead of the 'IF' function.

 

(cond
((and (> 200 dist1) (< 400 dist1))
	(princ "i am more than 200 and less then 400.")
)
((and (> 200 dist1) (< 600 dist1))
	(princ "i am more than 200 and less then 600.")
)
((and (> 200 dist1) (< 800 dist1))
	(princ "i am more than 200 and less then 800.")
)
((and (> 200 dist1) (< 1000 dist1))
	(princ "i am more than 200 and less then 1000.")
)
(t
	(princ "i am not any of the above..")
)
)

Share this post


Link to post
Share on other sites
hanhphuc

Dist1 minus 40 is greater than 200 or Dist1 minus 40 is not greater than 400 then set NewDist to Dist1 minus 40 divided by 2.

hi, just confused how negative value -40 can be greater than positif +400 ?

(< 200 ([color="blue"]abs[/color] dist1) 400)) 

?

or not?

Share this post


Link to post
Share on other sites
Aftertouch
hi, just confused how negative value -40 can be greater than positif +400 ?

(< 200 ([color="blue"]abs[/color] dist1) 400)) 

?

or not?

 

He doesnt want a negative 40 (-40).

He wants his dist1, lowered with 40. ;-)

Share this post


Link to post
Share on other sites
ronjonp
i suggest to use a 'COND' function instead of the 'IF' function.

 

(cond
((and (> 200 dist1) (< 400 dist1))
	(princ "i am more than 200 and less then 400.")
)
((and (> 200 dist1) (< 600 dist1))
	(princ "i am more than 200 and less then 600.")
)
((and (> 200 dist1) (< 800 dist1))
	(princ "i am more than 200 and less then 800.")
)
((and (> 200 dist1) (< 1000 dist1))
	(princ "i am more than 200 and less then 1000.")
)
(t
	(princ "i am not any of the above..")
)
)

 

FWIW .. you could simplify your code to this:

(setq dist (- dist1 40))
(cond ((< 200 dist1 400) (princ "i am more than 200 and less then 400."))
     ((< 200 dist1 600) (princ "i am more than 200 and less then 600."))
     ((< 200 dist1 800) (princ "i am more than 200 and less then 800."))
     ((< 200 dist1 1000) (princ "i am more than 200 and less then 1000."))
     ((princ "i am not any of the above.."))
)

Share this post


Link to post
Share on other sites
Grrr

I remember writing some findrange subfoo and thought its cool 8)

 

;| Example
(setq val 63)
(findrange val
 '(
   ( (0 51) "01")
   ( (52 76) "02")
   ( (77 121) "03")
   ( (122 176) "04")
   ( (177 nil) "05")
 )
)
>> "02"
|;

(setq findrange
 (lambda (v L)
   (if (and (numberp v) (listp L))
     (vl-some 
       '(lambda (x / mn mx) 
         (setq mn (caar x)) 
         (setq mx (cadar x)) 
         (if (apply '<= (append (if mn (list mn)) (list v) (if mx (list mx)) )) 
           (cadr x)
         ); if
       ); lambda
       L
     ); vl-some
   ); if 
 ); lambda (v L)
); setq findrange

Share this post


Link to post
Share on other sites
ronjonp

Very cool :)

Share this post


Link to post
Share on other sites
Lee Mac
FWIW .. you could simplify your code to this:

(setq dist (- dist1 40))
(cond ((< 200 dist1 400) (princ "i am more than 200 and less then 400."))
     ((< 200 dist1 600) (princ "i am more than 200 and less then 600."))
     ((< 200 dist1 800) (princ "i am more than 200 and less then 800."))
     ((< 200 dist1 1000) (princ "i am more than 200 and less then 1000."))
     ((princ "i am not any of the above.."))
)

 

Which could be simplified further to -

(setq dist (- dist1 40))
(cond ((<= dist1  200) (princ "\nI am less than 200."))
     ((<  dist1  400) (princ "\nI am more than 200 and less than 400."))
     ((<  dist1  600) (princ "\nI am more than 200 and less than 600."))
     ((<  dist1  800) (princ "\nI am more than 200 and less than 800."))
     ((<  dist1 1000) (princ "\nI am more than 200 and less than 1000."))
     ((princ "\nI am more than 1000."))
)

Share this post


Link to post
Share on other sites
ronjonp
Which could be simplified further to -
(setq dist (- dist1 40))
(cond ((<= dist1  200) (princ "\nI am less than 200."))
     ((<  dist1  400) (princ "\nI am more than 200 and less than 400."))
     ((<  dist1  600) (princ "\nI am more than 200 and less than 600."))
     ((<  dist1  800) (princ "\nI am more than 200 and less than 800."))
     ((<  dist1 1000) (princ "\nI am more than 200 and less than 1000."))
     ((princ "\nI am more than 1000."))
)

 

But of course .. sometimes I cannot see the forest through the trees. :lol:

Share this post


Link to post
Share on other sites
hanhphuc
He doesnt want a negative 40 (-40).

He wants his dist1, lowered with 40. ;-)

 

Thanks this makes sence now :)

arithmetic minus negative

 


(defun foo (n l)
 (eval
   (cons 'cond
  (append (mapcar '(lambda (x)
		     (list (cons '<= (list 200. n x)) [color="green"];(<= 200. n x) [/color]
			   (list 'princ (strcat "i am more than 200 and less than " (vl-princ-to-string x)))
			   ) 
		     ) 
		  l
		  ) 
	  '((t
	     (if
	      (< n 200.)
	      (princ "\nI am less than 200.")
	      (princ "i am not any of the above..")
	      )
	     )
	    )
	  ) 
  ) 
   ) 
 ) 

([color="blue"]foo[/color] (setq dist1 (- dist1 40)) '(400 600 800 1000 1500 2000 2500 3000) )

 

IMO, other options too?

Ronjonp is used to vl-remove-if- function,

perhaps Lee, grr or others would share some variants :)

assoc list & vl-sort etc..?

Share this post


Link to post
Share on other sites
Lee Mac

A variant using vl-some

(setq d (- dist1 40))
(or (vl-some '(lambda ( x ) (if (< d x) (princ (strcat "\nI am less than " (rtos x))))) '(200 400 600 800 1000))
   (princ "\nI am greater than 1000.")
)

Share this post


Link to post
Share on other sites
Tharwat

Another.

 (or (and (setq p (vl-position t (mapcar '(lambda (x) (< (- dist1 40) x)) (setq l '(200 400 600 800 1000)))))
          (princ (strcat "\nI am less than " (itoa (nth p l))))
          )
     (princ "\nI am greater than 1000.")
     )

Share this post


Link to post
Share on other sites
hanhphuc
I remember writing some findrange subfoo and thought its cool 8)

 

findrange accepts nil ! nice :thumbsup:

 

simplified version - it does not support nil (minimal tested)

(cadar (vl-remove-if-not '(lambda (x) (= 1 (vl-position 0 (vl-sort-i (cons n (car x)) '<) ))) l)
)

 

 

A variant using vl-some

Another.

:thumbsup: better variant as expected !

Share this post


Link to post
Share on other sites
Tharwat
:thumbsup: better variant as expected !

 

Thank you. :)

Share this post


Link to post
Share on other sites
Grrr
findrange accepts nil ! nice :thumbsup:

 

Thanks han,

you can provide arguments like

'(
 ((nil 30) "I'm below or equal to thirty") 
 ((40 nil) "I'm above or equal to forty") 
 (nil "I'm something else, in this case between 30-40 (exclusive)")
)

Share this post


Link to post
Share on other sites
TunzaGibbo

Thank you all for the help.

The "cond" now works fine

 

I'm puzzled with the command "copym" It works on the command line but not in lisp.

Does anyone know why this may be because this command suits my purpose perfectly.

 


(defun c:ww (/ P1 P2 P3 P4 Ang1 Dist1 Dist2 hole1a hole1b Copies) 
   (savevartoold)
     (setq P1 (getpoint "\n Select a Point  : "))
     (setq P2 (getpoint P1 "\n Select a Point  : "))
     (setq Dist1 (distance P1 P2))
     (setq Dist2 (- Dist1 40.0))
     (setq Ang1 (angle P1 P2))
     (setq P3 (polar P1 (dtr 0) 20))
     (setq P4 (polar P3 (dtr 0) Dist2))

   (cond
     ((and (> Dist2  1)      (< Dist2 200))  (setq Copies 1)) 
     ((and (> Dist2  200)  (< Dist2 400))  (setq Copies 2))
     ((and (> Dist2  200)  (< Dist2 600))  (setq Copies 3))
     ((and (> Dist2  200)  (< Dist2 800))  (setq Copies 4))
     ((and (> Dist2  200)  (< Dist2 1000)) (setq Copies 5))
     ((and (> Dist2  200)  (< Dist2 1200)) (setq Copies 6))
   )                  
   (setvar "osmode" 0)
     (command "_.circle" (polar P1 (+ ang1 (dtr 0)) 20.0) "D" 3.2 "" ) 
     (setq hole1a (entlast))
     (command "copym" hole1a "" P3 "d" P4 Copies "")

Share this post


Link to post
Share on other sites
hanhphuc
Thank you all for the help.

The "cond" now works fine

I'm puzzled with the command "copym" It works on the command line but not in lisp.

Does anyone know why this may be because this command suits my purpose perfectly.

 

1. IMO, in your case no needs cond , suggestions colored

2. (c:copym) ; is a command of express tool not a common command

3. (dtr 0) = 0

 

(defun c:ww (/ P1 P2 P3 P4 Ang1 Dist1 Dist2 hole1a hole1b Copies)
(savevartoold) 

(if
 (and (setq P1 (getpoint "\n Select a Point  : "))
      (setq P2 (getpoint P1 "\n Select a Point  : "))
      (setq Dist1  (distance P1 P2)
   Dist2  (- Dist1 40.0)
   Ang1	  (angle P1 P2)
   P3	  (polar P1 [color="red"]0.0[/color] 20.)
   P4	  (polar P3 [color="red"]0.0[/color] Dist2)
   [b]Copies[/b] [color="red"](if (< 0.0 Dist2 1200.)
	    (1+ (fix (/ Dist2 200.)))
	    ) [/color]
   )
       [color="red"](setq hole1a (entmakex (list '(0 . "CIRCLE") (cons 10 (polar P1 (+ ang1 0.) 20.0)) '(40 . 3.2))))[/color]
      )

[color="red"]          (vl-cmdf "-array" "_non" hole1a "" "r" 1 Copies (/ (distance p3 p4) Copies)) [/color][color="green"]; like this??[/color]
      [color="green"];;;     (acet-copym-divide (ssadd hole1a) p3 p4 copies) ;express tool [/color]

 (alert [color="purple"]"\nLimits! 0 < x < 1200 !!"[/color])

 )

 (princ)
 ) 

 

p/s: im not familiar with 'command' call , "_non" instead of (setvar 'osmode 0) not tested. i prefer vla-arrayrectangular

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×