- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Good day all,
I am still learning lisp, and how to modify code. Lee Mac has provided a txtinc.lsp from his website and another post (can not recall which forum) that will add/subtract numbers within text. I have already modified the code slightly, and used it. However, recently I found an issue with it and receive an error when a quotation mark (") is included in the selected text.
I.e. Selecting a text with 12" or "13.4 will produce an error message of "Error: malformed string on input" and halt the code where it is, without continuing, and no indication of which items were adjusted.
Ideally I would love to fix the code to work around this, while still having the functionality of working with negative numbers (using the - within text). However I assume that this would be difficult to change/adjust and I would love to take this opportunity to learn how to use error catching to disregard these instances. The code I am working with is below:
;; Text Increment - Lee Mac ;; Increments numerical data found in a selection of Text or MText ;; objects by a value specified by the user. ;;*************************************************************************************************************** ;; Added in repeat for multiple selection, worked with Lee Mac with rounding issue. ;; J.Granata - January 2017 ;;*************************************************************************************************************** (defun c:TEST ( / e i l s x ) (prompt "\n***Select Text To Adjust Elevations****") (if (null *inc*) (setq *inc* -1.1) );;If -set default value to -1.1 if no previous data (if (setq i (getreal (strcat "\nEnter value to change Elevation by: <" (rtos *inc* 2) ">: "))) (setq *inc* i) );;If - get value (if (equal 0.0 (rem *inc* 1) 1e-8) (setq *inc* (fix *inc*)) );;If (if (setq s (ssget "_:L" '((0 . "TEXT,MTEXT") (1 . "*#*")))) (repeat (setq i (sslength s)) (setq e (entget (ssname s (setq i (1- i)))) x (assoc 1 e) ) (entmod (subst (cons 1 (apply 'strcat (mapcar (function (lambda ( x ) (if (numberp x) (LM:num->str (+ x *inc*)) x ) );;Lambda );;Function (LM:splitstring (cdr x)) ) );;Apply );;Cons x e );;Subst );;EntMod );;Repeate );;If (princ);;Exit Cleanly );;End Function ;; Split String - Lee Mac ;; Splits a string into a list of text and numbers (defun LM:splitstring ( s ) ( (lambda ( l ) (read (strcat "(" (vl-list->string (apply 'append (mapcar (function (lambda ( a b c ) (cond ( (= 92 b) (list 32 34 92 b 34 32) ) ( (or (< 47 b 58) (and (= 45 b) (< 47 c 58) (not (< 47 a 58))) (and (= 46 b) (< 47 a 58) (< 47 c 58)) ) (list b) ) ( (list 32 34 b 34 32)) ) ) ) (cons nil l) l (append (cdr l) (list nil)) ) ) ) ")" ) ) ) (vl-string->list s) ) ) ;; Number to String - Lee Mac ;; Converts a supplied numerical argument to a string (defun LM:num->str ( num / dim rtn ) (if (equal num (atoi (rtos num 2 0)) 1e-8) (rtos num 2 0) (progn (setq dim (getvar 'dimzin)) (setvar 'dimzin 8) (setq rtn (rtos num 2 8)) (setvar 'dimzin dim) rtn ) ) ) (princ)
I was thinking of adding in an if statement within the repeat similar to what is below using a combination of Vl-catch-all-apply and vl-catch-all-error-p and then using a princ line at the end of the code with a statement telling how many instances were not included. There is potent in the loop to changing the color to 'highlight' these error instances where the code would not adjust the numbers.
However, I do not know where to add the (setq catchit (vl-catch-all-apply... )) to catch the "Error: malformed string on input". In the below code, I have set up the structure that I though could be used. I have commented out the AutoDesk example for vl-catch-all-apply to test my loop and output.
...... (setq T2a (Cdr T2)) ;;Extracts text item (setq catchit nil) ;;defaults the Catchit back to nil for current loop ;;TEST for Catchit ;(setq catchit (vl-catch-all-apply '/ '(50 0))) ;;Example line to test. need format of (vl-catch-all-apply 'function list) (if (vl-catch-all-error-p catchit) ;;if error catch is not nil (setq ErrorCnt (1+ ErrorCnt)) ;;Adds 1 to error counter, else continue with program. Potentially add code here to change the color to find these instances easier (progn (if (vl-some 'numberp (setq l (LM:SplitString T2a))) ......
)) ;; These right parenthesis are to close the new If and Progn statements at their correct locations
.....
(princ "\n")(princ ErrorCnt)(princ " Items have been skipped")
(princ);;Exit cleanly
);;End Function
Thank you for the help and guidance.
Solved! Go to Solution.