Isaac26a Posted April 10, 2022 Posted April 10, 2022 (edited) Hi, I was needing to make many texts' content into layers (19) and using the color of the text and created this particular lisp, wich I don't know if it's useful to you, maybe it is, maybe not, so as I haven't seen anything similar here decided to post it. ;;; 20220409 V1.0 Make the selected texts' content into layers with the selected text color ;;; 20220411 V1.1 Code updated, thanks to Tharwat and Mhupp for the comments and functions ;;; Isaac A. (vl-load-com) (defun c:tlay (/ c Drawing e p s) (setq Drawing (vla-get-activedocument (vlax-get-acad-object))) (vla-startundomark Drawing) (setvar "cmdecho" 0) (princ "\nSelect the texts to create layers") (setq p (ssget '((0 . "TEXT,MTEXT")))) (while (= p nil) (princ "\nNo text(s) selected, try again") (setq p (ssget '((0 . "TEXT,MTEXT")))) ) (foreach txt (vl-remove-if 'listp (mapcar 'cadr (ssnamex p))) (setq e (entget txt) s (cdr (assoc 1 e)) c (cdr (assoc 62 e)) ) (if c (vl-cmdf "_.layer" "_m" s "_c" c "" "") (vl-cmdf "_.layer" "_m" s "_c" "7" "" "") ) ) (princ (strcat"\n" (itoa (sslength p)) " layer(s) created")) (vla-endundomark Drawing) (setvar "cmdecho" 1) (princ) ) Good day and happy coding Edited April 11, 2022 by Isaac26a Quote
Tharwat Posted April 10, 2022 Posted April 10, 2022 A few notes if I may. 1- You changed the system variable cmdecho to 0 but you did not reset it back on exit. 2- Suppose the user did not select any text then the sslength function will throw an error. 3- You already set the filter for Text & Mtext so there is no need to check that again in the routine to guarantee the object is *Text. 4- You need to check if the text value is valid for layer name. 5- If any of the text objects' colour was ByLayer then the return of (cdr (assoc 62 ....)) would be nil. 6- Finally the use of rtos is not needed but itoa is enough because you are counting integers. 1 Quote
Isaac26a Posted April 10, 2022 Author Posted April 10, 2022 3 hours ago, Tharwat said: A few notes if I may. 1- You changed the system variable cmdecho to 0 but you did not reset it back on exit. 2- Suppose the user did not select any text then the sslength function will throw an error. 3- You already set the filter for Text & Mtext so there is no need to check that again in the routine to guarantee the object is *Text. 4- You need to check if the text value is valid for layer name. 5- If any of the text objects' colour was ByLayer then the return of (cdr (assoc 62 ....)) would be nil. 6- Finally the use of rtos is not needed but itoa is enough because you are counting integers. Thanks pointing the errors help me grow, I like those kind of anwers. Quote
Isaac26a Posted April 11, 2022 Author Posted April 11, 2022 On 4/10/2022 at 5:07 AM, Tharwat said: You need to check if the text value is valid for layer name. What would be an invalid layer name?, and what do you suggest to verify it? Quote
ronjonp Posted April 11, 2022 Posted April 11, 2022 2 minutes ago, Isaac26a said: What would be an invalid layer name?, and what do you suggest to verify it? Take a look HERE. Quote
Isaac26a Posted April 11, 2022 Author Posted April 11, 2022 29 minutes ago, ronjonp said: Take a look HERE. Thanks, very helpful. Quote
mhupp Posted April 11, 2022 Posted April 11, 2022 (edited) use vl-string-translate convert unwanted charters to space (or some other charter you will not use) and then remove those charters from the string. ;;----------------------------------------------------------------------------;; ;; Removes leading, traling, and double spaces (defun RemoveGaps (str) (while (/= str (setq str (vl-string-subst " " " " str)))) (vl-string-trim " " str) ) Edited April 11, 2022 by mhupp 1 Quote
Isaac26a Posted April 11, 2022 Author Posted April 11, 2022 3 hours ago, mhupp said: use vl-string-translate convert unwanted charters to space (or some other charter you will not use) and then remove those charters from the string. Thanks, I think this is a good solution. Quote
BIGAL Posted April 12, 2022 Posted April 12, 2022 Just me I would use underscore or minus rather than a space in layer names. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.