Retrieving than Replaceing Text in a Block an Attribute

Discussion in 'AutoCAD' started by Richard Franz, Apr 25, 2004.

  1. I know that it can be done I am just having trouble figuring out how to do it. I am trying to write a routine that will count the number of iterations done to a drawing using a block with an attribute. I don't what the user to have to select the block in the drawing because there will be only one block in a drawing. If the user has multiple tabs setup in the drawing I still don what them to have to select the block.

    I know the block name (DWG-Count) and the tag name (count)
    I what to pull out the value (00) from the tag name (count) setting it to CT1
    Then add 1 to CT1 and setting it to CT2
    Finally I want to place CT2 back into the tag name of block DWG-Count

    Example:
    Block before = (00)
    Block after = (01)

    I have done research and found examples similar to what I am look to do, but I keep getting lost with which lines of code I need to accomplish this. A point in the right direction would be a great help.

    Thanks,
    Richard
     
    Richard Franz, Apr 25, 2004
    #1
  2. Richard Franz

    bob.at Guest

    Hello Richard,

    you can try the following code snippet out of one of my programs (without error handling):

    (defun c:dwgcount ( / bl blel att ct1 ct2)
    (setq bl (ssget "_X" '((0 . "INSERT") (2 . "dwg-count"))))
    (setq blel (entget (ssname bl 0)))
    (setq att (entget (entnext (cdr (assoc -1 blel)))))
    (while (/= (cdr (assoc 0 att)) "SEQEND")
    (if (= (cdr (assoc 2 att)) "COUNT")
    (progn
    (setq ct1 (atoi (cdr (assoc 1 att))))
    (setq ct1 (1+ ct1))
    (if (< ct1 10) (setq cts (strcat "0" (rtos ct1 2 0)))
    (setq cts (rtos ct1 2 0))
    )
    (setq att (subst (cons 1 cts) (assoc 1 att) att))
    (entmod att)
    (entupd (cdr (assoc -1 att)))
    )
    )
    (setq att (entget (entnext (cdr (assoc -1 att)))))
    )
    (princ)
    )
     
    bob.at, Apr 25, 2004
    #2
  3. Hello Bob,

    I tried your code and it worked great, thank you. Now I will just have to read it so that I can understand how to do on my own.

    Thanks again,
    Richard
     
    Richard Franz, Apr 26, 2004
    #3
  4. Richard Franz

    bob.at Guest

    Some hints:

    (defun c:dwgcount ( / bl blel att ct1 ct2)
    (setq bl (ssget "_X" '((0 . "INSERT") (2 . "dwg-count")))) ;select all Blocks with name "dwg-count"
    (setq blel (entget (ssname bl 0))) ; get the first block (it shuold be the only one)
    (setq att (entget (entnext (cdr (assoc -1 blel))))) ;get the first attribute
    (while (/= (cdr (assoc 0 att)) "SEQEND") ; step through all attributes until you find "SEQEND"
    (if (= (cdr (assoc 2 att)) "COUNT") ; if it is the "count"-att
    (progn
    (setq ct1 (atoi (substr (cdr (assoc 1 att)) 2 2))) ; get the number out of the string ig "(00)"
    (setq ct1 (1+ ct1))
    (if (< ct1 10) (setq cts (strcat "0" (rtos ct1 2 0))), write new number (with leading 0 if less than 10)
    (setq cts (rtos ct1 2 0))
    )
    (setq cts (strcat "(" cts ")"))
    (setq att (subst (cons 1 cts) (assoc 1 att) att)) ; replace old number with new one
    (entmod att) ; store it in database
    (entupd (cdr (assoc -1 att))) ; update the display
    )
    )
    (setq att (entget (entnext (cdr (assoc -1 att))))) ; step to next attribut
    )
    (princ)
    )

    Hope it brings a little bit more light in the whole thing
    bob.at
     
    bob.at, Apr 26, 2004
    #4
  5. Thanks.
    This will help out a lot. Now I have to find out how to change the code if there is a drawing that has multiple sheet tabs. That would be more of a search the file for blocks named DWG-Count.

    Richard
     
    Richard Franz, Apr 26, 2004
    #5
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.