TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: JohnK on October 06, 2003, 01:09:20 PM

Title: Getting infinate points
Post by: JohnK on October 06, 2003, 01:09:20 PM
I need a second eye on this. It is KILLING me! I want a way to get as many points as the user wants but i cant seem to get it all to work out the way i want it to.  This one will work, but im getting a headache. Can anyone else see anything i could improve on.  (Oh i think i should mention that im also construction a pointlist and counter to go along with this too. I wanted to be able to do something to the pointlist... so that is why those exist. )

Code: [Select]
(defun getpoints (pt)
  (if (= cntr nil) (setq cntr 1))
  (cond
    ((/= pt nil)
     (setq ans (getpoint pt "\nSpecify point:"))
      (setq PointList (cons pt PointList))
      (setq cntr (1+ cntr))
      (grdraw pt ans 2))
    ((not pt)
     (setq ans (getpoint "\nSpecify point:"))
      (setq PointList (cons ans PointList))
      (setq cntr (1+ cntr)))
    )
  ans
 )

(defun startpoints (x)
  (cond
    ((not x)
     (setq x (getpoints (setq x (getpoints nil))))
     (startpoints x))
    ((/= x nil)
     (setq x (getpoints x))
     (startpoints x))
   )
  (princ)
 )

Start the example like this: (startpoints nil)
Title: Getting infinate points
Post by: daron on October 06, 2003, 02:05:24 PM
Okay, thinking out loud. You may have already found these issues.
First, grdraw if passed nil to ans, crashes. Needs to be set in cond/if:
Code: [Select]
(if (/= ans nil)
(grdraw pt ans 2)
  )

This keeps it from crashing, but now we're stuck in the eternal loop, because ans is the value of nil, which means x is the value of nil and will continue to use ((not x) as true, therefore continue to process. Tried passing ((= ans nil) (command "")) within the cond statement of startpoints, but that starts off ending. Question: What can we do to start ans off non-nil? Here's the code as the brainstorming I did. Beware:
Code: [Select]
(defun getpoints (pt)
     (if (= cntr nil)
 (setq cntr 1)
     )
     (cond
 ((/= pt nil)
  (setq ans (getpoint pt "\nSpecify point:"))
  (setq PointList (cons pt PointList))
  (setq cntr (1+ cntr))
  (if (/= ans nil)
(grdraw pt ans 2)
  )
 )
 ((not pt)
  (setq ans (getpoint "\nSpecify point:"))
  (setq PointList (cons ans PointList))
  (setq cntr (1+ cntr))
 )
     )
     ans
)

(defun startpoints (x)
     (cond
 ((= ans nil)
  (command "")
 )
 ((not x)
  (setq x (getpoints (setq x (getpoints nil))))
  (startpoints x)
 )
 ((/= x nil)
  (cond ((/= ans nil)
 (setq x (getpoints x))
 (startpoints x)
)
((= ans nil)
 (command "")
)
  )
 )
     )
     (princ)
)
Title: Getting infinate points
Post by: Mark on October 06, 2003, 03:43:03 PM
you lost me at
Code: [Select]
(getpoints (setq x (getpoints nil)))
Title: Getting infinate points
Post by: JohnK on October 06, 2003, 03:54:09 PM
Ok im going nuts! I got it tweeked down a bit.  :?

Here run this. (getpoints nil)
Code: [Select]
(defun getpoints (pt)
  (if (= cntr nil) (setq cntr 1))
  (cond
    ((and pt)
     (setq ans (getpoint pt "\nSpecify point:"))
     (setq PointList (cons pt PointList))
     (setq cntr (1+ cntr))
     (grdraw pt ans 2)
     (getpoints ans))
    ((not pt)
     (setq ans (getpoint "\nSpecify point:"))
     (setq PointList (cons ans PointList))
     (setq cntr (1+ cntr))
     (getpoints ans))
    )
  ans
 )

The real problem is that it errors out if i cancle and i cant think of a way to get out of it... AHHHH!
Title: Getting infinate points
Post by: SMadsen on October 06, 2003, 04:36:00 PM
Nice touch doing it recursively :)

Until you get it to work, will the stuff below do what you're trying to accomplish?
Code: [Select]
(defun points (/ plst pt p1 sp cnt)
  (cond ((setq pt (getpoint "\nStart point: "))
         (setq plst (cons pt plst)
               p1   pt
               sp   pt
               cnt  2)
        )
  )
  (while pt
    (initget "Close")
    (setq pt (getpoint p1 (strcat "\nGimme point no. " (itoa cnt) ": ")))
    (cond ((vl-consp pt)(grdraw p1 pt 2)
           (setq plst (cons pt plst) p1 pt))
          ((= pt "Close")(grdraw p1 sp 2)
           (setq pt sp plst (cons pt plst) pt nil))
    )
    (setq cnt (1+ cnt))
  )
  (reverse plst)
)
Title: Getting infinate points
Post by: JohnK on October 06, 2003, 04:36:59 PM
BLAM![/i]

