Author Topic: Find & Replace with wild card characters  (Read 10348 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: 3975
  • 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: 28683
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: 17653
  • 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 Programmer Analyst CAD Specialist
Multi-Discipline Design Drafting Document Control Automation.
cadanalyst@gmail.com www.linkedin.com/in/cadanalyst

Jeff_M

  • King Gator
  • Posts: 3975
  • 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: 28683
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: 28683
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: 28683
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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10387
Find & Replace with wild card characters
« Reply #15 on: July 13, 2005, 10:41:42 PM »
http://new.cadalyst.com/code/browseyear.cfm?fullyear=2005

Adjust Text for All Drawings in a Directory by Andrzej Gumula, p.0
Adjust Text for All Drawings in a Windows Directory, including title blocks, drawing notes, etc.

Code: [Select]
;;;CADALYST 04/05 Tip2028:  Replace Text In Dwgs.lsp      Adjust Text for All Drawings in a Directory (c) 2005 Andrzej Gumula

;;; [c]2004 Andrzej Gumula, Katowice, Poland
;;; e-mail: a.gumula@wp.pl
;;; This routine repleces text in dwg files from the selected folder.
;;; Also accept subfolders.

 (setq Tmp (list "This routine replace text in dwg files from the selected folder."
"Options:"
"-match case: allow for uppercase and lowercase alphabetic characters"
"-find whole words only: if active the routine analyse only whole words"
"-find in subfolder: if active the routine find dwg files also in subfolders of selected folder"
"-model space: if active the routine analize text entities only in model space"
"-paper space: if active the routine analize text entities only in paper space (layouts)"
"-both space: if active the routine analize text entities only in model and paper spaces"
I've reached the age where the happy hour is a nap. ()
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CADaver

  • Guest
Find & Replace with wild card characters
« Reply #16 on: July 14, 2005, 08:18:32 AM »
Quote from: tcdan
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.
I guess that was my point of confusion all along, the wildcard wasn't gonna help in any of the practical applications I could envision.

tcdan

  • Guest
Find & Replace with wild card characters
« Reply #17 on: July 14, 2005, 05:57:04 PM »
CAB - just looking at the description of that routine, it looks awesome - haven't tried it yet. . .


CADaver
Quote from: CADaver
\P is the code for a hard line feed; "My name\Pis CADaver"

I tried using \P in 2005 and it didn't seem to work.


Quote from: I
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).

OK, I take it back.  You CAN search mtext with line returns quite intuitively by simply pretending the lines wrap together.  So
"My name
is CADaver"
could be found by searching for "My nameis CADaver".  And if you wanted to search for "My name" you would simply uncheck the 'search for whole words option'.

Jeff_M

  • King Gator
  • Posts: 3975
  • C3D user & customizer
Find & Replace with wild card characters
« Reply #18 on: July 14, 2005, 06:56:11 PM »
Dan, note that \p & \P are 2 different things.....

tcdan

  • Guest
Find & Replace with wild card characters
« Reply #19 on: July 14, 2005, 07:41:33 PM »
Quote
Dan, note that \p & \P are 2 different things.....

I edited my post - I meant to type \P.  Neither \P nor \p work.

Jeff_M

  • King Gator
  • Posts: 3975
  • C3D user & customizer
Find & Replace with wild card characters
« Reply #20 on: July 14, 2005, 08:03:27 PM »
OK, you're right! Just shows one more reason why I use my own rather than Find. I just made a test Mtext. In it I put "THIS IS A TEST", then I copied it and edited it to place a hard return just prior to the "A". Then, using Find, I did a replace all with "THIS IS A TEST" & "TEST" as my for & replace lines......BOTH Mtext objects were modified! It completely overlooked the line feed....BAH!

CADaver

  • Guest
Find & Replace with wild card characters
« Reply #21 on: July 15, 2005, 08:48:31 AM »
hmmm... still running R2002 here, so that may be the difference.  But it won't jump a hard return for me.

How did you enter the hard return?

Jeff_M

  • King Gator
  • Posts: 3975
  • C3D user & customizer
Find & Replace with wild card characters
« Reply #22 on: July 15, 2005, 01:03:50 PM »
I'm using 2002 also. I used the Enter key in the Mtext Editor.
And here's what LIST returns on the edited one.

                  MTEXT     Layer: "0"
                            Space: Model space
                   Handle = 2C
Location:        X=   8.5501  Y=   5.5778  Z=   0.0000
Width:              4.1357
Normal:          X=   0.0000  Y=   0.0000  Z=   1.0000
Rotation:                0
Text style:      "Standard"
Text height:        0.2000
Line spacing:    Multiple (1.000000x =    0.3333)
Attachment:      TopLeft
Flow direction:  ByStyle
Contents:        THIS IS \PA TEST

CADaver

  • Guest
Find & Replace with wild card characters
« Reply #23 on: July 15, 2005, 01:25:43 PM »
Are you using the infernal ... ummm... internal editor?  cuz' it won't jump the line feed for me.

tcdan

  • Guest
Find & Replace with wild card characters
« Reply #24 on: July 15, 2005, 01:49:57 PM »
Jeff - 2005 gives the same listing for the contents "THIS IS \PA TEST"

Quote from: CADaver
Are you using the infernal ... ummm... internal editor? cuz' it won't jump the line feed for me.


I tried with notepad and with internal editor - same results.

tcdan

  • Guest
Find & Replace with wild card characters
« Reply #25 on: July 15, 2005, 01:54:55 PM »
Quote from: CADaver
Are you using the infernal ... ummm... internal editor? cuz' it won't jump the line feed for me.


OK - I might have found the difference.  If you place a space before inputting a line feed, CAD will find the text as it looks.  So, you can search for
"This is
a test."
in find/replace IF there is a space (inputted with the spacebar) between 'is' and 'a' even if there is also a line feed.

So CADaver, do you input a space before going to the next line, because I do not.

CADaver

  • Guest
Find & Replace with wild card characters
« Reply #26 on: July 15, 2005, 05:54:03 PM »
huh, whud ya' think o' that?  I never place a space prior to the line feed, but I may start.  How'd you happen upon that?

Really wierd thing: if you place text "this is \Pa test", and then REPLACE "this is a test" with "eeeeeeeeeeeeee", the result on screen stilll has the line feed in the same spot, "eeeeeeee\Peeeeee"

tcdan

  • Guest
Find & Replace with wild card characters
« Reply #27 on: July 15, 2005, 07:22:20 PM »
Strange indeed. . . I might ask why you came across THAT in return to your question to me. . .  I don't really know how I happened upon it honestly - but I don't think it's going to help me put a space before I press enter.