Author Topic: Lisp sometimes generates errors  (Read 2748 times)

0 Members and 1 Guest are viewing this topic.

Dave20165

  • Guest
Lisp sometimes generates errors
« on: June 13, 2011, 11:52:00 AM »
Hello everyone,

I've been using a lisp routine for many years now, I'm not sure who the author is as it was already on the company's server when I started working here.  The purpose of the lisp routine is to create a .txt file that can be imported into a spreadsheet which shows the location of a sanitary lateral on a sanitary main, the lot number the lateral services, the length of the lateral, the distance from the end of the lateral to the furthest point of the building and the basement floor elevation.

So it goes like this:
Select sewer line near downstream node point
Enter downstream manhole #
Enter upstream manhole #
Pick lateral near its end
Pick farthest house corner from lateral
Pick basement elevation text
Enter lateral number
then repeat.......

The problem I run into, only sometimes, is that when I pick the basement text, the command dies and says
"bad argument type: 2D/3D point: nil"

I've gone through everything I can think of and can't seem to find a pattern.  Does anyone know of anything in this code that might make it act up?  Thanks.


(setq *errorbak* *error*)
(defun *error* (msg)
   (if
   (or
      (= msg "Function cancelled")
      (= msg "quit / exit abort")
      (= msg "*Cancel*")
      (= msg "console break")
   )
   (progn
      (setq *error* *errorbak*)
      (close latfile)
      (princ)
   ) ; CLOSE PROGN
   (progn
      (setq *error* *errorbak*)
      (prompt msg)
   )
   ) ; CLOSE IF
)
  (setq tempvar 0)
  (setq s 0)
  (setq answer (getstring "\n<A>ppend or <O>verwrite? "))
  (if (or (= answer "A") (= answer "a"))
      (setq dfil (getfiled "Open Lateral File" "" "txt" 2))
      (progn
            (setq dfil (getfiled "Create New Lateral File" "" "txt" 1))
            (setq latfile (open dfil "w"))
       (close latfile)
      )
  )
  (while (= tempvar 0)
         (progn
              (setq s 0)
              (setq nodept (getpoint "\nSelect sewer line near downstream node point:"))
              (if (= nodept nil)
                  (setq tempvar 1)
                  (progn
            (setq lendata (entget (car (nentselp nodept))))
            (setq len (distance (cdr (assoc 11 lendata)) (cdr (assoc 10 lendata))))
            (setq lowpt (osnap nodept "end"))
            (if (<= (distance lowpt (cdr (assoc 11 lendata))) 0.05) (setq hipt (cdr (assoc 10 lendata))) (setq hipt (cdr (assoc 11 lendata))))
            (if (not dt_apisrfptelev)
            ()
            (PROGN (setq hiptelev (dt_apisrfptelev hipt))
                   (setq lowptelev (dt_apisrfptelev lowpt))
            )
            )
            (if (not lowptelev) (setq lowptelev (list 0.0 0.0 0.0)))
            (if (not hiptelev) (setq hiptelev (list 0.0 0.0 0.0)))
                        (setq downnode (getstring "\nEnter downstream manhole #: "))
                        (setq upnode (getstring "\nEnter upstream manhole #: "))
                        (setq nodepoint (osnap nodept "end"))
                        (while (= s 0)
                               (progn
                                     (setq plinedat nil)
                                     (setq latent (entsel "\nPick lateral near its end:"))
                                     (if (= latent nil)
                                         (setq s 1)
                                         (progn
                                               (setq latdata (entget (car latent)))
                                               (if (= (cdr (assoc 0 latdata)) "POLYLINE")
                                                   (progn
                                                         (command "explode" latent)
                                                         (setq latentnew entlast)
                                                         (setq latdata (entget (latentnew)))
                                                         (setq plinedat 1)
                                                   )
                                               )
                                               (setq farpoint (getpoint "\nPick farthest house corner from lateral:"))
                                               (setq baseelev (entsel "\nPick basement elevation text:"))
                                               (setq basedata (entget (car baseelev)))
                      (setq count 1)
                  (while (<= count (strlen (cdr (assoc 1 basedata))))
                     (setq test (substr (cdr (assoc 1 basedata)) count count))
                     (if (= test "0")
                        (progn
                           (setq counter count)
                           (setq count (+ 1 (strlen (cdr (assoc 1 basedata)))))
                        )
                        (progn
                           (if (/= (atof test) 0.0)
                              (progn
                                 (setq counter count)
                                 (setq count (+ 1 (strlen (cdr (assoc 1 basedata)))))
                              ) ;progn
                              (setq count (+ count 1))
                           ) ;IF
                        ) ;progn
                     ) ;if
                  ) ;while
                                               (setq belev (substr (cdr (assoc 1 basedata)) counter (strlen (cdr (assoc 1 basedata)))))
                                               (if (< (distance (osnap (cadr latent) "end") (cdr (assoc 10 latdata))) 0.05)
                                                   (setq latsta (distance (cdr (assoc 11 latdata)) nodepoint))
                                                   (setq latsta (distance (cdr (assoc 10 latdata)) nodepoint))
                                               )
                                         (setq latsta (atof (rtos latsta 2 2)))
                                         (setq latlen (distance (cdr (assoc 10 latdata)) (cdr (assoc 11 latdata))))
                                         (setq distfar (distance (osnap (cadr latent) "end") farpoint))
                                         (setq latnum (getstring "\nEnter lateral number:"))
                                         (setq linetext (strcat latnum "," downnode ",@," upnode "," (rtos latsta 2 0) "," (rtos latlen 2 0) "," belev "," (rtos distfar) "," (rtos len) "," (rtos (caddr lowptelev)) "," (rtos (caddr hiptelev))))
(setq latfile (open dfil "a"))
                                         (write-line linetext latfile)
(close latfile)
                                         (if (= plinedat 1)
                                             (command "pedit" (latentnew) "y" "")
                                             ()
                                         )
                                         )
                                     )
                               )
                        )
                  )
              )
         )
  )
  (setq s nil downnode nil upnode nil nodept nil nodepoint nil plinedat nil latent nil latdata nil latentnew nil farpoint nil)
  (setq baseelev nil basedata nil latsta nil latlen nil distfar nil latnum nil linetext nil)
