Author Topic: Prompt user, default option used is crashing  (Read 869 times)

0 Members and 1 Guest are viewing this topic.

tdeleske

  • Mosquito
  • Posts: 20
Prompt user, default option used is crashing
« on: December 18, 2022, 09:09:01 PM »
I am struggling with the default option prompt, if the user types in a value at the prompt it works but if the user chooses enter, and accepts the preset default value it crashes.
it is for labeling a cogo point elevation with annotative mtext, it uses the cannoscale to determine the offset placement.

"Error: bad argument type: stringp 656.285"

lisp is attached, any guidance as to what I may have done wrong would be appreciated.

Regards
Trevor

mhupp

  • Bull Frog
  • Posts: 250
Re: Prompt user, default option used is crashing
« Reply #1 on: December 19, 2022, 02:16:16 AM »
Code - Auto/Visual Lisp: [Select]
  1. (initget "0 1 2 3")
  2. (setq tmp
  3.   (cond
  4.     ((getkword (strcat "\nSet Label Precision [0/1/2/3] <" global:ans ">: "))) (global:ans)
  5.   )
  6. )
  7. (setq global:ans tmp) ;update global:ans for next time command is run or it will always be "2"
  8.  
  9. ;if you use getint you can bypass the if statements and just use x as the 2nd number in rtos in pz
  10. (or (setq x (getint "\nSet Label Precision [0/1/2/3] <2>: ")) (setq x 2))
  11. ....
  12. (setq pz (rtos pz 2 x))
  13.  

It's better to use cond when checking tmp
https://www.afralisp.net/autolisp/tutorials/cond-vs-if.php

Code - Auto/Visual Lisp: [Select]
  1.   ((= tmp "2")               ;put the default answer or most picked to least picked once a cond is met it stops looking
  2.     (setq pz (rtos pz 2 0))  ;if statements will check each one even if its already matched
  3.   )
  4.   ((= tmp "0")
  5.     (setq pz (rtos pz 2 1))
  6.   )
  7.   ((= tmp "1")
  8.     (setq pz (rtos pz 2 2))
  9.   )
  10.   ((= tmp "3")
  11.     (setq pz (rtos pz 2 3))
  12.   )
  13. )

