Jump to content

Lisp to change words in an Autocad drawing 2


davidson_cesar

Recommended Posts

Hello!

This lisp below is a lisp to change words in Autocad drawing (in text and mtext). I'd like to know how to modificate this lisp in order to change blocks and/or dimensions as well. Is it possible?:roll:

 

 

 

(vl-load-com)

(setq *QuickFR:Words*
      '(("milk" . "coffee")
        ("water" . "wine")
        ("juice" . "beer"))
     )
(defun str-search (pat str / p)
 (if (setq p (vl-string-search (strcase pat) (strcase str)))
   (substr str (1+ p) (strlen pat))
 )
)
(defun QuickFR (ss / obj item str found changed)
 (vlax-for obj (vla-get-ActiveSelectionSet (vla-get-activedocument (vlax-get-acad-object)))
   (setq str (vla-get-TextString obj) found nil)
   (foreach item *QuickFR:Words*
     (while (setq found (str-search (car item) str))
       (setq str (vl-string-subst (cdr item) found str) changed t)
     )
   )
   (if changed(vla-put-TextString obj str))
 )
)

(defun c:QuickFR (/ ss)
 (if (setq ss (ssget "_:L" '((0 . "TEXT,MTEXT"))))
   (QuickFR ss)
 )
 (princ)
)
(defun c:QuickFR_All (/ ss)
 (if (setq ss (ssget "_X" '((0 . "TEXT,MTEXT"))))
   (QuickFR ss)
 )
 (princ)
)

Edited by SLW210
Put CODE in BOX
Link to comment
Share on other sites

Thanks for replying Pb. But I'd like to improve that lisp that I put above. How can I improve that routine?

 

I never really did look at the code.

my reply was based on the title of the thread :), sorry about that

 

you know what they say, "dont try to re-invent the wheel"

 

I guess it can be improved (that is if there's anything to improve at all), but I understand what you mean :wink:

 

First glance:

An option to look Find "Whole Words Only" would be nice

 

(QuickFR ss T);

 

I'll have a look/see later

Edited by pBe
Link to comment
Share on other sites

So going back to your code

 

to include text on blocks is not that hard, but the question is would you want to include Attributes as well?

 

I would think including dimension text would be a tough one though, unless its an override, retriving the value would be a cinch.

 

> help!!!

 

My suggestion: (questions really)

I noticed that you iterate thru the list (*QuickFR:Words*) for every text/mtext selected, I would make something like a 2 phase string match.

 

1st phase: if nil dont bother going thru the list

2nd phase: if T then do it

 

I would do it this way, before the main routine (a one time thingy), assuming values for *QuickFR:Words* are from a sub routine

 

 (setq phase1 "")
     (foreach v *QuickFR:Words*
       (setq phase1  (strcat phase1 "*" (car v) "*,") ))

 

from your example it will give you "*milk*,*water*,*juice*,"

 

before running

  ...(foreach item *QuickFR:Words*
     (while (setq foun..... 

 

Run Phase 1 test

 (wcmatch "beer on the bottle" phase1);<< -- value of [b]str[/b]

 

Nil: process next object

 

 (wcmatch "milk on the bottle" phase1);<<-- value of [b]str[/b]

 

T: process the object and go thru list

 

its a bit faster (jsut a tad really) especially when you use the ALL option

but thats just me

Edited by pBe
Link to comment
Share on other sites

> help!!!

 

Two ways I can think of...

 

1) Digging through the Anonymous block definition for the Dimension:

(defun LM:GetDimensionString ( dim / dl db ds )
 (if
   (and
     (wcmatch (cdr (assoc 0 (setq dl (entget dim)))) "*DIMENSION")
     (setq db (tblobjname "BLOCK" (cdr (assoc 2 dl))))
   )
   (while (and (setq db (entnext db)) (not ds))
     (if (eq "MTEXT" (cdr (assoc 0 (setq dl (entget db)))))
       (setq ds (cdr (assoc 1 dl)))
     )
   )
 )
 ds
)

2) Grabbing the MText Entity using a nested selection (unreliable if objects overlap the Dimension MText though):

(defun LM:GetDimensionString ( dim / dl mt )
 (if
   (and
     (wcmatch (cdr (assoc 0 (setq dl (entget dim)))) "*DIMENSION")
     (setq mt (car (nentselp (trans (cdr (assoc 11 dl)) dim 1))))
   )
   (cdr (assoc 1 (entget mt)))
 )
)

Lee

Link to comment
Share on other sites

Two ways I can think of...

 

1) Digging through the Anonymous block definition for the Dimension:

(defun LM:GetDimensionString ( dim / dl db ds )
 (if
   (and
     (wcmatch (cdr (assoc 0 (setq dl (entget dim)))) "*DIMENSION")
     (setq db (tblobjname "BLOCK" (cdr (assoc 2 dl))))
   )
   (while (and (setq db (entnext db)) (not ds))
     (if (eq "MTEXT" (cdr (assoc 0 (setq dl (entget db)))))
       (setq ds (cdr (assoc 1 dl)))
     )
   )
 )
 ds
)

Lee

 

Fantastic! :thumbsup: I like it.

 

thanks Lee

Link to comment
Share on other sites

Sorry Lee, but I'm a starter abt lisp. I tried to put the first option -" Digging through the Anonymous block definition for the Dimension"-in the lisp, but it haven't worked.

Could you help me to put it in order?

:unsure:

Link to comment
Share on other sites

Sorry Lee, but I'm a starter abt lisp. I tried to put the first option -" Digging through the Anonymous block definition for the Dimension"-in the lisp, but it haven't worked.

 

Sorry Davidson, that was just answering pBe's question regarding the method of obtaining the Dimension string as displayed.

 

With regard to your program, I spent a lot of time on my 'BFind' program to account for the problems entailed with replacing text in MText with MText formatting codes, and also to enable the user to find and replace multiple text strings in a multitude of objects - I'd rather not re-invent the wheel on this one.

Link to comment
Share on other sites

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