Search code examples
automationattributeslabelautocad

AutoCAD: Edit object attribute table automatically


I have several (many) objects in an AutoCAD drawing and each of them has the same attribute field in it's preferences. Now I would like to fill this attribute field with a number (object one - number 1, object two - number 2 and so on). Putting the numbers in manually is very time consuming, therefore I would like to ask you if there's an automated approach for this matter.

Thanks a lot in advance!


Solution

  • An Example

    The following program is a very simple example which will prompt you for an attribute tag to be numbered and an integer from which to start the numbering, and will then continuously prompt you to select attributed block references to be numbered, incrementing the number by one for each valid selection:

    (defun c:attnum ( / ent enx num tag )
        (if (/= "" (setq tag (strcase (getstring "\nSpecify attribute tag <exit>: "))))
            (progn
                (setq num (cond ((getint "\nSpecify starting number <1>: ")) (1)))
                (while
                    (not
                        (progn
                            (setvar 'errno 0)
                            (setq ent (car (entsel (strcat "\nSelect block number " (itoa num) " <exit>: "))))
                            (cond
                                (   (= 7 (getvar 'errno))
                                    (prompt "\nMissed, try again.")
                                )
                                (   (null ent))
                                (   (/= "INSERT" (cdr (assoc 0 (setq enx (entget ent)))))
                                    (prompt "\nThe selected object is not a block.")
                                )
                                (   (/= 1 (cdr (assoc 66 enx)))
                                    (prompt "\nThe selected block is not attributed.")
                                )
                                (   (progn
                                        (setq ent (entnext ent)
                                              enx (entget  ent)
                                        )
                                        (while
                                            (and
                                                (= "ATTRIB" (cdr (assoc 0 enx)))
                                                (/= tag (strcase (cdr (assoc 2 enx))))
                                            )
                                            (setq ent (entnext ent)
                                                  enx (entget  ent)
                                            )
                                        )
                                        (/= "ATTRIB" (cdr (assoc 0 enx)))
                                    )
                                    (prompt (strcat "\nThe selected block does not contain the attribute \"" tag "\"."))
                                )
                                (   (entmod (subst (cons 1 (itoa num)) (assoc 1 enx) enx))
                                    (entupd ent)
                                    (setq num (1+ num))
                                    nil
                                )
                                (   (prompt "\nUnable to edit attribute value."))
                            )
                        )
                    )
                )
            )
        )
        (princ)
    )
    

    How to Load & Run the Above

    • Open Windows Notepad.
    • Copy & paste the above code into Notepad.
    • Save the file with a filename of your choice, with the file extension .lsp (ensure that Save As Type is set to All Files (*.*)).
    • Open AutoCAD to a new or existing drawing.
    • Type APPLOAD at the AutoCAD command-line.
    • Browse & select the file saved above, and click Load to load the program.
    • Close the APPLOAD dialog.
    • Type ATTNUM at the AutoCAD command-line to run the program.

    Similar instructions may be found as part of my tutorial on How to Run an AutoLISP Program.

    If you wish for the program to be automatically loaded for every new or existing drawing opened in AutoCAD, refer to my tutorial on Loading Programs Automatically.


    Other Existing Solutions

    In addition to the above, you may also be interested in the following programs:

    • My Incremental Numbering Suite application will provide a far more extensive set of options to allow you to customise the numbering format, permitting a Prefix & Suffix, multiple incrementing sections, and alphanumerical incrementing.

      You can number existing attributed block references using this application by typing 'R' or 'r' at the AutoCAD command-line during object placement to enter Replacement Mode.

    • My Incremental Array application will allow you to automatically increment the attribute values whilst arraying an attributed block reference (or other objects).