Your also using alot of if's in your error handling. and since they are the first thing being set its kinda unnecessary. Here is also a trick with mapcar to store and recall multiple variables.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:LabelPointElev ()
  2.   (defun *error* (msg)
  3.     (mapcar 'setvar vars vals) ;restore old values
  4.     (princ "\nUser Environment restored.")
  5.     (if (not
  6.           (member msg '( "Function cancelled" "quit / exit abort"))
  7.         )
  8.       (princ (strcat "\nError: " msg))
  9.     )
  10.     (princ)
  11.   )
  12.   (CheckForText)
  13.   (setOffsetYByCannoscale_SetVar_h)
  14.   (setq vars '("CLAYER" "OSMODE" "OSNAPZ" "TEXTSTYLE")  ;list of variables
  15.         vals  (mapcar 'getvar vars)                     ;store old values
  16.   )
« Last Edit: December 19, 2022, 02:27:37 AM by mhupp »

tdeleske

  • Mosquito
  • Posts: 20
Re: Prompt user, default option used is crashing
« Reply #2 on: December 19, 2022, 09:50:01 AM »
Thanks very much for the feedback mhupp, I tried to piece it together but something is not right...

Command: LABELPOINTELEV

Select Cogo Point:
Set Label Precision [0/1/2/3] <1>:

Set Label Precision [0/1/2/3] <2>:

User Environment restored.
Error: bad argument type: numberp: nil*Cancel*


it prompts for precision, I hit enter and it prompts again with a different value, hit enter again, and errors.
I went to last break source and took me to this...      (setq pz (rtos pz 2 x)).

please see the attached update.

tdeleske

  • Mosquito
  • Posts: 20
Re: Prompt user, default option used is crashing
« Reply #3 on: December 19, 2022, 10:03:29 AM »
mhupp, In the first window line 11 has 4 dots, are those supposed to be there?

mhupp

  • Bull Frog
  • Posts: 250
Re: Prompt user, default option used is crashing
« Reply #4 on: December 19, 2022, 10:46:22 AM »
I was saying to use one or the other.  The four dots are to show separation in code.
like now line 67:  (or (setq x (getint (strcat "\nSet Label Precision [0/1/2/3] <" (rtos global:ans 2 0) ">: "))) (setq x global:ans))
...
to line 85: (setq llb (strcat CompileX (rtos (caddr pt) 2 x)))

Here is updated code that uses the 2nd option since you can input it directly into rtos and bypass the if/cond

Code - Auto/Visual Lisp: [Select]
  1. ;***********************************************************************************************
  2. ;Routine to label Point Elevation.                                                              
  3. ;Command function Name: PZ                                                              
  4. ;Written by Jean Marie Vianney on June 3/ 2013.
  5. ;Last Modified on Jan 5th, 2019 by JM
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7.  
  8. ;Revamped routine to prompt the user
  9. ; 1. Precision
  10. ; 2. Show the word "Elev.="
  11. ;Added scale from insertion by cannoscale setting
  12. ; error to handle nil entry an notify the user.
  13. ;_______________________________________________________________________________________________
  14.  
  15. ;****Note: -the routine uses text styles
  16. ;           ML 2.0 Italic
  17. (defun CheckForText (/ *error* bNameLoc cmd)           ;error handling (compliments of Lee Mac)
  18.   (defun *error* (msg)
  19.     (setvar 'CMDECHO cmd)
  20.     ;(command "_.LAYER" "S" cl "") ;curent layer hasn't changed or been set yet in LabelPointElev
  21.     ;(setvar 'clayer (car vals))
  22.     (if (not (member msg '( "Function cancelled" "quit / exit abort")))
  23.       (princ (strcat "\nError: " msg))
  24.     )  ;if
  25.     (princ)
  26.   )
  27.   (load "StealFromTemplate.lsp")
  28.   (if (not (tblsearch "STYLE" "ML 2.0 Italic"))
  29.     (Steal "C:\\MySync\\CAD\\ML_20\\Templates\\ML-C3D20-VO-PR-m.dwt" '(( "Text Styles" ( "*"))))
  30.   )
  31. )
  32.  
  33. (defun setOffsetYByCannoscale_SetVar_h ()
  34.     ;;2022-06-13
  35.     ;;  use cannoscale to calc the insertion point distance from the line
  36.   (setq cannoscale (getvar "cannoscale"))
  37.     ; Example: 1:1000
  38.   (setq slen (strlen cannoscale))
  39.     ; 6 == 1:1000 == 6 characters
  40.   (setq denominator (atof (substr cannoscale 3 slen)))
  41.     ; 1000, string to float, 3rd position from 1, substring of cannoscale
  42.   (setq h (* (/ denominator 1000) 1))
  43.     ; expected value for 1:1000 = 1, 1:100 = 0.1
  44.     ; h is the offset y of the new label
  45. )      ;(Defun setOffsetYByCannoscale_SetVar_h
  46.  
  47. (defun c:LabelPointElev (/ vars vals x rep pt pt2 llb)
  48.    ;Error Function
  49.   (defun *error* (msg)
  50.     (mapcar 'setvar vars vals)  ;restore old values
  51.     (princ "\nUser Environment restored.")
  52.     (if (not (member msg '( "Function cancelled" "quit / exit abort")))
  53.       (princ (strcat "\nError: " msg))
  54.     )  ;if
  55.     (princ)
  56.   )
  57.   (CheckForText)
  58.   (setOffsetYByCannoscale_SetVar_h)
  59.   (setq vars '( "CLAYER" "OSMODE" "OSNAPZ" "TEXTSTYLE")  ;list of variables
  60.         vals (mapcar 'getvar vars)  ;store old values
  61.   )
  62.   (setvar "osmode" 8)
  63.   (setvar "osnapz" 0)
  64.   (or global:ans (setq global:ans 2)) ;if global:ans is nil it will be set to 2
  65.   (or (setq x (getint (strcat "\nSet Label Precision [0/1/2/3] <" (rtos global:ans 2 0) ">: "))) (setq x global:ans))
  66.   (setq global:ans x) ;update for next time
  67.   (initget "Yes No")
  68.   (setq rep
  69.     (cond
  70.       ((getkword "\nShow \"Elev.=\" in the string? [Yes/No] <Yes>: ")) ("Yes")
  71.     )
  72.   )
  73.   (if (eq rep "Yes")
  74.     (setq CompileX (strcat "Elev.= \n"))
  75.     (setq CompileX "")    
  76.   )
  77.   (if (tblsearch "layer" "V-TOPO-ANNO-SPOT")
  78.     (setvar 'clayer "V-TOPO-ANNO-SPOT")
  79.     (progn
  80.       (entmake '((0 . "LAYER") (100 . "AcDbSymbolTableRecord") (100 . "AcDbLayerTableRecord") (2 . "V-TOPO-ANNO-SPOT") (70 . 0) (62 . 7)))
  81.       (setvar 'clayer "V-TOPO-ANNO-SPOT")
  82.     )      
  83.   )
  84.   (while (setq pt (getpoint "\nSelect Cogo Point: "))
  85.     (setq pt2 (list (+ (car pt) (* h 1.5)) (- (cadr pt) 0)))  ;; set offset from insertion point
  86.     (setq llb (strcat CompileX (rtos (caddr pt) 2 x)))
  87.     (entmake (list '(0 . "MTEXT")        
  88.                    '(100 . "AcDbEntity")
  89.                    '(100 . "AcDbMText")
  90.                    '(71 . 4)
  91.                    '(72 . 1)
  92.                    '(7  . "ML 2.0 Italic")
  93.                    '(42 . 0)
  94.                     (cons 10 pt2)
  95.                     (cons 1 llb)
  96.              )
  97.     )
  98.   )
  99.   (mapcar 'setvar vars vals)  ;restore old values
  100.   (princ)
  101. )
« Last Edit: December 19, 2022, 10:51:31 AM by mhupp »

tdeleske

  • Mosquito
  • Posts: 20
Re: Prompt user, default option used is crashing
« Reply #5 on: December 19, 2022, 11:02:20 AM »
mhuff!! great work! that is fantastic!
it's better to prompt the user before selecting any points, thanks for that! it works so much better than it did, no issues.
I can't thank you enough! Kudos sir!

Regards

Trevor

BIGAL

  • Swamp Rat
  • Posts: 1459
  • 40 + years of using Autocad
Re: Prompt user, default option used is crashing
« Reply #6 on: December 21, 2022, 04:23:57 AM »
Comment 1 only loaded if does not exist.

Code: [Select]

  (if (not (tblsearch "STYLE" "ML 2.0 Italic"))
(progn
(load "StealFromTemplate.lsp")
    (Steal "C:\\MySync\\CAD\\ML_20\\Templates\\ML-C3D20-VO-PR-m.dwt" '(( "Text Styles" ( "*"))))
)
  )

Are you using CIV3D labeling COGO points for elevation can be done point by point using inbuilt CIV3D styles.
A man who never made a mistake never made anything