Jump to content

Find a string - do something with it (code modification) - Help please...


Recommended Posts

Posted

I have a couple of questions on how to do something...

Given the following..

 

Original Post

 

(defun chkmtxtstr (matchstr / mtxtset lst)
  (if (setq mtxtset (ssget "_X" '((0 . "MTEXT"))))
    (if
      (setq lst
        (vl-remove-if-not
         '(lambda (x)
            (wcmatch
              (strcase (vla-get-textstring x))
              (strcat "*" (strcase matchstr) "*")
            )
          )
          (mapcar
            'vlax-ename->vla-object
            (vl-remove-if
              'listp
              (mapcar 'cadr (ssnamex mtxtset))
            )
          )
        )
      )
      (alert (strcat "The string '" matchstr "' was found."))
    )
  )
  lst
)

;;;useage - (chkmtxtstr "MyTextString")

 

 

1) How can I hard code the text string into this code? Is it the "x"??

 

2) How can I assign the found text object to a variable so that I can do other things to it?

     ie: (command "change" XX "" "La" "0" "c" "bylayer" "")

 

     (In my case there will only be one instance of the found string on a particular tab/layout.) No possibility of multiples.

 

 

 

 

Posted

Just a guess since I can't really play around with the routine right now (and lambda functions scare me lol)

 

perhaps changing (setq mtxtset (ssget "_X" '((0 . "MTEXT")))) to (setq mtxtset "the hard coded string") might be what you need to do. 


 

Posted

Which text do you want to hard code? Thinking there are 2 choices, either the text to search for (which is passed to this LISP as matchstr) or hard code the text string to search in which here is mtxtset.

Posted (edited)

Since the string to be found is already being passed an argument to your function, you can 'hardcode' it by simply defining another function which evaluates your function with a hardcoded string, e.g.:

(defun c:test ( )
    (chkmtxtstr "MyTextString")
)

 

Though, if you're happy with a case-sensitive match and assuming the MText content has no formatting and does not straddle multiple DXF group 3 entries, the code can become much simpler - consider the following:

(defun find ( str )
    (ssget "_X" (list '(0 . "MTEXT") (cons 1 (strcat "*" str "*"))))
)

 

Since the above will return a selection set, you can call it in the following way:

(defun c:test ( / sel )
    (if (setq sel (find "YourString"))
        (command "_.change" sel "" "_p" "_la" "0" "_c" "ByLayer" "")
    )
    (princ)
)

 

Edited by Lee Mac
  • Like 4
Posted

Glad to see you posting Lee :)

  • Like 3
Posted

Thank you all for the several options to consider...
After all that, this little snippet did exactly what I needed and works perfectly.

 

(defun c:test ()
(setq SS1 (ssget "_X" '((0 . "*TEXT") (1 . "MyText String"))))
(if ss1
(command ".change" ss1 "" "p" "La" "LayerName" "c" "bylayer" "")
(princ "*Not Found*"))
(princ))

 

I know in advance the answer to my next question will be far beyond my understanding.

 

How can I perform this same operation if this text is contained within a block?

I know it has something to do with Extended Entity Data but that is all I know..

It finds nothing if it is in a block.

 

I'm reading though this: 

Afralisp

but I thought I'd ask...

 

Again.. thank you all.

 

Posted (edited)
10 hours ago, ILoveMadoka said:

Thank you all for the several options to consider...
After all that, this little snippet did exactly what I needed and works perfectly.

 

(defun c:test ()
(setq SS1 (ssget "_X" '((0 . "*TEXT") (1 . "MyText String"))))
(if ss1
(command ".change" ss1 "" "p" "La" "LayerName" "c" "bylayer" "")
(princ "*Not Found*"))
(princ))

 

I know in advance the answer to my next question will be far beyond my understanding.

 

How can I perform this same operation if this text is contained within a block?

I know it has something to do with Extended Entity Data but that is all I know..

It finds nothing if it is in a block.

 

I'm reading though this: 

Afralisp

but I thought I'd ask...

 

Again.. thank you all.

 



(setq str (strcase (strcat "*" str "*")))

(if (setq ssblk (ssget "_x" '((0 . "insert")) ))
  (progn
    (setq ssblkl (sslength ssblk))
    (setq ssblkindex 0)
    (repeat ssblkl
      (setq blkent (entget (ssname ssblk ssblkindex)))
      (setq blk (cdr (assoc 2 blkent)))
      (if (setq ent (tblobjname "BLOCK" blk))
        (while (and (setq ent (entnext ent)))
          (setq enx (entget ent))
          (if (or (eq "MTEXT" (cdr (assoc  0 enx)))(eq "TEXT" (cdr (assoc  0 enx))))
            (progn
              (if (wcmatch (LM:Unformat (cdr (assoc 1 enx)) nil) str)
                (progn
                  (setq resulttxt (cdr (assoc 1 enx)))
                  ; do something with enx (finded text)
                ); end progn
              ); end if
            ); end progn
          );end if
        );end while
      );end if
      (setq ssblkindex (+ ssblkindex 1))
    );end repeat ssblk
  );end progn
);end if

 

in that case, you can start like this

this is an excerpt from my lisp to find text and draw lines.

 

I think it works for normal blocks, not nested blocks. maybe

Edited by exceed
Posted

Just a comment using "X" in ssget can find text in layouts and model, if that is what you want stop reading, if you only want say "model" add (410 . "Model") if its current layout can use (cons 410 (getvar 'ctab))

Posted

I had to change gears at work and will look at this new code as soon as possible.

 

Wanted to say thank you no matter what...

  • Like 1

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