Author Topic: Help with error function  (Read 3830 times)

0 Members and 1 Guest are viewing this topic.

ronjonp

  • Needs a day job
  • Posts: 7531
Help with error function
« on: February 23, 2005, 04:19:17 PM »
I decided to put some error checking (thanks CAB) in my routine but I'm getting this error every after i run the code. Could somebody take a look and see what I am doing wrong?

Code: [Select]
Command:
Error: too many arguments; error: An error has occurred inside the *error*
functiontoo many arguments


Code: [Select]
(defun c:drip (/ P u-plinegen u-clayer u-plinewid)         ;Used for inserting bubbler laterals


;_____________________________
;Error function
;_____________________________

  (defun *error* (msg)
    (if
      (not
        (member
          msg
          '("console break" "Function cancelled" "quit / exit abort" "")
        )
      )
       (princ (strcat "\nError: " msg))
    ) ; if
(setvar 'plinegen u-plinegen)
(setvar 'clayer u-clayer)
(getvar 'plinewid u-plinewid)
    (princ)
  ) ;end error function


;_____________________________
;Function to draw polyline
;_____________________________

(defun drawpline (/)
  (command "_.pline")
  ;;   repeat a point input until Enter
  (if (while (> (getvar "CMDACTIVE") 0)
(command pause)
      )
    (*error* "")
  )
)

;_____________________________
;Get User Variables
;_____________________________

(setq u-plinegen (getvar 'plinegen)
u-clayer   (getvar 'clayer)
u-plinewid (getvar 'plinewid)
)

;_____________________________
;Get User Variables
;_____________________________

  (setvar 'plinegen 1)

  (initget 0 "Tree Shrub Bub Header Cv")      ;Gets T or S or B or H or C
  (if (not *defaultDRIP*)(setq *defaultDRIP* "Shrub"))
  (setq P (cond ((getkword (strcat "\nEnter an option (T)ree, (S)hrub, (B)ub, (H)eader, or (C)v Lateral  <<Hit Enter for " *defaultDRIP* ">>: ")))
  (*defaultDRIP*)))
  (if (not (eq P ""))(setq *defaultDRIP* P))

  (if (= P "Tree")
    (progn

(if (tblsearch "layer" "0280-tree-lateral")
(command "-layer" "thaw" "0280-tree-lateral" "on" "0280-tree-lateral" "")
)

      (setvar 'plinewid (* (getvar "dimscale") 0.025))
      (command "_.-Layer"   "_Make"    "0280-tree-lateral"
          "_L"       "hidden2"    ""         "_Color"
          "4"       ""       ""
         )
   (drawpline)
  )
)
)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Jeff_M

  • King Gator
  • Posts: 4099
  • C3D user & customizer
Help with error function
« Reply #1 on: February 23, 2005, 04:43:35 PM »
I think you want an "s" where you currently have a "g"....
(getvar 'plinewid u-plinewid)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Help with error function
« Reply #2 on: February 23, 2005, 05:00:46 PM »
You could see if these changes suit you too :

Declare the *error* function local ;
Code: [Select]

(defun c:drip (/ *error* p u-plinegen u-clayer u-plinewid)
<... >

revise the drawpline function to remove the (*error* "")
Code: [Select]

  (defun drawpline (/)
    (command "_.pline")
    ;;   repeat a point input until Enter
    (while (> (getvar "CMDACTIVE") 0) (command pause))    
  )


Revise your closing statement to call the *error* routine, passing nil , to clean up your variables.
Code: [Select]

< .. >
  (*error* nil)
  (princ)
)
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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Help with error function
« Reply #3 on: February 23, 2005, 05:44:55 PM »
Good start, take Kerry's advice.
Code: [Select]
(defun c:drip (/ *error* p u-plinegen u-clayer u-plinewid) ;Used for inserting bubbler laterals


 ;_____________________________
 ;Error function
 ;_____________________________
  (defun *error* (msg)
    (if
      (not
        (member
          msg
          '("console break" "Function cancelled" "quit / exit abort" "")
        )
      )
       (princ (strcat "\nError: " msg))
    ) ; if
    (setvar 'plinegen u-plinegen)
    (setvar 'clayer u-clayer)
    (setvar 'plinewid u-plinewid)
    (princ)
  ) ;end error function


 ;_____________________________
 ;Function to draw polyline
 ;_____________________________
  (defun drawpline ()
    (command "_.pline")
    ;;   repeat a point input until Enter
    (while (> (getvar "CMDACTIVE") 0) (command pause))
  )

  (defun ck_layer (lyr ltype color)
     (if (tblsearch "layer" lyr)
       (command "._-Layer" "_Thaw" lyr "_On" lyr "_UnLock" lyr "_Set" lyr "")
       (command "_.-Layer" "_Make" lyr "_L" ltype "" "_Color" color "" "")
     )
  )
 
 ;_____________________________
 ;Get User Variables
 ;_____________________________
  (setq u-plinegen (getvar 'plinegen)
        u-clayer   (getvar 'clayer)
        u-plinewid (getvar 'plinewid)
  )

 ;_____________________________
 ;Get User Variables
 ;_____________________________
  (setvar 'plinegen 1)

  (initget 0 "Tree Shrub Bub Header Cv") ;Gets T or S or B or H or C
  (if (not *defaultdrip*)
    (setq *defaultdrip* "Shrub")
  )
  (setq p (cond
            ((getkword
               (strcat "\nEnter an option (T)ree, (S)hrub, (B)ub, (H)eader, or (C)v Lateral  <<Hit Enter for "
                       *defaultdrip*
                       ">>: "
               )
             )
            )
            (*defaultdrip*)
          )
  )
  ;;  p will never be ""
  (setq *defaultdrip* p)
  (cond
    ((= p "Tree")
     (ck_layer "0280-tree-lateral" "hidden2" "4")
     (setvar 'plinewid (* (getvar "dimscale") 0.025))
     (drawpline)
    )
    ((= p "Shrub")
    )
    ((= p "Bulb")
    )
    ((= p "Header")
    )
    ((= p "Cv")
    )
  ) ; end cond stmt
  (*error* "")
  (princ)
)
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.

ronjonp

  • Needs a day job
  • Posts: 7531
Help with error function
« Reply #4 on: February 23, 2005, 05:45:26 PM »
That was the ticket Jeff :)

Kerry, I'll take a look at your sugestions.

Thank you both.

Ron

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Help with error function
« Reply #5 on: February 23, 2005, 07:27:46 PM »
Food for thought.

With a couple simple functions --

Code: [Select]
;;  retrieve all the values for the list
;;  of system variable names passed. No
;;  hand holding here, if you pass invalid
;;  varnames: rake handle in the face.

(defun getvars ( varNameList )
    (mapcar
       '(lambda (varName)
            (cons varName (getvar varName))
        )
        varNameList
    )
)

;;  attempt to set the system variable
;;  varName to varValue. Trap any errors
;;  like atempting to set clayer to a non
;;  existant layer. Return attempt to set
;;  as boolean value (not used in demo
;;  code below but could be).

(defun setvarp ( varName varValue )
    (null
        (vl-catch-all-error-p
            (vl-catch-all-apply
               '(lambda ()
                    (setvar
                        varName
                        varValue
                    )
                )
            )
        )
    )
)

;;  set the system variables according
;;  to the values in the list. Call our
;;  custom setvarp function to trap
;;  any pebkac errors.

(defun restore ( restoreList )
    (mapcar
       '(lambda (pair)
            (setvarp (car pair) (cdr pair))
        )
        restoreList
    )
    ;;  the following statement for
    ;;  demonstration purposes only
    (progn
        (princ "\nRestored settings:\n\n")
        (princDottedList
            (getvars
                (mapcar 'car restoreList)
            )
        )
    )
)

;;  just a perty printer for the demo

(defun princDottedList ( lst )
    (foreach pair lst
        (princ
            (strcat
                "    "
                (car pair)
                " = "
                (vl-princ-to-string (cdr pair))
                "\n"
            )
        )
    )
)

A demo app (overly wordy logic for illustrative puposes) --

Code: [Select]
(defun c:MyDemoApp ( / *error* restoreList varList )

    ;;  have our error routine
    ;;  call our restore utility

    (defun *error* ( msg )
        (princ (strcat "\n<" msg ">\n"))
        (restore restoreList)
        (princ)
    )

    ;;  save a list of the system variables
    ;;  we intend to muck about with

    (setq varList
       '(
            "cmdecho"
            "plinegen"
            "plinewid"
        )
    )

    ;;  save the system variable values

    (setq restoreList (getvars varList))

    ;;  show existing system variable values

    (princ "\nSettings prior to c:MyDemoApp execution:\n\n")
    (princDottedList restoreList)

    ;;  abuse our system variables

    (setvar "cmdecho"  (abs (1- (getvar "cmdecho"))))
    (setvar "plinegen" (abs (1- (getvar "plinegen"))))
    (setvar "plinewid" (* 100.0 (max 1 (getvar "plinewid"))))

    ;;  show new system variable values

    (princ "\nSettings used by c:MyDemoApp:\n\n")
    (princDottedList (getvars varList))

    ;;  now do something that might chuck a wobbly

    (getstring "\nPress <Enter> to continue, <Esc> to crash: ")

    ;;  if we made it here we didn't chuck a wobbly
    ;;  on the statement above; restore our saved
    ;;  settings explicity

    (princ "\n<Normal exit>\n")
    (restore restoreList)

    ;;  shhhh ...

    (princ)

)


Normal program termination --

Code: [Select]
MyDemoApp<Enter>

Settings prior to c:MyDemoApp execution:

    cmdecho = 0
    plinegen = 1
    plinewid = 0.0

Settings used by c:MyDemoApp:

    cmdecho = 1
    plinegen = 0
    plinewid = 100.0

Press <Enter> to continue, <Esc> to crash: <Enter>

<Normal exit>

Restored settings:

    cmdecho = 0
    plinegen = 1
    plinewid = 0.0

Error termination --

Code: [Select]
MyDemoApp<Enter>

Settings prior to c:MyDemoApp execution:

    cmdecho = 0
    plinegen = 1
    plinewid = 0.0

Settings used by c:MyDemoApp:

    cmdecho = 1
    plinegen = 0
    plinewid = 100.0

Press <Enter> to continue, <Esc> to crash: <Esc> *Cancel*

<Function cancelled>

Restored settings:

    cmdecho = 0
    plinegen = 1
    plinewid = 0.0

Foolproof? No, can't help a user who leans on the <Esc> key but it is better than nothing.

Cheers

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ronjonp

  • Needs a day job
  • Posts: 7531
Help with error function
« Reply #6 on: February 23, 2005, 07:45:33 PM »
Thanks MP...I'll try to absorb all this :)

Ron

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Help with error function
« Reply #7 on: February 23, 2005, 08:04:52 PM »
hehehehe

Is 'chuck a wobbly' the same as 'spit the dummy' , sort of ... ?
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.