Author Topic: Find & Replace with wild card characters  (Read 12391 times)

0 Members and 1 Guest are viewing this topic.

tcdan

  • Guest
Find & Replace with wild card characters
« on: July 12, 2005, 07:12:42 PM »
I want to use find & replace for mtext by using a *
I'm pretty sure you can write that funtionality with LISP - they have this funtionality it for 2006, I'm wondering if it's possible for 2005.

Jeff_M

  • King Gator
  • Posts: 4018
  • C3D user & customizer
Find & Replace with wild card characters
« Reply #1 on: July 12, 2005, 07:16:35 PM »
Look into the (wcmatch) function...... :)

tcdan

  • Guest
Find & Replace with wild card characters
« Reply #2 on: July 12, 2005, 07:30:16 PM »
Maybe I'll take the time to look into writing something sometime - but there's a couple other routines that I'm more interested in writing (which I've posted about) and I've been way too busy at work to spend any time working on them (I excuse my time posting on the Swamp :)

tcdan

  • Guest
Find & Replace with wild card characters
« Reply #3 on: July 12, 2005, 07:40:32 PM »
I'm not trying to force somebody else to do it (as if I had the power!), but was wondering if it's already been done. . . otherwise, it might be worth looking into.

Mark

  • Custom Title
  • Seagull
  • Posts: 28716
Find & Replace with wild card characters
« Reply #4 on: July 12, 2005, 07:56:10 PM »
Quote from: tcdan
...but was wondering if it's already been done. . . otherwise, it might be worth looking into.

Don't let that stop you!! When ever you write anything you'll be learning, just because it's been done before shouldn't stop you. Besides you might want to change the way the program works to suit *your* needs.

If you need some hints to get you started then by all means speak-up. :)
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Find & Replace with wild card characters
« Reply #5 on: July 12, 2005, 08:10:20 PM »
Yes, please don't let that stop you. A thirst to know how to do things for yourself, a desire to write them yourself will serve you very well in the years to come.
Engineering Technologist CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com http://cadanalyst.slack.com http://linkedin.com/in/cadanalyst

Jeff_M

  • King Gator
  • Posts: 4018
  • C3D user & customizer
Find & Replace with wild card characters
« Reply #6 on: July 12, 2005, 08:16:56 PM »
Well, Mark will probably hate me for this.......but here's one I put together a few years ago. It works on text, mtext, attributes and dimension text. I quit at putting a nice front end on it, but the function works. IIRC, this was one of my early attempts at using ActiveX.

And since I used (vl-string-search) no * was needed.....but I'm sure there's cause for wanting that option. I leave that up to tcdan or whoever else wants to try :)
Code: [Select]
;| Routine to find specified text and replace with new text. Works on Text,
   Mtext, Attributes and Dimension text overrides.
   WARNING: it will change all occurances of a pattern with the new text.
   Such as: if "test" "contest" "testing" are all valid text entries in the
   drawing, running this: (txtfind "test" "newtest") will change
   the original text to "newtest" "connewtest" "newtesting", but for the
   original intent of this routine that was not a problem. Modifications
   may be made to force matching of whole word only.

   by: Jeff Mishler Sept. 2003

   |;


