Author Topic: Reset variables  (Read 5265 times)

0 Members and 1 Guest are viewing this topic.

MvdP

  • Guest
Reset variables
« on: June 21, 2006, 02:25:25 AM »
This a small part off  a hatching routine i have.And i have a small problem with this.The problem is as follows.The routine prompt twice to select a line if you follow the procedure it works OK but when i do cancel (esc key)after selecting the first line and continue drawing and call this routine again it remmembers the first line i selected.Is there a way and if so how can this be programmed so that when i cancel it will forget what i have selected.

Code: [Select]
(defun sl (/)
(while (/= www 1)
  (progn
(princ "\nCommand:")
(setvar "highlight" 1)
(setq e (car (entsel "Select 1st line......:  ")))
(setq e1 (entget e))
(setq qqq (cdr (assoc 0 e1)))
(if (/= qqq "LINE")
(print "Sorry no line selected.!!")
)
(if (= qqq "LINE")
(setq www 1))
  ))
(while (/= vvv 1)
  (progn
(setq f (car (entsel "Select 2nd Line....:  ")))
(setq f1 (entget f))
(setq ppp (cdr (assoc 0 f1)))
(if (/= ppp "LINE")
(print "Sorry no line selected.!!")
)
(if (= ppp "LINE")
(setq vvv 1))
  )
(princ)
)
(setq pt1 (cdr (assoc 10 (entget e))))
(setq pt2 (cdr (assoc 11 (entget e))))
(setq pt3 (cdr (assoc 10 (entget f))))
(setq pt4 (cdr (assoc 11 (entget f))))

(setq vvv nil)
(setq www nil)
)

Serge J. Gianolla

  • Guest
Re: Reset variables
« Reply #1 on: June 21, 2006, 03:13:42 AM »
(setq vvv nil)
(setq www nil)
)

If you abort the routine before the end of it, these values too are not reset. You could have your error trapping doing it for you but too involved when you could ... read up on global and local variables :laugh:

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Reset variables
« Reply #2 on: June 21, 2006, 04:11:16 AM »
Make the variables local to the routine ; ie ..

