Find and Replace in blocks

Discussion in 'AutoCAD' started by AIRIC, Sep 28, 2004.

  1. AIRIC

    AIRIC Guest

    Is there a way to find and replace text within a block. Example:
    When opening a drawing I want to create a block check.
    I know the block name, lets say "BlockA", when the drawing opens it looks for this block then checks for a string of text, lets say "John Doe". If it finds this string, the lisp ask me if I want to replace "John Doe" with "Jack Frost". If yes, it updates these blocks. Is this possible?
    Any ideas?

    I sure hope that made since.

    Thanks
    Eric
     
    AIRIC, Sep 28, 2004
    #1
  2. AIRIC

    T.Willey Guest

    Yes it is possible. You can use "ssget" to search the data base of the drawing. It will give you all the items that meet the search criteria. Then you can search through each block till you find the attribute tag value you want, see what the value of it is, then act upon it if need be.

    Hope that helps.
    Tim
     
    T.Willey, Sep 28, 2004
    #2
  3. AIRIC

    AIRIC Guest

    Half of the block are attributes and the other half are plain text. I would need to search for that one string of text.
    Will your approach work for plain text also?
     
    AIRIC, Sep 28, 2004
    #3
  4. AIRIC

    T.Willey Guest

    The only problem that I can see is that with plain text, you will have to search the block table and change it there, which will change all occurances of the block. But if that is what you want then I don't really see a problem.

    Tim
     
    T.Willey, Sep 28, 2004
    #4
  5. AIRIC

    AIRIC Guest

    that's what I want, if it finds the text, it updates all blocks within that drawing.
    How do you search a block table?
     
    AIRIC, Sep 28, 2004
    #5
  6. AIRIC

    T.Willey Guest

    Here is how I would do it. Totally untested, and now I have a deadline real fast, but this should get you started on the right path, and maybe some one will chime in if you need more help.

    (vl-load-com)
    (setq ent1 (entsel "\n Select block:"))
    (setq ent1 (car ent1))
    (setq bname (cdr (assoc 2 (entget ent1))))
    (setq tbl1 (tblobjname "block" bname))
    (while (setq tbl1 (entnext tbl1))
    (if (= (cdr (assoc 0 (entget tbl1))) "TEXT")
    (progn
    (setq ob1 (vlax-ename->vla-object tbl1))
    (if (= (vla-get-TextString ob1) "YourSearchString"); <- change here to match your test string to change
    (vla-put-TextString ob1 "YourNewString); <- change here to match your new string.
    ); end if
    ); end progn
    ); end if
    ); end while

    Tim
     
    T.Willey, Sep 28, 2004
    #6
  7. AIRIC

    T.Willey Guest

    Did that work for you?

    Tim
     
    T.Willey, Sep 28, 2004
    #7
  8. AIRIC

    AIRIC Guest

    I can do simple lisp routines, but this one seems to be out of my reach.
    The lisp below is how I want to approach this problem.
    I want it to automatically look for block name (no selecting)
    Then, if block was found, find text. If text is found, ask user "do you want to update block? (Y/N). If yes, replace text.

    I started this lisp below, but of course it does not work....I tried.

    Note: I'm new to using "vla" functions


    (defun c:ReplaceBtext ()
    (vl-load-com)
    (setq block nil)
    (setq block (ssget "x" (list (cons 0 "insert") (cons 2 "blockname"))))
    (if (/= block nil)
    (progn
    (setq ob1 (vlax-ename->vla-object block))
    (if (= (vla-get-TextString ob1) "old text");
    (vla-put-TextString ob1 "new text");
    )
    )
    )
    )

    thanks
    Eric
     
    AIRIC, Sep 28, 2004
    #8
  9. AIRIC

    T.Willey Guest

    Ok, try this.

    Tim

    (defun c:ReplaceBlock (/ ss blk1 ent1 opt1 ntxt ob1)

    (vl-load-com)
    (command "_.undo" "_end")
    (command "_.undo" "_group")
    (setq ss (ssget "x" (list (cons 0 "INSERT") (cons 2 "blkname")))); <- change to your block name
    (if ss
    (progn
    (setq blk1 (tblobjname "block" "blkname")); <- change to your block name
    (while (setq blk1 (entnext blk1))
    (setq ent1 (entget blk1))
    (if (= (cdr (assoc 0 ent1)) "TEXT")
    (if (= (cdr (assoc 1 ent1)) "YourTestString"); <- change this to the text you want to change
    (progn
    (initget "Y N")
    (setq opt1 (getkword "\n Would you like to replace text [<Y>,N]: "))
    (if (or (not opt1) (= opt1 "Y"))
    (progn
    (setq ntxt (getstring "\n Enter new string: "))
    (setq ob1 (vlax-ename->vla-object blk1))
    (vla-put-TextString ob1 ntxt)
    ); progn
    ); if
    ); progn
    ); if
    ); if
    ); while
    ); progn
    (prompt "\n No blocks named \"blkname\" in drawing.")
    ); if
    (command "_.undo" "_end")
    (princ)
    ); defun
     
    T.Willey, Sep 28, 2004
    #9
  10. AIRIC

    AIRIC Guest

    WOW, you are the man.
    Now you did the hard part, now I am going to try to add a "YES/NO" dialog box since it automatically runs on startup.

    Thank you for your help.
    Hope you made your deadline

    Eric L
     
    AIRIC, Sep 28, 2004
    #10
  11. AIRIC

    T.Willey Guest

    I did, thanks, and I'm glad it works for you.

    Tim
     
    T.Willey, Sep 28, 2004
    #11
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.