Jump to content

"IF"


TunzaGibbo

Recommended Posts

(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

Link to comment
Share on other sites

  • Replies 20
  • Created
  • Last Reply

Top Posters In This Topic

  • hanhphuc

    4

  • TunzaGibbo

    4

  • ronjonp

    3

  • Aftertouch

    3

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

Link to comment
Share on other sites

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..")
)
)

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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. ;-)

Link to comment
Share on other sites

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.."))
)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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."))
)

Link to comment
Share on other sites

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:

Link to comment
Share on other sites

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..?

Link to comment
Share on other sites

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.")
)

Link to comment
Share on other sites

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.")
     )

Link to comment
Share on other sites

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 !

Link to comment
Share on other sites

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)")
)

Link to comment
Share on other sites

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 "")

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...