Jump to content

Replacing Any Block with a Wildcard by a specific block I made


Recommended Posts

Posted

Hello all! I wonder if there's any known LISP able to replace all blocks in a drawing which fulfill a certain wildcard by another specific block.

 

I have many many blocks containing the word "SM_Annotation" (SM_Annotation_161784, SM_Annotation_898437 and so on) and they have unsignificant differences between them. It would be nice if I were able to replace them all by a simple block I did myself. (The origin will be maintained, it will all work fine as long as I can just replace them all)

Those blocks are inside other blocks (They aren't nested inside themselves, but they are inside blocks overall)

 

Any idea, or possible solution, thanks a bunch! 🙂

Posted (edited)

Assuming that the blocks are not attributed or dynamic, then the following should work:

(defun c:test ( / doc new pat )
    (setq pat (strcase "*SM_Annotation*")
          new (strcase "SM_Annotation")
          doc (vla-get-activedocument (vlax-get-acad-object))
    )
    (vlax-for blk (vla-get-blocks doc)
        (if (and (= :vlax-false (vla-get-isxref blk))
                 (/= new (strcase (vla-get-name blk)))
            )
            (vlax-for obj blk
                (if (and (= "AcDbBlockReference" (vla-get-objectname obj))
                         (wcmatch (strcase (vla-get-name obj)) pat)
                    )
                    (vla-put-name obj new)
                )
            )
        )
    )
    (vla-regen doc acallviewports)
    (princ)
)
(vl-load-com) (princ)

 

Edited by Lee Mac
  • Thanks 1
Posted
On 10/31/2020 at 7:25 PM, Lee Mac said:

Assuming that the blocks are not attributed or dynamic, then the following should work:


(defun c:test ( / doc new pat )
    (setq pat (strcase "*SM_Annotation*")
          new (strcase "SM_Annotation")
          doc (vla-get-activedocument (vlax-get-acad-object))
    )
    (vlax-for blk (vla-get-blocks doc)
        (if (and (= :vlax-false (vla-get-isxref blk))
                 (/= new (strcase (vla-get-name blk)))
            )
            (vlax-for obj blk
                (if (and (= "AcDbBlockReference" (vla-get-objectname obj))
                         (wcmatch (strcase (vla-get-name obj)) pat)
                    )
                    (vla-put-name obj new)
                )
            )
        )
    )
    (vla-regen doc acallviewports)
    (princ)
)
(vl-load-com) (princ)

 

 

Worked perfectly!! Thanks a bunch 😁

Two issues though:

 

- It would be nice if I could prompt the user to enter the string used upon calling the function. (It would correspond to both the wildcard and the name of the block to substitute the others)

- It seems to not work whenever there's a special character in the string.. There's a group of blocks named "SM_início paginação" (name is in portuguese) with which I'd like to do the same process and the LISP doesn't seem to read those

 

Thank you! 🙂

 

Posted

The answer to user input is straight forward, 

(setq pat (strcase (getstring "\nEnter wild card block name "))
          new (strcase (getstring "\nEnter new block name "))

re portuguese not sure hopefully Lee can help.

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