Code: [Select]
(defun getpoints (pt)
  (if (= cntr nil) (setq cntr 1))
  (cond
    ((and pt)
     (setq ans (getpoint pt "\nSpecify point:"))
     (if (and ans)
       (progn
         (setq PointList (cons ans PointList))
         (setq cntr (1+ cntr))
         (grdraw pt ans 2)
         (getpoints ans)
        )
       )
     )
    ((not pt)
     (setq ans (getpoint "\nSpecify point:"))
     (if (and ans)
       (progn
         (setq PointList (cons ans PointList))
         (setq cntr (1+ cntr))
         (getpoints ans)
        )
       )
     )
   )
 pointlist
)
Title: Getting infinate points
Post by: JohnK on October 06, 2003, 04:37:37 PM
Oh i didnt see you snuck in there Stig. LOL.

...brb Ill look.
Title: Getting infinate points
Post by: JohnK on October 06, 2003, 04:40:59 PM
wow! :shock: Man, i like that.

OBTW, that is perfect.
Title: Getting infinate points
Post by: daron on October 06, 2003, 04:46:34 PM
Same in both cases. It errors out if I hit enter. The break point is on grdraw. So, what that's telling me is that you're trying to pass the ans variable as nil to grdraw. I'm guessing this can't be done. Also, (getpoints ans) seems to need an if statement like reversing the pointlist and seeing if the last element in the list was nil to keep it from cycling through getpoints again.

Okay, nevermind, I see you and Stig have worked this out.
Title: Getting infinate points
Post by: SMadsen on October 06, 2003, 04:56:53 PM
Se7en, how about just varying the getpoint function on account of pt. Of course, you'll have to handle both cntr and PointList from the calling code:

Code: [Select]
(defun getpointy (pt / ans)
  (if (= cntr nil)
    (setq cntr 1)
  )
  (cond ((setq ans (if pt (getpoint pt "\nSpecify point: ")
                          (getpoint "\nSpecify point: ")))
         (cond ((and ans pt)
                (setq PointList (cons pt PointList)
                      cntr      (1+ cntr))
                (grdraw pt ans 2)
               )
         )
         (getpointy ans)
        )
  )
  PointList
)
Title: Getting infinate points
Post by: JohnK on October 06, 2003, 05:15:41 PM
:shock:  :lol:  ...Now why didnt i think of that!?  :?

That's awesome! Thank you Stig.
Title: Getting infinate points
Post by: SMadsen on October 06, 2003, 05:19:08 PM
Wait, it throws away the last point! That's no good.

Fixed (I hope):
Code: [Select]
(defun getpointy (pt / ans)
  (if (= cntr nil)
    (setq cntr 1)
  )
  (cond ((setq ans (if pt (getpoint pt "\nSpecify point: ")
                          (getpoint "\nSpecify point: ")))
         (setq PointList (cons ans PointList))
         (cond ((and ans pt)
                (setq cntr (1+ cntr))
                (grdraw pt ans 2)
               )
         )
         (getpointy ans)
        )
  )
  (reverse PointList)
)
Title: Getting infinate points
Post by: JohnK on October 06, 2003, 06:06:40 PM
Ah, thank you.  Do you realise how nuts this was making me today. I couldnt stop thinking about it all day. :P  Thank you.
Title: Getting infinate points
Post by: SMadsen on October 06, 2003, 06:26:07 PM
Yeah, I think we all know those problems that just get stuck and drive one nuts.

Came to think of a small thing: what are you using the counter for? Can't you just count the number of points afterwards? It would make the code a bit clearer.

Code: [Select]

(defun getPoints (/ PointList cntr)
  (defun getpointy (pt / ans)
    (cond ((setq ans (if pt (getpoint pt "\nSpecify point: ")
                            (getpoint "\nSpecify point: ")))
           (setq PointList (cons ans PointList))
           (if pt (grdraw pt ans 2))
           (getpointy ans)
          )
    )
    (reverse PointList)
  )

  (setq PointList (getpointy nil)
          cntr  (length PointList))
)
Title: Getting infinate points
Post by: JohnK on October 06, 2003, 06:34:33 PM
Good idea! That makes a whole lot more sence dosent it?! (why keep track of two things when all you have to handle is one.)

Man Stig, your on fire!