Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Deleting attributes from blocks

Hybrid View

  1. #1
    Member
    Join Date
    2008-02
    Location
    Toronto, On
    Posts
    34

    Default Deleting attributes from blocks

    I'm trying to create a function which will remove all attributes from any block. The attributes need to be removed from all instances of the block.
    I wrote my function as below.
    It deletes the attributes, but when the Attsync command is called, AutoCAD tells me the following:
    "Block table contains no attributed block named SomeBlock2", and the attributes are still displayed.

    If, after this function has run, I manually type "ATTSYNC", and select one of the blocks, they will properly clean up the blocks.

    Is there any way of properly attsyncing the block after the attributes have been deleted using this method?


    (defun DeleteAtts (BName / Binfo En El EnTyp AttSync)

    (setq BInfo (tblsearch "BLOCK" BName)
    En (cdr (assoc -2 BInfo))
    )

    (while En
    (setq El (entget En)
    EnTyp (cdr (Assoc 0 El))
    )
    (if (= EnTyp "ATTDEF")
    (progn
    (entdel En)
    (setq AttSync T)
    );end progn
    );end if
    (setq En (entnext En))
    );end while

    (if AttSync
    (command "ATTSYNC" "N" Bname)
    );end if

    (regen)
    );end defun

  2. #2
    I could stop if I wanted to Tharwat's Avatar
    Join Date
    2010-06
    Posts
    477

    Default Re: Deleting attributes from blocks

    Try this .....
    Code:
    (defun c:Test (/ ss i sn x)(vl-load-com)
    ;;; Tharwat 04. May . 2012 ;;;
      (if (setq ss (ssget '((0 . "INSERT") (66 . 1))))
        (repeat (setq i (sslength ss))
          (setq sn (ssname ss (setq i (1- i))))
          (while
            (not
              (eq (cdr (assoc 0 (entget (setq x (entnext sn))))) "SEQEND")
            )
             (if (eq (cdr (assoc 0 (entget x))) "ATTRIB")
               (vla-delete
                 (vlax-ename->vla-object (cdr (assoc -1 (entget x))))
               )
             )
          )
        )
        (princ)
      )
      (princ)
    )

  3. #3
    I could stop if I wanted to Tharwat's Avatar
    Join Date
    2010-06
    Posts
    477

    Default Re: Deleting attributes from blocks

    or this would remove all attributes from all your blocks Definitions that are existed within your drawing ..

    Code:
    (defun c:Test nil
      (vl-load-com)
    ;;; Tharwat 04. May . 2012 ;;;
      (vlax-for x (vla-get-blocks
                    (vla-get-activedocument (vlax-get-acad-object))
                  )
        (if (and (eq :vlax-false (vla-get-isXref x))
                 (eq :vlax-false (vla-get-islayout x))
            )
          (vlax-for ent x
            (if (eq (vla-get-objectname ent) "AcDbAttributeDefinition")
              (vla-delete ent)
            )
          )
        )
      )
      (princ)
    )

  4. #4
    Member
    Join Date
    2008-02
    Location
    Toronto, On
    Posts
    34

    Default Re: Deleting attributes from blocks

    Tharwat,

    That's a nice piece of code, and seems to delete all attributes very efficiently. However...
    From what I can see, this didn't delete the attributes from blocks that are inserted into model space. The attributes are still visible.
    When I run AttSync to attempt to sync them up, I get the following message from AutoCAD:
    "This drawing contains no attributed blocks."
    Which, to me, proves that your code cleared out all the attributes from the blocks, but I'm still effectively seeing the same end result, the blocks aren't attsynced.

  5. #5
    I could stop if I wanted to Tharwat's Avatar
    Join Date
    2010-06
    Posts
    477

    Default Re: Deleting attributes from blocks

    Quote Originally Posted by JaCAD View Post
    Tharwat,

    That's a nice piece of code, and seems to delete all attributes very efficiently. However...
    From what I can see, this didn't delete the attributes from blocks that are inserted into model space. The attributes are still visible.
    When I run AttSync to attempt to sync them up, I get the following message from AutoCAD:
    "This drawing contains no attributed blocks."
    Which, to me, proves that your code cleared out all the attributes from the blocks, but I'm still effectively seeing the same end result, the blocks aren't attsynced.
    So does it mean that you want to delete all attributes for all inserted and block definition from your drawing ?

    Tharwat

  6. #6
    Member
    Join Date
    2008-02
    Location
    Toronto, On
    Posts
    34

    Default Re: Deleting attributes from blocks

    Ideally, given a block name, the function would delete all attributes from the block definition, then attsync the block (or cycle through all inserts of the block) in order to clear up all instances of the block.

    I'm trying to build a multi-purpose block-cleaning tool, with a variety of specific tasks. I have most of them figured out, it's just attributes that are messing me up.
    The other potential problem is that the block may sometimes be found inserted in another block, and I need to address those situations as well.

  7. #7
    I could stop if I wanted to Tharwat's Avatar
    Join Date
    2010-06
    Posts
    477

    Default Re: Deleting attributes from blocks

    My first routine would delete all attributes that are existed within the selected attributed block and the second
    routine would delete all attributes from all blocks definitions within your current drawing .

  8. #8
    Active Member
    Join Date
    2000-11
    Location
    Ontario, Canada
    Posts
    83

    Default Re: Deleting attributes from blocks

    Actually JaCAD, the only problem I had running your routine related to the
    Code:
    (regen)
    right at the end. Possibly you are using a custom lisp function called "regen"? In any case, once I changed that line to
    Code:
    (command "regen")
    the Deleteatts function worked well enough. I wonder if there might not have been a block in your drawing named "SomeBlock2" (i.e. the name might not be exactly correct)?

  9. #9
    Member
    Join Date
    2008-02
    Location
    Toronto, On
    Posts
    34

    Default Re: Deleting attributes from blocks

    (regen) is actually (vla-regen #AcDoc# T), which does work if the document is bound to #AcDoc#. I was only using (regen) as a placeholder. also, "SomeBlock2" is just a place holder. I got the block name using (cdr (assoc 2 (entget (Car (entsel)))))

  10. #10
    Active Member
    Join Date
    2000-11
    Location
    Ontario, Canada
    Posts
    83

    Default Re: Deleting attributes from blocks

    The following is a rather specialized function I wrote some time back to return a list containing all references (vla-objects) to a block, the name of which you supply as an argument.

    After using a variation of Tharwat's second posted function to strip the attribute definitions from your block definition, you could use something like this (below) to collect all references to the block (by name). Then, use a variation of Tharwat's first posted function to strip the attribute references from each insert.

    Code:
    ;;=============================================================================================
    ;; BLK-INS-FND RETURNS ALL INSERTIONS OF THE NAMED BLOCK, OTHERWISE NIL
    (defun blk-ins-fnd (blk-nm / BLK-INS-LST BLKLST N NM-X SUB-BLKLST SUB-BLKLST-N
                        SUB-LST SUB-LST-LN THE-BLK THE-BLK-LST VL-X X)
      (or blocks (setq blocks (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))))
      (setq blk-nm (strcase blk-nm))
      (if (not
            (vl-catch-all-error-p
              (setq the-blk (vl-catch-all-apply 'vla-item (list blocks blk-nm)))
            ) ; vl-catch-all-error-p
          ) ; not
        (progn
          (setq the-blk-lst     (entget (vlax-vla-object->ename the-blk))
                sub-lst    (member (assoc 102 the-blk-lst) the-blk-lst)
                sub-lst-ln (length sub-lst)
                n          0
          ) ; setq
          (while (< n sub-lst-ln)
            (setq x (nth n sub-lst))
            (cond ((/= 331 (car x)) nil) ; first condition
                  ((vl-catch-all-error-p
                     (setq vl-x
                            (vl-catch-all-apply
                              'vlax-ename->vla-object
                              (list (cdr x))
                            ) ; vl-catch-all-apply
                     ) ; setq
                   ) ; vl-catch-all-error-p
                   nil
                  ) ; second condition
                  (t (setq blk-ins-lst (append blk-ins-lst (list vl-x)))) ; default condition
            ) ; cond
            (setq n (1+ n))
          ) ; while
          (if (eq (vla-get-isdynamicblock the-blk) ':vlax-true)
            (progn
              (vlax-for blk blocks
                (if (wcmatch (strcase (vla-get-name blk)) "*U#*")
                  (progn
                    (setq blklst       (entget (vlax-vla-object->ename blk))
                          sub-blklst   (member (assoc 102 blklst) blklst)
                          sub-blklst-n (length sub-blklst)
                          n            0
                    ) ; setq
                    (while (< n sub-blklst-n)
                      (setq x (nth n sub-blklst))
                      (cond ((/= 331 (car x)) nil) ; first condition
                            ((vl-catch-all-error-p
                               (setq vl-x
                                      (vl-catch-all-apply
                                        'vlax-ename->vla-object
                                        (list (cdr x))
                                      ) ; vl-catch-all-apply
                               ) ; setq
                             ) ; vl-catch-all-error-p
                             nil
                            ) ; second condition
                            ((vl-catch-all-error-p
                               (setq nm-x
                                      (vl-catch-all-apply
                                        'vla-get-effectivename
                                        (list vl-x)
                                      ) ; vl-catch-all-apply
                               ) ; setq
                             ) ; vl-catch-all-error-p
                             nil
                            ) ; third condition
                            ((/= (strcase nm-x) blk-nm)
                             nil
                            ) ; fourth condition
                            (t
                             (setq blk-ins-lst
                                    (append blk-ins-lst
                                            (list vl-x)
                                    ) ; append
                             ) ; setq
                            ) ; default condition
                      ) ; cond
                      (setq n (1+ n))
                    ) ; while
                  ) ; progn
                ) ; if
              ) ; vlax-for
            ) ; progn
          ) ; if
        ) ; progn
      ) ; if
      (if (vl-consp blk-ins-lst)
        (setq blk-ins-lst (reverse (vl-remove nil blk-ins-lst)))
      ) ; if
      blk-ins-lst
    ); defun

Page 1 of 2 12 LastLast

Similar Threads

  1. 2012: Deleting blocks
    By cheryla in forum AutoCAD General
    Replies: 12
    Last Post: 2012-02-10, 01:34 PM
  2. 2011: Blocks with attributes
    By fujifilm3000 in forum AutoCAD General
    Replies: 3
    Last Post: 2011-05-12, 04:54 PM
  3. 2011: Blocks with attributes
    By fujifilm3000 in forum AutoCAD General
    Replies: 1
    Last Post: 2011-04-14, 07:15 PM
  4. Deleting blocks from all drawings
    By cadd4la in forum AutoLISP
    Replies: 19
    Last Post: 2005-08-25, 08:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •