Jump to content

LISP routine to global edit text on a unique layer


Recommended Posts

Posted

Does anyone know of such an animal? I need to add some characters to all the text on a particular layer in several drawings. I am Googling(sp?) this now but thought I would ask her to see if anyone else has seen such a thing.

 

TIA

 

Sean

  • Replies 23
  • Created
  • Last Reply

Top Posters In This Topic

  • f700es

    7

  • Lee Mac

    6

  • BlackBox

    6

  • alanjt

    3

Top Posters In This Topic

Posted

The best tool I've come across is Lee's Batch Find & Replace Text routine... I do not recall if it allows one to limit the function to a specified layer, or layers (yet).

Posted

Would you be looking for something that would run without any user input (so that it may be run via a script) to add text as a prefix/suffix to all text on a particular layer in a drawing; or something more complex?

Posted
The best tool I've come across is Lee's Batch Find & Replace Text routine...

 

Cheers dude! 8)

 

I do not recall if it allows one to limit the function to a specified layer, or layers (yet).

 

Not yet I'm afraid... but a nice idea!

Posted
Cheers dude! 8)

 

Welcome; I'm trying to earn enough "points" to win a free T-Shirt from the Lee-Mac.com store. :thumbsup:

 

Not yet I'm afraid... but a nice idea!

 

I thought that you might appreciate that, mate.

Posted
Would you be looking for something that would run without any user input (so that it may be run via a script) to add text as a prefix/suffix to all text on a particular layer in a drawing; or something more complex?

 

Yes, I basically need to add %%159 to the end of all text on this particular layer.

Posted

Try to create routine that turn off all layers except for one containing text need to be changed

Find-replace all and done.

Identical text won't get changed on other layers when those layer are off.

Posted

Isolating the layer is the easy part. That doesn't really have to be part of the lisp.

Posted
There are free shirts?

 

Sorry - No - I was only kidding with Lee. I often recommend his routines, tutorials, etc. to others when I feel that it would be helpful. I've learned a lot from his work, and I try to give much credit, where credit is due.

 

Same holds true for my learning from Alan - but he's not put together a website (yet)! :rofl: LoL

 

Yes, I basically need to add %%159 to the end of all text on this particular layer.

 

Depending on how many drawings you have to run through, a simple script (SCR) could do this for you, in which case ScriptPro may be helpful (scroll down).

 

If going through a large number of drawings, then you'd probably want to write a Visual LISP function that iterates through each ObjectDBX Document Object filtering for *Text Objects with the specified Layer Property, then appends the appropriate changes to the TextString Property.

 

HTH

Posted
Same holds true for my learning from Alan - but he's not put together a website (yet)! :rofl: LoL

LoL

Just waiting on my 4yo to one up for me. :P

Posted

It does not have to run multiple drawings at once, just in the current drawing would be fine. A .scr file would be great.

Posted

Haha, found one. AutoNumb. I'll share with the group.

 

Link

 

Sean

Posted

For fun :) ...

 

(defun c:MODT () (c:ModifyText))
(defun c:ModifyText  (/ *error* option string pattern _PutString)
 ;; © RenderMan, 2011
 (princ "\rMODIFYTEXT")
 (vl-load-com)

 (defun *error*  (msg)
   (and oldNomutt (setvar 'nomutt oldNomutt))
   (if acDoc
     (vla-endundomark acDoc))
   (cond ((not msg))                                                   ; Normal exit
         ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
         ((princ (strcat "\n** Error: " msg " ** "))))                 ; Fatal error, display it
   (princ))

 (initget "PREFIX REPLACE SUFFIX")
 (if
   (and
     (setq option
            (getkword "\nEnter option [PREFIX/REPLACE/SUFFIX]: "))
     (/= "" (setq string (getstring "\nEnter text to add: ")))
     (cond
       ((= "PREFIX" option)
        (defun _PutString  (oText)
          (vla-put-textstring
            oText
            (strcat string (vla-get-textstring oText)))))
       ((and (= "REPLACE" option)
             (setq
               pattern
                (getstring
                  T
                  "\nEnter text to replace (case sensitive): ")))
        (defun _PutString  (oText)
          (vla-put-textstring
            oText
            (vl-string-subst
              string
              pattern
              (vla-get-textstring oText)))))
       ((= "SUFFIX" option)
        (defun _PutString  (oText)
          (vla-put-textstring
            oText
            (strcat (vla-get-textstring oText) string))))))
    ((lambda (acDoc oldNomutt / ss)
       (vla-startundomark acDoc)
       (prompt
         "\nSelect entity on desired layer(s) to modify all text: ")
       (setvar 'nomutt 1)
       (while (and (/= nil (setq ss (ssget ":S:E:L")))
                   (cond (pattern
                          (setq ss
                                 (ssget
                                   "_X"
                                   (list
                                     '(0 . "MTEXT,TEXT")
                                     (cons 1 (strcat "*" pattern "*"))
                                     (cons 8
                                           (vla-get-layer
                                             (vlax-ename->vla-object
                                               (ssname ss 0))))))))
                         ((setq ss
                                 (ssget
                                   "_X"
                                   (list
                                     '(0 . "MTEXT,TEXT")
                                     (cons 8
                                           (vla-get-layer
                                             (vlax-ename->vla-object
                                               (ssname ss 0))))))))))
         (vlax-for oText
                   (setq ss (vla-get-activeselectionset acDoc))
           (_PutString oText))
         (vla-delete ss))
       (*error* nil))
      (vla-get-activedocument (vlax-get-acad-object))
      (getvar 'nomutt))
    (progn
      (cond
        (option
         (prompt "\n** Invalid input: Must enter a text string ** "))
        ((prompt "\n** Invalid input: Must select an option ** ")))
      (princ))))

Posted

Bit late to the party but perhaps something like this to call from your script:

 

;; (TextLayerEdit "YourLayer" "Text to Add" <mode>)
;; <mode> = 0  -  replace
;;        = 1  -  prefix
;;        = 2  -  suffix
;; <layer> can use wildcards.

(defun TextLayerEdit ( layer newtext mode / e i s v )
   (if (setq s (ssget "_X" (list '(0 . "TEXT,MTEXT") (cons 8 layer))))
       (repeat (setq i (sslength s))
           (setq e (entget (ssname s (setq i (1- i))))
                 v (assoc 1 e)
           )
           (entmod
               (subst
                   (cons 1
                       (cond
                           (   (= 0 mode) newtext)
                           (   (= 1 mode) (strcat newtext (cdr v)))
                           (   (strcat (cdr v) newtext))
                       )
                   )
                   v e
               )
           )
       )
   )
   (princ)
)

 

Call with name of Layer, Text to Add and relevant mode number:

 

(TextLayerEdit "YourLayerName" "%%159" 2)

 

[ Note that the layer name may use wildcards ]

 

_______________________________________________

 

With all this interest, 'Lee Mac Programming' t-shirts sounds like a lucrative business! :P

Posted

If you're seriously interested, I'll help with the design / branding, and you can setup a free Café Press online store. No up front cost, and the items are made to order.

 

... It's what I did for my local chapter of Engineers Without Borders (EWB), after they asked me to be the Newsletter Editor, and Committee Chair for the graphical enhancements I made to the newsletter. :wink:

Posted

Lee, as for the code... First, succinct as usual, but I am curious... Does the code iterate the mode condition for each entity in the selection set?

Posted
If you're seriously interested, I'll help with the design / branding, and you can setup a free Café Press online store. No up front cost, and the items are made to order.

 

I was also joking with my earlier post, but thanks for the suggestion and kind offer to volunteer your time Renderman.

 

Lee, as for the code... First, succinct as usual, but I am curious... Does the code iterate the mode condition for each entity in the selection set?

 

Thanks Renderman; and yes, you are correct, I sacrificed the efficiency of evaluating (at most) two '=' expressions within the loop for the sake of easy coding; but since you raise the point, here is a possible alternative:

 

;; (TextLayerEdit "YourLayer" "Text to Add" <mode>)
;; <mode> = 0  -  replace
;;        = 1  -  prefix
;;        = 2  -  suffix
;; <layer> can use wildcards.

(defun TextLayerEdit ( layer newtext mode / e f i s v )
   (setq f
       (eval
           (list 'lambda '( s )
               (cond
                   (   (= 0 mode) newtext)
                   (   (= 1 mode) (list 'strcat newtext 's))
                   (   (list 'strcat 's newtext))
               )
           )
       )
   )
   (if (setq s (ssget "_X" (list '(0 . "TEXT,MTEXT") (cons 8 layer))))
       (repeat (setq i (sslength s))
           (setq e (entget (ssname s (setq i (1- i))))
                 v (assoc 1 e)
           )
           (entmod (subst (cons 1 (f (cdr v))) v e))
       )
   )
   (princ)
)

Posted

@Lee : I don't think you need eval list to assign a function ^^ Just (setq f (lambda(s)....)

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...