(defun txtfind (patt newpatt / count ss ent str txthgt match?)
  (vl-load-com)
  (vla-startundomark (vla-Get-ActiveDocument (vlax-Get-Acad-Object)))
  (setq ss (ssget "X" '((0 . "TEXT,MTEXT,DIMENSION,INSERT"))))
  (if (not ss)
    (princ "\nNo Text entities found!")
    (progn
      (setq count -1
   )
      (while (< (setq count (1+ count))(sslength ss))
(setq ent (entget (ssname ss count))
     obj (vlax-ename->vla-object (cdr (car ent))))
(cond
 ((= (cdr (assoc 0 ent)) "TEXT")
  (progn
    (setq str (cdr (assoc 1 ent)))
    (while (setq match? (vl-string-search patt str))
      (setq str (vl-string-subst newpatt patt str))
      (vla-put-textstring obj str)
      );while
    );progn
  );first condition
 ((= (cdr (assoc 0 ent)) "DIMENSION")
  (progn
    (setq str (cdr (assoc 1 ent)))
    (while (setq match? (vl-string-search patt str))
      (setq str (vl-string-subst newpatt patt str))
      (vla-put-textoverride obj str)
      );while
    );progn
  );second condition
 ((= (cdr (assoc 0 ent)) "MTEXT")
  (progn
    (setq str (vla-get-textstring obj))
    (while (setq match? (vl-string-search patt str))
      (setq str (vl-string-subst newpatt patt str))
      (vla-put-textstring obj str)
      );while
    );progn
  );third condition
 (t
  (progn
    (if (= (vla-get-hasattributes obj) :vlax-true)
      (progn
(setq atts (vlax-invoke obj 'getattributes))
(foreach x atts
  (setq str (vla-get-textstring x))
  (while (setq match? (vl-string-search patt str))
    (setq str (vl-string-subst newpatt patt str))
    (vla-put-textstring x str)
    );while
  );for
);progn
      );if
    );progn
  );last condition
 );cond
);while
      );progn
    );if
  (vla-endundomark (vla-Get-ActiveDocument (vlax-Get-Acad-Object)))
  (princ "done....")
  (princ)
  );defun

;Example usage:
;(txtfind "badtext" "goodtext")

Mark

  • Custom Title
  • Seagull
  • Posts: 28716
Find & Replace with wild card characters
« Reply #7 on: July 12, 2005, 08:31:45 PM »
Quote from: Jeff_M
Mark will probably hate me for this......


:lol:
TheSwamp.org  (serving the CAD community since 2003)

CADaver

  • Guest
Find & Replace with wild card characters
« Reply #8 on: July 13, 2005, 08:03:12 AM »
I guess I'm confused.  What will the wildcard do for you that isn't done already in FIND?

tcdan

  • Guest
Find & Replace with wild card characters
« Reply #9 on: July 13, 2005, 01:30:33 PM »
CADaver-
Here are just 2 ways I would find wild card characters adding to FIND's functionality:

1.
Well, let's say I have mtext reading something like this:
"Sta. 100+50 This is a block"
"Sta. 999+50 This is a block"

By searching for "*This is a block" I will catch all of the entities ending in "This is a block" and replace them with whatever I want.  OK, so you knew that already.  But Find and Replace does not do that as far as I know.  I have needed to do that.

2.
It also helps me catch return characters.  If i have a 2-line mtext object that reads:
"My name
is CADaver"
I cannot search for "My name is CADaver" if their is a line return character between "name" and "is". I CAN search for "My name" and "is CADaver", but this is not useful when I want to replace the whole string of text with something new (which I actually do).

CADaver

  • Guest
Find & Replace with wild card characters
« Reply #10 on: July 13, 2005, 05:06:39 PM »
1.)
You can FIND all instances that CONTAIN "This is a block" and replace it with whatever you want, but finding all the strings that END with "This is a block" is a whole different question that has little to do with wildcards.

A lisp routine would be needed to find all text entities that contain "This is a block" and replace the entire string, but that too has little to do with wildcards.


2.)
\P is the code for a hard line feed; "My name\Pis CADaver"

Mark

  • Custom Title
  • Seagull
  • Posts: 28716
Find & Replace with wild card characters
« Reply #11 on: July 13, 2005, 05:27:56 PM »
Quote from: CADaver
1.)
You can FIND all instances that CONTAIN "This is a block" and replace it with whatever you want, but finding all the strings that END with "This is a block" is a whole different question that has little to do with wildcards.


Code: [Select]
"*This is a block"
that will find it. Unless I'm missing the point. :roll:
TheSwamp.org  (serving the CAD community since 2003)

CADaver

  • Guest
Find & Replace with wild card characters
« Reply #12 on: July 13, 2005, 05:42:22 PM »
Quote from: Mark Thomas
Quote from: CADaver
1.)
You can FIND all instances that CONTAIN "This is a block" and replace it with whatever you want, but finding all the strings that END with "This is a block" is a whole different question that has little to do with wildcards.


Code: [Select]
"*This is a block"
that will find it. Unless I'm missing the point. :roll:
The FIND command will only find a string that contains the asterisk, and then it will find all occurances, not just the ones at the end of a string... unless I'm missing something.

Mark

  • Custom Title
  • Seagull
  • Posts: 28716
Find & Replace with wild card characters
« Reply #13 on: July 13, 2005, 05:43:45 PM »
I was thinking in terms of 'wcmatch' sorry.
TheSwamp.org  (serving the CAD community since 2003)

tcdan

  • Guest
Find & Replace with wild card characters
« Reply #14 on: July 13, 2005, 09:20:00 PM »
Quote
\P is the code for a hard line feed; "My name\Pis CADaver"

Thanks CADaver - I thought there was a way to include a line feed, and I'm glad to you filled me in.

Quote
You can FIND all instances that CONTAIN "This is a block" and replace it with whatever you want, but finding all the strings that END with "This is a block" is a whole different question that has little to do with wildcards.

I'm not sure how finding every instance of a string that ends in a particular phrase has little to do with wild cards. . .  of course I'm not just interested in the silly example I gave you of finding and replacing instances of a string ending with "this is a block".  I'm thinking of the more general case in which you want to find any specific phrase in the middle of a string and replace all or part of the string.  This could be done by using a wild card in conjunction with the find and replace dialogue if Autodesk had built it in - and I was wondering if that functionality could be built in by a user.  But I found that AutoDesk beat me to it (even though it's inclusion doesn't help me with 2005):

Using wildcards in the Find dialog

In any case I'm not too concerned whether or not you can use the find/replace function this way cuz it sounds like you could write a lisp to do the same thing a wild card character could do within find/replace by properly using wcmatch.