(setq *error* *errorbak*)

Dave20165

  • Guest
Re: Lisp sometimes generates errors
« Reply #1 on: June 13, 2011, 12:18:10 PM »
Also, if anyone knows how to edit this lisp so that it would ask me to manually enter the basement floor elevation instead of select it, I would be grateful.

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: Lisp sometimes generates errors
« Reply #2 on: June 13, 2011, 12:35:00 PM »
Hi Dave,

Firstly, have a read of this  :wink:

Now, with regards to your code, a few things I see immediately:

  • The program has no function definition, meaning that it has to be reloaded everytime you want to use it, instead of typing a command at the command-line.
  • Because of the above, all the variables are manually set to nil at the end of the code - this is bad practice.
  • You are missing a few subfunctions, such as: 'dt_apisrfptelev' and 'latentnew'
  • Judging by your error there is very few checks for user input, i.e. a function was expecting a point but received 'nil'

That's only after a quick glance over the code, I haven't looked too much into it

Dave20165

  • Guest
Re: Lisp sometimes generates errors
« Reply #3 on: June 13, 2011, 02:36:11 PM »
Quote
Firstly, have a read of this  wink
Thanks I did not realize, will do from now on.

The way this lisp loads is through another .lsp file that contains 100 or so separate .lsp files like this one.  So I load the one lisp file, which loads all the commands to run the lisp files, but does not contain the code for each one......if that makes any sense.....
Main file contains something like this
Code: [Select]
)
(defun c:lat ( / )
  (prompt "\nMacro: Laterals")
  (load (strcat CADDpluslisppath "lat.lsp"))
  (princ)
)

Not sure if that is still considered bad practice but wanted to mention.

Thanks for your response, I'll see if I can make sense of it  ;-)

Edit: Also, is that your bike?

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Lisp sometimes generates errors
« Reply #4 on: June 13, 2011, 02:57:29 PM »
Not sure if that is still considered bad practice but wanted to mention.

It is bad practice in that the variables are not localized. IMO

Your problem is that you missed the text when asked to pick.
Code: [Select]
(setq baseelev (entsel "\nPick basement elevation text:"))You need to wrap that line in a while loop to test for a missed or incorrect pick before allowing the user and the lisp to continue.
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: Lisp sometimes generates errors
« Reply #5 on: June 13, 2011, 03:01:36 PM »
PS The programmer may spend more time filtering & testing the user input than doing the actual lisp.
That is why subroutines are created to filter user input.  The subroutine will filter for missed picks,
Escape key, wrong entity picks, and sometimes user options via key entry.
« Last Edit: June 13, 2011, 04:13:00 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.

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: Lisp sometimes generates errors
« Reply #6 on: June 13, 2011, 03:32:40 PM »
Edit: Also, is that your bike?

Hehe, I wish  :evil:  This is mine  8-)

Dave20165

  • Guest
Re: Lisp sometimes generates errors
« Reply #7 on: June 14, 2011, 09:43:16 AM »
Oh nice, I've have an CBR600 F4.  I'll throw up a pic on your thread.