Jump to content

Recommended Posts

Posted (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 by Isaac26a
Posted

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.

  • Like 1
Posted
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.

Posted
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?

Posted
2 minutes ago, Isaac26a said:

What would be an invalid layer name?, and what do you suggest to verify it?

Take a look HERE.

Posted
29 minutes ago, ronjonp said:

Take a look HERE.

Thanks, very helpful.

Posted (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 by mhupp
  • Like 1
Posted
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.

Posted

Just me I would use underscore or minus rather than a space in layer names. 

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...