Author Topic: Whats is wrong with this part of code?  (Read 1417 times)

0 Members and 1 Guest are viewing this topic.

HasanCAD

  • Swamp Rat
  • Posts: 1422
Whats is wrong with this part of code?
« on: February 17, 2013, 09:30:02 AM »
Code: [Select]
(setq sp (getpoint "\nSelect start Point")) 
  (while
    (progn     
      (setq np (getpoint "\nSelect next Point"))
      (setq elist (append (list elist) np))
      ))
(setq elist (append sp (list elist)))
(LWPoly elist)


Code: [Select]
(defun LWPoly (lst)
  (entmakex (append (list (cons 0 "LWPOLYLINE")
                          (cons 100 "AcDbEntity")
                          (cons 100 "AcDbPolyline")
                          (cons 90 (length lst))
                          (cons 70 0))
                    (mapcar (function (lambda (p) (cons 10 p))) lst))))
« Last Edit: February 17, 2013, 09:38:34 AM by HasanCAD »

ribarm

  • Gator
  • Posts: 3274
  • Marko Ribar, architect
Re: Whats is wrong with this part of code?
« Reply #1 on: February 17, 2013, 09:44:24 AM »
Code: [Select]
(setq sp (getpoint "\nSelect start Point")) 
  (while
    (progn     
      (setq np (getpoint "\nSelect next Point"))
      (setq elist (append elist (list np)))
      ))
(setq elist (append (list sp) elist))
(LWPoly elist)
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Whats is wrong with this part of code?
« Reply #2 on: February 17, 2013, 09:59:18 AM »
It's probably not ever going to stop. Your while loop is checking if the elist has been assigned something other than nil (in totality). So even if the user presses Enter when asked for the new point, the while check sees that elist becomes (2nd 3rd 4th nil) and asks for the next point again.

I'd have written the code more like this:
Code - Auto/Visual Lisp: [Select]
  1. (if (setq sp (getpoint "\nSelect start point: "))
  2.   (progn (setq elist (list sp) np sp)
  3.     (while (setq np (getpoint np "\nSelect next point: "))
  4.       (setq elist (cons np elist)))
  5.     (LWPoly (reverse elist))))

Edit: And if you don't want the check to see if the user selected the first point. Also not want to use the more efficient cons with only one reverse - instead using numerous lists and appends. Also not wanting the rubber band ... your code should look like this:
Code - Auto/Visual Lisp: [Select]
  1. (setq sp (getpoint "\nSelect start point: "))
  2. (while (setq np (getpoint "\nSelect next point: "))
  3.   (setq elist (append elist (list np))))
  4. (setq elist (append (list sp) elist))
  5. (LWPoly elist)
« Last Edit: February 17, 2013, 10:05:00 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: Whats is wrong with this part of code?
« Reply #3 on: February 17, 2013, 10:10:09 AM »
Thanx all
Working perfect

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Whats is wrong with this part of code?
« Reply #4 on: February 17, 2013, 10:13:12 AM »
The progn is what's causing your issue. I think you mistake some of the other functions for how while works:

The 1st item of while is checked for non-nil. If nil is found, then while skips all its other items and exits to the rest of the code. If non-nil it runs all of its other items, then loops back to check the 1st item again - repeating until the 1st item returns nil. Pseudo code:
Code: [Select]
(while <check>
  <item1>
  <item2>
  ...
  <itemN>
)
;; Returns nil when stopping since the last item evaluated was the <check> when it
;; returned nil. If it never returns nil, while will never stop

The progn runs all of its items always, then simply returns the result of its last item. Pseudo code:
Code: [Select]
(progn
  <Item1>
  <item2>
  ...
  <itemN>
)
;; Returns whatever <ItemN> returned. Doesn't matter if any of the other items returned nil, it will always return the last item evaluated.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.