(defun sl ( / www vvv etc...)
...... cont >> ..
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MvdP

  • Guest
Re: Reset variables
« Reply #3 on: June 21, 2006, 04:50:12 AM »
OK gentlemen thanks for the help.
Got it working.Found enough stuff about this on afralisp.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Reset variables
« Reply #4 on: June 21, 2006, 05:19:53 AM »
This a small part off  a hatching routine i have.And i have a small problem with this.The problem is as follows.The routine prompt twice to select a line if you follow the procedure it works OK but when i do cancel (esc key)after selecting the first line and continue drawing and call this routine again it remmembers the first line i selected.Is there a way and if so how can this be programmed so that when i cancel it will forget what i have selected.


My variant programs of a select of LINE...

Code: [Select]
(defun test (i / GR L)
            ;|
   Argument:
   Amount test LINE objects
  
   Return:
  '(("start point first LINE objects" "End point first LINE objects")
      ("start point second LINE objects" "End point second LINE objects")
      ........................
      ("start point N LINE objects" "End point N LINE objects"))
      |;
 (if (> i 0)
  (progn
   (prompt "\nSelect LINE objects: ")
   (while (and (not (VL-CATCH-ALL-ERROR-p (setq gr (VL-CATCH-ALL-APPLY 'grread '(t)))))
               (= (car gr) 5)
          ) ;_  and
    (if l
     (redraw l 4)
    ) ;_  if
    (if (and (setq gr (osnap (cadr gr) "_nea"))
             (setq l (ssget gr '((0 . "LINE"))))
             (setq l (ssname l 0))
        ) ;_  and
     (redraw l 3)
    ) ;_  if
   ) ;_  while
   (redraw l 4)
   (if (listp gr)
    (cons
     (list (cdr (assoc 10 (entget l))) (cdr (assoc 11 (entget l))))
     (test (1- i))
    ) ;_  cons
   ) ;_  if
  ) ;_  progn
 ) ;_  if
) ;_  defun

;; Example:
(test 2) ; For two LINE
(test 3); For three LINE

>Modify
 "/nSelect LINE objects: " =>  "\nSelect LINE objects: "
« Last Edit: June 22, 2006, 01:47:17 AM by ElpanovEvgeniy »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Reset variables
« Reply #5 on: June 21, 2006, 07:22:25 AM »
Neat solution Elpanov.  :-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Reset variables
« Reply #6 on: June 21, 2006, 07:22:38 AM »
Here is a simple routine that will do what you want.
Although it can be improved upon it does get the job done as an example.
Please note how the progn is used to control the exit of the while loop.
Actually the (progn is not needed because the (if is the only statement
but if you had additional code to use the progn would encapsulate it and
the last thing returned in the code would be sent to the WHILE to
determine to exit or not.

Code: [Select]
;;  user to select two lines, no exit except [escape]
;;  returns a list of points (p1 p2 p3 p4)
(defun sl (/ e f)
  (while
    (progn
      (if
        (not
          (and
            (setq e (entsel "\nCommand: Select 1st line......:  "))
            (= (cdr (assoc 0 (entget (car e)))) "LINE")
          )
        )
         (print "Sorry no line selected.!!")
         nil
      )
    )
  )

  (while
    (progn
      (if
        (not
          (and
            (setq f (entsel "\nSelect 2nd line......:  "))
            (= (cdr (assoc 0 (entget (car f)))) "LINE")
            (not (eq (car e) (car f)))
          )
        )
         (print "Sorry no line selected.!!")
         nil
      )
    )
  )

  (if (and e f)
    (list (cdr (assoc 10 (entget (car e))))
          (cdr (assoc 11 (entget (car e))))
          (cdr (assoc 10 (entget (car f))))
          (cdr (assoc 11 (entget (car f))))
    )
  )
)
« Last Edit: June 21, 2006, 07:26:10 AM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Reset variables
« Reply #7 on: June 21, 2006, 07:36:03 AM »
While we are at it, take a look at this bit of code:
Code: [Select]
  (if (and e f)
    (list (cdr (assoc 10 (entget (car e))))
          (cdr (assoc 11 (entget (car e))))
          (cdr (assoc 10 (entget (car f))))
          (cdr (assoc 11 (entget (car f))))
    )
  )
 
  Because it is the last thing in the (sl subroutine what ever it returns, your subroutine will
  also return that value. So if either e or f is nil the AND will return nil and the (if will return
  nil, then your subroutine will return nil.
  But, if there is a value in e AND f the list will be created and that will be returned to all. :)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MvdP

  • Guest
Re: Reset variables
« Reply #8 on: June 21, 2006, 08:08:27 AM »
Thanks ElpanovEvgeniy  and CAB,but your info is way to hard for me (as a newbie in lisp)  to understand.
I tried CAB's code and it is not working for me.It gives an error

Command: Select 1st line......:
Select 2nd line......:  ; error: bad argument type: lentityp (<Entity name:
7e278b40> (6166.51 7987.51 0.0))


So i will stick with the code i have now ,and the error trapping i got now is not so sophisticated but working.
But i see a line CAB's code that i think is very usefull.I think it is for not selecting the same line twice.


Code: [Select]
(not (eq (car e) (car f)))

I tried it in my code but it is not working .It is immediately going to *error* How can this be solved..and prompted on the commandline that the second line is the same as the first one.


Code: [Select]

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; error handling
(defun trap1 (errmsg)
(setq vvv nil)
(setq www nil)
(setq e nil)
(setq f nil)
(setvar "clayer" cla)
(setvar "snapang" sna)
(setq *error* temperr)
(prompt "\nResetting System Variables.")
   (princ)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; error handling

(defun sl (/ www e e e1 qqq vvv f f1 ppp)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; error handling

(setq temperr *error*)
(setq *error* trap1)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; error handling
 
(while (/= www 1)
  (progn
(princ "\nCommand:")
(setvar "highlight" 1)
(setq e (car (entsel "\nSelect 1st Line......:  ")))
(setq e1 (entget e))
(setq qqq (cdr (assoc 0 e1)))
(if (/= qqq "LINE")
(princ "\nSorry no line selected.!!")
)
(if (= qqq "LINE")
(setq www 1))
  ))
(while (/= vvv 1)
  (progn
(setq f (car (entsel "\nSelect 2nd Line......:  ")))
;;        (not (eq (car e) (car f)))
(setq f1 (entget f))
(setq ppp (cdr (assoc 0 f1)))
(not (eq (car e) (car f)))  ;<-------here
(if (/= ppp "LINE")
(princ "\nSorry no line selected.!!")
)
(if (= ppp "LINE")
(setq vvv 1))
  )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; error handling

(setq *error* temperr)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; error handling

(princ)
)
(setq pt1 (cdr (assoc 10 (entget e))))
(setq pt2 (cdr (assoc 11 (entget e))))
(setq pt3 (cdr (assoc 10 (entget f))))
(setq pt4 (cdr (assoc 11 (entget f))))

(setq vvv nil)
(setq www nil)
)

btw the points (pt1 - pt4) are used in the next section in my code...


Code: [Select]
(defun pl1 ()
     (setq hk0 (angle pt1 pt2))
     (setq hk1 (angle pt3 pt4)
           hk2 (+ hk1 pi)
     )
     (if (> (cos (- hk0 hk1)) 0)
         (setq l1 (list pt4 pt3))
         (setq l1 (list pt3 pt4))
     )
)



« Last Edit: June 21, 2006, 08:33:47 AM by MvdP »

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Reset variables
« Reply #9 on: June 21, 2006, 08:16:45 AM »
Hi Alan!

Good and clear code!
Of your variant will take advantage more likely... :)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Reset variables
« Reply #10 on: June 21, 2006, 09:21:07 AM »
Thank you, Elpanov.

MvdP
I can't explain the error you got with my code. It is working for me.

Here is the code you asked about, but what happens if the user hits the ENTER key before picking a point?

Code: [Select]
(while
  (progn
    (setq f (car (entsel "\nSelect 2nd Line......:  ")))
    (setq f1 (entget f))
    (setq ppp (cdr (assoc 0 f1)))
    (if (or (/= ppp "LINE") (eq e f))
      (princ "\nSorry no line selected.!!")
      nil
    )
  )
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MvdP

  • Guest
Re: Reset variables
« Reply #11 on: June 21, 2006, 09:41:48 AM »
Thanks CAB that code worked.And for the rest of your answers i will get back to you later.

MvdP

  • Guest
Re: Reset variables
« Reply #12 on: June 21, 2006, 11:29:02 AM »
Can the code be spilt in two (if selected the same or not selected a line) like this.


Code: [Select]
(while
 (progn
    (setq f (car (entsel "\nSelect 2nd Line......:  ")))
    (setq f1 (entget f))
    (setq ppp (cdr (assoc 0 f1)))
    (if  (/= ppp "LINE")       (princ "\nSorry no line selected.!!"))
    (or (/= ppp  (eq e f)    (princ "\nSame line selected.....!!")))


  )
)
Or is cond the way to do this
« Last Edit: June 21, 2006, 11:37:51 AM by MvdP »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Reset variables
« Reply #13 on: June 21, 2006, 01:03:31 PM »
There are many ways to construct the function.
This is one way.
Code: [Select]
(while
  (progn
    (setq f (car (entsel "\nSelect 2nd Line......:  ")))
    (setq f1 (entget f))
    (setq ppp (cdr (assoc 0 f1)))
    ;;  return true to stay in the loop
    (or
      ;;  if 1st compare is true the second is true, so returns true
      ;;  this satisfies the OR which returns True and stay in the loop
      (and (/= ppp "LINE") (print "Sorry no line selected.!!"))
      (and (eq e f) (print "Same line selected.....!!"))
    )
  )
)
« Last Edit: June 21, 2006, 01:07:10 PM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Reset variables
« Reply #14 on: June 21, 2006, 01:09:08 PM »
Here it is with the Condition, which may be more readable.
Code: [Select]
(while
  (progn
    (setq f (car (entsel "\nSelect 2nd Line......:  ")))
    (setq f1 (entget f))
    (setq ppp (cdr (assoc 0 f1)))
    ;;  return true to stay in the loop
    (cond
      ((/= ppp "LINE") (print "Sorry no line selected.!!"))
      ((eq e f) (print "Same line selected.....!!"))
    )
  )
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.